forked from austinleedavis/VBA-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListCol.cls
More file actions
506 lines (419 loc) · 12.6 KB
/
Copy pathArrayListCol.cls
File metadata and controls
506 lines (419 loc) · 12.6 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "ArrayListCol"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Base 0 'Index all arrays from 0
Implements iCollection
'Implements iList
Implements iIterable
Private Const DEFAULT_CAPACITY As Long = 10 ' The default intial capacity of the ArrayListCol
Private Const MAX_ARRAY_SIZE As Long = 2147483639 ' maximum size of array to allocate
Private elementData As Collection ' The actual array of Variants in the ArrayListCol
Private modCount As Long ' The number of times this list has been structurally modified
Private Sub Class_Initialize()
Set elementData = New Collection
End Sub
'Trims the capacity of this ArrayListCol instance to list's current size. An application can use this operation to minimize the storage of an ArrayListCol instance
'Public Sub trimToSize()
' modCount = modCount + 1
' capacity = elementData.Count - 1
'End Sub
'Public Sub ensureCapacity(minCapacity As Long)
' modCount = modCount + 1
'
' 'Overflow-conscious code
' If minCapacity - elementData.Count - 1 > 0 Then
' grow (minCapacity)
' End If
'
'End Sub
'Private Sub grow(minCapacity As Long)
' 'Overflow-conscious code
' Dim oldCapacity As Long
' oldCapacity = UBound(elementData)
' Dim newCapacity As Long
' newCapacity = oldCapacity + (oldCapacity / 2)
' If newCapacity - minCapacity < 0 Then
' newCapacity = minCapacity
' End If
' If newCapacity - MAX_ARRAY_SIZE > 0 Then
' newCapacity = hugeCapacity(minCapacity)
' End If
'
' 'minCapacity is usually close to size, so this is a win
' ReDim Preserve elementData(0 To newCapacity)
'
'End Sub
'Private Function hugeCapacity(minCapacity As Long) As Long
' If minCapacity < 0 Then
' err.Raise 6 ' overflow
' End If
' hugeCapacity = IIf(minCapacity > MAX_ARRAY_SIZE, _
' 2147483647, MAX_ARRAY_SIZE)
'End Function
'Private Sub ensureCapacityInternal(minCapacity As Long)
' ensureExplicitCapacity (minCapacity)
'End Sub
'
'Private Sub ensureExplicitCapacity(minCapacity As Long)
' modCount = modCount + 1
'
' 'overflow-conscious code
' If minCapacity - UBound(elementData) > 0 Then
' grow (minCapacity)
' End If
'
'End Sub
' Returns the number of elements in this list
Public Function size() As Long
size = elementData.Count
End Function
'Returns true if this list contains no elements
Public Function isEmpty() As Boolean
isEmpty = (size = 0)
End Function
' Returns true if this list contains the specified element
Public Function contains(ByRef o As Variant) As Boolean
contains = (indexOf(o) >= 0)
End Function
'Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
Public Function indexOf(ByRef o As Variant) As Long
Dim i As Long
If o = Empty Then
err.Raise 424, , "NullPointerException"
Else
If size = 0 Then
indexOf = -1
Exit Function
End If
For i = 0 To size - 1 Step 1
If o = getIndexInternal(i) Then
indexOf = i
Exit Function
End If
Next i
End If
indexOf = Longs.valueOf(-1)
End Function
'Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element
Public Function lastIndexOf(ByRef o As Variant) As Long
Dim i As Long
If o = Empty Then
err.Raise 424, , "NullPointerException"
Else
If size = 0 Then
lastIndexOf = -1
Exit Function
End If
For i = size - 1 To 0 Step -1
If o = getIndexInternal(i) Then
lastIndexOf = i
Exit Function
End If
Next i
End If
lastIndexOf = -1
End Function
Public Function ToArray() As Variant()
Dim arrCopy() As Variant
ReDim arrCopy(0 To size - 1)
Dim i As Long
For i = 0 To size - 1 Step 1
arrCopy(i) = getIndexInternal(i)
Next i
ToArray = arrCopy
End Function
Public Function getIndex(index As Long) As Variant
rangeCheck (index)
getIndex = elementData.Item(index + 1)
End Function
Private Function getIndexInternal(index As Long) As Variant
getIndexInternal = elementData.Item(index + 1)
End Function
Public Function setIndex(index As Long, ByRef element As Variant) As Variant
rangeCheck (index)
Dim oldValue As Variant
oldValue = getIndexInternal(index)
elementData.add element, , , index + 2 = element
removeIndex (index)
setIndex = oldValue
End Function
Public Function add(ByRef e As Variant) As Boolean
' ensureCapacityInternal (size + 1)
elementData.add e
' size = size + 1
add = True
End Function
Public Sub addAtIndex(index As Long, ByRef element As Variant)
rangeCheckForAdd (index)
' ensureCapacity (size + 1)
'shift current elements to the right one
' arrayCopy elementData, index, elementData, index + 1, size - index
If index = elementData.Count Then
elementData.add element
Else
elementData.add element, , index + 1
End If
End Sub
Public Function removeIndex(index As Long) As Variant
rangeCheck (index)
modCount = modCount + 1
Dim oldValue As Variant
oldValue = getIndex(index)
elementData.remove index + 2
' Dim numMoved As Long
' numMoved = size - index - 1
' If numMoved > 0 Then
' 'move the remaining elements left one
' arrayCopy elementData, index + 1, elementData, index, numMoved
' End If
'
' size = size - 1
' elementData(size) = Empty
removeIndex = oldValue
End Function
Private Sub rangeCheck(index As Long)
If index >= size Then
err.Raise 9, , outOfBoundsMsg(index)
End If
End Sub
Private Sub rangeCheckForAdd(index As Long)
If index > size Or index < 0 Then
err.Raise 9, , outOfBoundsMsg(index)
End If
End Sub
Private Function outOfBoundsMsg(index As Long) As String
outOfBoundsMsg = "Index " & index & ", Size: " & size
End Function
Public Function remove(ByRef o As Variant) As Boolean
Variants.requireNonNull o
Dim index As Long
For index = 0 To size - 1 Step 1
If getIndexInternal(index) = o Then
fastRemove (index)
remove = True
Exit Function
End If
Next index
remove = False
End Function
Private Sub fastRemove(index As Long)
modCount = modCount + 1
elementData.remove index + 1
' Dim numMoved As Long
' numMoved = size - index - 1
' If numMoved > 0 Then
' arrayCopy elementData, index + 1, elementData, index, numMoved
' End If
' size = size - 1
' elementData(size) = Null
End Sub
Public Sub clear()
modCount = modCount + 1
Set elementData = Nothing
Set elementData = New Collection
' Dim i As Long
' For i = 0 To size Step 1
' elementData(i) = Null
' Next i
'
' size = 0
End Sub
Public Function addAll(ByRef c As iCollection) As Boolean
Variants.requireNonNull c
Dim A() As Variant
A = c.ToArray
For Each element In A
elementData.add element
Next element
addAll = c.size > 0
' Dim numNew As Long
' numNew = UBound(arr) - LBound(arr)
'' ensureCapacityInternal (size + numNew)
'
' Dim i As Long
' Dim j As Long
' j = size
' For i = LBound(arr) To UBound(arr) Step 1
' elementData(j) = arr(i)
' j = j + 1
' Next i
' size = size + numNew
' addAll = numNew <> 0
End Function
Public Function addAllAtIndex(index As Long, ByRef c As iCollection) As Boolean
rangeCheckForAdd index
Variants.requireNonNull c
If index < elementData.Count Then
Dim offset As Long
offset = 0
Dim A() As Variant
A = c.ToArray
For Each element In A
elementData.add element, , , index + 1 + offset
offset = offset + 1
Next element
Else
addAll c
End If
addAllAtIndex = (c.size > 0)
' rangeCheckForAdd (index)
'
' Dim numNew As Long
' numNew = UBound(arr) - LBound(arr)
' ensureCapacityInternal (size + numNew)
'
' Dim numMoved As Long
' numMoved = size - index
' If numMoved > 0 Then
' arrayCopy elementData, index, elementData, index + numNew, numMoved
' End If
'
' Dim i As Long
' Dim j As Long
' j = LBound(arr)
' For i = index To index + numNew Step 1
' elementData(i) = arr(j)
' j = j + 1
' Next i
'
' size = size + numNew
' addAllAtIndex = numNew <> 0
End Function
Sub removeRange(fromIndex As Long, toIndex As Long)
modCount = modCount + 1
Dim numRemoved As Long
numRemoved = toIndex - fromIndex
Dim i As Long
For i = numRemoved To 0 Step -1
removeIndex (fromIndex)
Next i
' Dim numMoved As Long
' numMoved = size - toIndex
' arrayCopy elementData, toIndex, elementData, fromIndex, numMoved
'
' 'clear to let GC do its work
' Dim newSize As Long
' newSize = size - (toIndex - fromIndex)
' Dim i As Long
' For i = newSize To size Step 1
' elementData(i) = Null
' Next i
' size = newSize
End Sub
Public Function removeAll(ByRef c As iCollection) As Boolean
Variants.requireNonNull c
batchRemove c, False
End Function
Public Function retainAll(ByRef c As iCollection) As Boolean
Variants.requireNonNull c
batchRemove c, True
End Function
Private Function batchRemove(ByRef c As ArrayListCol, complement As Boolean) As Boolean
Dim r As Integer
Dim w As Integer
r = 0
w = 0
Dim modified As Boolean
modified = False
On Error GoTo finally
For r = 0 To size Step 1
If (c.contains(elementData(r)) = complement) Then
elementData(w) = elementData(r)
w = w + 1
End If
Next r
batchRemove = modified
Exit Function
finally:
If r <> size Then
''''arrayCopy elementData, r, elementData, w, size - r
w = w + size - r
End If
If w <> size Then
'clear to let GC do its work
Dim i As Long
For i = w To size Step 1
elementData(i) = Null
Next i
modCount = modCount + size - w
size = w
modified = True
End If
batchRemove = modified
Exit Function
End Function
Public Function subList(fromIndex As Long, toIndex As Long) As ArrayListCol
subListRangeCheck fromIndex, toIndex, Me.getSize
subList = New ArrayListCol
subList.addAll elementData
End Function
Private Sub subListRangeCheck(fromIndex As Long, toIndex As Long, curSize As Long)
If fromIndex < 0 Then
err.Raise 6, , "fromIndex = " & fromIndex
End If
If toIndex > curSize Then
err.Raise 6, , "toIndex = " & toIndex
End If
If fromIndex > toIndex Then
err.Raise 328, , "fromIndex(" & fromIndex & ") > toIndex(" & toIndex & ")"
End If
End Sub
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Set NewEnum = elementData.[_NewEnum]
End Property
' ------------------ iCollection Interface Methods
Public Function iCollection_add(ByRef e As Variant) As Boolean
iCollection_add = add(e)
End Function
Public Function iCollection_addAll(ByRef c As iCollection) As Boolean
iCollection_addAll = addAll(c)
End Function
Public Sub iCollection_clear()
clear
End Sub
Public Function iCollection_contains(ByRef o As Variant) As Boolean
iCollection_contains = contains(o)
End Function
Public Function iCollection_equals(ByRef o As Variant) As Boolean
''TODO
End Function
Public Function iCollection_hashCode() As Long
''TODO
End Function
Public Function iCollection_isEmpty() As Boolean
iCollection_isEmpty = isEmpty
End Function
'iterator()
'parallelStream()
Public Function iCollection_remove(o As Variant) As Boolean
iCollection_remove = remove(o)
End Function
Public Function iCollection_removeAll(ByRef c As iCollection) As Boolean
iCollection_removeAll = removeAll(c)
End Function
Public Function iCollection_retainAll(ByRef c As iCollection) As Boolean
iCollection_retainAll = retainAll(c)
End Function
Public Function iCollection_size() As Long
iCollection_size = size
End Function
'spliterator
'stream
Public Function iCollection_toArray() As Variant()
iCollection_toArray = ToArray
End Function
Public Property Get iCollection_NewEnum() As IUnknown
''specified in iIterable
Set iCollection_NewEnum = NewEnum
End Property
Public Property Get iIterable_NewEnum() As IUnknown
''specified in iIterable
Set iIterable_NewEnum = NewEnum
End Property