forked from 6234456/Excel-VBA-Dicts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.bas
303 lines (203 loc) · 7.51 KB
/
Test.bas
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
Sub Test()
Dim d As New Dicts
Dim l As New Lists
Debug.Assert d.Count = 0
Debug.Assert l.length = 0
Dim c0 As New Collection
c0.add 2
c0.add 3
c0.add Array(4, 5, 6)
Debug.Assert l.fromArray(c0).getVal(2, 0) = 4
l.clear
Dim dn As New Dicts
dn.add 1, 2
Debug.Assert dn.union(l.fromSerial(1, 10).toMap).diff(dn).Count = 9
Debug.Assert dn.Count = 1
dn.clear
d.clear
l.Remove 1
l.removeAt 0
Dim d2 As New Dicts
d2.add 2, 1
Debug.Assert l.fromSerial(1, 10).toMap.diff(d2).Count = 9
d2.load("", 1, 2, appendMode:=True).p
Debug.Assert d.wb.Name = ActiveWorkbook.Name
With d.fromArray(l.fromSerial(1, 10))
Debug.Assert .dict.Item(10) = 9
Debug.Assert .keysArr(9) = 10
Debug.Assert l.fromSerial(1, 10).contains(10)
Debug.Assert l.addAll(.valsArr, False).map("_*2").getVal(9) = 18
Debug.Assert l.addAll(.keysArr, False).contains(10)
Debug.Assert .Count = 10
Debug.Assert .reduceKey("_+?", -1) = 54
Debug.Assert .reduce("_+?", 10) = 55
Debug.Assert .filterKey("_>8").Count = 2
Debug.Assert .filter("_>8").Count = 1
Debug.Assert .mapKey("_+6").keysArr(0) = 7
Debug.Assert .map("_+6").valsArr(0) = 6
Debug.Assert .diff(l.fromSerial(-10, 8).toDict).reduceKey("_+?", 0) = 19
End With
Dim l2 As New Lists
l.clear
l.add l2
l2.add d
Debug.Assert TypeName(l.getVal(0, 0)) = "Dicts"
l.clear
Debug.Assert l.of(1, 2, 3, 4, 5, Array(1, 2, 3)).length = 6
Debug.Assert l.fromSerial(10, 15).mapX("Test.callback").slice(-1).getVal(0) = "225_"
Debug.Assert l.fromSerial(1, 10).subgroupBy(2, 2).mapX("Test.m").reduce("_+?", 0) = 10
Debug.Assert l.fromSerial(10, 15).filterX("Test.f").length = 0
Debug.Assert l.fromSerial(10, 15).reduceX("Test.r", New Dicts).Count = 6
With l.of(1, 2, 3, 4).permutation
Debug.Assert .length = 4 * 3 * 2 * 1
End With
d.clear
l.clear
d.add 1, Array(l.of(1, 2, 3), 2, 3, 4)
d.add 2, Array(l.of(2, 2, 3), 2, 3, 4)
d.add 3, Array(l.of(3, 2, 3), 2, 3, 4)
d.add 4, "Qiou"
d.add 5, Array(l.of("qiou", Null, Date), 1, 23.4, Empty, True)
d.add 6, Nothing
Dim d1 As New Dicts
Set d1 = d1.fromArray(Array(1, 2, 3))
d1.add 4, Array(1, 2, l.of(1, 2, 3, True, Empty, "qiou"), Null, d1.fromArray(Array("qiou", "yang")))
d.add 7, d1
d.add 8, 2
d.label = Array("index", "a1", "a2", "a3")
Debug.Assert d.getByLabel(3, "index").getVal(0) = 3
Debug.Assert d.x_toString(Null) = "null"
Debug.Assert d.x_toString(Nothing) = "null"
Debug.Assert d.x_toString(Empty) = "null"
Debug.Assert d.x_toString(True) = "true"
Debug.Assert d.x_toString(Array(Null, Nothing)) = "[ null, null ]"
Debug.Assert l.of(Null, Null).toString = "[ null, null ]"
Debug.Assert Not d.isNothing("123")
d.p
' d.toJSON ("src.json")
Dim t As New TreeSets
t.add 12
t.add 3
t.addAll 1, 2, 3, 4, 5
Debug.Assert t.contains(3)
Debug.Assert IsNull(t.ceiling(14))
d.clear
l.clear
d.fromMatrix(l.fromSerial(0, 10).zip(l.fromSerial(2, 12))).mapX("callback_dict").filterX("callback_judgement_dict").p
Debug.Assert d.fromMatrix(l.fromArray(Array(Array(1, 2), Array(3, 4)))).reduceX("callback_d") = 10
Debug.Print "All tests passed!"
End Sub
Sub callback_d(ByRef d As Dicts, k, v, Optional ByVal i As Long)
d.callbackKey = d.callbackKey + k + v
End Sub
Sub callback_dict(ByRef d As Dicts, k, v, Optional ByVal i As Long)
d.callbackKey = k * 3
d.callbackVal = v + 5
End Sub
Sub callback_judgement_dict(ByRef d As Dicts, k, v, Optional ByVal i As Long)
' get last 3
d.callbackKey = i > d.Count - 5
End Sub
Private Sub callback(ByRef l As Lists, e, Optional ByVal i As Long)
l.callback = e ^ 2 & "_"
End Sub
Private Sub f(ByRef l As Lists, e, Optional ByVal i As Long)
l.callback = i > 15
End Sub
Private Sub r(ByRef l As Lists, e, Optional ByVal i As Long)
l.callback.dict.add e, 1
End Sub
Private Sub m(ByRef l As Lists, e, Optional ByVal i As Long)
l.callback = e.length
End Sub
Function solutions(ByRef l As Lists) As Lists
Dim res As New Lists
If l.length = 0 Then
res.add 0
ElseIf l.length = 1 Then
res.add l.getVal(0)
ElseIf l.length = 2 Then
Set res = res.of(l.getVal(0) + l.getVal(1), l.getVal(0) * l.getVal(1), l.getVal(1) - l.getVal(0), l.getVal(0) - l.getVal(1))
If l.getVal(1) <> 0 Then
res.add l.getVal(0) / l.getVal(1)
End If
If l.getVal(0) <> 0 Then
res.add l.getVal(1) / l.getVal(0)
End If
Else
Dim j, k
Dim tmp1 As Lists
Dim tmp2 As Lists
Set tmp1 = l
For j = 0 To tmp1.length - 1
Set tmp2 = solutions(tmp1.copy.removeAt(j))
For k = 0 To tmp2.length - 1
res.addAll solutions(l.of(tmp1.getVal(j), tmp2.getVal(k)))
Next k
Next j
Set tmp1 = Nothing
Set tmp2 = Nothing
End If
Set solutions = res.unique
Set res = Nothing
End Function
Sub demoSolutions()
Dim l As New Lists
With solutions(l.of(5, 8, 1, 4, 9))
Debug.Print .contains(24)
.p
End With
End Sub
Function solutions1(ByRef l As Lists) As Dicts
Dim res As New Dicts
If l.length = 0 Then
res.add 0, 0
ElseIf l.length = 1 Then
res.add l.getVal(0), l.getVal(0)
ElseIf l.length = 2 Then
res.add l.getVal(0) + l.getVal(1), l.getVal(0) & " + " & l.getVal(1)
res.add l.getVal(0) * l.getVal(1), l.getVal(0) & " * " & l.getVal(1)
res.add l.getVal(0) - l.getVal(1), l.getVal(0) & " - " & l.getVal(1)
res.add l.getVal(1) - l.getVal(0), l.getVal(1) & " - " & l.getVal(0)
If l.getVal(1) <> 0 Then
res.add l.getVal(0) / l.getVal(1), l.getVal(0) & " / " & l.getVal(1)
End If
If l.getVal(0) <> 0 Then
res.add l.getVal(1) / l.getVal(0), l.getVal(1) & " / " & l.getVal(0)
End If
Else
Dim j, k, i
Dim tmp1 As Lists
Dim tmp2 As New Lists
Dim tmp As Dicts
Dim d2 As Dicts
Dim tmp3
Set tmp1 = l
For j = 0 To tmp1.length - 1
Set d2 = solutions1(tmp1.copy.removeAt(j))
tmp2.addAll d2.Keys, False
For k = 0 To tmp2.length - 1
Set tmp = solutions1(l.of(tmp1.getVal(j), tmp2.getVal(k)))
For Each i In tmp.Keys
tmp3 = Split(tmp.dict(i), " ")
If Trim(tmp3(0)) = CStr(tmp1.getVal(j)) Then
res.add i, tmp1.getVal(j) & " " & tmp3(1) & " ( " & d2.dict(tmp2.getVal(k)) & " ) "
Else
res.add i, " ( " & d2.dict(tmp2.getVal(k)) & " ) " & " " & tmp3(1) & tmp1.getVal(j)
End If
Next i
Next k
Next j
Set tmp1 = Nothing
Set tmp2 = Nothing
End If
Set solutions1 = res
Set res = Nothing
End Function
Sub demoSolutions1()
Dim l As New Lists
With solutions1(l.of(5, 8, 3, 4))
.p
Debug.Print .dict(-5)
End With
End Sub