forked from huggle/huggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortedList.vb
85 lines (64 loc) · 2.61 KB
/
SortedList.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
'This is a source code or part of Huggle project
'Copyright (C) 2011 Huggle team
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
Class SortedList(Of T)
Implements IEnumerable(Of T)
'Implements a generic sorted list
'Differs from System.Collections.Generic.SortedList in that it stores a list of objects, not key-value pairs
'and tolerates changes to items' sort orders provided they are removed and re-added
Private Items As List(Of T), Comparer As IComparer(Of T)
Public Sub New(ByVal Comparer As IComparer(Of T))
Items = New List(Of T)
Me.Comparer = Comparer
End Sub
Default Public ReadOnly Property Item(ByVal Index As Integer) As T
Get
If Items.Count > Index Then Return Items(Index) Else Return Nothing
End Get
End Property
Public Sub Add(ByVal Item As T)
If Not Items.Contains(Item) Then
'Binary insertion sort
Dim a As Integer = 0, b As Integer = Items.Count, n As Integer
Dim Curr As Integer = 0
While a <> b And Curr < Misc.GlExcess
n = CInt(Math.Ceiling((b + a) \ 2))
If Comparer.Compare(Item, Items(n)) > 0 Then a = n + 1 Else b = n
Curr = Curr + 1
End While
Items.Insert(a, Item)
End If
End Sub
Public Sub Clear()
Items.Clear()
End Sub
Public ReadOnly Property Contains(ByVal Item As T) As Boolean
Get
Return Items.Contains(Item)
End Get
End Property
Public ReadOnly Property Count() As Integer
Get
Return Items.Count
End Get
End Property
Public Sub Remove(ByVal Item As T)
If Items.Contains(Item) Then Items.Remove(Item)
End Sub
Public Sub RemoveAt(ByVal Index As Integer)
If Items.Count > Index Then Items.RemoveAt(Index)
End Sub
Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
Return Items.GetEnumerator
End Function
Public Function GetEnumerator1() As Collections.IEnumerator Implements Collections.IEnumerable.GetEnumerator
Return Items.GetEnumerator
End Function
End Class