とりあえず(?)作ってみた。(手荷物一時預かり 編)その3。

東方算程譚:手荷物一時預かり
まずはsetの用意。これが間違ってたらアウトですなぁ。

Imports System
Imports System.Collections.Generic


Public Class [Set](Of T As System.IComparable)


Private _list As SortedList(Of T, Boolean)


Public ReadOnly Property Empty() As Boolean
Get
Return Me._list.Count = 0
End Get
End Property


Public Sub New()

Me._list = New SortedList(Of T, Boolean)()

End Sub


Public Function Find(ByVal key As T) As Iterator

Dim result As New Iterator(Iterator.Mode.Normal, Me._list)

Dim index As Integer = Me._list.IndexOfKey(key)

If index = -1 Then

Return Me.End

Else

result += (index + 1)

End If

Return result

End Function


Public Function Insert(ByVal t As T) As Boolean

Try

Me._list.Add(t, False)

Catch ex As ArgumentException

Return False

End Try

Return True

End Function


Public Sub Clear()

Me._list.Clear()

End Sub


Public Function Erace(ByVal value As T) As Boolean

Return Me._list.Remove(value)

End Function


Public Function Begin() As Iterator

Dim result As New Iterator(Iterator.Mode.Normal, Me._list)

result += 1

Return result

End Function


Public Function [End]() As Iterator

Dim result As New Iterator(Iterator.Mode.Normal, Me._list)

result += Me._list.Count

result += 1

Return result

End Function


Public Function RBegin() As Iterator

Dim result As New Iterator(Iterator.Mode.Reverse, Me._list)

result += 1

Return result

End Function


Public Function REnd() As Iterator

Dim result As New Iterator(Iterator.Mode.Reverse, Me._list)

result += Me._list.Count

result += 1

Return result

End Function


Public Class Iterator

Enum Mode As Integer

Normal = 1
Reverse = -1

End Enum


Private _mode As Mode

Private _list As SortedList(Of T, Boolean)

Private _index As Integer

Public Property Index() As Integer
Get
Return _index
End Get
Set(ByVal value As Integer)
_index = value
If _index < 0 Then
_index = -1
End If
If _index > Me._list.Count - 1 Then
_index = Me._list.Count
End If
End Set
End Property

Public Sub New()

Me._mode = [Set](Of T).Iterator.Mode.Normal
Me._list = New SortedList(Of T, Boolean)

End Sub


Public Sub New(ByVal mode As Mode, ByVal list As SortedList(Of T, Boolean))

Me._mode = mode
Me._list = list

Reset()

End Sub


Public Sub Reset()

If Me._mode = Iterator.Mode.Normal Then

Index = -1

Else

Index = Me._list.Count

End If

End Sub


Public Property Current() As T
Get
Return Me._list.Keys(Index)
End Get
Set(ByVal value As T)
Me._list.Add(value, False)
End Set
End Property


Public Overloads Sub Resize(ByVal n As Integer)

Me.Resize(n, Nothing)

End Sub


Public Overloads Sub Resize(ByVal n As Integer, ByVal t As T)

If Me._list.Count >= n Then

Exit Sub

End If

For i As Integer = 0 To (n - Me._list.Count) - 1

Me._list.Add(t, False)

Next

End Sub



Public Function MoveNext() As Boolean

If Me._mode = Iterator.Mode.Normal Then

If Index = Me._list.Count - 1 Then

Return False

End If

Else

If Index = 0 Then

Return False

End If

End If

Index += Me._mode

Return True

End Function


Public Shared Operator +(ByVal target As Iterator, ByVal n As Integer) As Iterator

target.Index += (target._mode * n)

Return target

End Operator


Public Shared Operator -(ByVal target As Iterator, ByVal n As Integer) As Iterator

target.Index -= (target._mode * n)

Return target

End Operator


End Class


End Class