Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Core::Dictionary Class Reference

#include <Dictionary.h>

Inheritance diagram for Core::Dictionary:

Inheritance graph
[legend]
Collaboration diagram for Core::Dictionary:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Dictionary (int length=2)
virtual String * className (void) const
 Answer receiver class name.
virtual Object * at (Object *key)
virtual Object * at (Object *key, Object *ifAbsentValue)
virtual Object * at (const char *key)
virtual Object * at (const char *key, const char *ifAbsentValue)
virtual void put (Object *key, Object *value)
virtual void put (const char *key, Object *value)
virtual void put (const char *key, const char *value)
virtual Object * keyAtValue (Object *value)
virtual Object * keyAtValue (Object *value, Object *ifAbsentKey)
virtual void add (Object *obj)
virtual Object * copy (void)
virtual Object * copyEmpty (long size)
virtual KeysAndValuesIterator * keysAndValuesIterator (void) const
virtual Object * remove (Object *)
virtual Object * removeKey (Object *key)
virtual bool includes (Object *obj)
virtual bool includesKey (Object *key)
virtual void visitBy (Visitor *visitor)
virtual int findKey (Object *key)
virtual int findKeyOrNil (Object *key)

Protected Member Functions

virtual void changeCapacity (long newCapacity)
virtual void fixCollisionsFrom (int index)
virtual void noCheckAdd (Association *object)

Constructor & Destructor Documentation

Dictionary::Dictionary (  int  length = 2  ) 
 

Definition at line 35 of file Dictionary.cc.

Referenced by copy(), and copyEmpty().

00036   : Set(length)
00037 {
00038 }


Member Function Documentation

void Dictionary::add (  Object *  obj  )  [virtual]
 

Reimplemented from Core::Set.

Definition at line 123 of file Dictionary.cc.

References Core::Array::at(), Core::Set::atNewIndexPut(), Core::Set::content, findKeyOrNil(), nil, and Core::Association::value().

00124 {
00125     long index;
00126     Object *assocKey;
00127     Association *element;
00128 
00129     if (!assoc->isAssociation() ||
00130         (assocKey = ((Association *) assoc)->key()) == nil)
00131         (new SubscriptOutOfBoundsError(__PRETTY_FUNCTION__, 0))->raiseFrom(this);
00132     index = findKeyOrNil(assocKey);
00133     element = (Association *) content->at(index);
00134     if (element == nil) atNewIndexPut(index, assoc);
00135                    else element->value(((Association *)assoc)->value());
00136 }

Object * Dictionary::at (  const char *  key,
const char *  ifAbsentValue
)  [virtual]
 

Definition at line 75 of file Dictionary.cc.

References at(), and nil.

00075                                                                  {
00076     return at(new String(key), (ifAbsentValue == nil ? nil : new String(ifAbsentValue)));
00077 }

Object * Dictionary::at (  const char *  key  )  [virtual]
 

Definition at line 71 of file Dictionary.cc.

References at().

00071                                       {
00072     return at(new String(key));
00073 }

Object * Dictionary::at (  Object *  key,
Object *  ifAbsentValue
)  [virtual]
 

Definition at line 59 of file Dictionary.cc.

References Core::Array::at(), Core::Set::content, findKeyOrNil(), nil, and Core::Association::value().

00060 {
00061     long index;
00062     Association *obj;
00063 
00064     index = findKeyOrNil(key);
00065     obj = (Association *) content->at(index);
00066     if (obj == nil)
00067         return ifAbsentValue;
00068     return obj->value();
00069 }

Object * Dictionary::at (  Object *  key  )  [virtual]
 

Definition at line 47 of file Dictionary.cc.

References Core::Array::at(), Core::Set::content, findKeyOrNil(), nil, and Core::Association::value().

Referenced by at(), Tools::Configuration::at(), Core::ByteCharacterEncoder::encode(), Core::SearchTreeNode::privDeeperValueAt(), and Core::SearchTreeNode::putValueAt().

00048 {
00049     long index;
00050     Association *obj;
00051 
00052     index = findKeyOrNil(key);
00053     obj = (Association *) content->at(index);
00054     if (obj == nil)
00055         (new KeyNotFoundError(__PRETTY_FUNCTION__, key))->raiseFrom(this);
00056     return obj->value();
00057 }

void Dictionary::changeCapacity (  long  newCapacity  )  [protected, virtual]
 

Reimplemented from Core::Set.

Definition at line 193 of file Dictionary.cc.

References Core::Set::content, Core::Iterator::finished(), Core::Collection::iterator(), Core::Iterator::next(), nil, noCheckAdd(), Core::Set::tally, and Core::Iterator::value().

00194 {
00195     Array *oldContent;
00196     Iterator *i;
00197 
00198     oldContent = content;
00199     content = new Array(newCapacity);
00200     tally = 0;
00201     for (i = oldContent->iterator(); !i->finished(); i->next()) {
00202         if (i->value() != nil)
00203             noCheckAdd((Association *) i->value());
00204     }
00205     delete i;
00206     delete oldContent;
00207 }

String * Dictionary::className (  void   )  const [virtual]
 

Answer receiver class name.

Because there isn't any standard way to obtain class name this method comes to place.

Every class should rewrite this method but many didn't (yet).

Reimplemented from Core::Set.

Definition at line 41 of file Dictionary.cc.

00042 {
00043     return new String("Dictionary");
00044 }

Object * Dictionary::copy (  void   )  [virtual]
 

Reimplemented from Core::Set.

Definition at line 139 of file Dictionary.cc.

References Dictionary().

00140 {
00141     return new Dictionary(*this);
00142 }

Object * Dictionary::copyEmpty (  long  size  )  [virtual]
 

Reimplemented from Core::Set.

Definition at line 144 of file Dictionary.cc.

References Dictionary().

00145 {
00146     return new Dictionary(size);
00147 }

int Dictionary::findKey (  Object *  key  )  [virtual]
 

Definition at line 209 of file Dictionary.cc.

References Core::Array::at(), Core::Set::content, findKeyOrNil(), and nil.

Referenced by removeKey().

00210 {
00211     int index;
00212 
00213     index = findKeyOrNil(key);
00214     if (content->at(index) == nil)
00215         (new KeyNotFoundError(__PRETTY_FUNCTION__, key))->raiseFrom(this);
00216     return index;
00217 }

int Dictionary::findKeyOrNil (  Object *  key  )  [virtual]
 

Definition at line 219 of file Dictionary.cc.

References Core::Array::at(), Core::Set::capacity(), Core::Set::content, Core::Set::grow(), Core::Association::key(), and nil.

Referenced by add(), at(), findKey(), fixCollisionsFrom(), includesKey(), noCheckAdd(), and put().

00220 {
00221     int pass, length = capacity();
00222     Association *elem;
00223     int index;
00224 
00225     index = key->hash() % length;
00226     /* Unfortunately, result of modulo can be negative */
00227     if (index < 0) index = length + index;
00228     pass = 1;
00229     while ((elem = (Association *) content->at(index)) != nil &&
00230            !elem->key()->isEqual(key)) {
00231         index++;
00232         if (index >= length) {
00233             index = 0;
00234             pass++;
00235             if (pass > 2) {
00236                 grow();
00237                 return findKeyOrNil(key);
00238             }
00239         }
00240     }
00241     return index;
00242 }

void Dictionary::fixCollisionsFrom (  int  index  )  [protected, virtual]
 

Reimplemented from Core::Set.

Definition at line 244 of file Dictionary.cc.

References Core::Array::at(), Core::Set::capacity(), Core::Set::content, findKeyOrNil(), Core::Association::key(), nil, and Core::Array::put().

Referenced by removeKey().

00245 {
00246     int length, nextIndex;
00247     Association *nextObject = nil;
00248 
00249     length = capacity();
00250     nextIndex = oldIndex;
00251     do {
00252         if (nextIndex != oldIndex) {
00253             content->put(nextIndex, nextObject);
00254             content->put(oldIndex, nil);
00255         }
00256         oldIndex = (oldIndex + 1) % length;
00257         nextObject = (Association *) content->at(oldIndex);
00258         if (nextObject != nil)
00259             nextIndex = findKeyOrNil(nextObject->key());
00260     } while (nextObject != nil);
00261 }

bool Dictionary::includes (  Object *  obj  )  [virtual]
 

Reimplemented from Core::Set.

Definition at line 176 of file Dictionary.cc.

References Core::Collection::includes().

00177 {
00178     return Collection::includes(obj);
00179 }

bool Dictionary::includesKey (  Object *  key  )  [virtual]
 

Definition at line 181 of file Dictionary.cc.

References Core::Array::at(), Core::Set::content, findKeyOrNil(), and nil.

Referenced by Tools::Configuration::includesKey(), and Core::SearchTreeNode::putValueAt().

00182 {
00183     return content->at(findKeyOrNil(key)) != nil;
00184 }

Object * Dictionary::keyAtValue (  Object *  value,
Object *  ifAbsentKey
)  [virtual]
 

Definition at line 109 of file Dictionary.cc.

References keyAtValue().

00110 {
00111     Object *obj;
00112 
00113     try {
00114         obj = keyAtValue(value);
00115     }
00116     catch (ValueNotFoundError *ex) {
00117         obj = ifAbsentKey;
00118     }
00119     return obj;
00120 }

Object * Dictionary::keyAtValue (  Object *  value  )  [virtual]
 

Definition at line 100 of file Dictionary.cc.

Referenced by keyAtValue().

00101 {
00102     for (KeysAndValuesIterator i(this); !i.finished(); i.next()) {
00103         if (i.value()->isEqual(value))
00104             return i.key();
00105     }
00106     (new ValueNotFoundError(__PRETTY_FUNCTION__, value))->raiseFrom(this);
00107 }

KeysAndValuesIterator * Dictionary::keysAndValuesIterator (  void   )  const [virtual]
 

Definition at line 150 of file Dictionary.cc.

Referenced by Tools::Configuration::keys().

00151 {
00152     return new KeysAndValuesIterator(this);
00153 }

void Dictionary::noCheckAdd (  Association *  object  )  [protected, virtual]
 

Definition at line 263 of file Dictionary.cc.

References Core::Set::content, findKeyOrNil(), Core::Array::put(), and Core::Set::tally.

Referenced by changeCapacity().

00264 {
00265     content->put(findKeyOrNil(object->key()), object);
00266     tally = tally + 1;
00267 }

void Dictionary::put (  const char *  key,
const char *  value
)  [virtual]
 

Definition at line 96 of file Dictionary.cc.

References put().

00096                                                        {
00097     put(new String(key), new String(value));
00098 }

void Dictionary::put (  const char *  key,
Object *  value
)  [virtual]
 

Definition at line 92 of file Dictionary.cc.

References put().

00092                                                    {
00093     put(new String(key), value);
00094 }

void Dictionary::put (  Object *  key,
Object *  value
)  [virtual]
 

Definition at line 79 of file Dictionary.cc.

References Core::Array::at(), Core::Set::atNewIndexPut(), Core::Set::content, findKeyOrNil(), nil, and Core::Association::value().

Referenced by Core::ByteCharacterEncoder::initializeAsciiEncoder(), Core::ByteCharacterEncoder::parseLine(), put(), Tools::Configuration::put(), and Core::SearchTreeNode::putValueAt().

00080 {
00081     long index;
00082     Association *element;
00083 
00084     if (key == nil)
00085         (new SubscriptOutOfBoundsError(__PRETTY_FUNCTION__, 0))->raiseFrom(this);
00086     index = findKeyOrNil(key);
00087     element = (Association *) content->at(index);
00088     if (element == nil) atNewIndexPut(index, new Association(key, value));
00089                    else element->value(value);
00090 }

Object * Dictionary::remove (  Object *   )  [virtual]
 

Reimplemented from Core::Set.

Definition at line 156 of file Dictionary.cc.

References nil, and Core::Object::shouldNotImplement().

00157 {
00158     shouldNotImplement(new String(__PRETTY_FUNCTION__));
00159     return nil;
00160 }

Object * Dictionary::removeKey (  Object *  key  )  [virtual]
 

Definition at line 162 of file Dictionary.cc.

References Core::Array::at(), Core::Set::content, findKey(), fixCollisionsFrom(), nil, Core::Array::put(), Core::Set::tally, and Core::Association::value().

Referenced by Tools::Configuration::removeKey().

00163 {
00164     long index;
00165     Association *element;
00166 
00167     index = findKey(key);
00168     element = (Association *) content->at(index);
00169     content->put(index, nil);
00170     tally = tally - 1;
00171     fixCollisionsFrom(index);
00172     return element->value();
00173 }

void Dictionary::visitBy (  Visitor *  visitor  )  [virtual]
 

Reimplemented from Core::Collection.

Definition at line 187 of file Dictionary.cc.

00188 {
00189     visitor->visitDictionary(this);
00190 }


The documentation for this class was generated from the following files:
Generated on Mon Nov 27 09:51:19 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2