// Insert a record // k: The key for the record being inserted. // e: The record being inserted. virtualvoidinsert(const Key& k, const E& e)= 0;
// Remove and return a record. // k: The key of the record to be removed. // Return: A maching record. If multiple records match // "k", remove an arbitrary one. Return NULL if no record // with key "k" exists. virtual E remove(const Key& k)= 0;
// Remove and return an arbitrary record from dictionary. // Return: The record removed, or NULL if none exists. virtual E removeAny()= 0;
// Return: A record matching "k" (NULL if none exists). // If multiple records match, return an arbitrary one. // k: The key of the record to find virtual E find(const Key& k)const= 0;
// Return the number of records in the dictionary. virtualintsize()= 0; };
// Dictionary implemented with an unsorted array-based list template <typename Key, typename E> classUALdict : public Dictionary<Key, E> { private: AList<KVpair<Key,E> >* list; public: UALdict(int size=defaultSize) // Constructor { list = new AList<KVpair<Key,E> >(size); } ~UALdict() { delete list; } // Destructor voidclear(){ list->clear(); } // Reinitialize
// Insert an element: append to list voidinsert(const Key&k, const E& e){ KVpair<Key,E> temp(k, e); list->append(temp); }
// Use sequential search to find the element to remove E remove(const Key& k){ E temp = find(k); // "find" will set list position if(temp != NULL) list->remove(); return temp; }
E removeAny(){ // Remove the last element Assert(size() != 0, "Dictionary is empty"); list->moveToEnd(); list->prev(); KVpair<Key,E> e = list->remove(); return e.value(); }
// Find "k" using sequential search E find(const Key& k)const{ for(list->moveToStart(); list->currPos() < list->length(); list->next()) { KVpair<Key,E> temp = list->getValue(); if (k == temp.key()) return temp.value(); } returnNULL; // "k" does not appear in dictionary } intsize()// Return list size { return list->length(); } };
// Sorted array-based list // Inherit from AList as a protected base class template <typename Key, typename E> classSAList: protected AList<KVpair<Key,E> > { public: SAList(int size=defaultSize) : AList<KVpair<Key,E> >(size) {}
~SAList() {} // Destructor
// Redefine insert function to keep values sorted voidinsert(KVpair<Key,E>& it){ // Insert at right KVpair<Key,E> curr; for (moveToStart(); currPos() < length(); next()) { curr = getValue(); if(curr.key() > it.key()) break; } AList<KVpair<Key,E> >::insert(it); // Do AList insert }
// With the exception of append, all remaining methods are // exposed from AList. Append is not available to SAlist // class users since it has not been explicitly exposed. AList<KVpair<Key,E> >::clear; AList<KVpair<Key,E> >::remove; AList<KVpair<Key,E> >::moveToStart; AList<KVpair<Key,E> >::moveToEnd; AList<KVpair<Key,E> >::prev; AList<KVpair<Key,E> >::next; AList<KVpair<Key,E> >::length; AList<KVpair<Key,E> >::currPos; AList<KVpair<Key,E> >::moveToPos; AList<KVpair<Key,E> >::getValue; };
// Dictionary implemented with a sorted array-based list template <typename Key, typename E> classSALdict : public Dictionary<Key, E> { private: SAList<Key,E>* list; public: SALdict(int size=defaultSize) // Constructor { list = newSAList<Key,E>(size); } ~SALdict() { delete list; } // Destructor voidclear(){ list->clear(); } // Reinitialize
// Insert an element: Keep elements sorted voidinsert(const Key&k, const E& e){ KVpair<Key,E> temp(k, e); list->insert(temp); }
// Use sequential search to find the element to remove E remove(const Key& k){ E temp = find(k); if (temp != NULL) list->remove(); return temp; }
E removeAny(){ // Remove the last element Assert(size() != 0, "Dictionary is empty"); list->moveToEnd(); list->prev(); KVpair<Key,E> e = list->remove(); return e.value(); }
// Find "K" using binary search E find(const Key& k)const{ int l = -1; int r = list->length(); while (l+1 != r) { // Stop when l and r meet int i = (l+r)/2; // Check middle of remaining subarray list->moveToPos(i); KVpair<Key,E> temp = list->getValue(); if (k < temp.key()) r = i; // In left if (k == temp.key()) return temp.value(); // Found it if (k > temp.key()) l = i; // In right } returnNULL; // "k" does not appear in dictionary } intsize()// Return list size { return list->length(); } };