PPP Exercises: Dictionaries in hash tables


  1. Write a module that implements the following interface:
    interface NewDictionary
    import
      
      :Iter
      :Order
      
    export 
      
      insertError, lookupError :Exception with :String end
      (* Exception handling for 'insert' und 'lookup'. *)
    
        
      T (Element, Key <:Ok) <:Ok
      (* Dictionary of elements of type 'Element'
         with keys of type 'Key'. *)
    
          
      Let Hooks(Element, Key <:Ok ) = 
         Tuple
            (* ## implement as needed!!! *)
         end
      (* Set of elemantary function for dictionary handling. *)
    
      
      new(Element, Key <:Ok  hooks :Hooks(Element Key))
          :T(Element Key)
      (* Builds an empty dictionary being handled by functions
         in tuple 'hooks'. *) 
    
        
      empty(Element, Key <:Ok  dict :T(Element Key)) :Bool
      (* Checks if 'dict' is empty. *)
    
        
      insert(Element, Key <:Ok  dict :T(Element Key)
             element :Element) :Ok
      (* Inserts an 'element' into 'dict'. If 'getKey(element)'
         already exists, then 'raise insertError with 
         ...fmtElement(element)'. *)
    
               
      enter(Element, Key <:Ok  dict :T(Element Key)
            element :Element) :Ok
      (* Inserts an 'element' into 'dict'. If the key retrieved by
         'getKey(element)' already exists in 'dict', then override
         the former entry, so that the new 'element' is really inserted. *)
    
               
      lookup(Element, Key <:Ok  dict :T(Element Key)
             key :Key) :Element
      (* Searches for 'key' and returns the element found in 'dict'. 
         If search is unsuccesful 'raise lookupError with ...fmtKey(key)'. *)
    
      
      delete(Element, Key <:Ok  dict :T(Element Key)
             key :Key) :Ok    
      (* Searches for 'key' and deletes this element in 'dict'. 
         If search is unsuccesful 'raise lookupError with ...fmtKey(key)'. *)
    
    
      elements(Element, Key <:Ok  dict :T(Element Key))
               :Iter.T(Element)
      (* Returns an iteration over the elements of 'dict'. *) 
    
      
      create(Element, Key <:Ok  hooks :Hooks(Element Key)
             from :Iter.T(Element)) :T(Element Key)
        (* Creates a dictionary from the iteration 'from'.
           'hooks' contains functions for dictionary handling
           (see 'new'). *)
    
      end;
    
    Implement the generic 'NewDictionary' using the module 'hashTable :HashTable'. If necessary, modify 'Hooks' considering which of the existing functions are unnecessary and which other functions you might need in addition. Mind that this time the module 'dictionary' must also include a function 'delete'.
  2. Test your module in the same way as in the exercise Dictionaries in binary trees. Considering the instance of hooks, note that there is a function 'hash' in the module 'string'.
    Don't forget to test the additional function 'delete' on one of your dictionaries.

Ulrike Steffens, 1994