File: tjarray.cpp

package info (click to toggle)
odin 2.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,196 kB
  • sloc: cpp: 62,638; sh: 4,541; makefile: 779
file content (612 lines) | stat: -rw-r--r-- 16,820 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#include "tjarray.h"
#include "tjtypes.h"
#include "tjtest.h"


ndim::ndim (unsigned long d) : STD_vector<unsigned long>(d) { }  // ndim nn(3);



ndim::ndim (const STD_string& s) : STD_vector<unsigned long>() {
  Log<VectorComp> odinlog("ndim","ndim(const STD_string&)");
  unsigned long incosistent=0;

  STD_string tt=shrink(s);
  ODINLOG(odinlog,normalDebug) << "ndim::ndim (const STD_string& s): after shrinking >" << tt << "<" << STD_endl;

  if(tt[(unsigned int)0] != '(') incosistent++;
  if(tt[tt.length()-1] != ')') incosistent++;
  STD_string innertext=replaceStr(extract(tt,"(",")",true),",","");
//  if (typeofcontent(innertext)!=intnumbers) incosistent++;

  if(!incosistent) {
    ODINLOG(odinlog,normalDebug) << "ndim::ndim (const STD_string& s): no inconsistences in >" << tt << "<" << STD_endl;
    tt=replaceStr(tt,"(",",");
    tt=replaceStr(tt,")",",");
    ODINLOG(odinlog,normalDebug) << "ndim::ndim (const STD_string& s): string used for dim extraction: >" << tt << "<" << STD_endl;

    svector extends(tokens(tt,','));
    unsigned int n=extends.size();
    resize(n);
    for(unsigned int i=0;i<n;i++) (*this)[i]=atoi(extends[i].c_str());
  }
}



ndim::operator STD_string () const {
  unsigned long n=size();
  STD_string result;
  result="( ";
  if(!n) result+="0";
  for (unsigned long i=0;i<n;i++) {
    result+=itos((*this)[i]);
    if (i<(n-1)) result+=", ";
  }
  result+=" )";
  return result;
}



ndim&  ndim::operator -- () {
  Log<VectorComp> odinlog("ndim","--()");
  unsigned long n=size();
  if (n <= 0) ODINLOG(odinlog,errorLog) << "reduce to negative dimension ?!" << STD_endl;
  else {
    ndim tt(*this);
    n--;
    resize(n);
    for(unsigned long i=0;i<n;i++)  (*this)[i]=tt[i+1];
  }
  return *this;
}



ndim&  ndim::operator -- (int) {
  Log<VectorComp> odinlog("ndim","--(int)");
  unsigned long n=size();
  if (n <= 0) ODINLOG(odinlog,errorLog) << "reduce to negative dimension ?!" << STD_endl;
  else {
    ndim tt(*this);
    n--;
    resize(n);
    for(unsigned long i=0;i<n;i++)  (*this)[i]=tt[i];
  }
  return *this;
}



ndim& ndim::add_dim ( unsigned long e, bool first ) {
  unsigned long n=size();
  ndim tt(*this);
  n++;
  resize(n);
  for(unsigned long i=0;i<n-1;i++)  (*this)[i+int(first)]=tt[i];
  if(first) (*this)[0]=e;
  else      (*this)[n-1]=e;
  return *this;
}




unsigned long ndim::total() const {
  unsigned long n=size();
  unsigned long t=1;
  for(unsigned long i=0;i<n;i++) t*=(*this)[i];
  if(!n) t=0;
  return(t);
}



unsigned long ndim::extent2index(const ndim& mm) const {
  Log<VectorComp> odinlog("ndim","extent2index");
  if (dim() != mm.dim()) {
    ODINLOG(odinlog,errorLog) <<"dimension mismatch: dim()!=mm.dim()=" << dim() << "!=" << mm.dim() << STD_endl;
    return 0;
  }
  if(!dim()) return 0;

  unsigned long result=0;
  unsigned long subsize=1;
  for(long i=dim()-1;i>=0;i--) {
    result+=subsize*mm[i];
    subsize*=(*this)[i];
  }
  return result;
}

ndim ndim::index2extent(unsigned long index) const {
  ndim nn(dim());
  unsigned long subindex=index;
  for(long i=dim()-1;i>=0;i--){
    nn[i]=subindex%((*this)[i]);
    subindex/=(*this)[i];
  }
  return nn;
}




bool ndim::operator == (const ndim& nn) const {
  unsigned long i,flag=0;
  if (dim()==nn.dim()) {
    for(i=0;i<dim();i++) flag+=!((*this)[i]==nn[i]);
    return bool(!flag);
  } else return false;
}


bool ndim::operator != (const ndim& nn) const {
  unsigned long i,flag=0;
  if (dim()!=nn.dim()) return true;
  else {
    for(i=0;i<dim();i++) flag+=((*this)[i]!=nn[i]);
    return bool(flag);
  }
}


ndim& ndim::reduce ( unsigned long newdim ) {
  unsigned long n=size();
  if( newdim >= n ) return *this;
  ndim oldndim(*this);
  resize(newdim);

  unsigned long extent_factor=1;
  while(oldndim.dim()>newdim) {
    extent_factor=oldndim[0];
    --oldndim;
    oldndim[0]*=extent_factor;
  }
  for(unsigned int i=0;i<newdim;i++)  (*this)[i]=oldndim[i];
  return *this;
}


ndim& ndim::autosize () {
  bool at_least_one_element=bool(total());
#ifdef STL_REPLACEMENT
  STD_list<unsigned long> indexlist;
  unsigned long i;
  for(i=0;i<size();i++) {
    if((*this)[i]>1) indexlist.push_back((*this)[i]);
  }
  resize(indexlist.size());
  i=0;
  for(STD_list<unsigned long>::iterator it=indexlist.begin(); it!=indexlist.end(); ++it) {
    (*this)[i]=(*it);
    i++;
  }
#else
  STD_vector<unsigned long>::iterator new_end;
  new_end=std::remove(begin(),end(),(unsigned long)(1));
  erase(new_end,end());
#endif
  if((size()==0) && at_least_one_element) {
    resize(1);
    (*this)[0]=1;
  }
  return *this;
}



#ifndef NO_UNIT_TEST
class NdimTest : public UnitTest {

 public:
  NdimTest() : UnitTest("ndim") {}

 private:

  bool check() const {
    Log<UnitTest> odinlog(this,"check");
    ndim nn_reference(3);
    nn_reference[0]=4;
    nn_reference[1]=7;
    nn_reference[2]=9;

    nn_reference.add_dim(2,true);
    nn_reference.add_dim(3,false);

    ndim nn_parse(" ( 2, 4, 7, 9, 3 )");
    if(nn_parse!=nn_reference) {
      ODINLOG(odinlog,errorLog) << "Mismatch: nn_reference/nn_parse=" << STD_string(nn_reference) << "/" << STD_string(nn_parse) << STD_endl;    
      return false;
    }

    unsigned int nn_total_expected=1512;
    unsigned int nn_total_calc=nn_reference.total();
    if(nn_total_calc!=nn_total_expected) {
      ODINLOG(odinlog,errorLog) << "nn_reference.total()= " << nn_total_calc << " != " << nn_total_expected << STD_endl;    
      return false;
    }

    // Testing extent2index vs. index2extent
    for(unsigned int i=0; i<nn_reference.total(); i++) {
       unsigned int j=nn_reference.extent2index(nn_reference.index2extent(i));
       if(i!=j) {
         ODINLOG(odinlog,errorLog) << "i=" << i << " != " << "j=" << j << STD_endl;    
         return false;
       }
    }


    return true;
  }

};

void alloc_NdimTest() {new NdimTest();} // create test instance
#endif




/**************************************************************/
/******************       Template code     *******************/
/**************************************************************/

/*------------------------------------------------------*/

template<class V, class T>
tjarray<V,T>::tjarray () : V() {
  extent.resize(1);
  extent[0] = 0;
}


template<class V, class T>
tjarray<V,T>::tjarray (unsigned long n1) {redim(create_extent(n1));}

template<class V, class T>
tjarray<V,T>::tjarray (unsigned long n1, unsigned long n2) {redim(create_extent(n1,n2));}

template<class V, class T>
tjarray<V,T>::tjarray (unsigned long n1, unsigned long n2, unsigned long n3) {redim(create_extent(n1,n2,n3));}

template<class V, class T>
tjarray<V,T>::tjarray (unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4) {redim(create_extent(n1,n2,n3,n4));}

template<class V, class T>
tjarray<V,T>::tjarray (unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4, unsigned long n5) {redim(create_extent(n1,n2,n3,n4,n5));}



template<class V, class T>
tjarray<V,T>::tjarray (const tjarray<V,T> &ta) : V(ta) {
  extent=ta.extent;
}


template<class V, class T>
tjarray<V,T>::tjarray (const V& sv) : V(sv) {
  extent.resize(1);
  extent[0]=sv.size();
}


template<class V, class T>
tjarray<V,T>::tjarray (const ndim &nn) {
  V::resize(nn.total());
  extent=nn;
}

template<class V, class T>
tjarray<V,T>& tjarray<V,T>::operator = (const tjarray<V,T>& ta) {
  Log<VectorComp> odinlog("tjarray","operator = (const tjarray<V,T>&)");
  V::operator = (ta);
  extent=ta.extent;
  return *this;
}



template<class V, class T>
  tjarray<V,T>& tjarray<V,T>::operator = (const T& value) {
    for(unsigned int i=0; i<extent.total(); i++) (*this)[i]=value;
//    V::operator = (value);
    return *this;
  }



template<class V, class T>
V& tjarray<V,T>::resize (unsigned int newsize) {
  Log<VectorComp> odinlog("tjarray","resize");
  extent.resize (1);
  extent[0]=newsize;
  V::resize(extent.total());
  return *this;
}


template<class V, class T>
tjarray<V,T>& tjarray<V,T>::redim (const ndim &nn) {
  Log<VectorComp> odinlog("tjarray","redim");
  ODINLOG(odinlog,normalDebug) << "nn=" << STD_string(nn) << STD_endl;
  unsigned int newtot=nn.total();
  if(total()!=newtot) V::resize(newtot);
  extent=nn;
  return *this;
}


template<class V, class T>
tjarray<V,T>& tjarray<V,T>::redim (unsigned long n1) {redim(create_extent(n1)); return *this;}

template<class V, class T>
tjarray<V,T>& tjarray<V,T>::redim (unsigned long n1, unsigned long n2) {redim(create_extent(n1,n2)); return *this;}

template<class V, class T>
tjarray<V,T>& tjarray<V,T>::redim (unsigned long n1, unsigned long n2, unsigned long n3) {redim(create_extent(n1,n2,n3)); return *this;}

template<class V, class T>
tjarray<V,T>& tjarray<V,T>::redim (unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4) {redim(create_extent(n1,n2,n3,n4)); return *this;}

template<class V, class T>
tjarray<V,T>& tjarray<V,T>::redim (unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4, unsigned long n5) {redim(create_extent(n1,n2,n3,n4,n5)); return *this;}
//#endif


template<class V, class T>
const ndim& tjarray<V,T>::get_extent () const {
  return extent;
}





template<class V, class T>
T& tjarray<V,T>::operator () (const ndim& ii) {
  Log<VectorComp> odinlog("tjarray","operator ()");
  unsigned long totalIndex=extent.extent2index(ii);
  unsigned long totalExtent=extent.total();
  ODINLOG(odinlog,normalDebug) << "totalIndex/totalExtent=" << totalIndex << "/" << totalExtent << STD_endl;
  if(totalIndex>=totalExtent) return element_dummy;
  else return (*this)[totalIndex];
}

template<class V, class T>
const T& tjarray<V,T>::operator () (const ndim& ii) const {
  Log<VectorComp> odinlog("tjarray","operator () const");
  unsigned long totalIndex=extent.extent2index(ii);
  unsigned long totalExtent=extent.total();
  ODINLOG(odinlog,normalDebug) << "totalIndex/totalExtent=" << totalIndex << "/" << totalExtent << STD_endl;
  if(totalIndex>=totalExtent) return element_dummy;
  else return (*this)[totalIndex];
}


template<class V, class T>
T& tjarray<V,T>::operator () (unsigned long n1) {return tjarray<V,T>::operator () (create_extent(n1));}

template<class V, class T>
T& tjarray<V,T>::operator () (unsigned long n1, unsigned long n2) {return tjarray<V,T>::operator () (create_extent(n1,n2));}

template<class V, class T>
T& tjarray<V,T>::operator () (unsigned long n1, unsigned long n2, unsigned long n3) {return tjarray<V,T>::operator () (create_extent(n1,n2,n3));}

template<class V, class T>
T& tjarray<V,T>::operator () (unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4) {return tjarray<V,T>::operator () (create_extent(n1,n2,n3,n4));}

template<class V, class T>
T& tjarray<V,T>::operator () (unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4, unsigned long n5) {return tjarray<V,T>::operator () (create_extent(n1,n2,n3,n4,n5));}



template<class V, class T>
const T& tjarray<V,T>::operator () (unsigned long n1) const {return tjarray<V,T>::operator () (create_extent(n1));}

template<class V, class T>
const T& tjarray<V,T>::operator () (unsigned long n1, unsigned long n2) const {return tjarray<V,T>::operator () (create_extent(n1,n2));}

template<class V, class T>
const T& tjarray<V,T>::operator () (unsigned long n1, unsigned long n2, unsigned long n3) const {return tjarray<V,T>::operator () (create_extent(n1,n2,n3));}

template<class V, class T>
const T& tjarray<V,T>::operator () (unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4) const {return tjarray<V,T>::operator () (create_extent(n1,n2,n3,n4));}

template<class V, class T>
const T& tjarray<V,T>::operator () (unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4, unsigned long n5) const {return tjarray<V,T>::operator () (create_extent(n1,n2,n3,n4,n5));}




template<class V, class T>
unsigned long tjarray<V,T>::dim() const {return extent.dim();}

template<class V, class T>
unsigned long tjarray<V,T>::size(unsigned long i) const {return extent[i];}

template<class V, class T>
unsigned long tjarray<V,T>::total() const {return extent.total();}

template<class V, class T>
unsigned long tjarray<V,T>::length() const {return total();}

template<class V, class T>
unsigned int tjarray<V,T>::elementsize() const {return sizeof(T);}

template<class V, class T>
tjarray<V,T>& tjarray<V,T>::assignValues(const tjarray<V,T> &ta) {
  Log<VectorComp> odinlog("tjvector","assignValues");
  if(ta.length()==length()) {
    for(unsigned int i=0; i<length(); i++) (*this)[i]=ta[i];
  } else ODINLOG(odinlog,normalDebug) << "size mismatch " << itos(length()) << "!=" << itos(length()) << STD_endl;
  return *this;
}

template<class V, class T>
tjarray<V,T>& tjarray<V,T>::copy(const tjarray<V,T> &ta) {
  redim (ta.get_extent());
  assignValues(ta);
  return *this;
}




template<class V, class T>
ndim  tjarray<V,T>::create_extent(unsigned long n1) {
  ndim nn(1); nn[0]=n1;
  return nn;
}

template<class V, class T>
ndim  tjarray<V,T>::create_extent(unsigned long n1,unsigned long n2) {
  ndim nn(2); nn[0]=n1; nn[1]=n2;
  return nn;
}

template<class V, class T>
ndim  tjarray<V,T>::create_extent(unsigned long n1,unsigned long n2,unsigned long n3) {
  ndim nn(3); nn[0]=n1; nn[1]=n2; nn[2]=n3;
  return nn;
}

template<class V, class T>
ndim  tjarray<V,T>::create_extent(unsigned long n1,unsigned long n2,unsigned long n3,unsigned long n4) {
  ndim nn(4); nn[0]=n1; nn[1]=n2; nn[2]=n3; nn[3]=n4;
  return nn;
}

template<class V, class T>
ndim  tjarray<V,T>::create_extent(unsigned long n1,unsigned long n2,unsigned long n3,unsigned long n4,unsigned long n5) {
  ndim nn(5); nn[0]=n1; nn[1]=n2; nn[2]=n3; nn[3]=n4; nn[4]=n5;
  return nn;
}


template<class V, class T>
ndim tjarray<V,T>::create_index(unsigned long index) const {return extent.index2extent(index);}


/////////////////////////////////////////////////////////////////////////////////


#ifndef NO_UNIT_TEST
class ArrayTest : public UnitTest {

 public:
  ArrayTest() : UnitTest("array") {}

 private:
  bool check() const {
    Log<UnitTest> odinlog(this,"check");

    farray fa(1,2,3,4,5);

    STD_string expected="( 1, 2, 3, 4, 5 )";
    STD_string printed=fa.get_extent();

    if(printed!=expected) {
      ODINLOG(odinlog,errorLog) << "farray(...) failed: got extent >" << printed << "<, but expected >" << expected << "<" << STD_endl;
      return false;
    }

    fa.redim(3,2,1);
    expected="( 3, 2, 1 )";
    printed=fa.get_extent();

    if(printed!=expected) {
      ODINLOG(odinlog,errorLog) << "farray.redim(...) failed: got extent >" << printed << "<, but expected >" << expected << "<" << STD_endl;
      return false;
    }

    float testval=44.0;
    fa(2,1,0)=testval;
    if(fa.sum()!=44.0) {
      ODINLOG(odinlog,errorLog) << "farray.operator (...) failed: " << fa.sum() << "!=" << testval << STD_endl;
      return false;
    }

    return true;
  }

};

void alloc_ArrayTest() {new ArrayTest();} // create test instance
#endif



/**************************************************************/
/******************       Template instantiations     *********/
/**************************************************************/



template class tjarray<fvector,float>;
template class tjarray<dvector,double>;
template class tjarray<ivector,int>;
template class tjarray<svector,STD_string>;
template class tjarray<cvector,STD_complex>;



/////////////////////////////////////////////////////////////////////////////
// Helper functions:

STD_string print_table(const sarray& table) {
  Log<VectorComp> odinlog("","print_table");
  STD_string result;

  if(table.dim()!=2) {
    ODINLOG(odinlog,errorLog) << "Dimension of input array != 2" << STD_endl;
    return result;
  }

  int ncols=table.size(1);
  int nrows=table.size(0);

  int icol, irow;

  // Get max width of each column
  ivector width(ncols); width=0;
  for(irow=0; irow<nrows; irow++) {
    for(icol=0; icol<ncols; icol++) {
      width[icol]=STD_max(int(table(irow,icol).length()), width[icol]);
    }
  }

  // Print formatted table
  for(irow=0; irow<nrows; irow++) {
    for(icol=0; icol<ncols; icol++) {
      int nfillspaces=width[icol]-table(irow,icol).length()+1;
      if(icol==(ncols-1)) nfillspaces=0; // do not pad at end
      result+=table(irow,icol)+STD_string(nfillspaces,' ');
    }
    result+="\n";
  }

  return result;
}


sarray parse_table(const STD_string& str) {

  svector rows(tokens(str,'\n'));

  unsigned int nrows=rows.size();
  unsigned int ncols=0;
  if(nrows) ncols=tokens(rows[0]).size();

  sarray result(nrows,ncols);

  for(unsigned int irow=0; irow<nrows; irow++) {
    svector toks(tokens(rows[irow]));
    for(unsigned int icol=0; (icol<toks.size() && icol<ncols); icol++) {
      result(irow,icol)=toks[icol];
    }
  }

  return result;
}