00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include "array.h"
00037
00038
00039 #include "memory.h"
00040
00041 using namespace zmm;
00042
00043 ArrayBase::ArrayBase()
00044 {}
00045
00046 void ArrayBase::init(int capacity)
00047 {
00048 this->capacity = capacity;
00049 siz = 0;
00050 arr = (Object **)MALLOC(capacity * sizeof(Object *));
00051 memset(arr, 0, capacity * sizeof(Object *));
00052 }
00053 ArrayBase::~ArrayBase()
00054 {
00055 for(int i = 0; i < siz; i++)
00056 {
00057 Object *obj = arr[i];
00058 if(obj)
00059 obj->release();
00060 }
00061 FREE(arr);
00062 }
00063
00064 void ArrayBase::append(Object *obj)
00065 {
00066 obj->retain();
00067 resize(siz + 1);
00068 arr[siz++] = obj;
00069 }
00070
00071 void ArrayBase::set(Object *obj, int index)
00072 {
00073 Object *old = arr[index];
00074 if(old)
00075 old->release();
00076 if(obj)
00077 obj->retain();
00078 arr[index] = obj;
00079 }
00080 Object *ArrayBase::get(int index)
00081 {
00082 return arr[index];
00083 }
00084 void ArrayBase::remove(int index, int count)
00085 {
00086 if (index < 0 || index >= siz)
00087 return;
00088 int max = index + count;
00089 if (max > siz)
00090 max = siz;
00091 if (max <= index)
00092 return;
00093 for(int i = index; i < max; i++)
00094 {
00095 Object *obj = arr[i];
00096 if(obj)
00097 obj->release();
00098 arr[i] = NULL;
00099 }
00100 int move = siz - max;
00101 if (move)
00102 {
00103 memmove(
00104 (void *)(arr + index),
00105 (void *)(arr + index + count),
00106 move * sizeof(Object *)
00107 );
00108 }
00109 siz -= count;
00110 }
00111 void ArrayBase::removeUnordered(int index)
00112 {
00113 if (index < 0 || index >= siz)
00114 return;
00115 Object *obj = arr[index];
00116 obj->release();
00117 arr[index] = arr[--siz];
00118 arr[siz] = NULL;
00119 }
00120 void ArrayBase::insert(int index, Object *obj)
00121 {
00122 resize(siz + 1);
00123 memmove(
00124 (void *)(arr + (index + 1)),
00125 (void *)(arr + index),
00126 (siz - index) * sizeof(Object *)
00127 );
00128 obj->retain();
00129 arr[index] = obj;
00130 siz++;
00131 }
00132
00133 void ArrayBase::optimize()
00134 {
00135 capacity = siz;
00136 arr = (Object **)REALLOC(arr, capacity * sizeof(Object *));
00137 }
00138
00139 void ArrayBase::resize(int requiredSize)
00140 {
00141 if(requiredSize > capacity)
00142 {
00143 int newCapacity = siz + (siz / 2);
00144 if(requiredSize > newCapacity)
00145 newCapacity = requiredSize;
00146 capacity = newCapacity;
00147 arr = (Object **)REALLOC(arr, capacity * sizeof(Object *));
00148 }
00149 }
00150
00151 void ArrayBase::clear()
00152 {
00153 for(int i=0; i<siz; i++)
00154 {
00155 Object *obj = arr[i];
00156 obj->release();
00157 }
00158
00159 memset(arr, 0, siz * sizeof(Object *));
00160 siz = 0;
00161 }