27 #ifndef MESQUITE_PATCH_DATA_MEM_HPP
28 #define MESQUITE_PATCH_DATA_MEM_HPP
35 template <
typename X>
class PatchDataMem
57 inline size_t size()
const;
58 inline bool empty()
const;
60 inline const X&
operator[](
size_t index )
const;
66 inline void resize(
size_t new_size );
69 inline PatchDataMem<X>&
operator=(
const PatchDataMem<X>& );
87 PatchDataMem<X>::PatchDataMem(
size_t size )
94 PatchDataMem<X>::PatchDataMem(
const PatchDataMem<X>& other )
100 template <
typename X>
101 PatchDataMem<X>::~PatchDataMem()
102 { clear(); free( arrayData ); }
104 template <
typename X>
105 size_t PatchDataMem<X>::size()
const
106 {
return activeSize; }
108 template <
typename X>
109 const X* PatchDataMem<X>::to_array()
const
110 {
return arrayData; }
112 template <
typename X>
113 X* PatchDataMem<X>::to_array()
114 {
return arrayData; }
116 template <
typename X>
117 const X& PatchDataMem<X>::operator[](
size_t index )
const
118 {
return arrayData[index]; }
120 template <
typename X>
121 X& PatchDataMem<X>::operator[](
size_t index )
122 {
return arrayData[index]; }
124 template <
typename X>
126 {
return !activeSize; }
128 template <
typename X>
129 typename PatchDataMem<X>::iterator PatchDataMem<X>::begin()
130 {
return arrayData; }
132 template <
typename X>
133 typename PatchDataMem<X>::const_iterator PatchDataMem<X>::begin()
const
134 {
return arrayData; }
136 template <
typename X>
137 typename PatchDataMem<X>::iterator PatchDataMem<X>::end()
138 {
return arrayData + activeSize; }
140 template <
typename X>
141 typename PatchDataMem<X>::const_iterator PatchDataMem<X>::end()
const
142 {
return arrayData + activeSize; }
144 template <
typename X>
145 void PatchDataMem<X>::resize_storage(
size_t new_size )
147 assert( new_size >= activeSize );
148 X* new_array = (X*)malloc(
sizeof(X) * new_size );
149 for (
size_t i = 0;
i < activeSize; ++
i)
152 new (new_array +
i) X( arrayData[
i] );
156 arrayData = new_array;
157 arrayLength = new_size;
160 template <
typename X>
161 void PatchDataMem<X>::resize(
size_t new_size )
163 if (new_size > activeSize)
165 if (new_size > arrayLength)
166 resize_storage( new_size );
168 for (
size_t i = activeSize;
i < new_size; ++
i)
171 new (arrayData +
i) X();
173 activeSize = new_size;
175 else if (new_size < activeSize)
177 for (
size_t i = new_size;
i < activeSize; ++
i)
179 activeSize = new_size;
181 if (activeSize && ((SHRINK_PERCENT*activeSize) < arrayLength))
182 resize_storage( new_size );
186 template <
typename X>
187 void PatchDataMem<X>::clear()
189 for (
size_t i = 0;
i < activeSize; ++
i)
194 template <
typename X>
195 PatchDataMem<X>& PatchDataMem<X>::operator=(
const PatchDataMem<X>& other )
199 if (other.activeSize)
201 if (arrayLength < other.activeSize ||
202 (SHRINK_PERCENT * other.activeSize) < arrayLength)
203 resize_storage( other.activeSize );
205 activeSize = other.activeSize;
206 for (
size_t i = 0;
i < activeSize; ++
i)
209 new (arrayData +
i) X( other.arrayData[
i] );
void resize(size_t new_size)
const X * to_array() const
boolean empty(T_VertexSet s)
Shink memory when used portion is less than this percent.
const X & operator[](size_t index) const
void resize_storage(size_t size)
PatchDataMem< X > & operator=(const PatchDataMem< X > &)