1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
#include <algorithm>
#include <assert.h>
#include <list>
#ifndef DynamList_h
#define DynamList_h
#define HASHTABLE_CAPACITY 1009
template <class DataType>
class DynamList
{
public:
DynamList();
DynamList(const list<DataType>*);
~DynamList();
list<DataType>* operator=(const list<DataType>*);
void insert(const DataType&);
bool first(DataType& getData);
bool getNext(DataType&);
bool retrieve(DataType&);
bool remove(DataType&);
bool replace(DataType&);
int getSize() const; // getter
double getLoadFactor() const; // getter
private:
list<DataType>* aList;
typename list<DataType>::iterator it;
typename list<DataType>::const_iterator itRe; // const_ for retrieve only!
void reSize();
bool isPrime(int);
void setWrappedIndex(int); // setter
int wrappedIndex;
int size, current;
int capacity;
};
template <class DataType>
DynamList<DataType>::DynamList()
: size(0), wrappedIndex(0), current(-1), capacity(HASHTABLE_CAPACITY), aList(new list<DataType>[HASHTABLE_CAPACITY])
{ }
// copy constructor, need to fix
template <class DataType>
DynamList<DataType>::DynamList(const list<DataType>* a)
: size(a.size), current(a.current), capacity(a.capacity)
{
list<DataType>* newList = new list<DataType>[capacity];
DataType data;
first(data);
do
{
setWrappedIndex(data.hashCode());
newList[wrappedIndex].push_back(data);
}while(getNext(data));
// delete array
delete [] aList;
aList = newList; // put elements back into the re-sized array
current = wrappedIndex;
/*
// aList = a.aList;
aList = 0;
//if(capacity > 0)
aList = new list<DataType>[capacity];
for (int i = 0; i < capacity; i++)
aList[i] = a.aList[i];
*/
}
// operator= need to fix
// This was the last stuff tried but way off from my original
/*
template <class DataType>
list<DataType>* DynamList<DataType>::operator=(const list<DataType>* a)
{
if (this != &a) // if true
{
size = a.size; // set length
current = a.current; // set current
capacity = a.capacity;
list<DataType>* newList = new list<DataType>[capacity]
// } // close if
//return *this; // return *this
}*/
template <class DataType>
DynamList<DataType>::~DynamList()
{
delete [] aList;
}
template <class DataType>
void DynamList<DataType>::insert(const DataType& newData)
{
setWrappedIndex(newData.hashCode());
it = find(aList[wrappedIndex].begin(), aList[wrappedIndex].end(), newData);
if (it != aList[wrappedIndex].end()) *it = newData; // replace
else
{
aList[wrappedIndex].push_back(newData);
current = wrappedIndex;
size++;
} // close else
if (getLoadFactor() > .8)
reSize();
}
template <class DataType>
void DynamList<DataType>::setWrappedIndex(int DI)
{
if (DI > capacity)
{
wrappedIndex = DI % capacity;
if (wrappedIndex > capacity)
{
while (wrappedIndex > capacity)
wrappedIndex = wrappedIndex % capacity;
} // close if
} // close if
else if (DI < 0)
{
while (DI < 0 && DI < capacity)
DI += capacity;
wrappedIndex = DI;
} // close else-if
else
wrappedIndex = DI;
} // close setWrappedIndex
template <class DataType>
bool DynamList<DataType>::isPrime(int aNum)
{
int max = static_cast <int> (aNum); /* Cast it to remove warnings */
if (aNum <= 0 || aNum == 1 || (aNum % 2 == 0 && aNum != 2)) return false;
for (int i = 3; i < max; i += 2)
if (aNum % i == 0)
return false;
return true;
}
template <class DataType>
void DynamList<DataType>::reSize()
{
int i = 0;
while(!isPrime((capacity * 2) + i))
i++;
int newCapacity = (capacity * 2) + i;
int oldCapacity = capacity;
DataType data;
list<DataType>* newList = new list<DataType>[newCapacity];
first(data);
do
{
capacity = newCapacity;
setWrappedIndex(data.hashCode());
newList[wrappedIndex].push_back(data);
capacity = oldCapacity;
}while(getNext(data));
// delete array
delete [] aList;
aList = newList; // put elements back into the re-sized array
capacity = newCapacity;
current = wrappedIndex;
}
template <class DataType>
bool DynamList<DataType>::first(DataType& getData)
{
assert(current >= -1 && current < capacity);
for (current = 0; current < capacity; current++)
{
if (!aList[current].empty())
{
it = aList[current].begin();
break;
}
}
if (current == capacity) current = -1;
if (current == -1) return false;
getData = *it;
return true;
}
template <class DataType>
bool DynamList<DataType>::getNext(DataType& getData)
{
assert(current >= -1 && current < capacity);
if (current == -1) return false;
assert(!aList[current].empty());
getData = *it++;
if (it == aList[current].end())
{
for (current = current + 1; current < capacity; current++)
{
if (!aList[current].empty())
{
it = aList[current].begin();
break;
}
}
}
if (current == capacity) current = -1;
return current >= 0;
}
template <class DataType>
double DynamList<DataType>::getLoadFactor() const
{
return double(size) / capacity;
}
template <class DataType>
int DynamList<DataType>::getSize() const
{
return size;
} // close get size
template <class DataType>
bool DynamList<DataType>::retrieve(DataType& getData)
{
setWrappedIndex(getData.hashCode());
itRe = find(aList[wrappedIndex].begin(), aList[wrappedIndex].end(), getData);
if (itRe == aList[wrappedIndex].end()) return false;
getData = *itRe;
current = wrappedIndex;
return true;
}
template <class DataType>
bool DynamList<DataType>::remove(DataType& data)
{
setWrappedIndex(data.hashCode());
it = find(aList[wrappedIndex].begin(), aList[wrappedIndex].end(), data);
if (it == aList[wrappedIndex].end()) return false;
aList[wrappedIndex].erase(it);
size--;
current = wrappedIndex;
return true;
} // close remove
template <class DataType>
bool DynamList<DataType>::replace(DataType& data)
{
setWrappedIndex(data.hashCode());
it = find(aList[wrappedIndex].begin(), aList[wrappedIndex].end(), data);
if (it == aList[wrappedIndex].end()) return false;
*it = data;
current = wrappedIndex;
return true;
}
#endif
|