00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00065 String *Array::className(void) const
00066 {
00067 return new String("Array");
00068 }
00069
00070
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
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
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
00167 for (i = 0; i < oldSize && i < tally; i++) {
00168 content[i] = oldContent[i];
00169 }
00170
00171 for (; i < tally; i++) {
00172 content[i] = nil;
00173 }
00174
00175 GC_free(oldContent);
00176 }
00177
00178
00179 Array *Array::asArray(void)
00180 {
00181 return this;
00182 }
00183
00184
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
00196
00197 Object *Array::privNextForIterator(CollectionIterator *iter) const
00198 {
00199 return at(iter->position());
00200 }