File: nd_array.hh

package info (click to toggle)
lepton 1.2.1+20170405-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,248 kB
  • ctags: 3,431
  • sloc: cpp: 24,345; ansic: 7,872; python: 330; makefile: 246; sh: 92; perl: 30
file content (1153 lines) | stat: -rw-r--r-- 44,122 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
/*  Sirikata Utilities -- Sirikata Array Utilities
 *  ArrayNd.hpp
 *
 *  Copyright (c) 2009, Daniel Reiter Horn
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of Sirikata nor the names of its contributors may
 *    be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _SIRIKATA_ARRAY_ND_HPP_
#define _SIRIKATA_ARRAY_ND_HPP_
#include <assert.h>
#include <cstddef>
#include <cstring>
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus > 199711L || defined(_WIN32)
#include <cstdint>
#else
#include <assert.h>
#include <stdint.h>
#define constexpr
#define NOCONSTEXPR
#endif
#ifdef AVOID_ARRAY_BOUNDS_CHECKS
#define ARRAY_BOUNDS_ASSERT(x) assert(x)
#else
#include "memory.hh"
#define ARRAY_BOUNDS_ASSERT(x) always_assert(x)
#endif

namespace Sirikata {

template<class T> class ReferenceType {
public:
    typedef typename T::Array BaseArrayType;
    typedef typename T::Array * ArrayType;
    static BaseArrayType& dereference(BaseArrayType*a) {
        return *a;
    }
    static const BaseArrayType& dereference(const BaseArrayType*a) {
        return *a;
    }
};
template<class T> class DirectType {
public:
    typedef typename T::Array BaseArrayType;
    typedef typename T::Array ArrayType;
    static BaseArrayType& dereference(BaseArrayType &a) {
        return a;
    }
    static const BaseArrayType& dereference(const BaseArrayType&a) {
        return a;
    }
};
class RoundToPow2 { public:
    enum {
        SHOULD_ROUND_POW2 = 1
    };
};
class DontRoundPow2 { public:
    enum {
        SHOULD_ROUND_POW2 = 0
    };
};
template <uint32_t v, class ShouldRound> class RoundP2 {public:
    enum RoundingResult {
        value = (ShouldRound::SHOULD_ROUND_POW2 && (v & (v - 1)) ? (1 + (v | (v >> 1) | (v >> 2) | (v >> 4) | (v >> 8) | (v >> 16))) : v)
    };
    typedef char ARRAY_BOUNDS_ASSERT_power_of_two_constraint[(value & (value - 1)) == 0
                                                || !ShouldRound::SHOULD_ROUND_POW2 ? 1 : -1];
};
template<class T, uint32_t s0, class ShouldRoundPow2> struct ArrayBaseType1d {
    typedef T Array[RoundP2<s0 ? s0 : 1, ShouldRoundPow2>::value];
};
template<class T, uint32_t s0, uint32_t s1, class ShouldRoundPow2> struct ArrayBaseType2d {
    typedef T Array[RoundP2<s0, ShouldRoundPow2>::value][RoundP2<s1, ShouldRoundPow2>::value];
};
template<class T, uint32_t s0, uint32_t s1, uint32_t s2,
         class ShouldRoundPow2> struct ArrayBaseType3d {
    typedef T Array[RoundP2<s0, ShouldRoundPow2>::value][RoundP2<s1, ShouldRoundPow2>::value][RoundP2<s2, ShouldRoundPow2>::value];
};
template<class T, uint32_t s0, uint32_t s1, uint32_t s2, uint32_t s3,
         class ShouldRoundPow2> struct ArrayBaseType4d {
    typedef T Array[RoundP2<s0, ShouldRoundPow2>::value][RoundP2<s1, ShouldRoundPow2>::value][RoundP2<s2, ShouldRoundPow2>::value][RoundP2<s3, ShouldRoundPow2>::value];
};
template<class T, uint32_t s0, uint32_t s1, uint32_t s2, uint32_t s3, uint32_t s4,
         class ShouldRoundPow2> struct ArrayBaseType5d {
    typedef T Array[RoundP2<s0, ShouldRoundPow2>::value][RoundP2<s1, ShouldRoundPow2>::value][RoundP2<s2, ShouldRoundPow2>::value][RoundP2<s3, ShouldRoundPow2>::value][RoundP2<s4, ShouldRoundPow2>::value];
};
template<class T, uint32_t s0, uint32_t s1, uint32_t s2,
         uint32_t s3, uint32_t s4, uint32_t s5, class ShouldRoundPow2> struct ArrayBaseType6d {
    typedef T Array[RoundP2<s0, ShouldRoundPow2>::value][RoundP2<s1, ShouldRoundPow2>::value][RoundP2<s2, ShouldRoundPow2>::value][RoundP2<s3, ShouldRoundPow2>::value][RoundP2<s4, ShouldRoundPow2>::value][RoundP2<s5, ShouldRoundPow2>::value];
};
template<class T, uint32_t s0, uint32_t s1, uint32_t s2,
         uint32_t s3, uint32_t s4, uint32_t s5, uint32_t s6,
         class ShouldRoundPow2> struct ArrayBaseType7d {
    typedef T Array[RoundP2<s0, ShouldRoundPow2>::value][RoundP2<s1, ShouldRoundPow2>::value][RoundP2<s2, ShouldRoundPow2>::value][RoundP2<s3, ShouldRoundPow2>::value][RoundP2<s4, ShouldRoundPow2>::value][RoundP2<s5, ShouldRoundPow2>::value][RoundP2<s6, ShouldRoundPow2>::value];
};

template <class T,
          uint32_t s0, class ShouldRoundPow2 = DontRoundPow2,
          class IsReferenceType=DirectType<ArrayBaseType1d<T, s0, ShouldRoundPow2> >
          > struct Array1d {
    typedef typename ArrayBaseType1d<T,s0,ShouldRoundPow2>::Array Array;
    typedef IsReferenceType IsReference;
    typename IsReference::ArrayType data;
    typedef Array1d<T,
                    s0,
                    ShouldRoundPow2,
                    ReferenceType<ArrayBaseType1d<T, s0, ShouldRoundPow2> > > Slice;
    enum Sizes{
        size0 = s0
    };
    static constexpr uint32_t size() {
        return s0;
    }
    static constexpr uint32_t dimsize() {
        return s0;
    }
    static constexpr uint32_t dimension() {
        return 1;
    }
    T& at(uint32_t i0) {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        return IsReference::dereference(data)[i0];
    }
    template<int index> constexpr T kat() const {
        static_assert(index < s0, "template argument must be within bound");
        return data[index];
    }
    const T& at(uint32_t i0) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        return IsReference::dereference(data)[i0];
    }
    T& operator[](uint32_t i0) {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        return IsReference::dereference(data)[i0];
    }
    const T& operator[](uint32_t i0) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        return IsReference::dereference(data)[i0];
    }
    template <class StartEnd> typename Array1d<T,
                                               StartEnd::END - StartEnd::START,
                                               ShouldRoundPow2>::Slice slice(const StartEnd&range) {
        return slice<StartEnd::START, StartEnd::END>();
    }
    template <class StartEnd> typename Array1d<T,
                                               StartEnd::END - StartEnd::START,
                                               ShouldRoundPow2>::Slice slice(const StartEnd&range) const {
        return slice<StartEnd::START, StartEnd::END>();
    }
    template <uint32_t kstart, uint32_t kend> typename Array1d<T, kend - kstart,
                                                           ShouldRoundPow2>::Slice slice() {
            uint8_t ARRAY_BOUNDS_ASSERT_slice_legal[kend > s0 ? -1 : 1];
            uint8_t ARRAY_BOUNDS_ASSERT_slice_start_legal[kend < kstart ? -1 : 1];
            (void)ARRAY_BOUNDS_ASSERT_slice_legal;
            (void)ARRAY_BOUNDS_ASSERT_slice_start_legal;
            const typename Array1d<T, kend-kstart, ShouldRoundPow2>::Slice retval = {(typename Array1d<T, kend-kstart, ShouldRoundPow2>::Slice::IsReference::ArrayType)
                                                                  &IsReference::dereference(data)[kstart]};
            return retval;
    }
    template <uint32_t start, uint32_t end> const typename Array1d<T, end - start, ShouldRoundPow2>::Slice slice() const {
        uint8_t ARRAY_BOUNDS_ASSERT_slice_legal[end > s0 ? -1 : 1];
        uint8_t ARRAY_BOUNDS_ASSERT_slice_start_legal[end < start ? -1 : 1];
        (void)ARRAY_BOUNDS_ASSERT_slice_legal;
        (void)ARRAY_BOUNDS_ASSERT_slice_start_legal;
        const typename Array1d<T, end-start, ShouldRoundPow2>::Slice retval = {(typename Array1d<T, end-start, ShouldRoundPow2>::Slice::IsReference::ArrayType)
                                                              &IsReference::dereference(data)[start]};
        return retval;
    }

    template <uint32_t new_size> typename Array1d<T, new_size,
        ShouldRoundPow2>::Slice dynslice(uint32_t start) {
        uint8_t ARRAY_BOUNDS_ASSERT_slice_size_legal[new_size > s0 ? -1 : 1];
        (void)ARRAY_BOUNDS_ASSERT_slice_size_legal;
        ARRAY_BOUNDS_ASSERT(start + new_size <= s0 && "slice must fit within original array");
        const typename Array1d<T, new_size, ShouldRoundPow2>::Slice retval
            = {(typename Array1d<T, new_size, ShouldRoundPow2>::Slice::IsReference::ArrayType)
                &IsReference::dereference(data)[start]};
        return retval;
    }
          
    void memset(uint8_t val) {
        std::memset(data, val, sizeof(Array));
    }
    template<class F> void foreach(const F& f){
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            f(IsReference::dereference(data)[i0]);
        }
    }
    template<class F> void foreach(const F& f) const{
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            f(IsReference::dereference(data)[i0]);
        }
    }
    T* begin() {
        return (T*)data;
    }
    const T* begin() const {
        return (const T*)data;
    }
    T* end() {
        return (T*)data + s0;
    }
    const T* end() const {
        return (const T*)data + s0;
    }
};

template <class T,
          uint32_t s0, uint32_t s1, class ShouldRoundPow2 = DontRoundPow2,
          class IsReferenceType=DirectType<ArrayBaseType2d<T,
                                                           s0,
                                                           s1,
                                                           ShouldRoundPow2> > > struct Array2d {
    typedef typename ArrayBaseType2d<T,s0,s1,ShouldRoundPow2>::Array Array;
    typedef IsReferenceType IsReference;
    typename IsReference::ArrayType data;
    typedef Array2d<T,
                    s0,
                    s1,
                    ShouldRoundPow2,
                    ReferenceType<ArrayBaseType2d<T, s0, s1, ShouldRoundPow2> > > Slice;
    enum Sizes{
        size0 = s0,
        size1 = s1
    };
    static constexpr Array1d<uint32_t, 2, ShouldRoundPow2> size() {
#ifdef NOCONSTEXPR
        Array1d<uint32_t, 2, ShouldRoundPow2> retval = {{s0, s1}};
        return retval;
#else
        return {{s0, s1}};
#endif
    }
    static uint32_t dimension() {
        return 2;
    }
    static constexpr uint32_t dimsize() {
        return s0;
    }
    T& raster(uint32_t index) {
        ARRAY_BOUNDS_ASSERT(index < s0 * s1);
        return (&IsReference::dereference(data)[0][0])[index];
    }
    const T& raster(uint32_t index) const {
        ARRAY_BOUNDS_ASSERT(index < s0 * s1);
        return (&IsReference::dereference(data)[0][0])[index];
    }
    T& at(uint32_t i0,
                   uint32_t i1) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1);
        return IsReference::dereference(data)[i0][i1];
    }
    const T& at(uint32_t i0,
                         uint32_t i1) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1);
        return IsReference::dereference(data)[i0][i1];
    }
    typename Array1d<T, s1,ShouldRoundPow2>::Slice at(uint32_t i0) {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        typename Array1d<T, s1,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0]};
        return retval;
    }
    const typename Array1d<T, s1,ShouldRoundPow2>::Slice at(uint32_t i0) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        const typename Array1d<T, s1,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0]};
        return retval;
    }
    void memset(uint8_t val) {
        std::memset(data, val, sizeof(Array));
    }
    template<class F> void foreach(const F& f){
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                f(IsReference::dereference(data)[i0][i1]);
            }
        }
    }
    template<class F> void foreach(const F& f) const{
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                f(IsReference::dereference(data)[i0][i1]);
            }
        }
    }
    T* begin() {
        return (T*)data;
    }
    const T* begin() const {
        return (const T*)data;
    }
    T* end() {
        return (T*)data + s0 * s1;
    }
    const T* end() const {
        return (const T*)data + s0 * s1;
    }
};

template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          class ShouldRoundPow2 = DontRoundPow2,
          class IsReferenceType=DirectType<ArrayBaseType3d<T,s0,s1,s2,ShouldRoundPow2> > > struct Array3d {
    typedef typename ArrayBaseType3d<T,s0,s1,s2,ShouldRoundPow2>::Array Array;
    typedef IsReferenceType IsReference;
    typename IsReference::ArrayType data;
    typedef Array3d<T, s0, s1, s2,ShouldRoundPow2, ReferenceType<ArrayBaseType3d<T, s0, s1, s2 ,ShouldRoundPow2> > > Slice;

    enum Sizes{
        size0 = s0,
        size1 = s1,
        size2 = s2
    };
    static constexpr Array1d<uint32_t, 3, DontRoundPow2> size() {
#ifdef NOCONSTEXPR
        Array1d<uint32_t, 3, DontRoundPow2> retval = {{s0,s1,s2}};
        return retval;
#else
        return {{s0,s1,s2}};
#endif
    }
    static uint32_t dimension() {
        return 3;
    }
    static constexpr uint32_t dimsize() {
        return s0;
    }

    T& at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2);
        return IsReference::dereference(data)[i0][i1][i2];
    }
    const T& at(uint32_t i0,
                         uint32_t i1,
                         uint32_t i2) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2);
        return IsReference::dereference(data)[i0][i1][i2];
    }
    typename Array1d<T, s2,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1);
        typename Array1d<T, s2,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0][i1]};
        return retval;
    }
    const typename Array1d<T, s2,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1);
        const typename Array1d<T, s2,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0][i1]};
        return retval;
    }
    typename Array2d<T, s1, s2,ShouldRoundPow2>::Slice at(uint32_t i0) {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        typename Array2d<T, s1, s2,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0]};
        return retval;
    }
    const typename Array2d<T, s1, s2,ShouldRoundPow2>::Slice at(uint32_t i0) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        const typename Array2d<T, s1, s2,ShouldRoundPow2>::Slice retval = {(typename Array2d<T, s1, s2, ShouldRoundPow2>::IsReference::ArrayType*)
                                                           &IsReference::dereference(data)[i0]};
        return retval;
    }
    void memset(uint8_t val) {
        memset(data, val, sizeof(Array));
    }
    template<class F> void foreach(const F& f){
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    f(IsReference::dereference(data)[i0][i1][i2]);
                }
            }
        }
    }
    template<class F> void foreach(const F& f) const{
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    f(IsReference::dereference(data)[i0][i1][i2]);
                }
            }
        }
    }
    T* begin() {
        return (T*)data;
    }
    const T* begin() const {
        return (const T*)data;
    }
    T* end() {
        return (T*)data + s0 * s1 * s2;
    }
    const T* end() const {
        return (const T*)data + s0 * s1 * s2;
    }
};




template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3,
          class ShouldRoundPow2 = DontRoundPow2,
          class IsReferenceType=DirectType<ArrayBaseType4d<T,s0,s1,s2,s3,ShouldRoundPow2> > > struct Array4d {
    typedef typename ArrayBaseType4d<T,s0,s1,s2,s3,ShouldRoundPow2>::Array Array;
    typedef Array4d<T, s0, s1, s2, s3,ShouldRoundPow2, ReferenceType<ArrayBaseType4d<T, s0, s1, s2, s3,ShouldRoundPow2> > > Slice;
    typedef IsReferenceType IsReference;
    typename IsReference::ArrayType data;
    enum Sizes{
        size0 = s0,
        size1 = s1,
        size2 = s2,
        size3 = s3
    };

    static constexpr Array1d<uint32_t, 4, ShouldRoundPow2> size() {
#ifdef NOCONSTEXPR
        Array1d<uint32_t, 4, ShouldRoundPow2> retval = {{s0,s1,s2,s3}};
        return retval;
#else
        return {{s0,s1,s2,s3}};
#endif
    }
    static uint32_t dimension() {
        return 4;
    }
    static constexpr uint32_t dimsize() {
        return s0;
    }

    T& at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3);
        return IsReference::dereference(data)[i0][i1][i2][i3];
    }
    const T& at(uint32_t i0,
                         uint32_t i1,
                         uint32_t i2,
                         uint32_t i3) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3);
        return IsReference::dereference(data)[i0][i1][i2][i3];
    }
    typename Array1d<T, s3,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2);
        typename Array1d<T, s3,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0][i1][i2]};
        return retval;
    }
    const typename Array1d<T, s3,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2);
        const typename Array1d<T, s3,ShouldRoundPow2>::Slice retval ={&IsReference::dereference(data)[i0][i1][i2]};
        return retval;
    }
    typename Array2d<T, s2, s3,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1);
        typename Array2d<T, s2, s3,ShouldRoundPow2>::Slice retval ={&IsReference::dereference(data)[i0][i1]};
        return retval;
    }
    const typename Array2d<T, s2, s3,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1);
        const typename Array2d<T, s2, s3,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0][i1]};
        return retval;
    }

    typename Array3d<T, s1, s2, s3,ShouldRoundPow2>::Slice at(uint32_t i0) {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        typename Array3d<T, s1, s2, s3,ShouldRoundPow2>::Slice retval ={&IsReference::dereference(data)[i0]};
        return retval;
    }
    const typename Array3d<T, s1, s2, s3,ShouldRoundPow2>::Slice at(uint32_t i0) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        const typename Array3d<T, s1, s2, s3,ShouldRoundPow2>::Slice retval ={(typename Array3d<T, s1, s2, s3,ShouldRoundPow2>::IsReference::ArrayType*)
                                                              (&IsReference::dereference(data)[i0])};
        return retval;
    }
    void memset(uint8_t val) {
        memset(data, val, sizeof(Array));
    }
    template<class F> void foreach(const F& f){
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    for (uint32_t i3 = 0; i3 < s3; ++i3) {
                        f(IsReference::dereference(data)[i0][i1][i2][i3]);
                    }
                }
            }
        }
    }
    template<class F> void foreach(const F& f) const{
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    for (uint32_t i3 = 0; i3 < s3; ++i3) {
                        f(IsReference::dereference(data)[i0][i1][i2][i3]);
                    }
                }
            }
        }
    }
    T* begin() {
      return (T*)data;
    }
    const T* begin() const {
      return (const T*)data;
    }
    T* end() {
      return (T*)data + s0 * s1 * s2 * s3;
    }
    const T* end() const {
      return (const T*)data + s0 * s1 * s2 * s3;
    }

};




template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3, uint32_t s4,
          class ShouldRoundPow2 = DontRoundPow2,
          class IsReferenceType=DirectType<ArrayBaseType5d<T,s0,s1,s2,s3,s4,ShouldRoundPow2> > > struct Array5d {
    typedef typename ArrayBaseType5d<T,s0,s1,s2,s3,s4,ShouldRoundPow2>::Array Array;
    typedef IsReferenceType IsReference;
    typename IsReference::ArrayType data;


    typedef Array5d<T, s0, s1, s2, s3, s4,ShouldRoundPow2,
                    ReferenceType<ArrayBaseType5d<T, s0, s1, s2, s3, s4,ShouldRoundPow2> > > Slice;
    enum Sizes{
        size0 = s0,
        size1 = s1,
        size2 = s2,
        size3 = s3,
        size4 = s4
    };
    static constexpr Array1d<uint32_t, 5, DontRoundPow2> size() {
#ifdef NOCONSTEXPR
        Array1d<uint32_t, 5, DontRoundPow2> retval = {{s0,s1,s2,s3,s4}};
        return retval;
#else
        return {{s0,s1,s2,s3,s4}};
#endif
    }
    static uint32_t dimension() {
        return 5;
    }
    static constexpr uint32_t dimsize() {
        return s0;
    }

    T& at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3,
                   uint32_t i4) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4);
        return IsReference::dereference(data)[i0][i1][i2][i3][i4];
    }
    const T& at(uint32_t i0,
                         uint32_t i1,
                         uint32_t i2,
                         uint32_t i3,
                         uint32_t i4) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4);
        return IsReference::dereference(data)[i0][i1][i2][i3][i4];
    }
    typename Array1d<T, s4,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3);
        typename Array1d<T, s4,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0][i1][i2][i3]};
        return retval;
    }
    const typename Array1d<T, s4,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3);
        const typename Array1d<T, s4,ShouldRoundPow2>::Slice retval ={&IsReference::dereference(data)[i0][i1][i2][i3]};
        return retval;
    }
    typename Array2d<T, s3, s4,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2);
        typename Array2d<T, s3, s4,ShouldRoundPow2>::Slice retval ={&IsReference::dereference(data)[i0][i1][i2]};
        return retval;
    }
    const typename Array2d<T, s3, s4,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2);
        const typename Array2d<T, s3, s4,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0][i1][i2]};
        return retval;
    }

    typename Array4d<T, s1, s2, s3, s4,ShouldRoundPow2>::Slice at(uint32_t i0) {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        typename Array4d<T, s1, s2, s3, s4,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0]};
        return retval;
    }
    const typename Array4d<T, s1, s2, s3, s4,ShouldRoundPow2>::Slice at(uint32_t i0) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        const typename Array4d<T, s1, s2, s3, s4,ShouldRoundPow2>::Slice retval ={(typename Sirikata::ReferenceType<typename Sirikata::ArrayBaseType4d<T, s1, s2, s3, s4,ShouldRoundPow2> >::ArrayType )&IsReference::dereference(data)[i0]};
        return retval;
    }
    void memset(uint8_t val) {
        memset(data, val, sizeof(Array));
    }
    template<class F> void foreach(const F& f){
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    for (uint32_t i3 = 0; i3 < s3; ++i3) {
                        for (uint32_t i4 = 0; i4 < s4; ++i4) {
                            f(IsReference::dereference(data)[i0][i1][i2][i3][i4]);
                        }
                    }
                }
            }
        }
    }
    template<class F> void foreach(const F& f) const{
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    for (uint32_t i3 = 0; i3 < s3; ++i3) {
                        for (uint32_t i4 = 0; i4 < s4; ++i4) {
                            f(IsReference::dereference(data)[i0][i1][i2][i3][i4]);
                        }
                    }
                }
            }
        }
    }
    T* begin() {
      return (T*)data;
    }
    const T* begin() const {
      return (const T*)data;
    }
    T* end() {
      return (T*)data + s0 * s1 * s2 * s3 * s4;
    }
    const T* end() const {
      return (const T*)data + s0 * s1 * s2 * s3 * s4;
    }

};


template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3, uint32_t s4, uint32_t s5,class ShouldRoundPow2 = DontRoundPow2,
          class IsReferenceType=DirectType<ArrayBaseType6d<T,s0,s1,s2,s3,s4,s5,ShouldRoundPow2> > > struct Array6d {
    typedef typename ArrayBaseType6d<T,s0,s1,s2,s3,s4,s5,ShouldRoundPow2>::Array Array;
    typedef IsReferenceType IsReference;
    typename IsReference::ArrayType data;
    typedef Array6d<T, s0, s1, s2, s3, s4, s5,
                    ShouldRoundPow2,
                    ReferenceType<ArrayBaseType6d<T, s0, s1, s2, s3, s4, s5,
                                                  ShouldRoundPow2> > > Slice;

    enum Sizes{
        size0 = s0,
        size1 = s1,
        size2 = s2,
        size3 = s3,
        size4 = s4,
        size5 = s5,
    };
    static constexpr Array1d<uint32_t, 6, DontRoundPow2> size() {
#ifdef NOCONSTEXPR
        Array1d<uint32_t, 6, DontRoundPow2> retval = {{s0,s1,s2,s3,s4,s5}};
        return retval;
#else
        return {{s0,s1,s2,s3,s4,s5}};
#endif
    }

    static uint32_t dimension() {
        return 6;
    }
    static constexpr uint32_t dimsize() {
        return s0;
    }

    T& at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3,
                   uint32_t i4,
                   uint32_t i5) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4 && i5 < s5);
        return IsReference::dereference(data)[i0][i1][i2][i3][i4][i5];
    }
    const T& at(uint32_t i0,
                         uint32_t i1,
                         uint32_t i2,
                         uint32_t i3,
                         uint32_t i4,
                         uint32_t i5) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4 && i5 < s5);
        return IsReference::dereference(data)[i0][i1][i2][i3][i4][i5];
    }
    typename Array1d<T, s5,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3,
                   uint32_t i4) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4);
        typename Array1d<T, s5,ShouldRoundPow2>::Slice retval = {
            &IsReference::dereference(data)[i0][i1][i2][i3][i4]};
        return retval;
    }
    const typename Array1d<T, s5,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3,
                   uint32_t i4) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4);
        const typename Array1d<T, s5,ShouldRoundPow2>::Slice retval = {&IsReference::dereference(data)[i0][i1][i2][i3][i4]};
        return retval;
    }
    typename Array2d<T, s4, s5,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3);
        typename Array2d<T, s4, s5,ShouldRoundPow2>::Slice retval ={
            &IsReference::dereference(data)[i0][i1][i2][i3]};
        return retval;
    }
    const typename Array2d<T, s4, s5,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3);
        const typename Array2d<T, s4, s5,ShouldRoundPow2>::Slice retval ={
            &IsReference::dereference(data)[i0][i1][i2][i3]};
        return retval;
    }

    typename Array5d<T, s1, s2, s3, s4, s5,ShouldRoundPow2>::Slice at(uint32_t i0) {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        typename Array5d<T, s1, s2, s3, s4, s5,ShouldRoundPow2>::Slice retval = {
            &IsReference::dereference(data)[i0]};
        return retval;
    }
    const typename Array5d<T, s1, s2, s3, s4, s5,ShouldRoundPow2>::Slice at(uint32_t i0) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        const typename Array5d<T, s1, s2, s3, s4, s5,ShouldRoundPow2>::Slice retval = {
            (typename Sirikata::ReferenceType<typename Sirikata::ArrayBaseType5d<T,
                                                                                 s1,
                                                                                 s2,
                                                                                 s3,
                                                                                 s4,
                                                                                 s5,
                                                                                 ShouldRoundPow2> >
             ::ArrayType )&IsReference::dereference(data)[i0]};
        return retval;
    }
    void memset(uint8_t val) {
        memset(data, val, sizeof(Array));
    }
    template<class F> void foreach(const F& f){
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    for (uint32_t i3 = 0; i3 < s3; ++i3) {
                        for (uint32_t i4 = 0; i4 < s4; ++i4) {
                            for (uint32_t i5 = 0; i5 < s5; ++i5) {
                                f(IsReference::dereference(data)[i0][i1][i2][i3][i4][i5]);
                            }
                        }
                    }
                }
            }
        }
    }
    template<class F> void foreach(const F& f) const{
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    for (uint32_t i3 = 0; i3 < s3; ++i3) {
                        for (uint32_t i4 = 0; i4 < s4; ++i4) {
                            for (uint32_t i5 = 0; i5 < s5; ++i5) {
                                f(IsReference::dereference(data)[i0][i1][i2][i3][i4][i5]);
                            }
                        }
                    }
                }
            }
        }
    }
    T* begin() {
      return (T*)data;
    }
    const T* begin() const {
      return (const T*)data;
    }
    T* end() {
      return (T*)data + s0 * s1 * s2 * s3 * s4 * s5;
    }
    const T* end() const {
      return (const T*)data + s0 * s1 * s2 * s3 * s4 * s5;
    }

};

template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3, uint32_t s4, uint32_t s5,
          uint32_t s6, class ShouldRoundPow2 = DontRoundPow2,
          class IsReferenceType=DirectType<ArrayBaseType7d<T,s0,s1,s2,s3,s4,s5,s6,
                                                           ShouldRoundPow2> > > struct Array7d {
    typedef typename ArrayBaseType7d<T,s0,s1,s2,s3,s4,s5,s6,ShouldRoundPow2>::Array Array;
    typedef IsReferenceType IsReference;
    typename IsReference::ArrayType data;
    typedef Array7d<T, s0, s1, s2, s3, s4, s5, s6, ShouldRoundPow2,
        ReferenceType<ArrayBaseType7d<T, s0, s1, s2,
                                      s3, s4, s5, s6, ShouldRoundPow2> > > Slice;
    enum Sizes{
        size0 = s0,
        size1 = s1,
        size2 = s2,
        size3 = s3,
        size4 = s4,
        size5 = s5,
        size6 = s6,
    };
    static constexpr Array1d<uint32_t, 7, DontRoundPow2> size() {
#ifdef NOCONSTEXPR
        Array1d<uint32_t, 7, DontRoundPow2> retval = {{s0,s1,s2,s3,s4,s5,s6}};
        return retval;
#else
        return {{s0,s1,s2,s3,s4,s5,s6}};
#endif
    }
    static uint32_t dimension() {
        return 7;
    }
    static constexpr uint32_t dimsize() {
        return s0;
    }

    T& at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3,
                   uint32_t i4,
                   uint32_t i5,
                   uint32_t i6) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4 && i5 < s5 && i6 < s6);
        return IsReference::dereference(data)[i0][i1][i2][i3][i4][i5][i6];
    }
    const T& at(uint32_t i0,
                         uint32_t i1,
                         uint32_t i2,
                         uint32_t i3,
                         uint32_t i4,
                         uint32_t i5,
                         uint32_t i6) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4 && i5 < s5 && i6 < s6);
        return IsReference::dereference(data)[i0][i1][i2][i3][i4][i5][i6];
    }
    typename Array1d<T, s6,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3,
                   uint32_t i4,
                   uint32_t i5) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4 && i5 < s5);
        typename Array1d<T, s6,ShouldRoundPow2>::Slice retval =
            {&IsReference::dereference(data)[i0][i1][i2][i3][i4][i5]};
        return retval;
    }
    const typename Array1d<T, s6,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3,
                   uint32_t i4,
                   uint32_t i5) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4 && i5 < s5);
        const typename Array1d<T, s6,ShouldRoundPow2>::Slice retval =
            {&IsReference::dereference(data)[i0][i1][i2][i3][i4][i5]};
        return retval;
    }
    typename Array2d<T, s5, s6,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3,
                   uint32_t i4) {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4);
        typename Array2d<T, s5, s6,ShouldRoundPow2>::Slice retval =
            {&IsReference::dereference(data)[i0][i1][i2][i3][i4]};
        return retval;
    }
    const typename Array2d<T, s5, s6,ShouldRoundPow2>::Slice at(uint32_t i0,
                   uint32_t i1,
                   uint32_t i2,
                   uint32_t i3,
                   uint32_t i4) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0 && i1 < s1 && i2 < s2 && i3 < s3 && i4 < s4);
        const typename Array2d<T, s5, s6,ShouldRoundPow2>::Slice retval =
            {&IsReference::dereference(data)[i0][i1][i2][i3][i4]};
        return retval;
    }


    typename Array6d<T, s1, s2, s3, s4, s5, s6,ShouldRoundPow2>::Slice at(uint32_t i0) {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        typename Array6d<T, s1, s2, s3, s4, s5, s6,ShouldRoundPow2>::Slice retval =
            {&IsReference::dereference(data)[i0]};
        return retval;
    }
    const typename Array6d<T, s1, s2, s3, s4, s5, s6,
                           ShouldRoundPow2>::Slice at(uint32_t i0) const {
        ARRAY_BOUNDS_ASSERT(i0 < s0);
        const typename Array6d<T, s1, s2, s3, s4, s5, s6, ShouldRoundPow2>::Slice retval =
            {&IsReference::dereference(data)[i0]};
        return retval;
    }
    void memset(uint8_t val) {
        std::memset(data, val, sizeof(Array));
    }
    template<class F> void foreach(const F& f){
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    for (uint32_t i3 = 0; i3 < s3; ++i3) {
                        for (uint32_t i4 = 0; i4 < s4; ++i4) {
                            for (uint32_t i5 = 0; i5 < s5; ++i5) {
                                for (uint32_t i6 = 0; i6 < s6; ++i6) {
                                    f(IsReference::dereference(data)[i0][i1][i2][i3][i4][i5][i6]);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    template<class F> void foreach(const F &f) const{
        for (uint32_t i0 = 0; i0 < s0; ++i0) {
            for (uint32_t i1 = 0; i1 < s1; ++i1) {
                for (uint32_t i2 = 0; i2 < s2; ++i2) {
                    for (uint32_t i3 = 0; i3 < s3; ++i3) {
                        for (uint32_t i4 = 0; i4 < s4; ++i4) {
                            for (uint32_t i5 = 0; i5 < s5; ++i5) {
                                for (uint32_t i6 = 0; i6 < s6; ++i6) {
                                    f(IsReference::dereference(data)[i0][i1][i2][i3][i4][i5][i6]);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    T* begin() {
       return (T*)data;
    }
    const T* begin() const {
       return (const T*)data;
    }
    T* end() {
       return (T*)data + s0 * s1 * s2 * s3 * s4 * s5 * s6;
    }
    const T* end() const {
       return (const T*)data + s0 * s1 * s2 * s3 * s4 * s5 * s6;
    }

};


template<class Slice> struct AlignedArrayNd : public Slice {
    uint8_t backingStore[sizeof(typename Slice::Array) + 15];
    AlignedArrayNd() {
        init();
    }
    AlignedArrayNd(const AlignedArrayNd&other){
        // need to memcpy around to the aligned areas
        init();
        *this = other;
    }
    AlignedArrayNd& operator=(const AlignedArrayNd& other) {
        memcpy(this->data, other.data, sizeof(typename Slice::Array));
        return *this;
    }
private:
    void init() {
        uint8_t* begin = NULL;
        size_t offset = ((backingStore - begin) & 15);
        if (offset == 0) {
            this->data = (typename Slice::Array*)backingStore;
        } else {
            this->data = (typename Slice::Array*)(backingStore + 16 - offset);
        }

    }

};


template<class Slice> struct Aligned256ArrayNd : public Slice {
    uint8_t backingStore[sizeof(typename Slice::Array) + 31];
    Aligned256ArrayNd() {
        init();
    }
    Aligned256ArrayNd(const Aligned256ArrayNd&other){
        // need to memcpy around to the aligned areas
        init();
        *this = other;
    }
    Aligned256ArrayNd& operator=(const Aligned256ArrayNd& other) {
        memcpy(this->data, other.data, sizeof(typename Slice::Array));
        return *this;
    }
private:
    void init() {
        uint8_t* begin = NULL;
        size_t offset = ((backingStore - begin) & 31);
        if (offset == 0) {
            this->data = (typename Slice::Array*)backingStore;
        } else {
            this->data = (typename Slice::Array*)(backingStore + 32 - offset);
        }

    }

};

template <class T,
          uint32_t s0>
struct AlignedArray1d : AlignedArrayNd<typename Array1d<T, s0, RoundToPow2>::Slice > {};

template <class T,
          uint32_t s0, uint32_t s1>
struct AlignedArray2d : AlignedArrayNd<typename Array2d<T, s0, s1, RoundToPow2>::Slice > {};


template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2>
struct AlignedArray3d : AlignedArrayNd<typename Array3d<T, s0, s1, s2, RoundToPow2>::Slice > {};


template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3>
struct AlignedArray4d : AlignedArrayNd<typename Array4d<T, s0, s1, s2, s3, RoundToPow2>::Slice > {};


template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3, uint32_t s4>
struct AlignedArray5d : AlignedArrayNd<typename Array5d<T, s0, s1, s2, s3, s4, RoundToPow2>::Slice > {};


template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3, uint32_t s4, uint32_t s5>
struct AlignedArray6d : AlignedArrayNd<typename Array6d<T, s0, s1, s2, s3, s4, s5, RoundToPow2>::Slice > {};

template <class T,
uint32_t s0, uint32_t s1, uint32_t s2,
uint32_t s3, uint32_t s4, uint32_t s5,
          uint32_t s6> struct AlignedArray7d : AlignedArrayNd<typename Array7d<T, s0, s1, s2, s3, s4, s5, s6, RoundToPow2>::Slice > {};






template <class T,
          uint32_t s0>
struct Aligned256Array1d : Aligned256ArrayNd<typename Array1d<T, s0, RoundToPow2>::Slice > {};

template <class T,
          uint32_t s0, uint32_t s1>
struct Aligned256Array2d : Aligned256ArrayNd<typename Array2d<T, s0, s1, RoundToPow2>::Slice > {};


template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2>
struct Aligned256Array3d : Aligned256ArrayNd<typename Array3d<T, s0, s1, s2, RoundToPow2>::Slice > {};


template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3>
struct Aligned256Array4d : Aligned256ArrayNd<typename Array4d<T, s0, s1, s2, s3, RoundToPow2>::Slice > {};


template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3, uint32_t s4>
struct Aligned256Array5d : Aligned256ArrayNd<typename Array5d<T, s0, s1, s2, s3, s4, RoundToPow2>::Slice > {};


template <class T,
          uint32_t s0, uint32_t s1, uint32_t s2,
          uint32_t s3, uint32_t s4, uint32_t s5>
struct Aligned256Array6d : Aligned256ArrayNd<typename Array6d<T, s0, s1, s2, s3, s4, s5, RoundToPow2>::Slice > {};

template <class T,
uint32_t s0, uint32_t s1, uint32_t s2,
uint32_t s3, uint32_t s4, uint32_t s5,
          uint32_t s6> struct Aligned256Array7d : Aligned256ArrayNd<typename Array7d<T, s0, s1, s2, s3, s4, s5, s6, RoundToPow2>::Slice > {};
}


#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus > 199711L
#undef constexpr
#undef NOCONSTEXPR
#endif
#endif //_SIRIKATA_ARRAY_ND_HPP_