-
Notifications
You must be signed in to change notification settings - Fork 11
/
List.cls
666 lines (445 loc) · 17.6 KB
/
List.cls
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "List"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
Private Type tList
Encapsulated As Collection
ItemTypeName As String
End Type
Private this As tList
Option Explicit
Public Function Create(ParamArray Values() As Variant) As List
Dim valuesArray() As Variant
valuesArray = Values
Dim result As New List
result.AddArray valuesArray
Set Create = result
End Function
'' ***************************************************************************************
'' Private functions
'' ***************************************************************************************
Private Function IsReferenceType() As Boolean
If this.ItemTypeName = vbNullString Then Exit Function
IsReferenceType = IsObject(this.Encapsulated(1))
End Function
Private Function IsComparable() As Boolean
If IsReferenceType Then
IsComparable = TypeOf First Is IComparable
End If
End Function
Private Function CompareReferenceTypes(value As Variant, other As Variant) As Integer
Dim comparable As IComparable
If IsComparable Then
Set comparable = value
CompareReferenceTypes = comparable.CompareTo(other)
Else
RaiseErrorMustImplementIComparable "CompareReferenceTypes()"
End If
End Function
Private Function CompareValueTypes(value As Variant, other As Variant) As Integer
If value < other Then
CompareValueTypes = -1
ElseIf value > other Then
CompareValueTypes = 1
End If
End Function
Private Function IsEquatable() As Boolean
If IsReferenceType Then
IsEquatable = TypeOf First Is IEquatable
End If
End Function
Private Function EquateReferenceTypes(value As Variant, other As Variant) As Boolean
Dim equatable As IEquatable
If IsEquatable Then
Set equatable = value
EquateReferenceTypes = equatable.Equals(other)
Else
Debug.Print "WARNING: Reference type doesn't implement IEquatable, using reference equality."
EquateReferenceTypes = (ObjPtr(value) = ObjPtr(other))
End If
End Function
Private Function EquateValueTypes(value As Variant, other As Variant) As Boolean
EquateValueTypes = (value = other)
End Function
Private Function ValidateItemType(value As Variant)
If this.ItemTypeName = vbNullString Then this.ItemTypeName = TypeName(value)
ValidateItemType = IsTypeSafe(value)
End Function
Private Sub RaiseErrorUnsafeType(member As String, suppliedType As String)
Err.Raise 13, StringType.Format("{0}.{1}", ToString, member), _
StringType.Format("Type Mismatch. Expected: '{0}', '{1}' was supplied.", this.ItemTypeName, suppliedType)
End Sub
Private Sub RaiseErrorMustImplementIComparable(member As String)
Err.Raise 5, StringType.Format("{0}.{1}", ToString, member), "Invalid operation: method requires a list of numeric, date or string values, or a list of objects implementing the IComparable interface."
End Sub
Private Sub Class_Initialize()
Set this.Encapsulated = New Collection
End Sub
Private Sub Class_Terminate()
Set this.Encapsulated = Nothing
End Sub
'' ***************************************************************************************
'' Properties
'' ***************************************************************************************
Public Property Get Item(ByVal index As Long) As Variant
Attribute Item.VB_Description = "Gets or sets the element at the specified index."
Attribute Item.VB_UserMemId = 0
'Gets the element at the specified index.
If IsReferenceType Then
Set Item = this.Encapsulated(index)
Else
Item = this.Encapsulated(index)
End If
End Property
Public Property Let Item(ByVal index As Long, ByVal value As Variant)
'Sets the element at the specified index.
If Not IsTypeSafe(value) Then RaiseErrorUnsafeType "Item(Let)", TypeName(value)
RemoveAt index
If index = Count Then
Add value
Else
Insert index, value
End If
End Property
Public Property Set Item(ByVal index As Long, ByVal value As Variant)
'Sets the element at the specified index.
If Not IsTypeSafe(value) Then RaiseErrorUnsafeType "Item(Set)", TypeName(value)
RemoveAt index
If index = Count Then
Add value
Else
Insert index, value
End If
End Property
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_Description = "Gets an enumerator that iterates through the List."
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
'Gets an enumerator that iterates through the List.
Set NewEnum = this.Encapsulated.[_NewEnum]
End Property
Public Property Get Count() As Long
Attribute Count.VB_Description = "Gets the number of elements contained in the List."
'Gets the number of elements contained in the List.
Count = this.Encapsulated.Count
End Property
'' ***************************************************************************************
'' Public functions
'' ***************************************************************************************
Public Sub Add(ParamArray Values())
Attribute Add.VB_Description = "Adds the specified element(s) to the end of the List."
'Adds the specified element(s) to the end of the List.
Dim valuesArray() As Variant
valuesArray = Values
AddArray valuesArray
End Sub
Public Sub AddRange(Values As List)
Attribute AddRange.VB_Description = "Adds the specified elements to the end of the List."
'Adds the specified elements to the end of the List.
AddArray Values.ToArray
End Sub
Public Sub AddArray(Values() As Variant)
Attribute AddArray.VB_Description = "Adds the specified elements to the end of the List."
'Adds the specified elements to the end of the List.
Dim value As Variant, i As Long
For i = LBound(Values) To UBound(Values)
If ValidateItemType(value) Then
this.Encapsulated.Add Values(i)
Else
RaiseErrorUnsafeType "AddArray()", TypeName(value)
End If
Next
End Sub
Public Sub Clear()
Attribute Clear.VB_Description = "Removes all elements from the List."
'Removes all elements from the List.
Do Until Count = 0
this.Encapsulated.Remove 1
Loop
End Sub
Public Function Contains(value As Variant) As Boolean
Attribute Contains.VB_Description = "Determines whether an element is in the List."
'Determines whether an element is in the List.
Contains = (IndexOf(value) <> -1)
End Function
Public Function First() As Variant
Attribute First.VB_Description = "Returns the first element of the List."
'Returns the first element of the List.
If Count = 0 Then Exit Function
If IsReferenceType Then
Set First = Item(1)
Else
First = Item(1)
End If
End Function
Public Function GetRange(ByVal index As Long, ByVal valuesCount As Long) As List
Attribute GetRange.VB_Description = "Creates a copy of a range of elements in the source List."
'Creates a copy of a range of elements in the source List.
Dim result As List
If index > Count Then Err.Raise 9 'index out of range
Dim lastIndex As Long
lastIndex = IIf(index + valuesCount > Count, Count, index + valuesCount)
Set result = New List
Dim i As Long
For i = index To lastIndex
result.Add Item(i)
Next
Set GetRange = result
End Function
Public Function IndexOf(value As Variant) As Long
Attribute IndexOf.VB_Description = "Searches for the specified object and returns the 1-based index of the first occurrence within the entire List."
'Searches for the specified object and returns the 1-based index of the first occurrence within the entire List.
Dim found As Boolean
Dim isRef As Boolean
isRef = IsReferenceType
Dim i As Long
If Count = 0 Then IndexOf = -1: Exit Function
For i = 1 To Count
If isRef Then
found = EquateReferenceTypes(value, Item(i))
Else
found = EquateValueTypes(value, Item(i))
End If
If found Then IndexOf = i: Exit Function
Next
IndexOf = -1
End Function
Public Sub InsertArray(ByVal index As Long, Values() As Variant)
Attribute InsertArray.VB_Description = "Inserts the specified elements into the List at the specified index."
'Inserts the specified elements into the List at the specified index.
Dim tmp As List
Set tmp = GetRange(index, Count)
RemoveRange index, Count
AddArray Values
AddRange tmp
End Sub
Public Sub InsertRange(ByVal index As Long, Values As List)
Attribute InsertRange.VB_Description = "Inserts the specified elements into the List at the specified index."
'Inserts the specified elements into the List at the specified index.
Dim tmp As List
Set tmp = GetRange(index, Count)
RemoveRange index, Count
AddRange Values
AddRange tmp
End Sub
Public Sub Insert(ByVal index As Long, ParamArray Values())
Attribute Insert.VB_Description = "Inserts the specified element(s) into the List at the specified index."
'Inserts the specified element(s) into the List at the specified index.
Dim valuesArray() As Variant
valuesArray = Values
InsertArray index, valuesArray
End Sub
Public Function IsSortable() As Boolean
Attribute IsSortable.VB_Description = "Determines whether the List can be sorted."
'Determines whether the List can be sorted.
If Count = 0 Then Exit Function
Dim firstItem As Variant
If IsReferenceType Then
Set firstItem = First
Else
firstItem = First
End If
IsSortable = IsNumeric(firstItem) _
Or IsDate(firstItem) _
Or this.ItemTypeName = "String" _
Or IsComparable
End Function
Public Function IsTypeSafe(value As Variant) As Boolean
Attribute IsTypeSafe.VB_Description = "Determines whether a value can be safely added to the List."
'Determines whether a value can be safely added to the List.
'Returns true if the type of specified value matches the type of items already in the list,
'or it the type of specified value is a numeric type smaller than the type of items already in the list.
'This means a List<Long> can contain Integer values, but a List<Integer> cannot contain Long values.
Dim result As Boolean
'most common cases: this.ItemTypeName isn't yet defined, or matches TypeName(value):
result = this.ItemTypeName = vbNullString Or this.ItemTypeName = TypeName(value)
If result Then IsTypeSafe = result: Exit Function
'all other cases demand more processing:
IsTypeSafe = result _
Or this.ItemTypeName = "Integer" And StringType.MatchesAny(TypeName(value), "Byte") _
Or this.ItemTypeName = "Long" And StringType.MatchesAny(TypeName(value), "Integer", "Byte") _
Or this.ItemTypeName = "Single" And StringType.MatchesAny(TypeName(value), "Long", "Integer", "Byte") _
Or this.ItemTypeName = "Double" And StringType.MatchesAny(TypeName(value), "Long", "Integer", "Byte", "Single") _
Or this.ItemTypeName = "Currency" And StringType.MatchesAny(TypeName(value), "Long", "Integer", "Byte", "Single", "Double")
End Function
Public Function Last() As Variant
Attribute Last.VB_Description = "Returns the last element of the List."
'Returns the last element of the List.
If Count = 0 Then Exit Function
If IsReferenceType Then
Set Last = Item(Count)
Else
Last = Item(Count)
End If
End Function
Public Function LastIndexOf(value As Variant) As Long
Attribute LastIndexOf.VB_Description = "Searches for the specified object and returns the 1-based index of the last occurrence within the entire List."
'Searches for the specified object and returns the 1-based index of the last occurrence within the entire List.
Dim found As Boolean
Dim isRef As Boolean
isRef = IsReferenceType
LastIndexOf = -1
If Count = 0 Then Exit Function
Dim i As Long
For i = 1 To Count
If isRef Then
found = EquateReferenceTypes(value, Item(i))
Else
found = EquateValueTypes(value, Item(i))
End If
If found Then LastIndexOf = i
Next
End Function
Public Function Max() As Variant
Attribute Max.VB_Description = "Returns the maximum value in the List."
'Returns the maximum value in the List.
Dim isRef As Boolean
isRef = IsReferenceType
Dim largest As Variant
Dim isLarger As Boolean
Dim i As Long
For i = 1 To Count
If isRef Then
If IsEmpty(largest) Then Set largest = Item(i)
isLarger = CompareReferenceTypes(Item(i), largest) > 0
If isLarger Or IsEmpty(Max) Then
Set largest = Item(i)
Set Max = largest
End If
Else
If IsEmpty(largest) Then largest = Item(i)
isLarger = CompareValueTypes(Item(i), largest) > 0
If isLarger Or IsEmpty(Max) Then
largest = Item(i)
Max = largest
End If
End If
Next
End Function
Public Function Min() As Variant
Attribute Min.VB_Description = "Returns the minimum value in the List."
'Returns the minimum value in the List.
Dim isRef As Boolean
isRef = IsReferenceType
Dim smallest As Variant
Dim isSmaller As Boolean
Dim i As Long
For i = 1 To Count
If isRef Then
If IsEmpty(smallest) Then Set smallest = Item(i)
isSmaller = CompareReferenceTypes(Item(i), smallest) < 0
If isSmaller Or IsEmpty(Min) Then
Set smallest = Item(i)
Set Min = smallest
End If
Else
If IsEmpty(smallest) Then smallest = Item(i)
isSmaller = CompareValueTypes(Item(i), smallest) < 0
If isSmaller Or IsEmpty(Min) Then
smallest = Item(i)
Min = smallest
End If
End If
Next
End Function
Public Sub Reverse()
Attribute Reverse.VB_Description = "Reverses the order of the elements in the entire List."
'Reverses the order of the elements in the entire List.
Dim tmp As New List
Do Until Count = 0
tmp.Add Item(Count)
RemoveAt Count
Loop
AddRange tmp
End Sub
Public Sub Remove(ParamArray Values())
Attribute Remove.VB_Description = "Removes the first occurrence of specified object(s) from the List."
'Removes the first occurrence of specified object(s) from the List.
Dim i As Long
Dim index As Long
For i = LBound(Values) To UBound(Values)
index = IndexOf(Values(i))
If index <> -1 Then RemoveAt index
Next
End Sub
Public Sub RemoveAt(ByVal index As Long)
Attribute RemoveAt.VB_Description = "Removes the element at the specified index of the List."
'Removes the element at the specified index of the List.
this.Encapsulated.Remove index
End Sub
Public Sub RemoveRange(ByVal index As Long, ByVal valuesCount As Long)
Attribute RemoveRange.VB_Description = "Removes a range of elements from the List."
'Removes a range of elements from the List.
Dim i As Long
For i = index To index + valuesCount - 1
RemoveAt index
Next
End Sub
Public Sub Sort()
Attribute Sort.VB_Description = "Sorts the elements in the entire List."
'Sorts the elements in the entire List.
Dim tmp As List
Dim minValue As Variant
If Not IsSortable Then RaiseErrorMustImplementIComparable "Sort()"
Dim isRef As Boolean
isRef = IsReferenceType
Set tmp = New List
Do Until Count = 0
If isRef Then
Set minValue = Min
Else
minValue = Min
End If
tmp.Add minValue
Remove minValue
Loop
AddRange tmp
End Sub
Public Sub SortDescending()
Attribute SortDescending.VB_Description = "Sorts the elements in the entire List, in descending order."
'Sorts the elements in the entire List, in descending order.
Dim tmp As List
Dim maxValue As Variant
If Not IsSortable Then RaiseErrorMustImplementIComparable "SortDescending()"
Dim isRef As Boolean
isRef = IsReferenceType
Set tmp = New List
Do Until Count = 0
If isRef Then
Set maxValue = Max
Else
maxValue = Max
End If
tmp.Add maxValue
Remove maxValue
Loop
AddRange tmp
End Sub
Public Function ToArray() As Variant()
Attribute ToArray.VB_Description = "Copies the elements of the List to a new array."
'Copies the elements of the List to a new array.
Dim result() As Variant
ReDim result(1 To Count)
Dim i As Long
If Count = 0 Then Exit Function
If IsReferenceType Then
For i = 1 To Count
Set result(i) = Item(i)
Next
Else
For i = 1 To Count
result(i) = Item(i)
Next
End If
ToArray = result
End Function
Public Function ToString() As String
Attribute ToString.VB_Description = "Returns a string that represents the current List object."
'Returns a string that represents the current List object.
ToString = StringType.Format("{0}<{1}>", TypeName(Me), StringType.Coalesce(this.ItemTypeName, "Variant"))
End Function