Go to the documentation of this file.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 #ifndef INCREASESTORAGE_H
00029 #define INCREASESTORAGE_H
00030
00031 #include <stdlib.h>
00032
00033 #include "QGpCoreToolsDLLExport.h"
00034
00035 namespace QGpCoreTools {
00036
00037 class QGPCORETOOLS_EXPORT IncreaseStorage
00038 {
00039 public:
00040 inline IncreaseStorage(int defaultCapacity);
00041 inline IncreaseStorage(const IncreaseStorage& o);
00042 virtual ~IncreaseStorage() {}
00043
00044 inline void reserve(int n);
00045 int size() const {return _size;}
00046 inline void add(int n=1);
00047 inline void clear();
00048 void downSize(int s) {_size=s;}
00049 protected:
00050 virtual void reallocate()=0;
00051 inline void * allocateVector(int itemSize);
00052 inline void * reallocateVector(void * vect, int itemSize);
00053 private:
00054 int _defaultCapacity;
00055 int _capacity;
00056 volatile int _size;
00057 };
00058
00059 inline IncreaseStorage::IncreaseStorage(int defaultCapacity)
00060 {
00061 _defaultCapacity=defaultCapacity;
00062 clear();
00063 }
00064
00065 inline IncreaseStorage::IncreaseStorage(const IncreaseStorage& o)
00066 {
00067 _defaultCapacity=o._defaultCapacity;
00068 _capacity=o._capacity;
00069 _size=o._size;
00070 }
00071
00072 inline void * IncreaseStorage::allocateVector(int itemSize)
00073 {
00074 return malloc(_capacity * itemSize);
00075 }
00076
00077 inline void * IncreaseStorage::reallocateVector(void * vect, int itemSize)
00078 {
00079 return realloc(vect, _capacity * itemSize);
00080 }
00081
00082 inline void IncreaseStorage::reserve(int n)
00083 {
00084 if(_size+n >= _capacity) {
00085 while(_size+n >= _capacity) {
00086 _capacity=_capacity << 1;
00087 }
00088 reallocate();
00089 }
00090 }
00091
00092 inline void IncreaseStorage::add(int n)
00093 {
00094 _size+=n;
00095 if(_size>=_capacity) {
00096 do {
00097 _capacity=_capacity << 1;
00098 } while(_size>=_capacity);
00099 reallocate();
00100 }
00101 }
00102
00103 inline void IncreaseStorage::clear()
00104 {
00105 _capacity=_defaultCapacity;
00106 _size=0;
00107 }
00108
00109 }
00110
00111 #endif // INCREASESTORAGE_H