00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef HASHMAP_ARRAY_H_
00046 #define HASHMAP_ARRAY_H_
00047 #include "boost/unordered_map.hpp"
00048 #include "types.h"
00049 #include "tbb/spin_rw_mutex.h"
00050
00051 using namespace boost;
00052 using namespace std;
00053 using namespace tbb;
00054
00055
00056
00057
00058
00059 template<class Key, class Value>
00060 class Hashmap_Array {
00061 public:
00062 Hashmap_Array() {
00063 }
00064 virtual ~Hashmap_Array() {
00065 }
00066 typedef unordered_map<Key, Value> act_map;
00067 typedef typename unordered_map<Key, Value>::iterator act_map_iter;
00068
00069 size_t size() const {
00070 size_t size = 0;
00071 for (int i = 0; i < NUM_MAPS; i++)
00072 size += maps[i].size();
00073 return size;
00074 }
00075
00076 size_t count(Key key) const {
00077 register int ind = hasher(key) & MASK;
00078 const act_map& map = maps[ind];
00079 return map.count(key);
00080 }
00081
00082 Value& operator[](Key key) {
00083 register int ind = hasher(key) & MASK;
00084 act_map& map = maps[ind];
00085 return map[key];
00086 }
00087
00088 void erase(Key key) {
00089 register int ind = hasher(key) & MASK;
00090 act_map& map = maps[ind];
00091 map.erase(key);
00092 }
00093
00094 word_mutex_t* get_lock(Key key) {
00095 register int ind = hasher(key) & MASK;
00096 return &rw_mutexes[ind];
00097 }
00098
00099 word_mutex_t* get_structure_lock() {
00100 return &structure_mutex;
00101 }
00102
00103
00104
00105
00106 class iterator {
00107 private:
00108 act_map_iter current_end;
00109 int current_ind;
00110 act_map* bigram_counts;
00111
00112 public:
00113 act_map_iter current_iter;
00114
00115 iterator(int ind, act_map_iter iter, act_map_iter end, act_map* bc) {
00116 current_iter = iter;
00117 current_ind = ind;
00118 current_end = end;
00119 bigram_counts = bc;
00120 }
00121
00122 void operator++(int) {
00123 current_iter++;
00124 if (current_iter == current_end && current_ind != NUM_MAPS - 1) {
00125 ++current_ind;
00126 for (; current_ind < NUM_MAPS; current_ind++) {
00127 current_iter = bigram_counts[current_ind].begin();
00128 current_end = bigram_counts[current_ind].end();
00129 if (current_iter != current_end)
00130 break;
00131 }
00132 }
00133 }
00134
00135 bool operator!=(iterator& inp) {
00136 return (current_iter != inp.current_iter);
00137 }
00138 };
00139
00140 iterator begin() {
00141 int i = 0;
00142 for (; i < NUM_MAPS; i++) {
00143 if (maps[i].begin() != maps[i].end())
00144 break;
00145 }
00146
00147 if (i == NUM_MAPS) {
00148
00149 iterator beg(i, maps[NUM_MAPS - 1].begin(),
00150 maps[NUM_MAPS - 1].end(), maps);
00151 return beg;
00152 } else {
00153 iterator beg(i, maps[i].begin(), maps[i].end(), maps);
00154 return beg;
00155 }
00156
00157 }
00158
00159 iterator end() {
00160 iterator end(NUM_MAPS - 1, maps[NUM_MAPS - 1].end(),
00161 maps[NUM_MAPS - 1].end(), maps);
00162 return end;
00163 }
00164 private:
00165 const static int NUM_MAPS = 1024;
00166 const static int MASK = NUM_MAPS - 1;
00167 act_map maps[NUM_MAPS];
00168 word_mutex_t rw_mutexes[NUM_MAPS];
00169 word_mutex_t structure_mutex;
00170 hash<Key> hasher;
00171 };
00172
00173 #endif