33 template<
class K>
struct Equal {
bool operator()(
const K& k1,
const K& k2)
const {
return k1 == k2; } };
36 template<
class K>
struct DeepEqual {
bool operator()(
const K* k1,
const K* k2)
const {
return *k1 == *k2; } };
38 static inline uint32_t
hash(uint32_t x){
return x; }
39 static inline uint32_t
hash(uint64_t x){
return (uint32_t)x; }
40 static inline uint32_t
hash(int32_t x) {
return (uint32_t)x; }
41 static inline uint32_t
hash(int64_t x) {
return (uint32_t)x; }
49 static const int primes [
nprimes] = { 31, 73, 151, 313, 643, 1291, 2593, 5233, 10501, 21013, 42073, 84181, 168451, 337219, 674701, 1349473, 2699299, 5398891, 10798093, 21596719, 43193641, 86387383, 172775299, 345550609, 691101253 };
55 template<
class K,
class D,
class H = Hash<K>,
class E = Equal<K> >
72 bool checkCap(
int new_size)
const {
return new_size > cap; }
74 int32_t
index (
const K& k)
const {
return hash(k) % cap; }
83 int newsize = primes[0];
84 for (
int i = 1; newsize <= cap && i <
nprimes; i++)
90 for (
int i = 0; i < old_cap; i++){
91 for (
int j = 0; j < old[i].
size(); j++){
92 _insert(old[i][j].key, old[i][j].data); }}
97 printf(
" --- rehashing, old-cap=%d, new-cap=%d\n", cap, newsize);
104 Map() : table(nullptr), cap(0), size(0) {}
105 Map(
const H& h,
const E& e) : hash(h), equals(e), table(nullptr), cap(0), size(0){}
109 const D& operator [] (
const K& k)
const 112 const D* res =
nullptr;
114 for (
int i = 0; i < ps.
size(); i++)
115 if (equals(ps[i].key, k))
117 assert(res !=
nullptr);
122 D& operator [] (
const K& k)
127 for (
int i = 0; i < ps.
size(); i++)
128 if (equals(ps[i].key, k))
130 assert(res !=
nullptr);
135 void insert (
const K& k,
const D& d) {
if (checkCap(size+1)) rehash(); _insert(k, d); size++; }
136 bool peek (
const K& k, D& d)
const {
137 if (size == 0)
return false;
139 for (
int i = 0; i < ps.
size(); i++)
140 if (equals(ps[i].key, k)){
146 bool has (
const K& k)
const {
147 if (size == 0)
return false;
149 for (
int i = 0; i < ps.
size(); i++)
150 if (equals(ps[i].key, k))
156 void remove(
const K& k) {
157 assert(table !=
nullptr);
160 for (; j < ps.
size() && !equals(ps[j].key, k); j++);
161 assert(j < ps.
size());
178 delete [] other.
table;
Map(Map< K, D, H, E > &other)
bool operator()(const K &k1, const K &k2) const
uint32_t operator()(const K *k) const
bool operator()(const K *k1, const K *k2) const
bool has(const K &k) const
static const int primes[nprimes]
const vec< Pair > & bucket(int i) const
uint32_t operator()(const K &k) const
static uint32_t hash(uint32_t x)
bool checkCap(int new_size) const
int32_t index(const K &k) const
void insert(const K &k, const D &d)
Map(const H &h, const E &e)
void _insert(const K &k, const D &d)
bool peek(const K &k, D &d) const
const T & last(void) const