24 #ifndef COM_DAFER45_TBTK_ARRAY_MANAGER
25 #define COM_DAFER45_TBTK_ARRAY_MANAGER
42 static void* create(
const Index &ranges);
45 static void* create(
const Index &ranges, T fill);
48 static void destroy(
void *array,
const Index &ranges);
51 static T* flatten(
void *array,
const Index &ranges);
54 static void* unflatten(T *array,
const Index &ranges);
57 static void print(
void * array,
const Index &ranges);
60 static void createRecursive(Index ranges,
void **result);
63 static void createRecursive(Index ranges,
void **result, T fill);
66 static void destroyRecursive(
void *array, Index ranges);
69 static void flattenRecursive(
void *array, Index ranges, T *result,
int offset);
72 static void unflattenRecursive(T *array, Index ranges,
void **result,
int offset);
75 static void printRecursive(
void *array, Index ranges);
79 void* ArrayManager<T>::create(
const Index &ranges){
82 createRecursive(ranges, &result);
88 void ArrayManager<T>::createRecursive(Index ranges,
void **result){
89 if(ranges.getSize() == 1){
90 *((T**)result) =
new T[ranges.at(0)];
93 *((
void**)result) =
new void*[ranges.at(0)];
95 int currentRange = ranges.at(0);
98 for(
int n = 0; n < currentRange; n++)
99 createRecursive(ranges, &(((
void**)(*result))[n]));
104 void* ArrayManager<T>::create(
const Index &ranges, T fill){
107 createRecursive(ranges, &result, fill);
113 void ArrayManager<T>::createRecursive(Index ranges,
void **result, T fill){
114 if(ranges.getSize() == 1){
115 *((T**)result) =
new T[ranges.at(0)];
116 for(
int n = 0; n < ranges.at(0); n++)
117 (*((T**)result))[n] = fill;
120 *((
void**)result) =
new void*[ranges.at(0)];
122 int currentRange = ranges.at(0);
125 for(
int n = 0; n < currentRange; n++)
126 createRecursive(ranges, &(((
void**)(*result))[n]), fill);
131 void ArrayManager<T>::destroy(
void *array,
const Index &ranges){
132 destroyRecursive(array, ranges);
136 void ArrayManager<T>::destroyRecursive(
void *array, Index ranges){
137 if(ranges.getSize() == 1){
141 int currentRange = ranges.at(0);
144 for(
int n = 0; n < currentRange; n++)
145 destroyRecursive(((
void**)array)[n], ranges);
147 delete [] (
void**)array;
152 T* ArrayManager<T>::flatten(
void *array,
const Index &ranges){
154 for(
unsigned int n = 0; n < ranges.getSize(); n++)
155 size *= ranges.at(n);
157 T *result =
new T[size];
159 flattenRecursive(array, ranges, result, 0);
165 void ArrayManager<T>::flattenRecursive(
void *array, Index ranges, T *result,
int offset){
166 if(ranges.getSize() == 1){
167 for(
int n = 0; n < ranges.at(0); n++){
168 result[offset + n] = ((T*)array)[n];
172 int offsetMultiplier = 1;
173 for(
unsigned int n = 1; n < ranges.getSize(); n++)
174 offsetMultiplier *= ranges.at(n);
176 int currentRange = ranges.at(0);
179 for(
int n = 0; n < currentRange; n++)
180 flattenRecursive(((
void**)array)[n], ranges, result, offset + offsetMultiplier*n);
185 void* ArrayManager<T>::unflatten(T *array,
const Index &ranges){
188 unflattenRecursive(array, ranges, &result, 0);
190 return (
void*)result;
194 void ArrayManager<T>::unflattenRecursive(T *array, Index ranges,
void **result,
int offset){
195 if(ranges.getSize() == 1){
196 *((T**)result) =
new T[ranges.at(0)];
197 for(
int n = 0; n < ranges.at(0); n++)
198 (*((T**)result))[n] = array[offset + n];
201 *((
void**)result) =
new void*[ranges.at(0)];
203 int offsetMultiplier = 1;
204 for(
int n = 1; n < ranges.getSize(); n++)
205 offsetMultiplier *= ranges.at(n);
207 int currentRange = ranges.at(0);
210 for(
int n = 0; n < currentRange; n++)
211 unflattenRecursive(array, ranges, &(((
void**)(*result))[n]), offset + offsetMultiplier*n);
216 void ArrayManager<T>::print(
void *array,
const Index &ranges){
217 printRecursive(array, ranges);
221 void ArrayManager<T>::printRecursive(
void *array, Index ranges){
222 if(ranges.getSize() == 1){
223 for(
int n = 0; n < ranges.at(0); n++)
228 int currentRange = ranges.at(0);
230 for(
int n = 0; n < currentRange; n++)
231 printRecursive(((
void**)array)[n], ranges);