00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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