source: libcfa/src/containers/array.hfa @ ecfd758

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ecfd758 was c7625e0, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Adding manged-length arrays

  • Property mode set to 100644
File size: 4.5 KB
Line 
1
2
3// a type whose size is n
4#define Z(n) char[n]
5
6// the inverse of Z(-)
7#define z(Zn) sizeof(Zn)
8
9// if you're expecting a Z(n), say so, by asking for a ztype, instead of dtype or otype
10#define ztype(Zn) Zn & | sized(Zn)
11
12forall( T & ) struct tag {};
13#define ttag(T) ((tag(T)){})
14#define ztag(n) ttag(Z(n))
15
16
17//
18// Single-dim array sruct (with explicit packing and atom)
19//
20
21forall( ztype(Zn), ztype(S), Timmed &, Tbase & ) {
22    struct arpk {
23        S strides[z(Zn)];
24    };
25
26    Timmed & ?[?]( arpk(Zn, S, Timmed, Tbase) & a, ptrdiff_t i ) {
27        return (Timmed &) a.strides[i];
28    }
29
30    size_t ?`len( arpk(Zn, S, Timmed, Tbase) & a ) {
31        return z(Zn);
32    }
33
34    // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
35    void ?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
36        void ?{}( S (&inner)[z(Zn)] ) {}
37        ?{}(this.strides);
38    }
39    void ^?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
40        void ^?{}( S (&inner)[z(Zn)] ) {}
41        ^?{}(this.strides);
42    }
43}
44
45//
46// Sugar for declaring array structure instances
47//
48
49forall( Te )
50Te mkar_( tag(Te) ) {}
51
52forall( ztype(Zn), ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
53arpk(Zn, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(Zn), ZTags ) {}
54
55// based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
56
57    // Make a FOREACH macro
58    #define FE_0(WHAT)
59    #define FE_1(WHAT, X) WHAT(X)
60    #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
61    #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
62    #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
63    #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
64    //... repeat as needed
65
66    #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
67    #define FOR_EACH(action,...) \
68    GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
69
70#define COMMA_ttag(X) , ttag(X)
71#define array( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) )
72
73#define COMMA_ztag(X) , ztag(X)
74#define zarray( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) )
75
76//
77// Sugar for multidimensional indexing
78//
79
80// Core -[[-,-,-]] operator
81
82// Desired form.  One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
83// forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
84// TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
85//     return this[ab][bc];
86// }
87
88// Workaround form.  Listing all possibilities up to 4 dims.
89forall( TA &, TB &, IxAB | { TB & ?[?]( TA &, IxAB ); }
90            , TC &, IxBC | { TC & ?[?]( TB &, IxBC ); } )
91TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
92    return this[ab][bc];
93}
94forall( TA &, TB &, IxAB | { TB & ?[?]( TA &, IxAB ); }
95            , TC &, IxBC | { TC & ?[?]( TB &, IxBC ); }
96            , TD &, IxCD | { TD & ?[?]( TC &, IxCD ); } )
97TD & ?[?]( TA & this, IxAB ab, IxBC bc, IxCD cd ) {
98    return this[ab][bc][cd];
99}
100forall( TA &, TB &, IxAB | { TB & ?[?]( TA &, IxAB ); }
101            , TC &, IxBC | { TC & ?[?]( TB &, IxBC ); }
102            , TD &, IxCD | { TD & ?[?]( TC &, IxCD ); }
103            , TE &, IxDE | { TE & ?[?]( TD &, IxDE ); } )
104TE & ?[?]( TA & this, IxAB ab, IxBC bc, IxCD cd, IxDE de ) {
105    return this[ab][bc][cd][de];
106}
107
108// Adapters for "indexed by ptrdiff_t" implies "indexed by [this other integral type]"
109// Work around restriction that assertions underlying -[[-,-,-]] must match excatly
110forall( C &, E & | { E & ?[?]( C &, ptrdiff_t ); } ) {
111
112    // Targeted to support:  for( i; z(N) ) ... a[[ ..., i, ... ]]
113    E & ?[?]( C & this, size_t i ) {
114        return this[ (ptrdiff_t) i ];
115    }
116
117    // Targeted to support:  for( i; 5 ) ... a[[ ..., i, ... ]]
118    E & ?[?]( C & this, int i ) {
119        return this[ (ptrdiff_t) i ];
120    }
121}
122
123//
124// Rotation
125//
126
127// Base
128forall( ztype(Zq), ztype(Sq), Tbase & )
129tag(arpk(Zq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(Tbase) ) {}
130
131// Rec
132forall( ztype(Zq), ztype(Sq), ztype(Z), ztype(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(recq) ); } )
133tag(arpk(Z, S, recr, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(arpk(Z, S, recq, Tbase)) ) {}
134
135// Wrapper
136struct all_t {} all;
137forall( ztype(Z), ztype(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(Z), tag(S), tag(Te) ); } )
138result & ?[?]( arpk(Z, S, Te, Tbase) & this, all_t ) {
139    return (result&) this;
140}
141
142//
143// Trait of array or slice
144//
145
146trait ar(A &, Tv &) {
147    Tv& ?[?]( A&, ptrdiff_t );
148    size_t ?`len( A& );
149};
Note: See TracBrowser for help on using the repository browser.