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

Array.cc

Go to the documentation of this file.
00001 /*
00002  * Array.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Fixed 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 <stdio.h>
00025 
00026 #include <stlib/Array.h>
00027 #include <stlib/CollectionIterator.h>
00028 #include <stlib/String.h>
00029 #include <stlib/SOOBoundsError.h>
00030 
00031 Array::Array(int size)
00032 {
00033     content = NULL;
00034     tally = size;
00035     if (tally > 0) {
00036         content = (Object **) GC_malloc(sizeof(Object *) * tally);
00037         for (int i = 0; i < tally; i++) {
00038             content[i] = nil;
00039         }
00040     }
00041 }
00042 
00043 Array::~Array(void)
00044 {
00045     if (content != NULL) {
00046         GC_free(content);
00047     }
00048 }
00049 
00050 /* Copying protocol */
00051 Array::Array(const Array &origin)
00052   : SequenceableCollection(origin)
00053 {
00054     content = NULL;
00055     tally = origin.tally;
00056     if (tally > 0) {
00057         content = (Object **) GC_malloc(sizeof(Object *) * tally);
00058         for (int i = 0; i < tally; i++) {
00059             content[i] = origin.content[i];
00060         }
00061     }
00062 }
00063 
00064 /* Class-accessing protocol */
00065 String *Array::className(void) const
00066 {
00067     return new String("Array");
00068 }
00069 
00070 /* Class-instance creation protocol */
00071 Array *Array::with(Object *object)
00072 {
00073     Array *newArray = new Array(1);
00074     newArray->put(0, object);
00075     return newArray;
00076 }
00077 
00078 Array *Array::with(Object *obj1, Object *obj2)
00079 {
00080     Array *newArray = new Array(2);
00081     newArray->put(0, obj1);
00082     newArray->put(1, obj2);
00083     return newArray;
00084 }
00085 
00086 Array *Array::with(Object *obj1, Object *obj2, Object *obj3)
00087 {
00088     Array *newArray = new Array(3);
00089     newArray->put(0, obj1);
00090     newArray->put(1, obj2);
00091     newArray->put(2, obj3);
00092     return newArray;
00093 }
00094 
00095 Array *Array::with(Object *obj1, Object *obj2, Object *obj3, Object *obj4)
00096 {
00097     Array *newArray = new Array(4);
00098     newArray->put(0, obj1);
00099     newArray->put(1, obj2);
00100     newArray->put(2, obj3);
00101     newArray->put(3, obj4);
00102     return newArray;
00103 }
00104 
00105 Array *Array::withAll(Collection *coll)
00106 {
00107     Array *newArray = new Array(coll->size());
00108     int index = 0;
00109     for (Iterator *i = coll->iterator(); !i->finished(); i->next()) {
00110         newArray->put(index++, i->value());
00111     }
00112     return newArray;
00113 }
00114 
00115 /* Accessing protocol */
00116 Object *Array::any(void)
00117 {
00118     long idx = 0;
00119     do {
00120         if (at(idx) != nil) return at(idx);
00121         idx++;
00122     } while (idx < tally);
00123     return nil;
00124 }
00125 
00126 Object *Array::at(int index) const
00127 {
00128     if (index < 0 || index >= tally) {
00129         SubscriptOutOfBoundsError *ex;
00130         ex = new SubscriptOutOfBoundsError(__PRETTY_FUNCTION__, index);
00131         ex->raiseFrom(this);
00132     }
00133     return content[index];
00134 }
00135 
00136 void Array::put(int index, Object *obj)
00137 {
00138     if (index < 0 || index >= tally) {
00139         SubscriptOutOfBoundsError *ex;
00140         ex = new SubscriptOutOfBoundsError(__PRETTY_FUNCTION__, index);
00141         ex->raiseFrom(this);
00142     }
00143     content[index] = obj;
00144 }
00145 
00146 long Array::size(void) const
00147 {
00148     return tally;
00149 }
00150 
00151 /* Adding protocol */
00152 void Array::add(Object *)
00153 {
00154     shouldNotImplement(new String(__PRETTY_FUNCTION__));
00155 }
00156 
00157 void Array::changeSize(long newSize)
00158 {
00159     Object **oldContent = content;
00160     long oldSize = tally;
00161     long i;
00162 
00163     content = (Object **) GC_malloc(sizeof(Object *) * newSize);
00164     tally = newSize;
00165 
00166     /* Copy old content or the part that fits */
00167     for (i = 0; i < oldSize && i < tally; i++) {
00168         content[i] = oldContent[i];
00169     }
00170     /* Fill the rest with nils */
00171     for (; i < tally; i++) {
00172         content[i] = nil;
00173     }
00174 
00175     GC_free(oldContent);
00176 }
00177 
00178 /* Converting protocol */
00179 Array *Array::asArray(void)
00180 {
00181     return this;
00182 }
00183 
00184 /* Copying protocol */
00185 Object *Array::copy(void)
00186 {
00187     return new Array(*this);
00188 }
00189 
00190 Object *Array::copyEmpty(long size)
00191 {
00192     return new Array(size);
00193 }
00194 
00195 /* protected methods */
00196 /* Enumeration protocol */
00197 Object *Array::privNextForIterator(CollectionIterator *iter) const
00198 {
00199     return at(iter->position());
00200 }

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