00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __UIUC_CHARM_COLLIDE_BUFFERS_H
00009 #define __UIUC_CHARM_COLLIDE_BUFFERS_H
00010
00011 #include <stdlib.h>
00012
00013
00014
00015 class memoryBuffer {
00016 public:
00017 typedef unsigned int size_t;
00018 private:
00019 void *data;
00020 size_t len;
00021 void setData(const void *toData,size_t toLen);
00022 public:
00023 memoryBuffer();
00024 memoryBuffer(size_t initLen);
00025 ~memoryBuffer();
00026 memoryBuffer(const memoryBuffer &in) {data=NULL;setData(in.data,in.len);}
00027 memoryBuffer &operator=(const memoryBuffer &in) {setData(in.data,in.len); return *this;}
00028
00029 size_t length(void) const {return len;}
00030 void *getData(void) {return data;}
00031 const void *getData(void) const {return data;}
00032 void detachBuffer(void) {data=NULL;len=0;}
00033
00034 void resize(size_t newlen);
00035 void reallocate(size_t newlen);
00036 };
00037
00038
00039 template <class T> class bufferT {
00040 T *data;
00041 int len;
00042 protected:
00043 bufferT() :data(NULL),len(0) {}
00044 bufferT(T *data_,int len_) :data(data_),len(len_) {}
00045 void set(T *data_,int len_) {data=data_;len=len_;}
00046 void setData(T *data_) {data=data_;}
00047 void setLength(int len_) {len=len_;}
00048 int &getLength(void) {return len;}
00049 public:
00050 int length(void) const {return len;}
00051
00052 T &operator[](int t) {return data[t];}
00053 const T &operator[](int t) const {return data[t];}
00054 T &at(int t) {return data[t];}
00055 const T &at(int t) const {return data[t];}
00056
00057 T *getData(void) {return (T *)data;}
00058 const T *getData(void) const {return (T *)data;}
00059 };
00060
00061
00062 template <class T> class fixedBufferT : public bufferT<T> {
00063 public:
00064 fixedBufferT(T *data_,int len_) :bufferT<T>(data_,len_) {}
00065 };
00066
00067
00068
00069
00070 template <class T> class growableBufferT : public bufferT<T> {
00071 typedef bufferT<T> super;
00072 enum { sT=sizeof(T) };
00073 memoryBuffer buf;
00074 int max;
00075
00076 growableBufferT<T>(const growableBufferT<T> &in);
00077 growableBufferT<T> &operator=(const growableBufferT<T> &in);
00078 public:
00079 growableBufferT<T>() :buf() {max=0;}
00080 growableBufferT<T>(size_t Len) :buf(Len*sT) {this->set((T*)buf.getData(),Len);max=Len;}
00081
00082 inline int length(void) const {return super::length();}
00083 inline int size(void) const {return super::length();}
00084 inline int &length(void) {return this->getLength();}
00085 inline int capacity(void) const {return max;}
00086
00087 T *detachBuffer(void) {
00088 T *ret=(T *)buf.getData();
00089 buf.detachBuffer();
00090 this->set(NULL,0);
00091 max=0;
00092 return ret;
00093 }
00094 void empty(void) {reallocate(0);}
00095 void push_back(const T& v) {
00096 grow(length()+1);
00097 this->at(this->getLength()++)=v;
00098 }
00099
00100 void push_fast(const T& v) {
00101 grow(length()+1);
00102 this->at(this->getLength()++)=v;
00103 }
00104 void grow(int min) {
00105 if (min>max) {
00106 if (min > 1000000) {
00107
00108 atLeast(min+(min/4));
00109 }
00110 else {
00111 resize(min+max+8);
00112 }
00113 }
00114 }
00115 void atLeast(int min) {
00116 if (min>max) resize(min);
00117 }
00118 void resize(int Len) {
00119 buf.resize(Len*sT);
00120 this->setData((T*)buf.getData());
00121 max=Len;
00122 }
00123 void reallocate(int Len) {
00124 buf.reallocate(Len*sT);
00125 setData((T*)buf.getData());
00126 this->setLength(0);
00127 max=Len;
00128 }
00129 };
00130
00131 #endif