-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxll_array.cpp
More file actions
317 lines (286 loc) · 6.91 KB
/
xll_array.cpp
File metadata and controls
317 lines (286 loc) · 6.91 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
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
// xll_array.cpp - Array functions
#include <cmath>
#include <numeric>
#include "xll_array.h"
#include "fms_iterable.h"
#ifdef _DEBUG
//int fms_iterable_iota_test_ = fms::iterable::iota_test();
//int fms_iterable_take_test_ = fms::iterable::take_test();
//int fms_iterable_array_test_ = fms::iterable::array_test();
//int fms_iterable_iterator_test_ = fms::iterable::iterator_test();
#endif _DEBUG
#ifndef CATEGORY
#define CATEGORY "Array"
#endif
using namespace xll;
AddIn xai_array_(
Function(XLL_HANDLEX, "xll_array_", "\\ARRAY")
.Arguments({
Arg(XLL_FP, L"array", L"is an array or handle to an array of numbers."),
})
.Uncalced()
.FunctionHelp("Return a handle to the in-memory array.")
.Category(CATEGORY)
.Documentation(R"(
Create an in-memory two-dimensional array of numbers to be used by array functions.
If <code>array</code> is a scalar and <code>_columns</code> is not zero then
return a handle to an uninitialized array having <code>array</code> rows
and <code>_columns</code> columns.
Most array functions work in two modes: <em>function</em> and <em>command</em>.
If the first argument is an array then an new array is returned and the array function
has no side effects. If the first argument is a handle to an array then the function
modifies the in-memory array and returns the array handle.
)")
);
HANDLEX WINAPI xll_array_(const _FP12* pa)
{
#pragma XLLEXPORT
HANDLEX h = INVALID_HANDLEX;
try {
const FPX* _pa = ptr(pa);
if (_pa) {
handle<FPX> h_(new FPX(*_pa));
ensure(h_);
h = h_.get();
}
else {
handle<FPX> h_(new FPX(*pa));
h = h_.get();
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR("\\ARRAY: unknown exception");
}
return h;
}
AddIn xai_array_get(
Function(XLL_FP, L"xll_array_get", L"ARRAY")
.Arguments({
Arg(XLL_HANDLEX, L"handle", L"is a handle to an array of numbers."),
})
.FunctionHelp("Return an array associated with handle.")
.Category(CATEGORY)
.Documentation(R"(
Retrieve an in-memory array created by
<code>\ARRAY</code>. By default the handle is checked to
ensure the array was created by a previous call to <code>\ARRAY</code>.
)")
.SeeAlso({ "\\ARRAY" })
);
_FP12* WINAPI xll_array_get(HANDLEX h)
{
#pragma XLLEXPORT
_FP12* pa = nullptr;
try {
handle<FPX> h_(h);
if (h_) {
pa = h_->get();
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR(__FUNCTION__ ": unknown exception");
}
return pa;
}
AddIn xai_array_resize(
Function(XLL_FP, "xll_array_resize", "ARRAY.RESIZE")
.Arguments({
Arg(XLL_FP, "array", "is an array or handle to an array."),
Arg(XLL_LONG, "rows", "is the number of rows."),
Arg(XLL_LONG, "columns", "is the number of columns."),
})
.FunctionHelp("Resize an array.")
.Category(CATEGORY)
.Documentation(R"(
Resize array to <code>rows</code> and <code>columns</code>.
If <code>array</code> is a handle this function resizes the in-memory array and
returns its handle, otherwise the resized array is returned.
)")
);
_FP12* WINAPI xll_array_resize(_FP12* pa, LONG r, LONG c)
{
#pragma XLLEXPORT
static FPX a;
try {
a = *pa;
FPX* _a = ptr(pa);
if (_a) {
_a->resize(r, c);
}
else {
a.resize(r,c);
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR(__FUNCTION__ ": unknown exception");
}
return a.get();
}
AddIn xai_array_rows(
Function(XLL_LONG, "xll_array_rows", "ARRAY.ROWS")
.Arguments({
Arg(XLL_FP, "array", "is an array or handle to an array."),
})
.FunctionHelp("Return the number of rows of an array.")
.Category(CATEGORY)
.Documentation(R"(
Return the number of rows of an array.
)")
);
LONG WINAPI xll_array_rows(_FP12* pa)
{
#pragma XLLEXPORT
LONG r = 0;
try {
FPX* _a = ptr(pa);
if (_a) {
r = _a->rows();
}
else {
r = pa->rows;
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR("ARRAY.ROWS: unknown exception");
}
return r;
}
AddIn xai_array_columns(
Function(XLL_LONG, "xll_array_columns", "ARRAY.COLUMNS")
.Arguments({
Arg(XLL_FP, "array", "is an array or handle to an array."),
})
.FunctionHelp("Return the number of columns of an array.")
.Category(CATEGORY)
.Documentation(R"(
Return the number of columns of an array.
)")
);
LONG WINAPI xll_array_columns(_FP12* pa)
{
#pragma XLLEXPORT
LONG c = 0;
try {
FPX* _a = ptr(pa);
if (_a) {
c = _a->columns();
}
else {
c = pa->columns;
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR("ARRAY.COLUMNS: unknown exception");
}
return c;
}
AddIn xai_array_size(
Function(XLL_LONG, "xll_array_size", "ARRAY.SIZE")
.Arguments({
Arg(XLL_FP, "array", "is an array or handle to an array."),
})
.FunctionHelp("Return the size of an array.")
.Category(CATEGORY)
.Documentation(LR"(
Return the number of rows times the number of columns of an array.
)")
);
LONG WINAPI xll_array_size(_FP12* pa)
{
#pragma XLLEXPORT
LONG c = 0;
try {
FPX* _a = ptr(pa);
if (_a) {
c = _a->size();
}
else {
c = size(*pa);
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR(__FUNCTION__ ": unknown exception");
}
return c;
}
AddIn xai_array_flip(
Function(XLL_HANDLEX, "xll_array_flip", "ARRAY.FLIP")
.Arguments({
Arg(XLL_HANDLEX, "array", "is an array or handle to an array."),
Arg(XLL_INT, "i", "is the index to flip."),
})
.FunctionHelp("Set the i-th entry of an array.")
.Category(CATEGORY)
.Documentation(R"()")
);
HANDLEX WINAPI xll_array_flip(HANDLEX h, int i)
{
#pragma XLLEXPORT
//HANDLEX h_ = INVALID_HANDLEX;
try {
handle<FPX> h_(h);
if (h_) {
i = std::clamp(i, 0, h_->size() - 1);
double xi = h_->operator[](i);
h_->operator[](i) = 1 - xi;
h_ = h_.get();
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR(__FUNCTION__ ": unknown exception");
}
return h;
}
#ifdef _DEBUG
int xll_array_test()
{
try {
{
FPX a(1,1);
a[0] = 2;
HANDLEX ha = to_handle<FPX>(&a);
_FP12* pa = xll_array_get(ha);
ensure(pa->array[0] == 2);
ensure(pa->rows == 1);
ensure(xll_array_rows(pa) == 1);
ensure(pa->columns == 1);
ensure(xll_array_columns(pa) == 1);
ensure(xll_array_size(pa) == 1);
_FP12* pb = xll_array_resize(pa, 2, 3);
ensure(pb->array[0] == 2);
ensure(pb->rows == 2);
ensure(xll_array_rows(pb) == 2);
ensure(pb->columns == 3);
ensure(xll_array_columns(pb) == 3);
ensure(xll_array_size(pb) == 6);
}
}
catch (...) {
XLL_ERROR(__FUNCTION__ ": failed");
return FALSE;
}
return TRUE;
}
//Auto<OpenAfter> xaoa_array_test(xll_array_test);
#endif // _DEBUG