//----------------------------------------------------------------------------- // Dictionary.h // Header file for Dictionary ADT storing (key, value) pairs of types KEY_TYPE // and VAL_TYPE. //----------------------------------------------------------------------------- #include #include #include #ifndef DICTIONARY_H_INCLUDE_ #define DICTIONARY_H_INCLUDE_ #define KEY_TYPE char* #define VAL_TYPE int* #define KEY_UNDEF NULL #define VAL_UNDEF NULL #define KEY_FORMAT "%s" #define VAL_FORMAT "%p" #define KEY_CMP(x,y) strcmp(x,y) // Exported type -------------------------------------------------------------- typedef struct DictionaryObj* Dictionary; // Constructors-Destructors --------------------------------------------------- // newDictionary() // Creates a new empty Dictionary. If unique==false (0), then the Dictionary // will accept duplicate keys, i.e. distinct pairs with identical keys. If // unique==true (1 or any non-zero value), then duplicate keys will not be // accepted. In this case, the operation insert(D, k) will enforce the // precondition: lookup(D, k)==VAL_UNDEF Dictionary newDictionary(int unique); // freeDictionary() // Frees heap memory associated with *pD, sets *pD to NULL. void freeDictionary(Dictionary* pD); // Access functions ----------------------------------------------------------- // size() // Returns the number of (key, value) pairs in Dictionary D. int size(Dictionary D); // getUnique() // Returns true (1) if D requires that all pairs have unique keys. Returns // false (0) if D accepts distinct pairs with identical keys. int getUnique(Dictionary D); // lookup() // If Dictionary D contains a (key, value) pair whose key matches k (i.e. if // KEY_CMP(key, k)==0), then returns value. If D contains no such pair, then // returns VAL_UNDEF. VAL_TYPE lookup(Dictionary D, KEY_TYPE k); // Manipulation procedures ---------------------------------------------------- // insert() // Insert the pair (k,v) into Dictionary D. // If getUnique(D) is false (0), then there are no preconditions. // If getUnique(D) is true (1), then the precondition lookup(D, k)==VAL_UNDEF // is enforced. void insert(Dictionary D, KEY_TYPE k, VAL_TYPE v); // delete() // Remove the pair whose key is k from Dictionary D. // Pre: lookup(D,k)!=VAL_UNDEF (i.e. D contains a pair whose key is k.) void delete(Dictionary D, KEY_TYPE k); // makeEmpty() // Reset Dictionary D to the empty state, containing no pairs. void makeEmpty(Dictionary D); // beginForward() // If D is non-empty, starts a forward iteration over D at the first key // (as defined by the order operator KEY_CMP()), then returns the first // value. If D is empty, returns VAL_UNDEF. VAL_TYPE beginForward(Dictionary D); // beginReverse() // If D is non-empty, starts a reverse iteration over D at the last key // (as defined by the order operator KEY_CMP()), then returns the last // value. If D is empty, returns VAL_UNDEF. VAL_TYPE beginReverse(Dictionary D); // currentKey() // If an iteration (forward or reverse) over D has started, returns the // the current key. If no iteration is underway, returns KEY_UNDEF. KEY_TYPE currentKey(Dictionary D); // currentVal() // If an iteration (forward or reverse) over D has started, returns the // value corresponding to the current key. If no iteration is underway, // returns VAL_UNDEF. VAL_TYPE currentVal(Dictionary D); // next() // If an iteration (forward or reverse) over D has started, and has not // reached the last pair, moves to the next key in D (as defined by the // order operator KEY_CMP()), and returns the value corresponding to the // new key. If an iteration has started, and has reached the last pair, // ends the iteration and returns VAL_UNDEF. If no iteration is underway, // returns VAL_UNDEF. VAL_TYPE next(Dictionary D); // prev() // If an iteration (forward or reverse) over D has started, and has not // reached the first pair, moves to the previous key in D (as defined by the // order operator KEY_CMP()), and returns the value corresponding to the // new key. If an iteration has started, and has reached the first pair, // ends the iteration and returns VAL_UNDEF. If no iteration is underway, // returns VAL_UNDEF. VAL_TYPE prev(Dictionary D); // Other operations ----------------------------------------------------------- // printDictionary() // Prints the keys (only) of D in an order determined by the parameter ord. // If ord is "pre", "in" or "post", executes the corresponding tree traversal // on the underlying RBT, printing keys only (no values). If ord is some // other string, prints nothing. void printDictionary(FILE* out, Dictionary D, const char* ord); #endif