-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathxlOper.h
291 lines (234 loc) · 7.91 KB
/
xlOper.h
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
#pragma once
#pragma warning(disable:4996)
// General interface for ranges with strings
#include "xlcall.h"
#include "xlframework.h"
#include <string>
using namespace std;
#include "matrix.h"
// Additional / alternative functions to the ones in xlframework.h
LPXLOPER12 TempXLOPER12()
{
LPXLOPER12 lpx;
lpx = (LPXLOPER12)GetTempMemory(sizeof(XLOPER12));
if (!lpx)
{
return 0;
}
return lpx;
}
LPXLOPER12 TempStr12(const string str)
{
LPXLOPER12 lpx = TempXLOPER12();
if (!lpx)
{
return 0;
}
lpx->xltype = xltypeStr;
lpx->val.str = (wchar_t*)GetTempMemory((str.size() + 1) * sizeof(wchar_t));
if (!lpx->val.str)
{
return 0;
}
if (str.size()>0) mbstowcs(lpx->val.str + 1, str.data(), str.size());
lpx->val.str[0] = static_cast<wchar_t>(str.size());
return lpx;
}
// Getters
// Get argument from Excel
// Register as type Q
// Get as LPXLOPER12
// Number of rows or 0 if invalid
size_t getRows(const LPXLOPER12& oper)
{
if (!oper) return 0;
if (oper->xltype != xltypeMulti) return 1;
return oper->val.array.rows;
}
// Number of columns or 0 if invalid
size_t getCols(const LPXLOPER12& oper)
{
if (!oper) return 0;
if (oper->xltype != xltypeMulti) return 1;
return oper->val.array.columns;
}
// String in position (i, j) or "" if invalid
string getString(const LPXLOPER12& oper, const size_t i = 0, const size_t j = 0)
{
if (!oper) return "";
LPXLOPER12 strOper;
if (oper->xltype == xltypeStr && i == 0 && j == 0)
{
strOper = oper;
}
else if (oper->xltype == xltypeMulti)
{
strOper = oper->val.array.lparray + i * getCols(oper) + j;
if (strOper->xltype != xltypeStr) return "";
}
else
{
return "";
}
string str;
str.resize(strOper->val.str[0]);
wcstombs(&str[0], strOper->val.str + 1, strOper->val.str[0]);
return str;
}
// Number in position (i, j) or infinity if invalid
double getNum(const LPXLOPER12& oper, const size_t i = 0, const size_t j = 0)
{
if (!oper) return numeric_limits<double>::infinity();
if (oper->xltype == xltypeNum && i == 0 && j == 0)
{
return oper->val.num;
}
else if (oper->xltype == xltypeMulti)
{
XLOPER12 nOper = oper->val.array.lparray[i * getCols(oper) + j];
if (nOper.xltype != xltypeNum) return numeric_limits<double>::infinity();
return nOper.val.num;
}
else
{
return numeric_limits<double>::infinity();
}
}
// Setter to return result to Excel
// Register as type Q
// Return as LPXLOPER12
void resize(LPXLOPER12& oper, const size_t rows, const size_t cols)
{
oper->xltype = xltypeMulti;
oper->val.array.rows = rows;
oper->val.array.columns = cols;
oper->val.array.lparray = (LPXLOPER12)GetTempMemory(rows*cols*sizeof(XLOPER12));
LPXLOPER12 nilOper = TempStr12("");
fill(oper->val.array.lparray, oper->val.array.lparray + rows*cols, *nilOper);
}
// Set string in position (i, j)
void setString(LPXLOPER12& oper, const string& str, const size_t i = 0, const size_t j = 0)
{
XLOPER12& strOper = oper->val.array.lparray[i * getCols(oper) + j];
strOper = * TempStr12(str.c_str());
}
// Number in position (i, j) or infinity if invalid
void setNum(LPXLOPER12& oper, const double num, const size_t i = 0, const size_t j = 0)
{
XLOPER12& nOper = oper->val.array.lparray[i * getCols(oper) + j];
nOper.xltype = xltypeNum;
nOper.val.num = num;
}
// Various utilities
// FP12* to vector<double>
vector<double> to_vector(const FP12* oper)
{
size_t rows = oper->rows;
size_t cols = oper->columns;
const double* numbers = oper->array;
vector<double> v(rows * cols);
copy(numbers, numbers + rows * cols, v.begin());
return v;
}
// LPXLOPER to vector<string>
vector<string> to_strVector(const LPXLOPER12& oper)
{
vector<string> vstr;
if (oper->xltype == xltypeStr)
{
vstr.push_back(getString(oper));
}
else if (oper->xltype == xltypeMulti)
{
for (size_t i = 0; i < getRows(oper); ++i) for (size_t j = 0; j < getCols(oper); ++j)
{
vstr.push_back(getString(oper, i, j));
}
}
return vstr;
}
// FP12* to matrix<double>
matrix<double> to_matrix(const FP12* oper)
{
size_t rows = oper->rows;
size_t cols = oper->columns;
const double* numbers = oper->array;
matrix<double> m(rows, cols);
copy(numbers, numbers + rows * cols, m.begin());
return m;
}
LPXLOPER12 from_strVector(const vector<string>& strv, const bool rowVector = true)
{
LPXLOPER12 oper = TempXLOPER12();
oper->xltype = xltypeMulti;
oper->val.array.rows = rowVector ? strv.size() : 1;
oper->val.array.columns = rowVector ? 1 : strv.size();
oper->val.array.lparray = (LPXLOPER12) GetTempMemory(strv.size() * sizeof(XLOPER12));
transform(strv.begin(), strv.end(), oper->val.array.lparray,
[](const string& s)
{
return *TempStr12(s);
});
return oper;
}
LPXLOPER12 from_labelsAndNumbers(const vector<string>& labels, const vector<double>& numbers)
{
const size_t n = labels.size();
if (n == 0 || n != numbers.size()) return TempErr12(xlerrNA);
LPXLOPER12 oper = TempXLOPER12();
oper->xltype = xltypeMulti;
oper->val.array.rows = n;
oper->val.array.columns = 2;
oper->val.array.lparray = (LPXLOPER12)GetTempMemory(2 * n * sizeof(XLOPER12));
for (size_t i = 0; i < n; ++i)
{
oper->val.array.lparray[2 * i] = *TempStr12(labels[i]);
oper->val.array.lparray[2 * i + 1] = *TempNum12(numbers[i]);
}
return oper;
}
LPXLOPER12 from_matrix(const matrix<double>& mat)
{
const size_t n = mat.rows(), m = mat.cols();
if (n == 0 || m == 0) return TempErr12(xlerrNA);
LPXLOPER12 oper = TempXLOPER12();
resize(oper, n, m);
for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < m; ++j) setNum(oper, mat[i][j], i, j);
return oper;
}
LPXLOPER12 from_labelledMatrix(const vector<string>& rowLabels, const vector<string>& colLabels, const matrix<double>& mat)
{
const size_t n = rowLabels.size(), m = colLabels.size();
if (n == 0 || m == 0 || n != mat.rows() || m != mat.cols()) return TempErr12(xlerrNA);
LPXLOPER12 oper = TempXLOPER12();
resize(oper, n + 1, m + 1);
for (size_t i = 0; i < n; ++i) setString(oper, rowLabels[i], i + 1, 0);
for (size_t i = 0; i < m; ++i) setString(oper, colLabels[i], 0, i + 1);
for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < m; ++j) setNum(oper, mat[i][j], i + 1, j + 1);
return oper;
}
LPXLOPER12 from_labelledMatrix(const vector<double>& rowLabels, const vector<double>& colLabels, const matrix<double>& mat)
{
const size_t n = rowLabels.size(), m = colLabels.size();
if (n == 0 || m == 0 || n != mat.rows() || m != mat.cols()) return TempErr12(xlerrNA);
LPXLOPER12 oper = TempXLOPER12();
resize(oper, n + 1, m + 1);
for (size_t i = 0; i < n; ++i) setNum(oper, rowLabels[i], i + 1, 0);
for (size_t i = 0; i < m; ++i) setNum(oper, colLabels[i], 0, i + 1);
for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < m; ++j) setNum(oper, mat[i][j], i + 1, j + 1);
return oper;
}
LPXLOPER12 from_labelledMatrix(const vector<string>& rowLabels, const vector<string>& colLabels, const matrix<double>& mat,
const string& firstLineLabel, const vector<double>& firstLine)
{
const size_t n = rowLabels.size(), m = colLabels.size();
if (n == 0 || m == 0 || n != mat.rows() || m != mat.cols() || m != firstLine.size()) return TempErr12(xlerrNA);
LPXLOPER12 oper = TempXLOPER12();
resize(oper, n + 2, m + 1);
setString(oper, firstLineLabel, 1, 0);
for (size_t i = 0; i < m; ++i) setString(oper, colLabels[i], 0, i + 1);
for (size_t i = 0; i < m; ++i) setNum(oper, firstLine[i], 1, i + 1);
for (size_t i = 0; i < n; ++i) setString(oper, rowLabels[i], i + 2, 0);
for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < m; ++j) setNum(oper, mat[i][j], i + 2, j + 1);
return oper;
}