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

OrderedCollection.cc

Go to the documentation of this file.
00001 /*
00002  * OrderedCollection.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Variable size collection, index-accessable.
00006  *
00007  * Copyright (c) 2003 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #include <stlib/OrderedCollection.h>
00025 #include <stlib/Array.h>
00026 #include <stlib/CollectionIterator.h>
00027 #include <stlib/String.h>
00028 
00029 OrderedCollection::OrderedCollection(int size)
00030 {
00031     content = new Array(size);
00032     firstIndex = 0;
00033     lastIndex = -1;
00034 }
00035 
00036 OrderedCollection::~OrderedCollection(void)
00037 {
00038     delete content;
00039 }
00040 
00041 /* Copying protocol */
00042 OrderedCollection::OrderedCollection(const OrderedCollection &origin)
00043 {
00044     firstIndex = origin.firstIndex;
00045     lastIndex = origin.lastIndex;
00046     content = (Array *) origin.content->copy();
00047 }
00048 
00049 /* Class-accessing protocol */
00050 String *OrderedCollection::className(void) const
00051 {
00052     return new String("OrderedCollection");
00053 }
00054 
00055 /* Class-instance creation protocol */
00056 OrderedCollection *OrderedCollection::with(Object *object)
00057 {
00058     OrderedCollection *coll = new OrderedCollection(1);
00059     coll->add(object);
00060     return coll;
00061 }
00062 
00063 OrderedCollection *OrderedCollection::with(Object *obj1, Object *obj2)
00064 {
00065     OrderedCollection *coll = new OrderedCollection(2);
00066     coll->add(obj1);
00067     coll->add(obj2);
00068     return coll;
00069 }
00070 
00071 OrderedCollection *OrderedCollection::with(Object *obj1, Object *obj2, Object *obj3)
00072 {
00073     OrderedCollection *coll = new OrderedCollection(3);
00074     coll->add(obj1);
00075     coll->add(obj2);
00076     coll->add(obj3);
00077     return coll;
00078 }
00079 
00080 OrderedCollection *OrderedCollection::with(Object *obj1, Object *obj2, Object *obj3, Object *obj4)
00081 {
00082     OrderedCollection *coll = new OrderedCollection(4);
00083     coll->add(obj1);
00084     coll->add(obj2);
00085     coll->add(obj3);
00086     coll->add(obj4);
00087     return coll;
00088 }
00089 
00090 OrderedCollection *OrderedCollection::withAll(Collection *coll)
00091 {
00092     OrderedCollection *newColl = new OrderedCollection(coll->size());
00093     for (Iterator *i = coll->iterator(); !i->finished(); i->next()) {
00094         newColl->add(i->value());
00095     }
00096     return newColl;
00097 }
00098 
00099 /* Accessing protocol */
00100 Object *OrderedCollection::at(int index) const
00101 {
00102     if (index < 0 || index + firstIndex > lastIndex) {
00103         SubscriptOutOfBoundsError *ex;
00104         ex = new SubscriptOutOfBoundsError(__PRETTY_FUNCTION__, index);
00105         ex->raiseFrom(this);
00106     }
00107     return content->at(index + firstIndex);
00108 }
00109 
00110 void OrderedCollection::put(int index, Object *obj)
00111 {
00112     if (index < 0 || index + firstIndex > lastIndex) {
00113         SubscriptOutOfBoundsError *ex;
00114         ex = new SubscriptOutOfBoundsError(__PRETTY_FUNCTION__, index);
00115         ex->raiseFrom(this);
00116     }
00117     return content->put(index + firstIndex, obj);
00118 }
00119 
00120 long OrderedCollection::capacity(void) const
00121 {
00122     return content->size();
00123 }
00124 
00125 long OrderedCollection::size(void) const
00126 {
00127     return lastIndex - firstIndex + 1;
00128 }
00129 
00130 Object *OrderedCollection::first(void)
00131 {
00132     emptyCheck();
00133     return content->at(firstIndex);
00134 }
00135 
00136 Object *OrderedCollection::last(void)
00137 {
00138     emptyCheck();
00139     return content->at(lastIndex);
00140 }
00141 
00142 /* Adding protocol */
00143 void OrderedCollection::add(Object *object)
00144 {
00145     addLast(object);
00146 }
00147 
00148 void OrderedCollection::addFirst(Object *object)
00149 {
00150     if (firstIndex == 0) makeRoomAtFirst();
00151     firstIndex--;
00152     content->put(firstIndex, object);
00153 }
00154 
00155 void OrderedCollection::addLast(Object *object)
00156 {
00157     lastIndex++;
00158     if (lastIndex == capacity()) makeRoomAtLast();
00159     content->put(lastIndex, object);
00160 }
00161 
00162 void OrderedCollection::changeSize(long newSize)
00163 {
00164     Array *oldContent = content;
00165     content = new Array(newSize);
00166     long length = size();
00167     content->replace(0, (length < newSize) ? length : newSize, oldContent);
00168     delete oldContent;
00169 }
00170 
00171 /* Converting protocol */
00172 OrderedCollection *OrderedCollection::asOrderedCollection(void)
00173 {
00174     return this;
00175 }
00176 
00177 /* Copying protocol */
00178 Object *OrderedCollection::copy(void)
00179 {
00180     return new OrderedCollection(*this);
00181 }
00182 
00183 Object *OrderedCollection::copyEmpty(long size)
00184 {
00185     return new OrderedCollection(size);
00186 }
00187 
00188 /* Removing protocol */
00189 Object *OrderedCollection::remove(Object *object)
00190 {
00191     for (int i = firstIndex; i <= lastIndex; i++) {
00192         if (content->at(i)->isEqual(object)) {
00193             removeIndex(i);
00194             return object;
00195         }
00196     }
00197     (new NotFoundError(__PRETTY_FUNCTION__, object))->raiseFrom(this);
00198 }
00199 
00200 Object *OrderedCollection::removeFirst(void)
00201 {
00202     Object *firstObject;
00203 
00204     emptyCheck();
00205     firstObject = content->at(firstIndex);
00206     content->put(firstIndex, nil);
00207     firstIndex++;
00208     return firstObject;
00209 }
00210 
00211 Object *OrderedCollection::removeLast(void)
00212 {
00213     Object *lastObject;
00214 
00215     emptyCheck();
00216     lastObject = content->at(lastIndex);
00217     content->put(lastIndex, nil);
00218     lastIndex--;
00219     return lastObject;
00220 }
00221 
00222 /* protected methods */
00223 /* Enumeration protocol */
00224 Object *OrderedCollection::privNextForIterator(CollectionIterator *iter) const
00225 {
00226     if (iter->position() + firstIndex > lastIndex) {
00227         iter->position(capacity());
00228         return nil;
00229     }
00230     return content->at(iter->position() + firstIndex);
00231 }
00232 
00233 /* Private protocol */
00234 void OrderedCollection::changeCapacity(int newCapacity)
00235 {
00236     Array *oldContent;
00237 
00238     oldContent = content;
00239     content = new Array(newCapacity);
00240     for (int i = firstIndex; i < lastIndex; i++) {
00241         content->put(i, oldContent->at(i));
00242     }
00243     delete oldContent;
00244 }
00245 
00246 void OrderedCollection::grow(void)
00247 {
00248     increaseCapacity();
00249 }
00250 
00251 void OrderedCollection::increaseCapacity(void)
00252 {
00253     changeCapacity(size() + growSize());
00254 }
00255 
00256 void OrderedCollection::makeRoomAtFirst(void)
00257 {
00258     int delta;
00259 
00260     delta = capacity() - lastIndex - 1;
00261     if (delta <= 0) {
00262         /* There is no room left */
00263         increaseCapacity();
00264         if (firstIndex == 0) makeRoomAtFirst();
00265         return;
00266     }
00267     for (int i = lastIndex; i >= firstIndex; i--) {
00268         content->put(i + delta, content->at(i));
00269     }
00270     for (int i = firstIndex; i < firstIndex + delta; i++) {
00271         content->put(i, nil);
00272     }
00273     firstIndex += delta;
00274     lastIndex = capacity() - 1;
00275 }
00276 
00277 void OrderedCollection::makeRoomAtLast(void)
00278 {
00279     int delta;
00280 
00281     delta = -firstIndex;
00282     if (delta == 0) {
00283         /* There is no room left */
00284         increaseCapacity();
00285         return;
00286     }
00287     for (int i = firstIndex; i < lastIndex; i++) {
00288         content->put(i + delta, content->at(i));
00289     }
00290     for (int i = lastIndex + delta; i < lastIndex; i++) {
00291         content->put(i, nil);
00292     }
00293     firstIndex = 0;
00294     lastIndex += delta;
00295 }
00296 
00297 void OrderedCollection::removeIndex(int index)
00298 {
00299     for (; index < lastIndex; index++) {
00300         content->put(index, content->at(index+1));
00301     }
00302     content->put(lastIndex, nil);
00303     lastIndex--;
00304 }

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2