HOG2
FixedSizeSet.h
Go to the documentation of this file.
1 //
2 // FixedSizeSet.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 7/27/16.
6 // Copyright © 2016 University of Denver. All rights reserved.
7 //
8 
9 #ifndef FixedSizeSet_h
10 #define FixedSizeSet_h
11 
12 #include <string.h>
13 #include <functional>
14 #include <cstddef>
15 template<typename T>
17 
18 template <typename T, class Hash = std::hash<T>>
19 class FixedSizeSet {
20 private:
21  struct field {
22  T item;
24  bool valid;
25  };
26 public:
27  FixedSizeSet(size_t capacity);
28  ~FixedSizeSet();
29  void resize(size_t capacity);
30  void swap(FixedSizeSet<T, Hash> &s);
31  void clear();
32  size_t size() { return currentMemoryEntry/*-removed*/; }
34 
35  iterator begin(){return iterator(&memory[0]);}
37  iterator find(const T &item) const
38  {
39  size_t hash = h(item);
40  for (field* f = hashTable[hash%hashTableSize]; f; f = f->next)
41  {
42  if (f->item == item && f->valid)
43  return iterator(f);
44  }
45  return end();
46  }
47  void erase(iterator &i);
48  void insert(const T &item);
49  void PrintStats();
50 private:
54  size_t capacity;
55  size_t hashTableSize;
56  size_t removed;
57  Hash h;
58 };
59 
60 template <typename T, class Hash>
62 {
63  field **a, *b;
64  size_t c, d, e, f;
65  a = hashTable;
66  b = memory;
67  c = currentMemoryEntry;
68  d = capacity;
69  e = hashTableSize;
70  f = removed;
71  *this = s;
72  s.hashTable = a;
73  s.memory = b;
74  s.currentMemoryEntry = c;
75  s.capacity = d;
76  s.hashTableSize = e;
77  s.removed = f;
78 }
79 
80 
81 template <typename T, class Hash>
83 {
84  hashTableSize = (2*capacity)|1;//capacity*2+3;
85  hashTable = new field*[hashTableSize];
86  memory = new field[capacity];
87  memset(memory, 0x0, capacity*sizeof(field));
88  memset(hashTable, 0x0, hashTableSize*sizeof(field*));
89  currentMemoryEntry = 0;
90  this->capacity = capacity;
91  removed = 0;
92 }
93 
94 template <typename T, class Hash>
96 {
97  delete [] hashTable;
98  delete [] memory;
99  memory = 0;
100  hashTable = 0;
101  currentMemoryEntry = 0;
102  capacity = 0;
103  removed = 0;
104 }
105 
106 template <typename T, class Hash>
107 void FixedSizeSet<T, Hash>::resize(size_t capacity)
108 {
109 // if (capacity <= this->capacity)
110 // {
111 // currentMemoryEntry = 0;
112 // removed = 0;
113 // memset(hashTable, 0x0, hashTableSize*sizeof(field*));
114 // memset(memory, 0x0, capacity*sizeof(field));
115 // }
116 // else {
117  this->capacity = capacity;
118  delete [] hashTable;
119  delete [] memory;
120  hashTableSize = (2*capacity)|1;//capacity*2+3;
121  hashTable = new field*[hashTableSize];
122  memory = new field[capacity];
123  memset(memory, 0x0, capacity*sizeof(field));
124  memset(hashTable, 0x0, hashTableSize*sizeof(field*));
125  currentMemoryEntry = 0;
126  removed = 0;
127 // }
128 }
129 
130 template <typename T, class Hash>
132 {
133  currentMemoryEntry = 0;
134  removed = 0;
135  memset(hashTable, 0x0, capacity*sizeof(field*));
136  memset(memory, 0x0, capacity*sizeof(field));
137 }
138 
139 template <typename T, class Hash>
141 {
142  std::vector<int> dist;
143  for (int x = 0; x < hashTableSize; x++)
144  {
145  int len = 0;
146  for (field* f = hashTable[x]; f; f = f->next)
147  {
148  len++;
149  }
150  if (len >= dist.size())
151  dist.resize(len+1);
152  dist[len]++;
153  }
154  for (int x = 0; x < dist.size(); x++)
155  printf("%d : %d\n", x, dist[x]);
156 }
157 
158 template <typename T, class Hash>
160 {
161 // if ((*i).valid == true)
162 // {
163  (*i).valid = false;
164 // removed++;
165 // }
166 }
167 
168 template <typename T, class Hash>
169 void FixedSizeSet<T, Hash>::insert(const T &item)
170 {
171  size_t hash = h(item);
172  if (hashTable[hash%hashTableSize] == 0)
173  {
174  field *f = &memory[currentMemoryEntry];
175  currentMemoryEntry++;
176  f->item = item;
177  f->next = 0;
178  f->valid = true;
179  hashTable[hash%hashTableSize] = f;
180  }
181  else {
182  for (field* t = hashTable[hash%hashTableSize]; t; t = t->next)
183  {
184  if (t->item == item)
185  return;
186  if (t->next == 0)
187  {
188  field *f = &memory[currentMemoryEntry];
189  currentMemoryEntry++;
190  f->item = item;
191  f->next = 0;
192  f->valid = true;
193  t->next = f;
194  }
195  }
196  }
197 }
198 
199 
200 template<typename T>
201 class fssIterator : public std::iterator<std::random_access_iterator_tag, T, ptrdiff_t, T*, T&>
202 {
203 public:
204  fssIterator(T* ptr = nullptr){m_ptr = ptr;}
205  fssIterator(const fssIterator<T>& rawIterator) = default;
207 
208  fssIterator<T>& operator=(const fssIterator<T>& rawIterator) = default;
209  fssIterator<T>& operator=(T* ptr){m_ptr = ptr;return (*this);}
210 
211  operator bool()const
212  {
213  if(m_ptr)
214  return true;
215  else
216  return false;
217  }
218 
219  bool operator==(const fssIterator<T>& rawIterator)const{return (m_ptr == rawIterator.getConstPtr());}
220  bool operator!=(const fssIterator<T>& rawIterator)const{return (m_ptr != rawIterator.getConstPtr());}
221  fssIterator<T>& operator+=(const ptrdiff_t& movement){m_ptr += movement;return (*this);}
222  fssIterator<T>& operator-=(const ptrdiff_t& movement){m_ptr -= movement;return (*this);}
223  fssIterator<T>& operator++(){++m_ptr;return (*this);}
224  fssIterator<T>& operator--(){--m_ptr;return (*this);}
225  fssIterator<T> operator++(int){auto temp(*this);++m_ptr;return temp;}
226  fssIterator<T> operator--(int){auto temp(*this);--m_ptr;return temp;}
227  fssIterator<T> operator+(const ptrdiff_t& movement){auto oldPtr = m_ptr;m_ptr+=movement;auto temp(*this);m_ptr = oldPtr;return temp;}
228  fssIterator<T> operator-(const ptrdiff_t& movement){auto oldPtr = m_ptr;m_ptr-=movement;auto temp(*this);m_ptr = oldPtr;return temp;}
229 
230  ptrdiff_t operator-(const fssIterator<T>& rawIterator){return std::distance(rawIterator.getPtr(),this->getPtr());}
231  T& operator*(){return *m_ptr;}
232  const T& operator*()const{return *m_ptr;}
233  T* operator->(){return m_ptr;}
234  T* getPtr()const{return m_ptr;}
235  const T* getConstPtr()const{return m_ptr;}
236 
237 protected:
238 
239  T* m_ptr;
240 };
241 //-------------------------------------------------------------------
242 
243 #endif /* FixedSizeSet_h */
fssIterator::operator-
ptrdiff_t operator-(const fssIterator< T > &rawIterator)
Definition: FixedSizeSet.h:230
FixedSizeSet::swap
void swap(FixedSizeSet< T, Hash > &s)
Definition: FixedSizeSet.h:61
FixedSizeSet::field::item
T item
Definition: FixedSizeSet.h:22
FixedSizeSet::capacity
size_t capacity
Definition: FixedSizeSet.h:54
FixedSizeSet::clear
void clear()
Definition: FixedSizeSet.h:131
fssIterator::operator--
fssIterator< T > & operator--()
Definition: FixedSizeSet.h:224
FixedSizeSet::field
Definition: FixedSizeSet.h:21
fssIterator::operator-=
fssIterator< T > & operator-=(const ptrdiff_t &movement)
Definition: FixedSizeSet.h:222
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
FixedSizeSet::find
iterator find(const T &item) const
Definition: FixedSizeSet.h:37
fssIterator::operator=
fssIterator< T > & operator=(T *ptr)
Definition: FixedSizeSet.h:209
FixedSizeSet::field::valid
bool valid
Definition: FixedSizeSet.h:24
FixedSizeSet::memory
field * memory
Definition: FixedSizeSet.h:52
fssIterator::operator=
fssIterator< T > & operator=(const fssIterator< T > &rawIterator)=default
FixedSizeSet::size
size_t size()
Definition: FixedSizeSet.h:32
fssIterator::operator++
fssIterator< T > operator++(int)
Definition: FixedSizeSet.h:225
fssIterator::operator*
const T & operator*() const
Definition: FixedSizeSet.h:232
fssIterator::operator+
fssIterator< T > operator+(const ptrdiff_t &movement)
Definition: FixedSizeSet.h:227
fssIterator::getConstPtr
const T * getConstPtr() const
Definition: FixedSizeSet.h:235
fssIterator::operator->
T * operator->()
Definition: FixedSizeSet.h:233
FixedSizeSet::hashTable
field ** hashTable
Definition: FixedSizeSet.h:51
fssIterator::operator!=
bool operator!=(const fssIterator< T > &rawIterator) const
Definition: FixedSizeSet.h:220
fssIterator::~fssIterator
~fssIterator()
Definition: FixedSizeSet.h:206
fssIterator::operator--
fssIterator< T > operator--(int)
Definition: FixedSizeSet.h:226
FixedSizeSet::erase
void erase(iterator &i)
Definition: FixedSizeSet.h:159
fssIterator::getPtr
T * getPtr() const
Definition: FixedSizeSet.h:234
FixedSizeSet
Definition: FixedSizeSet.h:19
FixedSizeSet::PrintStats
void PrintStats()
Definition: FixedSizeSet.h:140
fssIterator::operator+=
fssIterator< T > & operator+=(const ptrdiff_t &movement)
Definition: FixedSizeSet.h:221
FixedSizeSet::begin
iterator begin()
Definition: FixedSizeSet.h:35
FixedSizeSet::FixedSizeSet
FixedSizeSet(size_t capacity)
Definition: FixedSizeSet.h:82
FixedSizeSet::insert
void insert(const T &item)
Definition: FixedSizeSet.h:169
FixedSizeSet::removed
size_t removed
Definition: FixedSizeSet.h:56
fssIterator::operator==
bool operator==(const fssIterator< T > &rawIterator) const
Definition: FixedSizeSet.h:219
FixedSizeSet::field::next
field * next
Definition: FixedSizeSet.h:23
fssIterator
Definition: FixedSizeSet.h:16
fssIterator::m_ptr
T * m_ptr
Definition: FixedSizeSet.h:239
FixedSizeSet::end
iterator end() const
Definition: FixedSizeSet.h:36
fssIterator::operator++
fssIterator< T > & operator++()
Definition: FixedSizeSet.h:223
FixedSizeSet::resize
void resize(size_t capacity)
Definition: FixedSizeSet.h:107
fssIterator::fssIterator
fssIterator(T *ptr=nullptr)
Definition: FixedSizeSet.h:204
FixedSizeSet::hashTableSize
size_t hashTableSize
Definition: FixedSizeSet.h:55
FixedSizeSet::iterator
fssIterator< field > iterator
Definition: FixedSizeSet.h:33
FixedSizeSet::currentMemoryEntry
size_t currentMemoryEntry
Definition: FixedSizeSet.h:53
FixedSizeSet::~FixedSizeSet
~FixedSizeSet()
Definition: FixedSizeSet.h:95
fssIterator::operator*
T & operator*()
Definition: FixedSizeSet.h:231
FixedSizeSet::h
Hash h
Definition: FixedSizeSet.h:57
fssIterator::operator-
fssIterator< T > operator-(const ptrdiff_t &movement)
Definition: FixedSizeSet.h:228