Dynalib Utils
DynaHashSet.h
Go to the documentation of this file.
1 //
2 // Created by Ken Kopelson on 27/12/17.
3 //
4 
5 #ifndef DYNAHASHSET_H
6 #define DYNAHASHSET_H
7 
8 #include <typeinfo>
9 #include <iostream>
10 #include "TypeDefs.h"
11 #include "Exception.h"
12 #include "ICopyable.h"
13 #include "IHashable.h"
14 
15 #define INVALID_INDEX -1
16 
17 #define MAKE_SETTYPE_DEF(E, T) \
18  typedef DynaHashSet<E> T##Set
19 
25 template <typename V> class SetEntry : IComparable<SetEntry<V>>, ICopyable<SetEntry<V>> {
26  V* _value = nullptr;
27  bool _ownsValue;
28 
29 public:
30  SetEntry(V* value, bool ownsValue);
31  virtual ~SetEntry<V>();
32 
33  SetEntry(const SetEntry<V>& other);
34  SetEntry<V>* copy() override;
35 
36  V* getValue() const { return _value; }
37 
38  const V* setValue(V* newValue);
39  bool operator== (const SetEntry<V>& other) const override;
40  bool isOwnsValue() const { return _ownsValue; }
41 };
42 
47 // template <typename V> class SetObject : IHashable<V> {
48 // public:
49 // SetObject<V>() = default;
50 // virtual ~SetObject() = default;
51 
52 // int hashCode() const override {
53 // return 0;
54 // }
55 // bool operator== (const V& other) const override {
56 // return *this == other;
57 // }
58 // };
59 
60 template <typename V> class DynaSetIter;
61 template <typename V> class SetIter;
62 
63 template <typename V> class DynaHashSet : public ICopyable<DynaHashSet<V>> {
64 protected:
65 
66  friend class SetEntry<V>;
67  friend class DynaSetIter<V>;
68  friend class SetIter<V>;
69 
70  constexpr static int INITIAL_SIZE = 3;
71  constexpr static double LOAD_FACTOR = 0.75;
72 
75 
76  SetEntry<V>** _table = nullptr;
77  int _count;
78  int _capacity;
79  int _freeCells; // _capacity - _bufEnd + deleteCells
80  int _modCount;
82 
83  void _init(int size);
84  int _getHashCode(const V* value) const;
85  void _reHash(int newCapacity);
86  int _getTableIndex(V* value);
87 
88 public:
90  static V* nullObject;
91 
92  explicit DynaHashSet();
93  explicit DynaHashSet(int size);
94  virtual ~DynaHashSet();
95 
96  DynaHashSet(const DynaHashSet<V>& other);
97  DynaHashSet<V>* copy() override;
98 
99  //=========================================================================================
100  // Getters/Setters
101  //=========================================================================================
102  int count() const { return _count; }
103  int capacity() const { return _capacity; }
104  int freeCells() const { return _freeCells; }
105  bool isEmpty() const { return _count == 0; }
106  bool contains(V* obj) { return getEntry(obj) != nullptr; }
107  bool isOwnsMembers() const { return _ownsMembers; }
108  void setOwnsMembers(bool ownsMembers) {
109  _ownsMembers = ownsMembers;
110  }
111 
112  //=========================================================================================
113  // Public Methods
114  //=========================================================================================
115  SetEntry<V>* getEntry(V* value);
116  SetEntry<V>* getEntry(V value);
117  bool add(V* value);
118  bool add(V value);
119  V* remove(V* value);
120  V* remove(V value);
121  void deleteEntry(V* value);
122  void deleteEntry(V value);
123  SetEntry<V>* removeEntry(SetEntry<V>* entry);
124  void clear();
125 
128 
129  SetIter<V> values();
130 };
131 
132 template <typename V> class DynaSetIter {
133  int _curIndex = 0;
134  int _lastReturned = INVALID_INDEX;
135  int _expectedModCount;
136  const DynaHashSet<V>* _set = nullptr;
137 
138 public:
139  DynaSetIter (const DynaHashSet<V>* set, int start)
140  : _curIndex(start), _expectedModCount(0), _set(set) {}
141 
142  int getIndex() { return _curIndex; }
143  bool hasNext() { return _curIndex < _set->_capacity; }
144  bool hasPrev() { return _curIndex >= 0; }
145 
146  bool operator== (const DynaSetIter<V>& other) const {
147  return _curIndex == other._curIndex;
148  }
149 
150  bool operator!= (const DynaSetIter<V>& other) const {
151  return _curIndex != other._curIndex;
152  }
153 
154  SetEntry<V>* operator* () const {
155  return _set->_table[_curIndex];
156  }
157 
158  const DynaSetIter<V>& operator++ () {
159  ++_curIndex;
160  return *this;
161  }
162 
163  const DynaSetIter<V>& operator-- () {
164  --_curIndex;
165  return *this;
166  }
167 };
168 
169 template <typename V> class SetIter {
170  int _curIndex = 0;
171  const DynaHashSet<V>* _set = nullptr;
172 
173 public:
174  SetIter (const DynaHashSet<V>* set, int start);
175  explicit SetIter (const DynaHashSet<V>* set) : SetIter(set, 0) {}
176 
177  int getIndex() { return _curIndex; }
178  bool hasNext() { return _curIndex < _set->_capacity; }
179  bool hasPrev() { return _curIndex >= 0; }
180 
181  bool operator== (const SetIter<V>& other) const {
182  return _curIndex == other._curIndex;
183  }
184 
185  bool operator!= (const SetIter<V>& other) const {
186  return _curIndex != other._curIndex;
187  }
188 
189  V* operator* () const {
190  V* value = _set->_table[_curIndex]->getValue();
191  return value == _set->nullObject ? nullptr : value;
192  }
193 
194  const SetIter<V>& operator++ ();
195  const SetIter<V>& operator-- ();
196 
198  return SetIter<V>( _set, _curIndex );
199  }
200 
202  return SetIter<V>( _set, _set->capacity() );
203  }
204 };
205 
206 
207 #endif //DYNAHASHSET_H
GeneratorWrapper< T > values(std::initializer_list< T > values)
Definition: catch.hpp:4009
static V * nullObject
Definition: DynaHashSet.h:90
int _modCount
Definition: DynaHashSet.h:80
bool hasNext()
Definition: DynaHashSet.h:143
bool hasNext()
Definition: DynaHashSet.h:178
bool isEmpty() const
Definition: DynaHashSet.h:105
int _count
Definition: DynaHashSet.h:77
V * getValue() const
Definition: DynaHashSet.h:36
SetEntry< V > * copy() override
Definition: DynaHashSetImpl.h:102
static V nullObjectInstance
Definition: DynaHashSet.h:89
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:4005
Definition: DynaHashSet.h:25
bool contains(V *obj)
Definition: DynaHashSet.h:106
int getIndex()
Definition: DynaHashSet.h:142
bool operator==(const SetEntry< V > &other) const override
Definition: DynaHashSetImpl.h:197
bool hasPrev()
Definition: DynaHashSet.h:144
SetEntry(V *value, bool ownsValue)
Definition: DynaHashSetImpl.h:74
SetIter(const DynaHashSet< V > *set)
Definition: DynaHashSet.h:175
int capacity() const
Definition: DynaHashSet.h:103
bool isOwnsValue() const
Definition: DynaHashSet.h:40
int count() const
Definition: DynaHashSet.h:102
void setOwnsMembers(bool ownsMembers)
Definition: DynaHashSet.h:108
int _capacity
Definition: DynaHashSet.h:78
bool _ownsMembers
Definition: DynaHashSet.h:81
DynaSetIter(const DynaHashSet< V > *set, int start)
Definition: DynaHashSet.h:139
#define INVALID_INDEX
Definition: DynaHashSet.h:15
bool isOwnsMembers() const
Definition: DynaHashSet.h:107
int getIndex()
Definition: DynaHashSet.h:177
Definition: DynaHashSet.h:63
CONSTCD11 bool operator!=(const day &x, const day &y) NOEXCEPT
Definition: date.h:1282
Definition: DynaHashSet.h:61
int add(int code, T value)
Definition: HashCoder.h:34
bool hasPrev()
Definition: DynaHashSet.h:179
Definition: DynaHashSet.h:60
Definition: IComparable.h:8
int _freeCells
Definition: DynaHashSet.h:79
SetIter< V > end()
Definition: DynaHashSet.h:201
static SetEntry< V > * deletedObject
Definition: DynaHashSet.h:74
SetIter< V > begin()
Definition: DynaHashSet.h:197
static SetEntry< V > deletedObjectInstance
Definition: DynaHashSet.h:73
Definition: ICopyable.h:8
SetEntry< V > ** _table
Definition: DynaHashSet.h:76
int freeCells() const
Definition: DynaHashSet.h:104
const V * setValue(V *newValue)
Definition: DynaHashSetImpl.h:192