-
Notifications
You must be signed in to change notification settings - Fork 1
/
Array.h
1631 lines (1464 loc) · 39.9 KB
/
Array.h
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Array.h: A high-performance multi-dimensional C++ array class
Copyright (C) 1997-2010 John C. Bowman
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef __Array_h__
#define __Array_h__ 1
#define __ARRAY_H_VERSION__ 1.44
// Defining NDEBUG improves optimization but disables argument checking.
// Defining __NOARRAY2OPT inhibits special optimization of Array2[].
#include <iostream>
#include <sstream>
#include <climits>
#include <cstdlib>
#include <cerrno>
#ifdef NDEBUG
#define __check(i,n,dim,m)
#define __checkSize()
#define __checkEqual(a,b,dim,m)
#define __checkActivate(i,align) this->Activate(align)
#else
#define __check(i,n,dim,m) this->Check(i,n,dim,m)
#define __checkSize() this->CheckSize()
#define __checkEqual(a,b,dim,m) this->CheckEqual(a,b,dim,m)
#define __checkActivate(i,align) this->CheckActivate(i,align)
#ifndef __NOARRAY2OPT
#define __NOARRAY2OPT
#endif
#endif
#ifndef HAVE_POSIX_MEMALIGN
#ifdef __GLIBC_PREREQ
#if __GLIBC_PREREQ(2,3)
#define HAVE_POSIX_MEMALIGN
#endif
#else
#ifdef _POSIX_SOURCE
#define HAVE_POSIX_MEMALIGN
#endif
#endif
#else
#ifdef _AIX
extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size);
#endif
#endif
namespace Array {
inline std::ostream& _newl(std::ostream& s) {s << '\n'; return s;}
inline void ArrayExit(const char *x);
#ifndef __ExternalArrayExit
#include <cassert>
inline void ArrayExit(const char *x)
{
std::cerr << _newl << "ERROR: " << x << "." << std::endl;
assert(false);
exit(1);
}
#endif
#ifndef __fftwpp_h__
// Adapted from FFTW aligned malloc/free. Assumes that malloc is at least
// sizeof(void*)-aligned. Allocated memory must be freed with free0.
inline int posix_memalign0(void **memptr, size_t alignment, size_t size)
{
if(alignment % sizeof (void *) != 0 || (alignment & (alignment - 1)) != 0)
return EINVAL;
void *p0=malloc(size+alignment);
if(!p0) return ENOMEM;
void *p=(void *)(((size_t) p0+alignment)&~(alignment-1));
*((void **) p-1)=p0;
*memptr=p;
return 0;
}
inline void free0(void *p)
{
if(p) free(*((void **) p-1));
}
template<class T>
inline void newAlign(T *&v, size_t len, size_t align)
{
void *mem=NULL;
const char *invalid="Invalid alignment requested";
const char *nomem="Memory limits exceeded";
#ifdef HAVE_POSIX_MEMALIGN
int rc=posix_memalign(&mem,align,len*sizeof(T));
#else
int rc=posix_memalign0(&mem,align,len*sizeof(T));
#endif
if(rc == EINVAL) Array::ArrayExit(invalid);
if(rc == ENOMEM) Array::ArrayExit(nomem);
v=(T *) mem;
for(size_t i=0; i < len; i++) new(v+i) T;
}
template<class T>
inline void deleteAlign(T *v, size_t len)
{
for(size_t i=len-1; i > 0; i--) v[i].~T();
v[0].~T();
#ifdef HAVE_POSIX_MEMALIGN
free(v);
#else
free0(v);
#endif
}
#endif
template<class T>
class array1 {
protected:
T *v;
unsigned int size;
mutable int state;
public:
enum alloc_state {unallocated=0, allocated=1, temporary=2, aligned=4};
virtual unsigned int Size() const {return size;}
void CheckSize() const {
if(!test(allocated) && size == 0)
ArrayExit("Operation attempted on unallocated array");
}
void CheckEqual(int a, int b, unsigned int dim, unsigned int m) const {
if(a != b) {
std::ostringstream buf;
buf << "Array" << dim << " index ";
if(m) buf << m << " ";
buf << "is incompatible in assignment (" << a << " != " << b << ")";
ArrayExit(buf.str().c_str());
}
}
int test(int flag) const {return state & flag;}
void clear(int flag) const {state &= ~flag;}
void set(int flag) const {state |= flag;}
void Activate(size_t align=0) {
if(align) {
newAlign(v,size,align);
set(allocated | aligned);
} else {
v=new T[size];
set(allocated);
}
}
void CheckActivate(int dim, size_t align=0) {
if (test(allocated)) {
std::ostringstream buf;
buf << "Reallocation of Array" << dim
<< " attempted (must Deallocate first)";
ArrayExit(buf.str().c_str());
}
Activate(align);
}
void Deallocate() const {
if(test(allocated)) {
if(test(aligned)) deleteAlign(v,size);
else delete [] v;
state=unallocated;
}
}
void Dimension(unsigned int nx0) {size=nx0;}
void Dimension(unsigned int nx0, T *v0) {
Dimension(nx0); v=v0; clear(allocated);
}
void Dimension(const array1<T>& A) {
Dimension(A.size,A.v); state=A.test(temporary);
}
void Allocate(unsigned int nx0, size_t align=0) {
Dimension(nx0);
__checkActivate(1,align);
}
void Reallocate(unsigned int nx0, size_t align=0) {
Deallocate();
Allocate(nx0,align);
}
array1() : size(0), state(unallocated) {}
array1(const void *) : size(0), state(unallocated) {}
array1(unsigned int nx0, size_t align=0) : state(unallocated) {
Allocate(nx0,align);
}
array1(unsigned int nx0, T *v0) : state(unallocated) {Dimension(nx0,v0);}
array1(T *v0) : state(unallocated) {Dimension(INT_MAX,v0);}
array1(const array1<T>& A) : v(A.v), size(A.size),
state(A.test(temporary)) {}
virtual ~array1() {Deallocate();}
void Freeze() {state=unallocated;}
void Hold() {if(test(allocated)) {state=temporary;}}
void Purge() const {if(test(temporary)) {Deallocate(); state=unallocated;}}
virtual void Check(int i, int n, unsigned int dim, unsigned int m,
int o=0) const {
if(i < 0 || i >= n) {
std::ostringstream buf;
buf << "Array" << dim << " index ";
if(m) buf << m << " ";
buf << "is out of bounds (" << i+o;
if(n == 0) buf << " index given to empty array";
else {
if(i < 0) buf << " < " << o;
else buf << " > " << n+o-1;
}
buf << ")";
ArrayExit(buf.str().c_str());
}
}
unsigned int Nx() const {return size;}
#ifdef NDEBUG
typedef T *opt;
#else
typedef array1<T> opt;
#endif
T* begin() const { return v; }
T* end() const { return v + size; }
T& operator [] (int ix) const {__check(ix,size,1,1); return v[ix];}
T& operator () (int ix) const {__check(ix,size,1,1); return v[ix];}
T* operator () () const {return v;}
operator T* () const {return v;}
array1<T> operator + (int i) const {return array1<T>(size-i,v+i);}
void Load(T a) const {
__checkSize();
for(unsigned int i=0; i < size; i++) v[i]=a;
}
void Load(const T *a) const {
for(unsigned int i=0; i < size; i++) v[i]=a[i];
}
void Store(T *a) const {
for(unsigned int i=0; i < size; i++) a[i]=v[i];
}
void Set(T *a) {v=a; clear(allocated);}
T Min() {
if(size == 0)
ArrayExit("Cannot take minimum of empty array");
T min=v[0];
for(unsigned int i=1; i < size; i++) if(v[i] < min) min=v[i];
return min;
}
T Max() {
if(size == 0)
ArrayExit("Cannot take maximum of empty array");
T max=v[0];
for(unsigned int i=1; i < size; i++) if(v[i] > max) max=v[i];
return max;
}
std::istream& Input (std::istream &s) const {
__checkSize();
for(unsigned int i=0; i < size; i++) s >> v[i];
return s;
}
array1<T>& operator = (T a) {Load(a); return *this;}
array1<T>& operator = (const T *a) {Load(a); return *this;}
array1<T>& operator = (const array1<T>& A) {
__checkEqual(size,A.Size(),1,1);
Load(A());
A.Purge();
return *this;
}
array1<T>& operator += (const array1<T>& A) {
__checkSize();
for(unsigned int i=0; i < size; i++) v[i] += A(i);
return *this;
}
array1<T>& operator -= (const array1<T>& A) {
__checkSize();
for(unsigned int i=0; i < size; i++) v[i] -= A(i);
return *this;
}
array1<T>& operator *= (const array1<T>& A) {
__checkSize();
for(unsigned int i=0; i < size; i++) v[i] *= A(i);
return *this;
}
array1<T>& operator /= (const array1<T>& A) {
__checkSize();
for(unsigned int i=0; i < size; i++) v[i] /= A(i);
return *this;
}
array1<T>& operator += (T a) {
__checkSize();
for(unsigned int i=0; i < size; i++) v[i] += a;
return *this;
}
array1<T>& operator -= (T a) {
__checkSize();
for(unsigned int i=0; i < size; i++) v[i] -= a;
return *this;
}
array1<T>& operator *= (T a) {
__checkSize();
for(unsigned int i=0; i < size; i++) v[i] *= a;
return *this;
}
array1<T>& operator /= (T a) {
__checkSize();
T ainv=1.0/a;
for(unsigned int i=0; i < size; i++) v[i] *= ainv;
return *this;
}
double L1() const {
__checkSize();
double norm=0.0;
for(unsigned int i=0; i < size; i++) norm += abs(v[i]);
return norm;
}
#ifdef __ArrayExtensions
double Abs2() const {
__checkSize();
double norm=0.0;
for(unsigned int i=0; i < size; i++) norm += abs2(v[i]);
return norm;
}
double L2() const {
return sqrt(Abs2());
}
double LInfinity() const {
__checkSize();
double norm=0.0;
for(unsigned int i=0; i < size; i++) {
T a=abs(v[i]);
if(a > norm) norm=a;
}
return norm;
}
double LMinusInfinity() const {
__checkSize();
double norm=DBL_MAX;
for(unsigned int i=0; i < size; i++) {
T a=abs(v[i]);
if(a < norm) norm=a;
}
return norm;
}
#endif
};
template<class T>
void swaparray(T& A, T& B)
{
T C;
C.Dimension(A);
A.Dimension(B);
B.Dimension(C);
}
template<class T>
void leftshiftarray(T& A, T& B, T& C)
{
T D;
D.Dimension(A);
A.Dimension(B);
B.Dimension(C);
C.Dimension(D);
}
template<class T>
void rightshiftarray(T& A, T& B, T& C)
{
T D;
D.Dimension(C);
C.Dimension(B);
B.Dimension(A);
A.Dimension(D);
}
template<class T>
std::ostream& operator << (std::ostream& s, const array1<T>& A)
{
T *p=A();
for(unsigned int i=0; i < A.Nx(); i++) {
s << *(p++) << " ";
}
return s;
}
template<class T>
std::istream& operator >> (std::istream& s, const array1<T>& A)
{
return A.Input(s);
}
template<class T>
class array2 : public array1<T> {
protected:
unsigned int nx;
unsigned int ny;
public:
void Dimension(unsigned int nx0, unsigned int ny0) {
nx=nx0; ny=ny0;
this->size=nx*ny;
}
void Dimension(unsigned int nx0, unsigned int ny0, T *v0) {
Dimension(nx0,ny0);
this->v=v0;
clear(this->allocated);
}
void Dimension(const array1<T> &A) {ArrayExit("Operation not implemented");}
void Allocate(unsigned int nx0, unsigned int ny0, size_t align=0) {
Dimension(nx0,ny0);
__checkActivate(2,align);
}
array2() : nx(0), ny(0) {}
array2(unsigned int nx0, unsigned int ny0, size_t align=0) {
Allocate(nx0,ny0,align);
}
array2(unsigned int nx0, unsigned int ny0, T *v0) {Dimension(nx0,ny0,v0);}
unsigned int Nx() const {return nx;}
unsigned int Ny() const {return ny;}
#ifndef __NOARRAY2OPT
T *operator [] (int ix) const {
return this->v+ix*ny;
}
#else
array1<T> operator [] (int ix) const {
__check(ix,nx,2,1);
return array1<T>(ny,this->v+ix*ny);
}
#endif
T& operator () (int ix, int iy) const {
__check(ix,nx,2,1);
__check(iy,ny,2,2);
return this->v[ix*ny+iy];
}
T& operator () (int i) const {
__check(i,this->size,2,0);
return this->v[i];
}
T* operator () () const {return this->v;}
array2<T>& operator = (T a) {Load(a); return *this;}
array2<T>& operator = (T *a) {Load(a); return *this;}
array2<T>& operator = (const array2<T>& A) {
__checkEqual(nx,A.Nx(),2,1);
__checkEqual(ny,A.Ny(),2,2);
Load(A());
A.Purge();
return *this;
}
array2<T>& operator += (const array2<T>& A) {
__checkSize();
for(unsigned int i=0; i < this->size; i++) this->v[i] += A(i);
return *this;
}
array2<T>& operator -= (const array2<T>& A) {
__checkSize();
for(unsigned int i=0; i < this->size; i++) this->v[i] -= A(i);
return *this;
}
array2<T>& operator *= (const array2<T>& A);
array2<T>& operator += (T a) {
__checkSize();
unsigned int inc=ny+1;
for(unsigned int i=0; i < this->size; i += inc) this->v[i] += a;
return *this;
}
array2<T>& operator -= (T a) {
__checkSize();
unsigned int inc=ny+1;
for(unsigned int i=0; i < this->size; i += inc) this->v[i] -= a;
return *this;
}
array2<T>& operator *= (T a) {
__checkSize();
for(unsigned int i=0; i < this->size; i++) this->v[i] *= a;
return *this;
}
void Identity() {
Load((T) 0);
__checkSize();
unsigned int inc=ny+1;
for(unsigned int i=0; i < this->size; i += inc) this->v[i]=(T) 1;
}
};
template<class T>
std::ostream& operator << (std::ostream& s, const array2<T>& A)
{
T *p=A();
for(unsigned int i=0; i < A.Nx(); i++) {
for(unsigned int j=0; j < A.Ny(); j++) {
s << *(p++) << " ";
}
s << _newl;
}
s << std::flush;
return s;
}
template<class T>
std::istream& operator >> (std::istream& s, const array2<T>& A)
{
return A.Input(s);
}
template<class T>
class array3 : public array1<T> {
protected:
unsigned int nx;
unsigned int ny;
unsigned int nz;
unsigned int nyz;
public:
void Dimension(unsigned int nx0, unsigned int ny0, unsigned int nz0) {
nx=nx0; ny=ny0; nz=nz0; nyz=ny*nz;
this->size=nx*nyz;
}
void Dimension(unsigned int nx0, unsigned int ny0, unsigned int nz0, T *v0) {
Dimension(nx0,ny0,nz0);
this->v=v0;
clear(this->allocated);
}
void Allocate(unsigned int nx0, unsigned int ny0, unsigned int nz0,
size_t align=0) {
Dimension(nx0,ny0,nz0);
__checkActivate(3,align);
}
array3() : nx(0), ny(0), nz(0), nyz(0) {}
array3(unsigned int nx0, unsigned int ny0, unsigned int nz0,
size_t align=0) {
Allocate(nx0,ny0,nz0,align);
}
array3(unsigned int nx0, unsigned int ny0, unsigned int nz0, T *v0) {
Dimension(nx0,ny0,nz0,v0);
}
unsigned int Nx() const {return nx;}
unsigned int Ny() const {return ny;}
unsigned int Nz() const {return nz;}
array2<T> operator [] (int ix) const {
__check(ix,nx,3,1);
return array2<T>(ny,nz,this->v+ix*nyz);
}
T& operator () (int ix, int iy, int iz) const {
__check(ix,nx,3,1);
__check(iy,ny,3,2);
__check(iz,nz,3,3);
return this->v[ix*nyz+iy*nz+iz];
}
T& operator () (int i) const {
__check(i,this->size,3,0);
return this->v[i];
}
T* operator () () const {return this->v;}
array3<T>& operator = (T a) {Load(a); return *this;}
array3<T>& operator = (T *a) {Load(a); return *this;}
array3<T>& operator = (const array3<T>& A) {
__checkEqual(nx,A.Nx(),3,1);
__checkEqual(ny,A.Ny(),3,2);
__checkEqual(nz,A.Nz(),3,3);
Load(A());
A.Purge();
return *this;
}
array3<T>& operator += (array3<T>& A) {
__checkSize();
for(unsigned int i=0; i < this->size; i++) this->v[i] += A(i);
return *this;
}
array3<T>& operator -= (array3<T>& A) {
__checkSize();
for(unsigned int i=0; i < this->size; i++) this->v[i] -= A(i);
return *this;
}
array3<T>& operator += (T a) {
__checkSize();
unsigned int inc=nyz+nz+1;
for(unsigned int i=0; i < this->size; i += inc) this->v[i] += a;
return *this;
}
array3<T>& operator -= (T a) {
__checkSize();
unsigned int inc=nyz+nz+1;
for(unsigned int i=0; i < this->size; i += inc) this->v[i] -= a;
return *this;
}
};
template<class T>
std::ostream& operator << (std::ostream& s, const array3<T>& A)
{
T *p=A();
for(unsigned int i=0; i < A.Nx(); i++) {
for(unsigned int j=0; j < A.Ny(); j++) {
for(unsigned int k=0; k < A.Nz(); k++) {
s << *(p++) << " ";
}
s << _newl;
}
s << _newl;
}
s << std::flush;
return s;
}
template<class T>
std::istream& operator >> (std::istream& s, const array3<T>& A)
{
return A.Input(s);
}
template<class T>
class array4 : public array1<T> {
protected:
unsigned int nx;
unsigned int ny;
unsigned int nz;
unsigned int nw;
unsigned int nyz;
unsigned int nzw;
unsigned int nyzw;
public:
void Dimension(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0) {
nx=nx0; ny=ny0; nz=nz0; nw=nw0; nzw=nz*nw; nyzw=ny*nzw;
this->size=nx*nyzw;
}
void Dimension(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0, T *v0) {
Dimension(nx0,ny0,nz0,nw0);
this->v=v0;
clear(this->allocated);
}
void Allocate(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0, size_t align=0) {
Dimension(nx0,ny0,nz0,nw0);
__checkActivate(4,align);
}
array4() : nx(0), ny(0), nz(0), nw(0), nyz(0), nzw(0), nyzw(0) {}
array4(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0, size_t align=0) {Allocate(nx0,ny0,nz0,nw0,align);}
array4(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0, T *v0) {
Dimension(nx0,ny0,nz0,nw0,v0);
}
unsigned int Nx() const {return nx;}
unsigned int Ny() const {return ny;}
unsigned int Nz() const {return ny;}
unsigned int N4() const {return nw;}
array3<T> operator [] (int ix) const {
__check(ix,nx,3,1);
return array3<T>(ny,nz,nw,this->v+ix*nyzw);
}
T& operator () (int ix, int iy, int iz, int iw) const {
__check(ix,nx,4,1);
__check(iy,ny,4,2);
__check(iz,nz,4,3);
__check(iw,nw,4,4);
return this->v[ix*nyzw+iy*nzw+iz*nw+iw];
}
T& operator () (int i) const {
__check(i,this->size,4,0);
return this->v[i];
}
T* operator () () const {return this->v;}
array4<T>& operator = (T a) {Load(a); return *this;}
array4<T>& operator = (T *a) {Load(a); return *this;}
array4<T>& operator = (const array4<T>& A) {
__checkEqual(nx,A.Nx(),4,1);
__checkEqual(ny,A.Ny(),4,2);
__checkEqual(nz,A.Nz(),4,3);
__checkEqual(nw,A.N4(),4,4);
Load(A());
A.Purge();
return *this;
}
array4<T>& operator += (array4<T>& A) {
__checkSize();
for(unsigned int i=0; i < this->size; i++) this->v[i] += A(i);
return *this;
}
array4<T>& operator -= (array4<T>& A) {
__checkSize();
for(unsigned int i=0; i < this->size; i++) this->v[i] -= A(i);
return *this;
}
array4<T>& operator += (T a) {
__checkSize();
unsigned int inc=nyzw+nzw+nw+1;
for(unsigned int i=0; i < this->size; i += inc) this->v[i] += a;
return *this;
}
array4<T>& operator -= (T a) {
__checkSize();
unsigned int inc=nyzw+nzw+nw+1;
for(unsigned int i=0; i < this->size; i += inc) this->v[i] -= a;
return *this;
}
};
template<class T>
std::ostream& operator << (std::ostream& s, const array4<T>& A)
{
T *p=A;
for(unsigned int i=0; i < A.Nx(); i++) {
for(unsigned int j=0; j < A.Ny(); j++) {
for(unsigned int k=0; k < A.Nz(); k++) {
for(unsigned int l=0; l < A.N4(); l++) {
s << *(p++) << " ";
}
s << _newl;
}
s << _newl;
}
s << _newl;
}
s << std::flush;
return s;
}
template<class T>
std::istream& operator >> (std::istream& s, const array4<T>& A)
{
return A.Input(s);
}
template<class T>
class array5 : public array1<T> {
protected:
unsigned int nx;
unsigned int ny;
unsigned int nz;
unsigned int nw;
unsigned int nv;
unsigned int nwv;
unsigned int nzwv;
unsigned int nyzwv;
public:
void Dimension(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0, unsigned int nv0) {
nx=nx0; ny=ny0; nz=nz0; nw=nw0; nv=nv0; nwv=nw*nv; nzwv=nz*nwv;
nyzwv=ny*nzwv;
this->size=nx*nyzwv;
}
void Dimension(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0, unsigned int nv0, T *v0) {
Dimension(nx0,ny0,nz0,nw0,nv0);
this->v=v0;
clear(this->allocated);
}
void Allocate(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0, unsigned int nv0, size_t align=0) {
Dimension(nx0,ny0,nz0,nw0,nv0);
__checkActivate(5,align);
}
array5() : nx(0), ny(0), nz(0), nw(0), nv(0), nwv(0), nzwv(0), nyzwv(0) {}
array5(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0, unsigned int nv0, size_t align=0) {
Allocate(nx0,ny0,nz0,nw0,nv0,align);
}
array5(unsigned int nx0, unsigned int ny0, unsigned int nz0,
unsigned int nw0, unsigned int nv0, T *v0) {
Dimension(nx0,ny0,nz0,nw0,nv0,nv0);
}
unsigned int Nx() const {return nx;}
unsigned int Ny() const {return ny;}
unsigned int Nz() const {return ny;}
unsigned int N4() const {return nw;}
unsigned int N5() const {return nv;}
array4<T> operator [] (int ix) const {
__check(ix,nx,4,1);
return array4<T>(ny,nz,nw,nv,this->v+ix*nyzwv);
}
T& operator () (int ix, int iy, int iz, int iw, int iv) const {
__check(ix,nx,5,1);
__check(iy,ny,5,2);
__check(iz,nz,5,3);
__check(iw,nw,5,4);
__check(iv,nv,5,5);
return this->v[ix*nyzwv+iy*nzwv+iz*nwv+iw*nv+iv];
}
T& operator () (int i) const {
__check(i,this->size,5,0);
return this->v[i];
}
T* operator () () const {return this->v;}
array5<T>& operator = (T a) {Load(a); return *this;}
array5<T>& operator = (T *a) {Load(a); return *this;}
array5<T>& operator = (const array5<T>& A) {
__checkEqual(nx,A.Nx(),5,1);
__checkEqual(ny,A.Ny(),5,2);
__checkEqual(nz,A.Nz(),5,3);
__checkEqual(nw,A.N4(),5,4);
__checkEqual(nv,A.N5(),5,5);
Load(A());
A.Purge();
return *this;
}
array5<T>& operator += (array5<T>& A) {
__checkSize();
for(unsigned int i=0; i < this->size; i++) this->v[i] += A(i);
return *this;
}
array5<T>& operator -= (array5<T>& A) {
__checkSize();
for(unsigned int i=0; i < this->size; i++) this->v[i] -= A(i);
return *this;
}
array5<T>& operator += (T a) {
__checkSize();
unsigned int inc=nyzwv+nzwv+nwv+nv+1;
for(unsigned int i=0; i < this->size; i += inc) this->v[i] += a;
return *this;
}
array5<T>& operator -= (T a) {
__checkSize();
unsigned int inc=nyzwv+nzwv+nwv+nv+1;
for(unsigned int i=0; i < this->size; i += inc) this->v[i] -= a;
return *this;
}
};
template<class T>
std::ostream& operator << (std::ostream& s, const array5<T>& A)
{
T *p=A;
for(unsigned int i=0; i < A.Nx(); i++) {
for(unsigned int j=0; j < A.Ny(); j++) {
for(unsigned int k=0; k < A.Nz(); k++) {
for(unsigned int l=0; l < A.N4(); l++) {
for(unsigned int l=0; l < A.N5(); l++) {
s << *(p++) << " ";
}
s << _newl;
}
s << _newl;
}
s << _newl;
}
s << _newl;
}
s << std::flush;
return s;
}
template<class T>
std::istream& operator >> (std::istream& s, const array5<T>& A)
{
return A.Input(s);
}
#undef __check
#ifdef NDEBUG
#define __check(i,n,o,dim,m)
#else
#define __check(i,n,o,dim,m) Check(i-o,n,dim,m,o)
#endif
template<class T>
class Array1 : public array1<T> {
protected:
T *voff; // Offset pointer to memory block
int ox;
public:
void Offsets() {
voff=this->v-ox;
}
void Dimension(unsigned int nx0, int ox0=0) {
this->size=nx0;
ox=ox0;
Offsets();
}
void Dimension(unsigned int nx0, T *v0, int ox0=0) {
this->v=v0;
Dimension(nx0,ox0);
clear(this->allocated);
}
void Dimension(const Array1<T>& A) {
Dimension(A.size,A.v,A.ox); this->state=A.test(this->temporary);
}
void Allocate(unsigned int nx0, int ox0=0, size_t align=0) {
Dimension(nx0,ox0);
__checkActivate(1,align);
Offsets();
}
void Reallocate(unsigned int nx0, int ox0=0, size_t align=0) {
this->Deallocate();
Allocate(nx0,ox0,align);
}
Array1() : ox(0) {}
Array1(unsigned int nx0, int ox0=0, size_t align=0) {
Allocate(nx0,ox0,align);
}
Array1(unsigned int nx0, T *v0, int ox0=0) {
Dimension(nx0,v0,ox0);
}
Array1(T *v0) {
Dimension(INT_MAX,v0);
}
#ifdef NDEBUG
typedef T *opt;
#else
typedef Array1<T> opt;
#endif
T& operator [] (int ix) const {__check(ix,this->size,ox,1,1); return voff[ix];}
T& operator () (int i) const {__check(i,this->size,0,1,1); return this->v[i];}
T* operator () () const {return voff;}
operator T* () const {return voff;}
Array1<T> operator + (int i) const {return Array1<T>(this->size-i,this->v+i,ox);}
void Set(T *a) {this->v=a; Offsets(); clear(this->allocated);}
Array1<T>& operator = (T a) {Load(a); return *this;}
Array1<T>& operator = (const T *a) {Load(a); return *this;}
Array1<T>& operator = (const Array1<T>& A) {
__checkEqual(this->size,A.Size(),1,1);
__checkEqual(ox,A.Ox(),1,1);
Load(A());
A.Purge();
return *this;
}
Array1<T>& operator = (const array1<T>& A) {
__checkEqual(this->size,A.Size(),1,1);
__checkEqual(ox,0,1,1);
Load(A());
A.Purge();
return *this;
}
int Ox() const {return ox;}
};
template<class T>
class Array2 : public array2<T> {
protected:
T *voff,*vtemp;
int ox,oy;
public:
void Offsets() {
vtemp=this->v-ox*(int) this->ny;
voff=vtemp-oy;