#ifndef _MAP_H #define _MAP_H #include "iterator.h" // Owen Astrachan // 11/9/95, for CPS 100 // // abstract base class for a Map class // (sometimes called Table or Dictionary or Associative Array class) // // The class Map supports a mapping of Keys -> Values // for example: string -> int // // Abstract functions // // // Apply(function pointer) -- applies passed function to ALL map entries // (this is an internal iterator) // use: map.Apply(func); // // Apply(MapBase & obj) -- apply a class inherited from MapBase // to ALL map entries, for MapBase see below // // use: map.Apply(instance); -- instance inherits from MapBase // // bool IncludesKey(key) -- returns true if key stored in map // // use: if (map.IncludesKey("apple")) ... // // bool IncludesKey(key,value) -- returns true if key in map, if true // returns value associated with key // // use: if (map.IncludesKey("apple",value)) ... // // Value & GetValue(key) -- returns reference to Value associated // with key, error if key not in table // (const version exists too) // // use: value = map.GetValue("apple") // // GetValue(pair); -- returns a pair with given key // (and associated value) // use: map.GetValue(p); // // Insert(Key,Value) -- add new key,value pair // Insert(pair) -- add new pair (with key) to map // if key in map this is a no-op // // use: map.Insert(key,value); // use: map.Insert(p); // // MakeIterator() -- returns pointer to a usable iterator // the iterator returns (Key,Value) pairs // template struct Pair { Key first; // data Value second; // associated value Pair() {}; // default and other constructors Pair(const Key & key, const Value & val) : first(key), second(val) {} }; template class MapBase { public: virtual ~MapBase() {}; virtual void Function(Key & key, Value & value) = 0; virtual void Report() = 0; //reporting function }; template class Map { public: virtual ~Map() {}; virtual void Apply(void (*Func) (Pair & p)) = 0; // apply Func virtual void Apply(MapBase & obj) = 0; // apply obj virtual bool IncludesKey(const Key & key) const = 0; // is key in map? virtual const Value& GetValue(const Key & key) const = 0; // get value virtual bool IncludesKey(const Key & key, Value & val) = 0; virtual Value& GetValue(const Key & key) = 0; // get value virtual void GetValue(Pair & pair) = 0; // get value virtual void Delete(const Key & key) = 0; virtual void Insert(const Key & key, const Value & value) = 0; virtual void Insert(const Pair & pair) = 0; // add pair to map // (no duplicates) virtual Iterator > * MakeIterator() = 0; }; // void Apply(void (*Func) (Pair & p)) // postcondition: Func() applied to all map elements // // void Apply(MapBase & obj) // postcondition: obj.Function() applied to all map elements // // bool IncludesKey(const Key & key) // postcondition: returns true if key in map, otherwise returns false // // Value& GetValue(const Key & key) // postcondition: returns value associated with key in map, // ERROR : if key not in map, garbage returned (should be exception) // // void GetValue(Pair & pair) // postcondition: pair = pair associated with key (pair.first) in map // if key NOT in map, no value assigned // // void Insert(const Key & key, const Value & val) // precondition: key NOT stored in map // postcondition: (key,value) stored in map // // void Insert(const Pair & pair) // precondition: pair.first key NOT stored in map // postcondition: pair is added to map (map pair.first -> pair.second) // // Iterator > * MakeIterator() // postcondition: returns dynamically allocated iterator #endif // _MAP_H undefined