5 #ifndef DYNAHASHSETIMPL_H 6 #define DYNAHASHSETIMPL_H 16 #define MAKE_SETTYPE_INSTANCE(E, T) \ 17 template class DynaHashSet<E>; \ 18 typedef DynaHashSet<E> T##Set 25 memset((
void*)_table, 0, size *
sizeof(
SetEntry<V>*));
31 hash = value->hashCode();
40 int oldCapacity = _capacity;
42 memset((
void*)newTable, 0, newCapacity *
sizeof(
SetEntry<V>*));
43 for (
int idx = 0; idx < oldCapacity; ++idx) {
45 if (obj ==
nullptr || obj == deletedObject)
47 int hash = _getHashCode(obj->
getValue());
48 int index = ( hash & 0x7FFFFFF ) % newCapacity;
50 while (newTable[index] !=
nullptr) {
51 index = ((index + offset) & 0x7FFFFFFF) % newCapacity;
52 offset = offset * 2 + 1;
56 newTable[index] = obj;
60 _capacity = newCapacity;
61 _freeCells = newCapacity - _count;
75 _value(value), _ownsValue(ownsValue) {
79 _ownsValue = other._ownsValue;
81 if (other._value !=
nullptr) {
83 _value = (V*) (other._value)->copy();
86 _value = other._value;
92 _value = other._value;
113 _count(0), _capacity(INITIAL_SIZE), _freeCells(INITIAL_SIZE), _modCount(0), _ownsMembers(true) {
125 if (entry !=
nullptr) {
142 for (
int i = 0, len = other.
_capacity; i < len; ++i) {
144 if (entry !=
nullptr) {
160 for (;
hasNext() && (table[_curIndex] ==
nullptr || table[_curIndex] == _set->deletedObject); ++_curIndex);
164 if (value ==
nullptr)
166 int hash = _getHashCode(value);
167 int index = ( hash & 0x7FFFFFF ) % _capacity;
171 while ( entry !=
nullptr &&
172 (entry == deletedObject || !((_getHashCode(entry->
getValue()) == hash) && (*(entry->
getValue()) == *value)))) {
173 index = ((index + offset) & 0x7FFFFFFF) % _capacity;
174 entry = _table[index];
175 offset = offset * 2 + 1;
193 V* oldValue = _value;
200 return v1 == v2 || (v1 !=
nullptr && *v1 == *v2);
211 int index = _getTableIndex(value);
212 return _table[index];
216 return getEntry(&value);
220 if (value ==
nullptr)
222 int hash = _getHashCode(value);
223 int index = ( hash & 0x7FFFFFF ) % _capacity;
228 while ( entry !=
nullptr &&
229 (entry == deletedObject || !((_getHashCode(entry->
getValue()) == hash) && (*(entry->
getValue()) == *value)))) {
230 if (entry == deletedObject)
233 index = ((index + offset) & 0x7FFFFFFF) % _capacity;
234 entry = _table[index];
235 offset = offset * 2 + 1;
240 if (entry ==
nullptr) {
241 if (deletedIdx != -1)
249 if (1 - (_freeCells / (
double)_capacity) > LOAD_FACTOR) {
251 if (1 - (_freeCells / (
double)_capacity) > LOAD_FACTOR) {
252 _reHash(_capacity * 2 + 1);
263 return add(
new V(value));
267 int index = _getTableIndex(value);
269 if (_table[index] !=
nullptr && _table[index] != deletedObject) {
270 V* saveValue = _table[index]->getValue();
271 _table[index]->setValue(
nullptr);
272 delete _table[index];
273 _table[index] = deletedObject;
283 return remove(&
value);
287 int index = _getTableIndex(value);
289 if (_table[index] !=
nullptr && _table[index] != deletedObject) {
290 delete _table[index];
291 _table[index] = deletedObject;
298 return deleteEntry(&value);
304 int index = _getTableIndex(ent->
getValue());
306 if (_table[index] !=
nullptr && _table[index] != deletedObject) {
308 _table[index] = deletedObject;
319 for (
int idx = 0; idx < _capacity; ++idx) {
321 if (entry !=
nullptr) {
326 _table[idx] =
nullptr;
330 _freeCells = _capacity;
349 (table[_curIndex] ==
nullptr || table[_curIndex] == _set->deletedObject); ++_curIndex);
356 (table[_curIndex] ==
nullptr || table[_curIndex] == _set->deletedObject); --_curIndex);
369 #endif //DYNAHASHSETIMPL_H
int _modCount
Definition: DynaHashSet.h:80
bool hasNext()
Definition: DynaHashSet.h:178
GeneratorWrapper< std::tuple< Ts... > > table(std::initializer_list< std::tuple< typename std::decay< Ts >::type... >> tuples)
Definition: catch.hpp:4058
int _getTableIndex(V *value)
Definition: DynaHashSetImpl.h:163
int _count
Definition: DynaHashSet.h:77
SetIter< V > values()
Definition: DynaHashSetImpl.h:342
virtual ~SetEntry()
Definition: DynaHashSetImpl.h:96
V * getValue() const
Definition: DynaHashSet.h:36
SetEntry< V > * copy() override
Definition: DynaHashSetImpl.h:102
V * remove(V *value)
Definition: DynaHashSetImpl.h:266
void _init(int size)
Definition: DynaHashSetImpl.h:23
int _getHashCode(const V *value) const
Definition: DynaHashSetImpl.h:28
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:4005
Definition: DynaHashSet.h:25
void clear()
Definition: DynaHashSetImpl.h:318
bool operator==(const SetEntry< V > &other) const override
Definition: DynaHashSetImpl.h:197
virtual ~DynaHashSet()
Definition: DynaHashSetImpl.h:122
SetEntry(V *value, bool ownsValue)
Definition: DynaHashSetImpl.h:74
SetEntry< V > * removeEntry(SetEntry< V > *entry)
Definition: DynaHashSetImpl.h:301
void _reHash(int newCapacity)
Definition: DynaHashSetImpl.h:39
static constexpr int INITIAL_SIZE
Definition: DynaHashSet.h:70
SetEntry< V > * getEntry(V *value)
Definition: DynaHashSetImpl.h:210
int _capacity
Definition: DynaHashSet.h:78
bool _ownsMembers
Definition: DynaHashSet.h:81
void deleteEntry(V *value)
Definition: DynaHashSetImpl.h:286
Definition: DynaHashSet.h:63
const SetIter< V > & operator++()
Definition: DynaHashSetImpl.h:346
Definition: DynaHashSet.h:61
int add(int code, T value)
Definition: HashCoder.h:34
bool add(V *value)
Definition: DynaHashSetImpl.h:219
DynaSetIter< V > begin()
Definition: DynaHashSetImpl.h:334
DynaSetIter< V > end()
Definition: DynaHashSetImpl.h:338
Definition: Exception.h:13
bool hasPrev()
Definition: DynaHashSet.h:179
Definition: DynaHashSet.h:60
DynaHashSet< V > * copy() override
Definition: DynaHashSetImpl.h:154
DynaHashSet()
Definition: DynaHashSetImpl.h:112
SetIter(const DynaHashSet< V > *set, int start)
Definition: DynaHashSetImpl.h:158
int _freeCells
Definition: DynaHashSet.h:79
SetEntry< V > ** _table
Definition: DynaHashSet.h:76
const SetIter< V > & operator--()
Definition: DynaHashSetImpl.h:353
const V * setValue(V *newValue)
Definition: DynaHashSetImpl.h:192