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

Core::Set Class Reference

#include <Set.h>

Inheritance diagram for Core::Set:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Set (int length=2)
virtual ~Set (void)
 Set (Set &origin)
virtual StringclassName (void) const
 Answer receiver class name.
virtual long capacity (void) const
 Answer how many elements can be stored in the collection.
virtual long size (void) const
 Answer how many elements the collection includes.
virtual Objectany (void)
 Answer any item from the collection.
virtual void add (Object *obj)
virtual SetasSet (void)
virtual Objectcopy (void)
virtual ObjectcopyEmpty (long size)
virtual Objectremove (Object *obj)
virtual bool includes (Object *obj)
virtual void atNewIndexPut (int index, Object *obj)
virtual int findElementOrNil (Object *obj)
virtual bool isEmptyAt (int index)
virtual ObjectprivAtIndex (int index)

Protected Member Functions

virtual ObjectprivNextForIterator (CollectionIterator *iter) const
virtual void changeCapacity (long newCapacity)
virtual void fixCollisionsFrom (int index)
virtual void fullCheck (void)
virtual void grow (void)

Protected Attributes

Arraycontent
int tally

Constructor & Destructor Documentation

Set::Set int  length = 2  ) 
 

Definition at line 32 of file Set.cc.

References content, and tally.

Referenced by copy(), and copyEmpty().

00033 {
00034     content = new Array(length);
00035     tally = 0;
00036 }

Set::~Set void   )  [virtual]
 

Definition at line 38 of file Set.cc.

References content.

00039 {
00040     delete content;
00041 }

Set::Set Set origin  ) 
 

Definition at line 44 of file Set.cc.

References content, Core::Array::copy(), and tally.

00045 {
00046     tally = origin.tally;
00047     content = (Array *) origin.content->copy();
00048 }


Member Function Documentation

void Set::add Object obj  )  [virtual]
 

Reimplemented from Core::Collection.

Reimplemented in Core::Dictionary.

Definition at line 73 of file Set.cc.

References atNewIndexPut(), findElementOrNil(), isEmptyAt(), and nil.

Referenced by Core::Collection::asSet(), changeCapacity(), Tools::Configuration::keys(), OS::Thread::run(), and OS::Process::run().

00074 {
00075     int index;
00076 
00077     if (obj == nil) return;
00078     index = findElementOrNil(obj);
00079     if (isEmptyAt(index))
00080         atNewIndexPut(index, obj);
00081 }

Object * Set::any void   )  [virtual]
 

Answer any item from the collection.

Reimplemented from Core::Collection.

Definition at line 67 of file Set.cc.

References Core::Array::any(), and content.

00068 {
00069     return content->any();
00070 }

Set * Set::asSet void   )  [virtual]
 

Reimplemented from Core::Collection.

Definition at line 84 of file Set.cc.

00085 {
00086     return this;
00087 }

void Set::atNewIndexPut int  index,
Object obj
[virtual]
 

Definition at line 140 of file Set.cc.

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

Referenced by add(), Core::Dictionary::add(), and Core::Dictionary::put().

00141 {
00142     content->put(index, obj);
00143     tally = tally + 1;
00144     fullCheck();
00145 }

long Set::capacity void   )  const [virtual]
 

Answer how many elements can be stored in the collection.

Reimplemented from Core::Collection.

Definition at line 57 of file Set.cc.

References content, and Core::Array::size().

Referenced by findElementOrNil(), Core::Dictionary::findKeyOrNil(), fixCollisionsFrom(), Core::Dictionary::fixCollisionsFrom(), fullCheck(), grow(), and privNextForIterator().

00058 {
00059     return content->size();
00060 }

void Set::changeCapacity long  newCapacity  )  [protected, virtual]
 

Reimplemented in Core::Dictionary.

Definition at line 147 of file Set.cc.

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

Referenced by grow().

00148 {
00149     Array *oldContent;
00150     Iterator *i;
00151 
00152     oldContent = content;
00153     content = new Array(newCapacity);
00154     tally = 0;
00155     for (i = oldContent->iterator(); !i->finished(); i->next()) {
00156         if (i->value() != nil)
00157             add(i->value());
00158     }
00159     delete i;
00160     delete oldContent;
00161 }

String * Set::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::Collection.

Reimplemented in Core::Dictionary.

Definition at line 51 of file Set.cc.

00052 {
00053     return new String("Set");
00054 }

Object * Set::copy void   )  [virtual]
 

Reimplemented from Core::Collection.

Reimplemented in Core::Dictionary.

Definition at line 90 of file Set.cc.

References Set().

Referenced by OS::Process::finalizeProcesses(), OS::Thread::finalizeThreads(), and OS::Process::sigchildHandler().

00091 {
00092     return new Set(*this);
00093 }

Object * Set::copyEmpty long  size  )  [virtual]
 

Reimplemented from Core::Collection.

Reimplemented in Core::Dictionary.

Definition at line 95 of file Set.cc.

References Set().

00096 {
00097     return new Set(size);
00098 }

int Set::findElementOrNil Object obj  )  [virtual]
 

Definition at line 163 of file Set.cc.

References Core::Array::at(), capacity(), content, grow(), Core::Object::hash(), Core::Object::isEqual(), and nil.

Referenced by add(), fixCollisionsFrom(), includes(), and remove().

00164 {
00165     int pass, length = capacity();
00166     Object *elem;
00167     int index;
00168 
00169     index = obj->hash() % length;
00170     /* Unfortunately, result of modulo can be negative */
00171     if (index < 0) index = length + index;
00172     pass = 1;
00173     while ((elem = content->at(index)) != nil && !elem->isEqual(obj)) {
00174         index++;
00175         if (index >= length) {
00176             index = 0;
00177             pass++;
00178             if (pass > 2) {
00179                 grow();
00180                 return findElementOrNil(obj);
00181             }
00182         }
00183     }
00184     return index;
00185 }

void Set::fixCollisionsFrom int  index  )  [protected, virtual]
 

Reimplemented in Core::Dictionary.

Definition at line 187 of file Set.cc.

References Core::Array::at(), capacity(), content, findElementOrNil(), nil, and Core::Array::put().

Referenced by remove().

00188 {
00189     int length, nextIndex;
00190     Object *nextObject = nil;
00191 
00192     length = capacity();
00193     nextIndex = oldIndex;
00194     do {
00195         if (nextIndex != oldIndex) {
00196             content->put(nextIndex, nextObject);
00197             content->put(oldIndex, nil);
00198         }
00199         oldIndex = (oldIndex + 1) % length;
00200         nextObject = content->at(oldIndex);
00201         if (nextObject != nil)
00202             nextIndex = findElementOrNil(nextObject);
00203     } while (nextObject != nil);
00204 }

void Set::fullCheck void   )  [protected, virtual]
 

Definition at line 206 of file Set.cc.

References capacity(), grow(), and size().

Referenced by atNewIndexPut().

00207 {
00208     int sizePlusOne, cap;
00209 
00210     cap = capacity();
00211     sizePlusOne = size() + 1;
00212     if (sizePlusOne >= cap || cap - sizePlusOne < (cap >> 2))
00213         grow();
00214 }

void Set::grow void   )  [protected, virtual]
 

Definition at line 216 of file Set.cc.

References capacity(), changeCapacity(), and Core::Collection::growSize().

Referenced by findElementOrNil(), Core::Dictionary::findKeyOrNil(), and fullCheck().

00217 {
00218     changeCapacity(capacity() + growSize());
00219 }

bool Set::includes Object obj  )  [virtual]
 

Reimplemented from Core::Collection.

Reimplemented in Core::Dictionary.

Definition at line 117 of file Set.cc.

References findElementOrNil(), and isEmptyAt().

00118 {
00119     return !isEmptyAt(findElementOrNil(obj));
00120 }

bool Set::isEmptyAt int  index  )  [virtual]
 

Definition at line 221 of file Set.cc.

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

Referenced by add(), includes(), and remove().

00222 {
00223     return content->at(index) == nil;
00224 }

Object * Set::privAtIndex int  index  )  [virtual]
 

Definition at line 226 of file Set.cc.

References Core::Array::at(), and content.

00227 {
00228     return content->at(index);
00229 }

Object * Set::privNextForIterator CollectionIterator iter  )  const [protected, virtual]
 

Reimplemented from Core::Collection.

Definition at line 124 of file Set.cc.

References Core::Array::at(), capacity(), content, nil, and Core::CollectionIterator::position().

00125 {
00126     Object *obj;
00127     long index = iter->position();
00128 
00129     for (; index < capacity(); index += 1) {
00130         if ((obj = content->at(index)) != nil) {
00131             iter->position(index);
00132             return obj;
00133         }
00134     }
00135     iter->position(index);
00136     return nil;
00137 }

Object * Set::remove Object obj  )  [virtual]
 

Reimplemented from Core::Collection.

Reimplemented in Core::Dictionary.

Definition at line 101 of file Set.cc.

References content, findElementOrNil(), fixCollisionsFrom(), isEmptyAt(), nil, Core::Array::put(), and tally.

Referenced by OS::Thread::finalize(), and OS::Process::finalize().

00102 {
00103     int index;
00104 
00105     index = findElementOrNil(obj);
00106     if (isEmptyAt(index)) {
00107         (new NotFoundError(__PRETTY_FUNCTION__, obj))->raiseFrom(this);
00108     } else {
00109         content->put(index, nil);
00110         tally = tally - 1;
00111         fixCollisionsFrom(index);
00112     }
00113     return obj;
00114 }

long Set::size void   )  const [virtual]
 

Answer how many elements the collection includes.

Reimplemented from Core::Collection.

Definition at line 62 of file Set.cc.

References tally.

Referenced by fullCheck(), and Tools::Configuration::keys().

00063 {
00064     return tally;
00065 }


Member Data Documentation

Array* Core::Set::content [protected]
 

Definition at line 38 of file Set.h.

Referenced by Core::Dictionary::add(), any(), Core::Dictionary::at(), atNewIndexPut(), capacity(), changeCapacity(), Core::Dictionary::changeCapacity(), findElementOrNil(), Core::Dictionary::findKey(), Core::Dictionary::findKeyOrNil(), fixCollisionsFrom(), Core::Dictionary::fixCollisionsFrom(), Core::Dictionary::includesKey(), isEmptyAt(), Core::Dictionary::noCheckAdd(), privAtIndex(), privNextForIterator(), Core::Dictionary::put(), remove(), Core::Dictionary::removeKey(), Set(), and ~Set().

int Core::Set::tally [protected]
 

Definition at line 39 of file Set.h.

Referenced by atNewIndexPut(), changeCapacity(), Core::Dictionary::changeCapacity(), Core::Dictionary::noCheckAdd(), remove(), Core::Dictionary::removeKey(), Set(), and size().


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