#include "hmap.h" #include "hiterator.h" #include // Owen Astrachan // 11/8/95 template HMap::HMap(unsigned int (*hash) (const Key &), int size) : myList(size,0), myFunction(hash) // postcondition: all fields constructed, initialized { // work done in initializer list } template HMap::~HMap() { // nothing to be freed } template Value & HMap::GetValue(const Key & key) // postcondition: returns value associated with key in map // exception: if key not in map, error printed, program halted { Node >* index = Find(key); // find // location if (index != 0) { return index->info.second; } cerr << "Error in GetValue, aborting execution" << endl; abort(); } template const Value & HMap::GetValue(const Key & key) const // postcondition: returns value associated with key in map // exception: if key not in map, error printed, program halted { Node >* index = Find(key); // find // location if (index != 0) { return index->info.second; } cerr << "Error in GetValue (const), aborting execution" << endl; abort(); } template void HMap::Apply(void (*Func) (Pair & p)) // postcondition: Func() applied to all map elements { int k; for(k=0; k < myList.Length(); k++) { Node >* ptr = myList[k]; while (ptr) { (*Func)(ptr->info); ptr = ptr->next; } } } template void HMap::Apply(MapBase & obj) // postcondition: obj.Function() applied to all map elements { int k; for(k=0; k < myList.Length(); k++) { Node >* ptr = myList[k]; while (ptr) { obj.Function(ptr->info.first,ptr->info.second); ptr = ptr->next; } } } template bool HMap::IncludesKey(const Key & key) const // postcondition: returns true if key in map, otherwise returns false { return Find(key) != 0; } template Node > * HMap::Find(const Key & key) const // postcondition: returns index of key in private vector (pointer) // returns 0 if key NOT in vector { int loc = (*myFunction)(key) % myList.Length(); Node > * ptr = myList[loc]; while (ptr) { if (ptr->info.first == key) return ptr; ptr = ptr->next; } return 0; } template void HMap::GetValue(Pair & pair) // postcondition: pair = pair associated with key (pair.first) in map // if key NOT in map, no value assigned { Node > * index = Find(pair.first); if (index != 0) { pair = index->info; } } template void HMap::Delete(const Key & key) // postcondition: returns index of key in private vector (pointer) // returns 0 if key NOT in vector { int loc = (*myFunction)(key) % myList.Length(); Node > * ptr = myList[loc]; Node > * trail = 0; while (ptr && ptr->info.first!=key) { trail = ptr; ptr = ptr->next; } if (ptr) { if (trail) { trail->next = ptr->next; } else //Head of list { myList[loc] = ptr->next; } delete ptr; } } template void HMap::Insert(const Pair & pair) // precondition: pair.first key NOT stored in map // postcondition: pair is added to map (maps pair.first -> pair.second) { Node > * index = Find(pair.first); if (index == 0) { int loc = (*myFunction)(pair.first) % myList.Length(); index = new Node < Pair >; index->next = myList[loc]; index->info = pair; myList[loc] = index; myNumElts++; } } template void HMap::Insert(const Key & key, const Value & val) { Insert(Pair(key,val)); } template Iterator > * HMap::MakeIterator() // postcondition: returns dynamically allocated iterator { Iterator > * temp = new HMapIterator >(myList); return temp; }