| 47 | | // a visitor for hashing phash keys |
|---|
| 48 | | class hKeyHasher : public boost::static_visitor<std::size_t> { |
|---|
| | 47 | // a visitor for hashing phash keys |
|---|
| | 48 | class hKeyHasher : public boost::static_visitor<std::size_t> { |
|---|
| | 49 | |
|---|
| | 50 | public: |
|---|
| | 51 | |
|---|
| | 52 | std::size_t operator()(const pInt &k) const { |
|---|
| | 53 | return static_cast<std::size_t>(k); |
|---|
| | 54 | } |
|---|
| | 55 | |
|---|
| | 56 | /* |
|---|
| | 57 | std::size_t operator()(const pBString &k) const { |
|---|
| | 58 | return boost::hash_value(k); |
|---|
| | 59 | } |
|---|
| | 60 | */ |
|---|
| | 61 | |
|---|
| | 62 | std::size_t operator()(const pUString &k) const { |
|---|
| | 63 | return static_cast<std::size_t>(k.hashCode()); |
|---|
| | 64 | } |
|---|
| | 65 | |
|---|
| | 66 | }; |
|---|
| | 67 | |
|---|
| | 68 | // a visitor for determining phash key type |
|---|
| | 69 | class hKeyGetType : public boost::static_visitor<hKeyType> { |
|---|
| | 70 | |
|---|
| | 71 | public: |
|---|
| | 72 | |
|---|
| | 73 | hKeyType operator()(const pInt &k) const { |
|---|
| | 74 | return hKeyInt; |
|---|
| | 75 | } |
|---|
| | 76 | |
|---|
| | 77 | /* |
|---|
| | 78 | hKeyType operator()(const pBString &k) const { |
|---|
| | 79 | return hKeyBStr; |
|---|
| | 80 | } |
|---|
| | 81 | */ |
|---|
| | 82 | |
|---|
| | 83 | hKeyType operator()(const pUString &k) const { |
|---|
| | 84 | return hKeyUStr; |
|---|
| | 85 | } |
|---|
| | 86 | |
|---|
| | 87 | }; |
|---|
| | 88 | |
|---|
| | 89 | // define the container stored in stableHash |
|---|
| | 90 | struct h_container { |
|---|
| | 91 | |
|---|
| | 92 | pVarP pData; |
|---|
| | 93 | hKeyVar key; |
|---|
| | 94 | |
|---|
| | 95 | h_container(const pUString k, pVarP d) : pData(d), key(k) { } |
|---|
| | 96 | |
|---|
| | 97 | // h_container(const pBString k, pVarP d) : pData(d), key(k) { } |
|---|
| | 98 | |
|---|
| | 99 | h_container(const pInt k, pVarP d) : pData(d), key(k) { } |
|---|
| | 100 | |
|---|
| | 101 | }; |
|---|
| | 102 | |
|---|
| | 103 | // the stableHash container |
|---|
| | 104 | // a boost.multiindex that stores data with two indexes: hashed and sequenced |
|---|
| | 105 | typedef multi_index_container< |
|---|
| | 106 | // the container structure we store for each item in the hash |
|---|
| | 107 | h_container, |
|---|
| | 108 | // index definitions: hash and sequence |
|---|
| | 109 | indexed_by< |
|---|
| | 110 | hashed_unique< member<h_container, hKeyVar, &h_container::key> >, |
|---|
| | 111 | sequenced<> |
|---|
| | 112 | > |
|---|
| | 113 | > stableHash; |
|---|
| | 114 | |
|---|
| | 115 | // sequenced index accessor |
|---|
| | 116 | typedef nth_index<stableHash,1>::type seq_index; |
|---|
| | 117 | |
|---|
| | 118 | /** |
|---|
| | 119 | * pHash definition |
|---|
| | 120 | */ |
|---|
| | 121 | class pHash { |
|---|
| | 122 | |
|---|
| | 123 | private: |
|---|
| | 124 | stableHash hashData; |
|---|
| | 125 | pInt maxIntKey; |
|---|
| 77 | | /* |
|---|
| 78 | | hKeyType operator()(const pBString &k) const { |
|---|
| 79 | | return hKeyBStr; |
|---|
| 80 | | } |
|---|
| 81 | | */ |
|---|
| 82 | | |
|---|
| 83 | | hKeyType operator()(const pUString &k) const { |
|---|
| 84 | | return hKeyUStr; |
|---|
| 85 | | } |
|---|
| 86 | | |
|---|
| 87 | | }; |
|---|
| 88 | | |
|---|
| 89 | | // define the container stored in stableHash |
|---|
| 90 | | struct h_container { |
|---|
| 91 | | |
|---|
| 92 | | pVarP pData; |
|---|
| 93 | | hKeyVar key; |
|---|
| 94 | | |
|---|
| 95 | | h_container(const pUString k, pVarP d) : pData(d), key(k) { } |
|---|
| 96 | | |
|---|
| 97 | | // h_container(const pBString k, pVarP d) : pData(d), key(k) { } |
|---|
| 98 | | |
|---|
| 99 | | h_container(const pInt k, pVarP d) : pData(d), key(k) { } |
|---|
| 100 | | |
|---|
| 101 | | }; |
|---|
| 102 | | |
|---|
| 103 | | // the stableHash container |
|---|
| 104 | | // a boost.multiindex that stores data with two indexes: hashed and sequenced |
|---|
| 105 | | typedef multi_index_container< |
|---|
| 106 | | // the container structure we store for each item in the hash |
|---|
| 107 | | h_container, |
|---|
| 108 | | // index definitions: hash and sequence |
|---|
| 109 | | indexed_by< |
|---|
| 110 | | hashed_unique< member<h_container, hKeyVar, &h_container::key> >, |
|---|
| 111 | | sequenced<> |
|---|
| 112 | | > |
|---|
| 113 | | > stableHash; |
|---|
| 114 | | |
|---|
| 115 | | // sequenced index accessor |
|---|
| 116 | | typedef nth_index<stableHash,1>::type seq_index; |
|---|
| 117 | | |
|---|
| 118 | | /** |
|---|
| 119 | | * pHash definition |
|---|
| 120 | | */ |
|---|
| 121 | | class pHash { |
|---|
| 122 | | |
|---|
| 123 | | private: |
|---|
| 124 | | stableHash hashData; |
|---|
| 125 | | pInt maxIntKey; |
|---|
| 126 | | |
|---|
| 127 | | public: |
|---|
| 128 | | |
|---|
| 129 | | // types |
|---|
| 130 | | typedef stableHash::size_type size_type; |
|---|
| 131 | | |
|---|
| 132 | | typedef stableHash::iterator iterator; |
|---|
| 133 | | |
|---|
| 134 | | // construct/destroy/copy |
|---|
| 135 | | pHash() : maxIntKey(0) { std::cout << "creating fresh pHash" << std::endl; } |
|---|
| 136 | | |
|---|
| 137 | | pHash(pHash const& p) : maxIntKey(p.maxIntKey) { |
|---|
| 138 | | std::cout << "pHash copy construct" << std::endl; |
|---|
| 139 | | hashData = p.hashData; |
|---|
| 140 | | } |
|---|
| 141 | | |
|---|
| 142 | | ~pHash() { std::cout << "destroying pHash" << std::endl; } |
|---|
| 143 | | |
|---|
| 144 | | // modifiers |
|---|
| 145 | | void insert(const pUString &key, pVarP data); |
|---|
| 146 | | //void insert(const pBString &key, pVarP data); |
|---|
| 147 | | void insert(const pInt &key, pVarP data); |
|---|
| 148 | | void insertNext(pVarP data); |
|---|
| 149 | | |
|---|
| 150 | | size_type remove(const pUString &key); |
|---|
| 151 | | //void remove(const pBString &key); |
|---|
| 152 | | size_type remove(const pInt &key); |
|---|
| 153 | | |
|---|
| 154 | | // queries |
|---|
| 155 | | const size_type getSize() { return hashData.size(); } |
|---|
| 156 | | const bool keyExists(const pUString &key); |
|---|
| 157 | | //const bool keyExists(const pBString &key); |
|---|
| 158 | | const bool keyExists(const pInt &key); |
|---|
| 159 | | |
|---|
| 160 | | // dump of contents |
|---|
| 161 | | void varDump(); |
|---|
| 162 | | |
|---|
| 163 | | // lookup |
|---|
| 164 | | pVarP operator[] (const pUString &key); |
|---|
| 165 | | //pVarP operator[] (const pBString &key); |
|---|
| 166 | | pVarP operator[] (const pInt &key); |
|---|
| | 163 | // lookup |
|---|
| | 164 | pVarP operator[] (const pUString &key); |
|---|
| | 165 | //pVarP operator[] (const pBString &key); |
|---|
| | 166 | pVarP operator[] (const pInt &key); |
|---|