19 #include "mongo/util/assert_util.h"
20 #include "mongo/util/debug_util.h"
23 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
24 inline UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::Area::Area(
unsigned capacity,
26 : _capacity(capacity),
27 _maxProbe(static_cast<unsigned>(capacity * maxProbeRatio)),
28 _entries(new Entry[_capacity]) {}
30 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
31 inline UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::Area::Area(
const Area& other)
32 : _capacity(other._capacity), _maxProbe(other._maxProbe), _entries(new Entry[_capacity]) {
33 for (
unsigned i = 0; i < _capacity; i++) {
34 _entries[i] = other._entries[i];
38 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
39 inline int UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::Area::find(
44 for (
unsigned probe = 0; probe < _maxProbe; probe++) {
45 unsigned pos = (hash + probe) % _capacity;
47 if (!_entries[pos].used) {
49 if (firstEmpty && *firstEmpty == -1)
51 if (!_entries[pos].everUsed)
56 if (_entries[pos].curHash != hash) {
61 if (!sm._equals(key, sm._convertor(_entries[pos].data.first))) {
74 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
75 inline bool UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::Area::transfer(
77 for (
unsigned i = 0; i < _capacity; i++) {
78 if (!_entries[i].used)
82 int loc = newArea->find(
83 sm._convertor(_entries[i].data.first), _entries[i].curHash, &firstEmpty, sm);
90 newArea->_entries[firstEmpty] = _entries[i];
95 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
97 unsigned startingCapacity,
double maxProbeRatio)
98 : _maxProbeRatio(maxProbeRatio), _area(startingCapacity, maxProbeRatio) {
102 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
105 : _size(other._size),
106 _maxProbeRatio(other._maxProbeRatio),
109 _equals(other._equals),
110 _convertor(other._convertor),
111 _convertorOther(other._convertorOther) {}
113 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
114 inline void UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::copyTo(
115 UnorderedFastKeyTable* out)
const {
117 out->_maxProbeRatio = _maxProbeRatio;
122 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
123 inline V& UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::get(
const K_L& key) {
124 const size_t hash = _hash(key);
126 for (
int numGrowTries = 0; numGrowTries < 5; numGrowTries++) {
128 int pos = _area.find(key, hash, &firstEmpty, *
this);
130 return _area._entries[pos].data.second;
134 if (firstEmpty >= 0) {
136 _area._entries[firstEmpty].used =
true;
137 _area._entries[firstEmpty].everUsed =
true;
138 _area._entries[firstEmpty].curHash = hash;
139 _area._entries[firstEmpty].data.first = _convertorOther(key);
140 return _area._entries[firstEmpty].data.second;
146 msgasserted(16471,
"UnorderedFastKeyTable couldn't add entry after growing many times");
149 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
151 const size_t hash = _hash(key);
152 int pos = _area.find(key, hash, NULL, *
this);
158 _area._entries[pos].used =
false;
159 _area._entries[pos].data.second = V();
163 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
165 dassert(it._position >= 0);
166 dassert(it._area == &_area);
169 _area._entries[it._position].used =
false;
170 _area._entries[it._position].data.second = V();
173 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
174 inline void UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::_grow() {
175 unsigned capacity = _area._capacity;
176 for (
int numGrowTries = 0; numGrowTries < 5; numGrowTries++) {
178 Area newArea(capacity, _maxProbeRatio);
179 bool success = _area.transfer(&newArea, *
this);
183 _area.swap(&newArea);
186 msgasserted(16845,
"UnorderedFastKeyTable::_grow couldn't add entry after growing many times");
189 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
190 inline typename UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::const_iterator
194 int pos = _area.find(key, _hash(key), 0, *
this);
200 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
203 return const_iterator();
206 template <
typename K_L,
typename K_S,
typename V,
typename H,
typename E,
typename C,
typename C_LS>
207 inline typename UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::const_iterator
208 UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::begin()
const {
209 return const_iterator(&_area);
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
size_t erase(const K_L &key)
Definition: unordered_fast_key_table_internal.h:150
Definition: unordered_fast_key_table.h:41
const_iterator find(const K_L &key) const
Definition: unordered_fast_key_table_internal.h:191
UnorderedFastKeyTable(unsigned startingCapacity=DEFAULT_STARTING_CAPACITY, double maxProbeRatio=0.05)
Definition: unordered_fast_key_table_internal.h:96
Definition: unordered_fast_key_table.h:130