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 #include <vector> 00032 00033 #include <iostream> 00034 00043 template <class T> 00044 class BasicArray :public std::vector<T>{ 00045 // unsigned int capacity;D 00046 // unsigned int size; 00047 // std::vector<T> vec; 00048 // T *data; 00049 00050 public: 00051 00057 // BasicArray() /*: data(0)*/ { 00058 // using namespace std; 00059 // cerr<<"DEFAULT CONSTRUCTOR"<<endl; 00060 // std::vector<T>::clear(); 00061 // cerr<<"vec.size()="<<std::vector<T>::size()<<" getSize()="<<getSize()<<endl; 00062 // } 00063 00064 BasicArray(unsigned int initialCapacity=0) /*: data(0)*/ { 00065 // reset(initialCapacity); 00066 std::vector<T>::clear(); 00067 // using namespace std; 00068 if(initialCapacity>0) 00069 std::vector<T>::assign(initialCapacity,T()); 00070 00071 // cerr<<"size vector="<<std::vector<T>::size()<<" getSize()="<<getSize()<<endl; 00072 } 00073 00077 ~BasicArray() {/*if (data) free(data);*/} 00078 00079 00083 bool isEmpty() const {return std::vector<T>::size() == 0;} 00084 00088 unsigned int getSize() const {return std::vector<T>::size();} 00089 00100 // void setSize(const unsigned int size) { 00101 // increase(size); 00102 // this->size = size; 00103 // } 00104 00110 unsigned int getCapacity() const {return std::vector<T>::capacity();} 00111 00112 00119 // T *getBuffer() { 00120 // return &((*this)[0]); 00121 // } 00122 00130 // T *adopt() { 00131 // T *tmp = data; 00132 // 00133 // data = 0; 00134 // capacity = 0; 00135 // size = 0; 00136 // 00137 // return tmp; 00138 // } 00139 00140 00146 // void reset(unsigned int initialCapacity = 0) { 00147 // if (data) { 00148 // free(data); 00149 // data = 0; 00150 // } 00151 // 00152 // capacity = 0; 00153 // size = 0; 00154 // increase(initialCapacity); 00155 // } 00156 00161 void clear() { 00162 std::vector<T>::clear(); 00163 } 00164 00189 // void increase(const unsigned int minCapacity, 00190 // const unsigned int multiple = 2) { 00191 // if (minCapacity <= capacity) return; 00192 // if (minCapacity < multiple * capacity) capacity *= multiple; 00193 // else capacity = minCapacity; 00194 // 00195 // void *newMem = realloc(data, capacity * sizeof(T)); 00196 // if (!newMem) { 00197 // if (capacity > minCapacity) capacity = minCapacity; 00198 // newMem = realloc(data, capacity * sizeof(T)); 00199 // 00200 // if (!newMem) throw std::bad_alloc(); 00201 // } 00202 // 00203 // data = (T *)newMem; 00204 // memset(&data[size], 0, sizeof(T) * (capacity - size)); 00205 // } 00206 00219 // void shrink() { 00220 // if (capacity > size) { 00221 // data = (T *)realloc(data, size * sizeof(T)); 00222 // capacity = size; 00223 // } 00224 // } 00225 00234 unsigned int put(const T &x) { 00235 using namespace std; 00236 std::vector<T>::push_back(x); 00237 return std::vector<T>::size()-1; 00238 } 00239 00248 // void put(const unsigned int i, const T &x) { 00249 // put(i, &x, 1); 00250 // } 00251 00260 // unsigned int put(const T *x, const int count) { 00261 // put(getSize(), x, count); 00262 // return getSize() - count; 00263 // } 00264 00274 // void put(const unsigned int i, const T *x, const int count) { 00275 // using namespace std; 00276 // cerr<<"i="<<i<<endl; 00277 // cerr<<"count="<<count<<endl; 00278 // cerr<<"sizeof(T)="<<sizeof(T)<<endl; 00279 // cerr<<"x="<<x<<endl; 00280 // void *dest=alloc(i, count); 00281 // cerr<<"dest="<<dest<<endl; 00282 // 00283 // memcpy(alloc(i, count), x, sizeof(T) * count); 00284 // // memmove(dest, x, sizeof(T) * count); 00285 // } 00286 00293 // void *alloc(const unsigned int count) { 00294 // return alloc(getSize(), count); 00295 // } 00296 00308 // void *alloc(const unsigned int i, const unsigned int count) { 00309 // increase(i + count); 00310 // if (size < i + count) size = i + count; 00311 // return &data[i]; 00312 // } 00313 00322 T &get(const unsigned int i) { 00323 // using namespace std; 00324 // ASSERT_OR_THROW("BasicArray index out of range!", i < std::vector<T>::size()); 00325 // cerr<<"inside GET"<<endl; 00326 return const_cast<T&>(((*this)[i])); 00327 } 00328 00329 BasicArray<T> & operator=(const BasicArray<T> & rhs){ 00330 if(this==&rhs) return *this; 00331 static_cast<std::vector<T>&>(*this)=rhs; 00332 return *this; 00333 } 00338 // T &operator[](const unsigned int i) const { 00339 // using namespace std; 00340 // cerr<<"inside GET"<<endl; 00341 // return const_cast<T&>(((*((std::vector<T>)this))[i])); 00342 // } 00343 }; 00344 00345 #endif // BASICARRAY_H
1.5.6