Hash Table
Nov 28, 2011 at 1:17am UTC
Im creating a Hash Table with chaining with an array of STL list(s). My getNext function works but after calling the first function then getNext it gives me the same thing. Don't know why. Also when using my retrieve function how would I get the correct one at that location since Im using chaining. Also can I resize the array even if im using #define HASHTABLE_CAPACITY. I need to re-size when the load factor gets to a certain %
MY DataType is a struct
1 2 3 4 5
struct Node
{
string term, course, instructor;
};
I use term & course to get desired index then its wrapped if needed
HERE IS MY templated class HashTable.h
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
#include <algorithm>
#include <assert.h> // for assertions
#include <list>
#ifndef HashTable_h
#define HashTable_h
#define HASHTABLE_CAPACITY 1009
template <class DataType>
class HashTable
{
public :
HashTable(); // constructor
HashTable(const list<DataType>&);
void insert(int , const DataType&); // setter
void setWrappedIndex(int ); // setter
bool first(DataType& getData);
bool getNext(DataType&);
bool retrieve(DataType&, int );
int getWrappedIndex() const ; // getter
int getSize() const ; // getter
double getLoadFactor() const ; // getter
private :
list<DataType> aList[HASHTABLE_CAPACITY];
typename list<DataType>::iterator it;
int wrappedIndex;
int size, current;
}; // close HashTable
template <class DataType>
HashTable<DataType>::HashTable()
: size(0), wrappedIndex(0), current(-1)
{}
template <class DataType>
HashTable<DataType>::HashTable(const list<DataType>& a)
: size(a.size), current(a.current)
{
aList = a.aList;
}
template <class DataType>
void HashTable<DataType>::insert(int DI, const DataType& newData)
{
setWrappedIndex(DI);
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
}
template <class DataType>
void HashTable<DataType>::setWrappedIndex(int DI)
{
if (DI > HASHTABLE_CAPACITY)
{
wrappedIndex = DI % HASHTABLE_CAPACITY;
if (wrappedIndex > HASHTABLE_CAPACITY)
{
while (wrappedIndex > HASHTABLE_CAPACITY)
wrappedIndex = wrappedIndex % HASHTABLE_CAPACITY;
} // close if
} // close if
else if (DI < 0)
{
while (DI < 0 && DI < HASHTABLE_CAPACITY)
{
DI += HASHTABLE_CAPACITY;
}
wrappedIndex = DI;
} // close else-if
else
wrappedIndex = DI;
} // close setWrappedIndex
template <class DataType>
bool HashTable<DataType>::first(DataType& getData)
{
assert(current >= -1 && current < HASHTABLE_CAPACITY);
for (current = 0; current < HASHTABLE_CAPACITY; current++)
{
if (!aList[current].empty())
{
it = aList[current].begin();
break ;
}
}
if (current == HASHTABLE_CAPACITY) current = -1;
if (current == -1) return false ;
getData = *it;
return true ;
}
template <class DataType>
bool HashTable<DataType>::getNext(DataType& getData)
{
assert(current >= -1 && current < HASHTABLE_CAPACITY);
if (current == -1) return false ;
assert(!aList[current].empty());
getData = *it++;
if (it == aList[current].end())
{
for (current = current + 1; current < HASHTABLE_CAPACITY; current++)
{
if (!aList[current].empty())
{
it = aList[current].begin();
break ;
}
}
}
if (current == HASHTABLE_CAPACITY) current = -1;
return current >= 0;
}
template <class DataType>
int HashTable<DataType>::getWrappedIndex() const
{
return wrappedIndex;
}
template <class DataType>
int HashTable<DataType>::getSize() const
{
return size;
} // close get size
template <class DataType>
double HashTable<DataType>::getLoadFactor() const
{
return size / HASHTABLE_CAPACITY;
}
template <class DataType>
bool HashTable<DataType>::retrieve(DataType& data, int DI)
{
bool results = false ;
setWrappedIndex(DI);
current = wrappedIndex;
// aList[current].empty()
if (!aList[current].empty())
{
it = aList[current].begin();
data = *it;
results = true ;
}
return results;
}
#endif
Last edited on Nov 28, 2011 at 1:51am UTC
Topic archived. No new replies allowed.