-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy patharraybasics.mac
More file actions
133 lines (116 loc) · 6.43 KB
/
arraybasics.mac
File metadata and controls
133 lines (116 loc) · 6.43 KB
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
/******************************************************************************
*
* Macros for basic operations on arrays.
*
******************************************************************************/
/******************************************************************************
*
* @fn int dim(<a>[d:shp] arr)
*
* @brief Yields an array's dimensionality.
*
******************************************************************************/
#define DIM(typ, _postfix, _fmt, _zval, _oval) \
inline int dim(typ[d:shp] arr) \
{ \
return d; \
}
/******************************************************************************
*
* @fn int[d] shape(<a>[d:shp] arr)
*
* @brief Yields an array's shape vector.
*
******************************************************************************/
#define SHAPE(typ, _postfix, _fmt, _zval, _oval) \
inline int[d] shape(typ[d:shp] arr) \
{ \
return shp; \
}
/******************************************************************************
*
* @fn <a>[i:ishp] sel(int[o] idx, <a>[o:oshp,i:ishp] arr)
*
* @brief Selects the subarray of the array at position idx, provided the index
* vector is no longer than the rank of the array, and each index element is no
* longer than the corresponding array shape element.
*
******************************************************************************/
#define SEL_VxA(typ, _postfix, _fmt, _zval, _oval) \
inline typ[i:ishp] sel(int[o] idx, typ[o:oshp,i:ishp] arr) \
| _all_V_(_le_SxV_(0, idx)), _all_V_(_lt_VxV_(idx, oshp)) \
{ \
return { iv -> _sel_VxA_(_cat_VxV_(idx, iv), arr) | iv < ishp }; \
}
#define SEL_SxA(typ, _postfix, _fmt, _zval, _oval) \
inline typ[d:shp] sel(int idx, typ[n,d:shp] arr) \
| _le_SxS_(0, idx), _lt_SxS_(idx, n) \
{ \
return sel([idx], arr); \
}
#define SEL(typ, postfix, fmt, zval, oval) \
SEL_VxA(typ, postfix, fmt, zval, oval) \
SEL_SxA(typ, postfix, fmt, zval, oval)
/******************************************************************************
*
* @fn <a>[d:shp] reshape(int[d] shp, <a>[*] arr)
*
* @brief Creates a new array with identical data vector but new shape, provided
* the length of the data vector matches the product of the new shape vector.
*
* @todo Requires a type pattern constraint that checks whether prod(shp) ==
* prod(shape(arr)). But prod is in ArrayTransform, which would introduce A
* circular dependency. We need to find a nice solution.
*
******************************************************************************/
#define RESHAPE(typ, _postfix, _fmt, _zval, _oval) \
inline typ[d:shp] reshape(int[d] shp, typ[*] arr) \
| _all_V_(_le_SxV_(0, shp)) \
{ \
return _reshape_VxA_(shp, arr); \
}
/******************************************************************************
*
* @fn <a>[o:oshp,i:ishp] genarray(int[o] oshp, <a>[i:ishp] val)
*
* @brief Generates a new array from the given shape and default val.
*
******************************************************************************/
#define GENARRAY(typ, _postfix, _fmt, _zval, _oval) \
inline typ[o:oshp,i:ishp] genarray(int[o] oshp, typ[i:ishp] val) \
| _all_V_(_le_SxV_(0, oshp)) \
{ \
return { iv -> val | iv < oshp }; \
}
/******************************************************************************
*
* @fn <a>[d:shp] modarray(<a>[d:shp] arr, int[d] idx, <a> val)
*
* @brief modifies the element of the array at position idx.
*
******************************************************************************/
#define MODARRAY_AxVxA(typ, _postfix, _fmt, _zval, _oval) \
inline typ[o:oshp,i:ishp] modarray(typ[o:oshp,i:ishp] arr, \
int[o] idx, typ[i:ishp] val) \
| _all_V_(_le_SxV_(0, idx)), _all_V_(_lt_VxV_(idx, oshp)) \
{ \
return with { \
(idx <= iv <= idx) : val; \
} : modarray(arr); \
}
#define MODARRAY_AxVxS(typ, _postfix, _fmt, _zval, _oval) \
inline typ[d>0:shp] modarray(typ[d>0:shp] arr, int[d] idx, typ val) \
| _all_V_(_le_SxV_(0, idx)), _all_V_(_lt_VxV_(idx, shp)) \
{ \
return _modarray_AxVxS_(arr, idx, val); \
}
#define MODARRAY_AxSxA(typ, _postfix, _fmt, _zval, _oval) \
inline typ[n,i:ishp] modarray(typ[n,i:ishp] arr, int idx, typ[i:ishp] val) \
| _le_SxS_(0, idx), _lt_SxS_(idx, n) \
{ \
return modarray(arr, [idx], val); \
}
#define MODARRAY(typ, postfix, fmt, zval, oval) \
MODARRAY_AxVxA(typ, postfix, fmt, zval, oval) \
MODARRAY_AxVxS(typ, postfix, fmt, zval, oval) \
MODARRAY_AxSxA(typ, postfix, fmt, zval, oval)