Prof. Dr. Theo D'Hondt
Last revision: August 7th, 1996

Dictionary Example


 [AbstractDictionary µvar;
  SmallDictionary µvar;
  FastDictionary µvar;
  
  Self makeAbstractDictionary
    µmethod:
       [names  µvar: Vector length: 0 of: "";
        values µvar: Vector length: 0 of: "";
 
        self at: key
          µmethod: 
             [self at: key 
                   notFound: ["key not found" µerror]];

        self at: key notFound: block
          µmethod: 
             [index µvar: self indexAtKey: key;
              index < 0     
                µthen: 
                   [block µvalue]
                µelse: 
                   [values at: index]];

        self at: key put: value
          µmethod: 
             [self at: key put: value
                   duplicate:  ["duplicate key" µerror]];

        self at: key put: value duplicate: block
          µmethod: 
             [index µvar: self newIndexAtKey: key;
              index < 0     
                µthen: 
                   [block µvalue]
                µelse: 
                   [values at: index put: value;
                    self]];

        self at: key replace: value
          µmethod: 
             [self at: key replace: value 
                           notFound: ["key not found" µerror]];

        self at: key replace: value notFound: block
          µmethod: 
             [index µvar: self indexAtKey: key;
              index < 0     
                µthen:  
                   [block µvalue]
                µelse: 
                   [values at: index put: value;
                    self]];
                    
        self size
          µmethod: 
             ["abstract method" µerror];

        self display
          µmethod: 
             ["abstract method" µerror];

        self indexAtKey: key
          µmethod: 
             ["abstract method" µerror];

        self newIndexAtKey: key
          µmethod: 
             ["abstract method" µerror];

        Self makeSmallDictionary
          µmethod:
             [self size
                µmethod: 
                   [names length];

              self display
                µmethod: 
                   [index µvar: integer µclone;
                    index µfrom: 0
                          µto: names length - 1
                          µdo: 
                             [(names at: index) display;
                              " >>> " display;
                              (values at: index) display;
                              Eoln display];
                    self];

              self indexAtKey: key
                µmethod: 
                   [index µvar: names length - 1;
                    ((index>=0) and:[(names at: index) <> key])
                         µwhile:
                             [index µ<- index - 1];
                    index];

              self newIndexAtKey: key
                µmethod: 
                   [index µvar: names length;
                    (self indexAtKey: key) < 0
                       µthen:
                          [self extend;
                           names at: index put: key;
                           index]
                       µelse: [-1]];

              self extend
                µmethod: 
                   [oldNames µvar: names;
                    oldValues µvar: values;
                    newSize µvar: names length + 1;
                    index µvar: integer µclone;
                    names µ<- Vector length: newSize of: "";
                    values µ<- Vector length: newSize of: "";
                    index
                      µfrom: 0
                      µto: oldNames length - 1
                      µdo:
                         [names at: index put: (oldNames at: index);
                          values at: index put: (oldValues at: index)];
                    self];
              µself];

        Self makeFastDictionary
          µmethod:
             [size µvar: 0;

              self size
                µmethod: 
                   [size];

              self display
                µmethod: 
                   [index µvar: integer µclone;
                    index µfrom: 0
                          µto: names length - 1
                          µdo: 
                             [ name µvar: names at: index;
                               name <> ""
                                 µthen:
                                    [name display;
                                     " >>> " display;
                                     (values at: index) display;
                                     Eoln display]];
                    self];

              self indexAtKey: key
                µmethod: 
                   [index µvar: key hash mod: names length;
                    ((index>=0) and:[(names at: index) <> key])
                       µwhile:
                         [(names at: index) = ""
                             µthen:
                               [index µ<- -1]
                             µelse:
                               [index µ<- (index + 1) mod: names length]];
                    index];

              self newIndexAtKey: key
                µmethod: 
                   [(size + 1) >= names length 
                      µthen:
                         [self extend];
                    index µvar: key hash mod: names length;
                    ((index>=0) and: [(names at: index) <> ""])
                       µwhile:
                         [(names at: index) = key
                             µthen:
                               [index µ<-  -1]
                             µelse:
                               [index µ<- (index + 1) mod: names length]];
                    index >= 0
                       µthen:
                          [size µ<- size + 1;
                           names at: index put: key];
                    index];

              self extend
                µmethod: 
                   [oldNames µvar: names;
                    oldValues µvar: values;
                    newSize µvar: (names length + 1) * 2;
                    index µvar: integer µclone;
                    names µ<- Vector length: newSize of: "";
                    values µ<- Vector length: newSize of: "";
                    size µ<- 0;
                    index
                      µfrom: 0
                      µto: oldNames length - 1
                      µdo:
                         [name µvar: oldNames at: index;
                          name <> ""
                            µthen:
                               [self at: name put: (oldValues at: index)]];
                    self];
              µself];
          µself];

     AbstractDictionary µ<- Object makeAbstractDictionary;
     SmallDictionary    µ<- AbstractDictionary makeSmallDictionary;
     FastDictionary     µ<- AbstractDictionary makeFastDictionary;
   

     dict µvar: FastDictionary µclone;
   
     dict at: "Maan" put: 3476;
     dict at: "Phobos" put: 22;
     dict at: "Deimos" put: 8;
     dict at: "Io" put: 3550;
     dict at: "Europa" put: 3100;
     dict at: "Ganymedes" put: 5600;
     dict at: "Callisto" put: 5050;
     dict at: "Mimas" put: 520;
     dict at: "Enceladus" put: 600;
     dict at: "Tethys" put: 1200;
     dict at: "Dione" put: 1300;
     dict at: "Rhea" put: 1300;
     dict at: "Titan" put: 4950;
     dict at: "Hyperion" put: 400;
     dict at: "Japetus" put: 1200;
     dict at: "Phoebe" put: 300;
     dict at: "Janus" put: 350;
     dict at: "Ariel" put: 600;
     dict at: "Umbriel" put: 400;
     dict at: "Titania" put: 1000;
     dict at: "Oberon" put: 800;
     dict at: "Miranda" put: 100;
     dict at: "Triton" put: 4000;
     dict at: "Nereide" put: 300;
   
     dict display;
   
     (dict at: "Tethys") display;
     
     (dict at: "Aarde") display] µvalue