Advertisement
Guest User

SortedCollection

a guest
May 30th, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.07 KB | None | 0 0
  1. Imports System.Collections
  2. Imports System.Collections.ObjectModel
  3. Imports System.Collections.Specialized
  4.  
  5. Public Class SortedCollection(Of TKey, TItem)
  6.     Implements IEnumerable(Of TItem)
  7.  
  8. #Region "Properties"
  9.     Default Public ReadOnly Property Item(key As TKey) As TItem
  10.         Get
  11.             Dim index = Me.Keys.IndexOf(key)
  12.             Return Me.Items(index)
  13.         End Get
  14.     End Property
  15.  
  16.     Private _Keys As List(Of TKey)
  17.     Private ReadOnly Property Keys As List(Of TKey)
  18.         Get
  19.             If _Keys Is Nothing Then
  20.                 _Keys = New List(Of TKey)()
  21.             End If
  22.  
  23.             Return _Keys
  24.         End Get
  25.     End Property
  26.  
  27.     Private _Items As List(Of TItem)
  28.     Private ReadOnly Property Items As List(Of TItem)
  29.         Get
  30.             If _Items Is Nothing Then
  31.                 _Items = New List(Of TItem)()
  32.             End If
  33.  
  34.             Return _Items
  35.         End Get
  36.     End Property
  37.  
  38.     Public ReadOnly Property Count As Integer
  39.         Get
  40.             Return Me.Items.Count
  41.         End Get
  42.     End Property
  43. #End Region
  44.  
  45. #Region "Methods"
  46.     Public Overridable Sub Add(key As TKey, item As TItem)
  47.         Me.Keys.Add(key)
  48.         Me.Keys.Sort()
  49.         Dim index = Me.Keys.IndexOf(key)
  50.         Me.Items.Insert(index, item)
  51.     End Sub
  52.  
  53.     Public Overridable Sub Remove(key As TKey)
  54.         Dim index = Me.Keys.IndexOf(key)
  55.         Me.Items.RemoveAt(index)
  56.         Me.Keys.RemoveAt(index)
  57.     End Sub
  58.  
  59.     Public Overridable Sub Clear()
  60.         Me.Items.Clear()
  61.         Me.Keys.Clear()
  62.     End Sub
  63.  
  64.     Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
  65.         Return Me.GetEnumerator()
  66.     End Function
  67.  
  68.     Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of TItem) Implements System.Collections.Generic.IEnumerable(Of TItem).GetEnumerator
  69.         Return Me.Items.GetEnumerator()
  70.     End Function
  71.  
  72.     Public Function IndexOfKey(key As TKey) As Integer
  73.         Return Me.Keys.IndexOf(key)
  74.     End Function
  75. #End Region
  76.  
  77. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement