# BasicArray.h

Go to the documentation of this file.

    00001 /*******************************************************************\
    00002
    00003               Copyright (C) 2003 Joseph Coffland
    00004
    00005     This program is free software; you can redistribute it and/or
    00006      modify it under the terms of the GNU General Public License
    00007     as published by the Free Software Foundation; either version 2
    00008         of the License, or (at your option) any later version.
    00009
    00010    This program is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013              GNU General Public License for more details.
    00014
    00015   You should have received a copy of the GNU General Public License
    00016      along with this program; if not, write to the Free Software
    00017       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
    00018                            02111-1307, USA.
    00019
    00020             For information regarding this software email
    00021                    jcofflan@users.sourceforge.net
    00022
    00023 \*******************************************************************/
    00024 #ifndef BASICARRAY_H
    00025 #define BASICARRAY_H
    00026
    00027 #include "BasicException.h"
    00028
    00029 #include <stdlib.h>
    00030 #include <string.h>
    00031
    00040 template <class T>
    00041 class BasicArray {
    00042   unsigned int capacity;
    00043   unsigned int size;
    00044   T *data;
    00045
    00046  public:
    00052   BasicArray(unsigned int initialCapacity = 0) : data(0) {
    00053     reset(initialCapacity);
    00054   }
    00055
    00059   ~BasicArray() {if (data) free(data);}
    00060
    00061
    00065   bool isEmpty() const {return size == 0;}
    00066
    00070   unsigned int getSize() const {return size;}
    00071
    00082   void setSize(const unsigned int size) {
    00083     increase(size);
    00084     this->size = size;
    00085   }
    00086
    00092   unsigned int getCapacity() const {return capacity;}
    00093
    00094
    00101   T *getBuffer() {
    00102     return data;
    00103   }
    00104
    00112   T *adopt() {
    00113     T *tmp = data;
    00114
    00115     data = 0;
    00116     capacity = 0;
    00117     size = 0;
    00118
    00119     return tmp;
    00120   }
    00121
    00122
    00128   void reset(unsigned int initialCapacity = 0) {
    00129     if (data) {
    00130       free(data);
    00131       data = 0;
    00132     }
    00133
    00134     capacity = 0;
    00135     size = 0;
    00136     increase(initialCapacity);
    00137   }
    00138
    00143   void clear() {
    00144     size = 0;
    00145   }
    00146
    00171   void increase(const unsigned int minCapacity,
    00172                 const unsigned int multiple = 2) {
    00173     if (minCapacity <= capacity) return;
    00174     if (minCapacity < multiple * capacity) capacity *= multiple;
    00175     else capacity = minCapacity;
    00176
    00177     void *newMem = realloc(data, capacity * sizeof(T));
    00178     if (!newMem) {
    00179       if (capacity > minCapacity) capacity = minCapacity;
    00180       newMem = realloc(data, capacity * sizeof(T));
    00181
    00182       if (!newMem) throw std::bad_alloc();
    00183     }
    00184
    00185     data = (T *)newMem;
    00186     memset(&data[size], 0, sizeof(T) * (capacity - size));
    00187   }
    00188
    00201   void shrink() {
    00202     if (capacity > size) {
    00203       data = (T *)realloc(data, size * sizeof(T));
    00204       capacity = size;
    00205     }
    00206   }
    00207
    00216   unsigned int put(const T &x) {
    00217     return put(&x, 1);
    00218   }
    00219
    00228   void put(const unsigned int i, const T &x) {
    00229     put(i, &x, 1);
    00230   }
    00231
    00240   unsigned int put(const T *x, const int count) {
    00241     put(getSize(), x, count);
    00242     return getSize() - count;
    00243   }
    00244
    00254   void put(const unsigned int i, const T *x, const int count) {
    00255     memcpy(alloc(i, count), x, sizeof(T) * count);
    00256   }
    00257
    00264   void *alloc(const unsigned int count) {
    00265     return alloc(getSize(), count);
    00266   }
    00267
    00279   void *alloc(const unsigned int i, const unsigned int count) {
    00280     increase(i + count);
    00281     if (size < i + count) size = i + count;
    00282     return &data[i];
    00283   }
    00284
    00293   T &get(const unsigned int i) const {
    00294     ASSERT_OR_THROW("BasicArray index out of range!", i < size);
    00295     return data[i];
    00296   }
    00297
    00302   T &operator[](const unsigned int i) const {return get(i);}
    00303 };
    00304
    00305 #endif // BASICARRAY_H