/
Collection.vb
730 lines (608 loc) · 29.6 KB
/
Collection.vb
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Diagnostics
Imports Microsoft.VisualBasic.CompilerServices
Imports Microsoft.VisualBasic.CompilerServices.ExceptionUtils
Imports Microsoft.VisualBasic.CompilerServices.Utils
Namespace Microsoft.VisualBasic
<DebuggerTypeProxy(GetType(Collection.CollectionDebugView))>
<DebuggerDisplay("Count = {Count}")>
Public NotInheritable Class Collection : Implements ICollection, IList
Private m_CultureInfo As CultureInfo 'The CultureInfo used for key comparisons
Private m_KeyedNodesHash As Generic.Dictionary(Of String, Node) 'Hashtable mapping key (string) -> Node, contains only items added to the collection with a key
Private m_ItemsList As FastList 'Doubly-linked list of Node containing all items in the collection
Private m_Iterators As List(Of Object) 'List of iterators currently iterating this collection
Public Sub New()
MyBase.New()
Initialize(GetCultureInfo())
End Sub
'*****************************************************************************
'These methods are 1 based
'*****************************************************************************
''' <summary>
''' Add an item to the collection
''' </summary>
''' <param name="Item"></param>
''' <param name="Key"></param>
''' <param name="Before"></param>
''' <param name="After"></param>
Public Sub Add(ByVal Item As Object, Optional ByVal Key As String = Nothing, Optional ByVal Before As Object = Nothing, Optional ByVal After As Object = Nothing)
'Before and After are mutually exclusive
If (Before IsNot Nothing) AndAlso (After IsNot Nothing) Then
Throw New ArgumentException(SR.Collection_BeforeAfterExclusive)
End If
'Create a new node
Dim newNode As Node = New Node(Key, Item)
'If a key was specified, add the new node to the hashtable of keys. If this is
' a duplicate key, we'll get an argument exception. This prevents us from
' having to do a hashtable look-up to verify the key is unique.
'However, we then have to be careful to remove the node from the hashtable if we fail to add the item to the
' list because the Before or After keys were bad.
If Key IsNot Nothing Then
Try
m_KeyedNodesHash.Add(Key, newNode)
Catch ex As ArgumentException
Debug.Assert(m_KeyedNodesHash.ContainsKey(Key), "We got an argumentexception from a hashtable add, but it wasn't a duplicate key.")
' Duplicate key. Throw our own exception and our own message.
Throw VbMakeException(New ArgumentException(SR.Collection_DuplicateKey), vbErrors.DuplicateKey)
End Try
End If
Try
'Add the item to list and also (if a key was specified) to the hashtable
If (Before Is Nothing) AndAlso (After Is Nothing) Then 'Neither Before nor After have been specified
'Add the value to the linked list
m_ItemsList.Add(newNode)
ElseIf Before IsNot Nothing Then
'Before has been specified
Dim beforeString As String = TryCast(Before, String)
If beforeString IsNot Nothing Then
Dim BeforeNode As Node = Nothing
If Not m_KeyedNodesHash.TryGetValue(beforeString, BeforeNode) Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(Before)), NameOf(Before))
End If
Debug.Assert(BeforeNode IsNot Nothing)
m_ItemsList.InsertBefore(newNode, BeforeNode)
Else
m_ItemsList.Insert(CInt(Before) - 1, newNode) 'Convert from 1 based to 0 based.
End If
Else
'After has been specified
Dim afterString As String = TryCast(After, String)
If afterString IsNot Nothing Then
Dim AfterNode As Node = Nothing
If Not m_KeyedNodesHash.TryGetValue(afterString, AfterNode) Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(After)), NameOf(After))
End If
Debug.Assert(AfterNode IsNot Nothing)
m_ItemsList.InsertAfter(newNode, AfterNode)
Else
m_ItemsList.Insert(CInt(After), newNode) 'Conversion from 1 based to 0 based offsets need to add 1.
End If
End If
Catch ex As OutOfMemoryException
Throw
Catch ex As StackOverflowException
Throw
Catch ex As Exception
'We couldn't add the item to the list because the Before or After key was not found. We need to back out the
' insert that we did into the hash table.
If Key IsNot Nothing Then
m_KeyedNodesHash.Remove(Key)
End If
Throw
End Try
'Adjust the ForEach iterators
AdjustEnumeratorsOnNodeInserted(newNode)
End Sub
''' <summary>
''' Clears all items in the collection
''' </summary>
Public Sub Clear()
m_KeyedNodesHash.Clear()
m_ItemsList.Clear()
'Notify the enumerators
Dim i As Integer = m_Iterators.Count - 1
While i >= 0
Dim ref As WeakReference = DirectCast(m_Iterators(i), WeakReference)
If ref.IsAlive Then
Dim enumerator As ForEachEnum = CType(ref.Target, ForEachEnum)
If Not enumerator Is Nothing Then
enumerator.AdjustOnListCleared()
End If
Else
m_Iterators.RemoveAt(i)
End If
i -= 1
End While
End Sub
''' <summary>
''' Returns true if the given key is in the collection
''' </summary>
''' <param name="Key"></param>
''' <returns>True - the given key is in the collection. False otherwise.</returns>
Public Function Contains(ByVal Key As String) As Boolean
If Key Is Nothing Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(Key)), NameOf(Key))
End If
Return m_KeyedNodesHash.ContainsKey(Key)
End Function
Public Overloads Sub Remove(ByVal Key As String)
Dim node As Node = Nothing
If m_KeyedNodesHash.TryGetValue(Key, node) Then
Debug.Assert(node IsNot Nothing)
'Adjust the ForEach iterators
AdjustEnumeratorsOnNodeRemoved(node) 'Must be done before the prev/next pointers are removed
'Remove the item from the list and hash table (we know it has a key)
Debug.Assert(node.m_Key IsNot Nothing, "How can that be? We just found it by its key.")
m_KeyedNodesHash.Remove(Key)
m_ItemsList.RemoveNode(node)
'Remove prev/next pointers because the iterators will be thrown off if this isn't done. Also it's easier for debugging.
node.m_Prev = Nothing
node.m_Next = Nothing
Else
Debug.Assert(node Is Nothing)
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(Key)), NameOf(Key))
End If
End Sub
Public Overloads Sub Remove(ByVal Index As Integer)
IndexCheck(Index)
Dim node As Node = m_ItemsList.RemoveAt(Index - 1) '0 based
Debug.Assert(node IsNot Nothing, "Should have thrown exception rather than return Nothing")
AdjustEnumeratorsOnNodeRemoved(node) 'Must be done before the prev/next pointers are removed
'Remove from the hash table if it has a key
If node.m_Key IsNot Nothing Then
m_KeyedNodesHash.Remove(node.m_Key)
End If
'Remove prev/next pointers because the iterators will be thrown off if this isn't done. Also it's easier for debugging.
node.m_Prev = Nothing
node.m_Next = Nothing
End Sub
Default Public Overloads ReadOnly Property Item(ByVal Index As Integer) As Object
'This method uses 1 based arrays.
Get
IndexCheck(Index)
Dim node As Node = m_ItemsList.Item(Index - 1)
Debug.Assert(node IsNot Nothing, "Should have thrown rather than returning Nothing")
Return node.m_Value
End Get
End Property
Default Public Overloads ReadOnly Property Item(ByVal Key As String) As Object
Get
If Key Is Nothing Then
'Backwards compat - throw IndexOutOfRange if Key = Nothing
Throw New IndexOutOfRangeException(SR.Argument_CollectionIndex)
End If
Dim node As Node = Nothing
If Not m_KeyedNodesHash.TryGetValue(Key, node) Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Index"), NameOf(Key))
End If
Debug.Assert(node IsNot Nothing)
Return node.m_Value
End Get
End Property
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>
Default Public Overloads ReadOnly Property Item(ByVal Index As Object) As Object
Get
If (TypeOf Index Is String) OrElse (TypeOf Index Is Char) OrElse (TypeOf Index Is Char()) Then
' Index is string and is being treated as key
Dim key As String = CStr(Index)
Return Me.Item(key)
Else
' Index is being treated as numeric expression
Dim indexValue As Integer
Try
indexValue = CInt(Index)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(Index)), NameOf(Index))
End Try
Return Me.Item(indexValue)
End If
End Get
End Property
Public ReadOnly Property Count() As Integer
Get
Return m_ItemsList.Count
End Get
End Property
Public Function GetEnumerator() As IEnumerator
' Remove Dead Iterators if any from Iterator list m_Iterators
Dim oldWeakref As WeakReference
Dim i As Integer = m_Iterators.Count - 1
While (i >= 0)
oldWeakref = CType(m_Iterators(i), WeakReference)
If Not oldWeakref.IsAlive Then
m_Iterators.RemoveAt(i)
End If
i -= 1
End While
' Create A New Iterator, add to Iterator List and return
Dim enumerator As ForEachEnum = New ForEachEnum(Me)
Dim weakref As WeakReference = New WeakReference(enumerator)
enumerator.WeakRef = weakref
m_Iterators.Add(weakref)
Return enumerator
End Function
Friend Sub RemoveIterator(ByVal weakref As WeakReference)
m_Iterators.Remove(weakref)
End Sub
Friend Sub AddIterator(ByVal weakref As WeakReference)
m_Iterators.Add(weakref)
End Sub
'Returns the first node in the linked list
Friend Function GetFirstListNode() As Node
Return m_ItemsList.GetFirstListNode()
End Function
Friend NotInheritable Class Node
'Constructor. Key or Value may be Nothing
Friend Sub New(ByVal Key As String, ByVal Value As Object)
m_Value = Value
m_Key = Key
End Sub
Friend m_Value As Object 'The value.
Friend m_Key As String 'The key. If Nothing, the collection item has no user-specified key.
Friend m_Next As Node 'Doubly-linked list pointer to the next node
Friend m_Prev As Node 'Doubly-linked list pointer to the previous node
End Class 'Node
''' <summary>
''' Debugger proxy for the Collection class. Provides a view of the collection
''' that just displays the collection contents.
''' </summary>
Friend NotInheritable Class CollectionDebugView
<DebuggerBrowsable(DebuggerBrowsableState.Never)>
Private m_InstanceBeingWatched As Collection
Public Sub New(ByVal RealClass As Collection)
m_InstanceBeingWatched = RealClass
End Sub
'Returns an array with all items in the collection.
<DebuggerBrowsable(DebuggerBrowsableState.RootHidden)>
Public ReadOnly Property Items() As Object()
Get
Dim count As Integer = m_InstanceBeingWatched.Count
If count = 0 Then
Return Nothing
End If
Dim results(count) As Object
results(0) = SR.EmptyPlaceHolderMessage
For Index As Integer = 1 To count
Dim newNode As Node = m_InstanceBeingWatched.InternalItemsList.Item(Index - 1)
results(Index) = New KeyValuePair(newNode.m_Key, newNode.m_Value)
Next Index
Return results
End Get
End Property
End Class
''' <summary>
''' Initialize all internal structures to set up an empty collection.
''' </summary>
''' <param name="CultureInfo">the culture info to use for key comparisons for the lifetime of this collection.</param>
''' <param name="StartingHashCapacity"></param>
Private Sub Initialize(ByVal CultureInfo As CultureInfo, Optional ByVal StartingHashCapacity As Integer = 0)
Debug.Assert(CultureInfo IsNot Nothing)
If StartingHashCapacity > 0 Then
m_KeyedNodesHash = New Generic.Dictionary(Of String, Node)(StartingHashCapacity, StringComparer.Create(CultureInfo, ignoreCase:=True))
Else
m_KeyedNodesHash = New Generic.Dictionary(Of String, Node)(StringComparer.Create(CultureInfo, ignoreCase:=True))
End If
m_ItemsList = New FastList()
m_Iterators = New List(Of Object)
m_CultureInfo = CultureInfo
End Sub
Private NotInheritable Class FastList
Private m_StartOfList As Node
Private m_EndOfList As Node
Private m_Count As Integer
Friend Sub New()
MyBase.New()
End Sub
Friend Sub Add(ByVal Node As Node)
If m_StartOfList Is Nothing Then
m_StartOfList = Node
Else
m_EndOfList.m_Next = Node
Node.m_Prev = m_EndOfList
End If
m_EndOfList = Node
m_Count += 1
End Sub
'Searches for a given value in the list. Returns its node's index or -1 if not found.
Friend Function IndexOfValue(ByVal Value As Object) As Integer
Dim currentNode As Node = m_StartOfList
Dim index As Integer = 0
While Not currentNode Is Nothing
If DataIsEqual(currentNode.m_Value, Value) Then
Return index
End If
currentNode = currentNode.m_Next
index += 1
End While
Return -1
End Function
Friend Sub RemoveNode(ByVal NodeToBeDeleted As Node)
Debug.Assert(NodeToBeDeleted IsNot Nothing, "How can we remove a non-existent node ?")
DeleteNode(NodeToBeDeleted, NodeToBeDeleted.m_Prev)
End Sub
'Removes the node at the given index.
' Returns the node that was removed
Friend Function RemoveAt(ByVal Index As Integer) As Node
Dim currentNode As Node = m_StartOfList
Dim currentIndex As Integer = 0
Dim prevNode As Node = Nothing
While currentIndex < Index AndAlso (currentNode IsNot Nothing)
prevNode = currentNode
currentNode = currentNode.m_Next
currentIndex += 1
End While
If (currentNode Is Nothing) Then
Throw New ArgumentOutOfRangeException(NameOf(Index))
End If
DeleteNode(currentNode, prevNode)
Return currentNode
End Function
Friend Function Count() As Integer
Return m_Count
End Function
Friend Sub Clear()
m_StartOfList = Nothing
m_EndOfList = Nothing
m_Count = 0
End Sub
'Retrieves the node at the given index (0-based)
Friend ReadOnly Property Item(ByVal Index As Integer) As Node
Get
Dim node As Node = GetNodeAtIndex(Index)
If (node Is Nothing) Then
Throw New ArgumentOutOfRangeException(NameOf(Index))
End If
Return node
End Get
End Property
Friend Sub Insert(ByVal Index As Integer, ByVal Node As Node)
Dim prevNode As Node = Nothing
' (Index > m_Count) is here for a reason. We allow insertion immediately beyond the end of the
' list i.e. if there are 0 to m_Count -1 elements, we allow insertion into the
' m_Count index
If (Index < 0) OrElse (Index > m_Count) Then
Throw New ArgumentOutOfRangeException(NameOf(Index))
End If
Dim nodeAtIndex As Node = GetNodeAtIndex(Index, prevNode) 'Note: PrevNode passed ByRef
Insert(Node, prevNode, nodeAtIndex)
End Sub
'Inserts a node into the list.
' The item is inserted before NodeToInsertBefore (may not be Nothing).
Friend Sub InsertBefore(ByVal Node As Node, ByVal NodeToInsertBefore As Node)
Debug.Assert(NodeToInsertBefore IsNot Nothing, "FastList.InsertBefore: NodeToInsertBefore may not be nothing")
Insert(Node, NodeToInsertBefore.m_Prev, NodeToInsertBefore)
End Sub
'Inserts a node into the list.
' The item is inserted after NodeToInsertAfter (may not be Nothing).
Friend Sub InsertAfter(ByVal Node As Node, ByVal NodeToInsertAfter As Node)
Debug.Assert(NodeToInsertAfter IsNot Nothing, "FastList.InsertAfter: NodeToInsertAfter may not be nothing")
Insert(Node, NodeToInsertAfter, NodeToInsertAfter.m_Next)
End Sub
'Returns the first node in the list
Friend Function GetFirstListNode() As Node
Return m_StartOfList
End Function
Private Function DataIsEqual(ByVal obj1 As Object, ByVal obj2 As Object) As Boolean
If obj1 Is obj2 Then
Return True
End If
If obj1.GetType() Is obj2.GetType() Then
Return Object.Equals(obj1, obj2)
Else
Return False
End If
End Function
Private Function GetNodeAtIndex(ByVal Index As Integer, Optional ByRef PrevNode As Node = Nothing) As Node
Dim currentNode As Node = m_StartOfList
Dim currentIndex As Integer = 0
PrevNode = Nothing
While currentIndex < Index AndAlso (currentNode IsNot Nothing)
PrevNode = currentNode
currentNode = currentNode.m_Next
currentIndex += 1
End While
Return currentNode
End Function
'Inserts the given node into the list between the two given nodes (may be Nothing at the beginning/end of the list)
Private Sub Insert(ByVal Node As Node, ByVal PrevNode As Node, ByVal CurrentNode As Node)
Node.m_Next = CurrentNode
If Not CurrentNode Is Nothing Then
CurrentNode.m_Prev = Node
End If
If PrevNode Is Nothing Then
m_StartOfList = Node
Else
PrevNode.m_Next = Node
Node.m_Prev = PrevNode
End If
If Node.m_Next Is Nothing Then
m_EndOfList = Node
End If
m_Count += 1
End Sub
Private Sub DeleteNode(ByVal NodeToBeDeleted As Node, ByVal PrevNode As Node)
Debug.Assert(NodeToBeDeleted IsNot Nothing, "How can we delete a non-existent node ?")
If PrevNode Is Nothing Then ' are we are deleting the first node ?
Debug.Assert(NodeToBeDeleted Is m_StartOfList, "How can any node besides the first node not have a previous node ?")
m_StartOfList = m_StartOfList.m_Next
If m_StartOfList Is Nothing Then
m_EndOfList = Nothing
Else
m_StartOfList.m_Prev = Nothing
End If
Else
PrevNode.m_Next = NodeToBeDeleted.m_Next
If PrevNode.m_Next Is Nothing Then
m_EndOfList = PrevNode
Else
PrevNode.m_Next.m_Prev = PrevNode
End If
End If
m_Count -= 1
End Sub
End Class 'FastList
'NOTE: This structure exists so that the debugger windows can show the items in a collection. See the Items property.
Private Structure KeyValuePair
Private m_Key As Object
Private m_Value As Object
Friend Sub New(ByVal NewKey As Object, ByVal NewValue As Object)
m_Key = NewKey
m_Value = NewValue
End Sub
Public ReadOnly Property Key() As Object
Get
Return m_Key
End Get
End Property
Public ReadOnly Property Value() As Object
Get
Return m_Value
End Get
End Property
End Structure
Private Sub AdjustEnumeratorsOnNodeInserted(ByVal NewNode As Node)
AdjustEnumeratorsHelper(NewNode, ForEachEnum.AdjustIndexType.Insert)
End Sub
Private Sub AdjustEnumeratorsOnNodeRemoved(ByVal RemovedNode As Node)
AdjustEnumeratorsHelper(RemovedNode, ForEachEnum.AdjustIndexType.Remove)
End Sub
Private Sub AdjustEnumeratorsHelper(ByVal NewOrRemovedNode As Node, ByVal Type As ForEachEnum.AdjustIndexType)
Debug.Assert(NewOrRemovedNode IsNot Nothing, "AdjustIndexes: Node shouldn't be Nothing")
Dim weakref As WeakReference
Dim i As Integer = m_Iterators.Count - 1
While (i >= 0)
weakref = CType(m_Iterators(i), WeakReference)
If weakref.IsAlive Then
Dim enumerator As ForEachEnum = CType(weakref.Target, ForEachEnum)
If Not enumerator Is Nothing Then
enumerator.Adjust(NewOrRemovedNode, Type) '1 based
End If
Else
m_Iterators.RemoveAt(i)
End If
i -= 1
End While
End Sub
Private Sub IndexCheck(ByVal Index As Integer)
If (Index < 1 OrElse Index > m_ItemsList.Count) Then
Throw New IndexOutOfRangeException(SR.Argument_CollectionIndex)
End If
End Sub
Private Function InternalItemsList() As FastList
Return m_ItemsList
End Function
#Region "Interface Implementation"
'*****************************************************************************
'These methods are 0 based.
'*****************************************************************************
Private Function ICollectionGetEnumerator() As IEnumerator Implements ICollection.GetEnumerator
Return GetEnumerator()
End Function
Private ReadOnly Property ICollectionCount() As Integer Implements ICollection.Count
Get
Return m_ItemsList.Count
End Get
End Property
Private ReadOnly Property ICollectionIsSynchronized() As Boolean Implements ICollection.IsSynchronized
Get
Return False
End Get
End Property
Private ReadOnly Property ICollectionSyncRoot() As Object Implements ICollection.SyncRoot
Get
Return Me
End Get
End Property
Private ReadOnly Property IListIsFixedSize() As Boolean Implements IList.IsFixedSize
Get
Return False
End Get
End Property
Private ReadOnly Property IListIsReadOnly() As Boolean Implements IList.IsReadOnly
Get
Return False
End Get
End Property
Private Sub ICollectionCopyTo(ByVal [array] As System.Array, ByVal index As Integer) Implements ICollection.CopyTo
If [array] Is Nothing Then
Throw New ArgumentNullException(NameOf(array), SR.Format(SR.Argument_InvalidNullValue1, NameOf(array)))
End If
If [array].Rank <> 1 Then
Throw New ArgumentException(SR.Format(SR.Argument_RankEQOne1, NameOf(array)), NameOf(array))
End If
If (index < 0) OrElse ([array].Length - index < Count) Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(index)), NameOf(index))
End If
Dim i As Integer
Dim objArray As Object() = TryCast(array, Object())
If objArray IsNot Nothing Then
For i = 1 To Count
objArray(index + i - 1) = Me.Item(i)
Next i
Else
For i = 1 To Count
[array].SetValue(Me.Item(i), index + i - 1)
Next i
End If
End Sub
Private Function IListAdd(ByVal value As Object) As Integer Implements IList.Add
Add(value, Nothing)
Return m_ItemsList.Count - 1 'IList is 0 based. Return a 0-based index. If m_ItemsList.Count is zero, we threw during Add() so no need to gate the Count = 0 case.
End Function
Private Sub IListInsert(ByVal index As Integer, ByVal value As Object) Implements IList.Insert
Dim newNode As New Node(Nothing, value)
m_ItemsList.Insert(index, newNode) 'FastList is 0-indexed just like IList, so no transformation of "index" needed
'No key, so no need to add to the hash table.
'Adjust the ForEach iterators
AdjustEnumeratorsOnNodeInserted(newNode)
End Sub
Private Sub IListRemoveAt(ByVal index As Integer) Implements IList.RemoveAt
Dim node As Node = m_ItemsList.RemoveAt(index) '0 based
Debug.Assert(node IsNot Nothing, "Should have thrown exception rather than return Nothing")
AdjustEnumeratorsOnNodeRemoved(node) 'Adjust the ForEach iterators
If node.m_Key IsNot Nothing Then
m_KeyedNodesHash.Remove(node.m_Key)
End If
node.m_Prev = Nothing
node.m_Next = Nothing
End Sub
Private Sub IListRemove(ByVal value As Object) Implements IList.Remove
Dim index As Integer
index = IListIndexOf(value)
If index <> -1 Then
IListRemoveAt(index)
End If
'No exception thrown if not found
End Sub
Private Sub IListClear() Implements IList.Clear
Clear()
End Sub
Private Property IListItem(ByVal index As Integer) As Object Implements IList.Item
Get
Dim node As Node
node = m_ItemsList.Item(index)
Return node.m_Value
End Get
Set(ByVal value As Object)
Dim node As Node = m_ItemsList.Item(index)
node.m_Value = value
End Set
End Property
Private Function IListContains(ByVal value As Object) As Boolean Implements IList.Contains
Return (IListIndexOf(value) <> -1)
End Function
Private Function IListIndexOf(ByVal value As Object) As Integer Implements IList.IndexOf
Return m_ItemsList.IndexOfValue(value)
End Function
#End Region
End Class 'Collection
End Namespace