source: libcfa/src/containers/array.hfa @ 63a4b92

arm-ehjacob/cs343-translation
Last change on this file since 63a4b92 was 63a4b92, checked in by Michael Brooks <mlbrooks@…>, 3 months ago

Improved support for new arrays subscripting by tuples, --,-,-?.

  • Property mode set to 100644
File size: 4.1 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    Timmed & ?[?]( arpk(Zn, S, Timmed, Tbase) & a, int i ) {
31        return (Timmed &) a.strides[i];
32    }
33
34    Timmed & ?[?]( arpk(Zn, S, Timmed, Tbase) & a, size_t i ) {
35        return (Timmed &) a.strides[i];
36    }
37
38    size_t ?`len( arpk(Zn, S, Timmed, Tbase) & a ) {
39        return z(Zn);
40    }
41
42    // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
43    void ?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
44        void ?{}( S (&inner)[z(Zn)] ) {}
45        ?{}(this.strides);
46    }
47    void ^?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
48        void ^?{}( S (&inner)[z(Zn)] ) {}
49        ^?{}(this.strides);
50    }
51}
52
53//
54// Sugar for declaring array structure instances
55//
56
57forall( Te )
58Te mkar_( tag(Te) ) {}
59
60forall( ztype(Zn), ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
61arpk(Zn, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(Zn), ZTags ) {}
62
63// based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
64
65    // Make a FOREACH macro
66    #define FE_0(WHAT)
67    #define FE_1(WHAT, X) WHAT(X)
68    #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
69    #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
70    #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
71    #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
72    //... repeat as needed
73
74    #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
75    #define FOR_EACH(action,...) \
76    GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
77
78#define COMMA_ttag(X) , ttag(X)
79#define array( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) )
80
81#define COMMA_ztag(X) , ztag(X)
82#define zarray( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) )
83
84//
85// Sugar for multidimensional indexing
86//
87
88// Core -[[-,-,-]] operator
89
90#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
91
92// Desired form.  One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
93
94forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
95TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
96    return this[ab][bc];
97}
98
99#else
100
101// Workaround form.  Listing all possibilities up to 4 dims.
102
103forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
104TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
105    return this[ab][bc];
106}
107
108forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
109TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
110    return this[[ab0,ab1]][bc];
111}
112
113forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
114TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
115    return this[[ab0,ab1,ab2]][bc];
116}
117
118#endif
119
120//
121// Rotation
122//
123
124// Base
125forall( ztype(Zq), ztype(Sq), Tbase & )
126tag(arpk(Zq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(Tbase) ) {}
127
128// Rec
129forall( ztype(Zq), ztype(Sq), ztype(Z), ztype(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(recq) ); } )
130tag(arpk(Z, S, recr, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(arpk(Z, S, recq, Tbase)) ) {}
131
132// Wrapper
133struct all_t {} all;
134forall( ztype(Z), ztype(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(Z), tag(S), tag(Te) ); } )
135result & ?[?]( arpk(Z, S, Te, Tbase) & this, all_t ) {
136    return (result&) this;
137}
138
139//
140// Trait of array or slice
141//
142
143trait ar(A &, Tv &) {
144    Tv& ?[?]( A&, ptrdiff_t );
145    size_t ?`len( A& );
146};
Note: See TracBrowser for help on using the repository browser.