[557] | 1 |
|
---|
| 2 | Namespace System
|
---|
| 3 | Namespace Collections
|
---|
| 4 | Namespace Generic
|
---|
| 5 |
|
---|
| 6 | Class Stack<T>
|
---|
[582] | 7 | Implements IEnumerable<T>
|
---|
[557] | 8 |
|
---|
| 9 | items As *T
|
---|
| 10 | size As Long
|
---|
| 11 | count As Long
|
---|
| 12 |
|
---|
| 13 | Sub Realloc ( allocateSize As Long )
|
---|
| 14 | This.items = realloc( This.items, allocateSize * SizeOf(T) )
|
---|
| 15 | End Sub
|
---|
| 16 |
|
---|
[631] | 17 | Sub initialize()
|
---|
| 18 | This.items = GC_malloc(1)
|
---|
| 19 | End Sub
|
---|
[557] | 20 | Public
|
---|
| 21 | Sub Stack ()
|
---|
[631] | 22 | This.initialize()
|
---|
[557] | 23 | End Sub
|
---|
| 24 |
|
---|
| 25 | Sub Stack ( capacity As Long )
|
---|
[631] | 26 | This.initialize()
|
---|
[557] | 27 | If capacity > 0 Then
|
---|
| 28 | This.size = capacity
|
---|
| 29 | Realloc(capacity)
|
---|
| 30 | End If
|
---|
| 31 | End Sub
|
---|
| 32 |
|
---|
[631] | 33 | Sub Stack ( enumerator As IEnumerator<T> )
|
---|
| 34 | This.initialize()
|
---|
| 35 | While enumerator.MoveNext()
|
---|
| 36 | This.Push(enumerator.Current())
|
---|
| 37 | Wend
|
---|
| 38 | End Sub
|
---|
| 39 |
|
---|
[557] | 40 | Public
|
---|
| 41 | /*!
|
---|
| 42 | @brief Stackからすべての要素を削除する
|
---|
| 43 | @author NoWest
|
---|
| 44 | @date 2008/07/20
|
---|
| 45 | */
|
---|
[577] | 46 | Sub Clear ()
|
---|
[557] | 47 | This.count = 0
|
---|
| 48 | End Sub
|
---|
| 49 |
|
---|
| 50 | 'ある要素が Stack 内に存在するかどうかを判断
|
---|
| 51 | /* Override Function Contains ( item As T ) As Boolean
|
---|
| 52 | TODO
|
---|
| 53 | End Function*/
|
---|
| 54 |
|
---|
| 55 | '既存の 1 次元の Array に Stack をコピーします。コピー操作は、配列の指定したインデックスから始まります。
|
---|
| 56 | /* CopyTo*/
|
---|
| 57 |
|
---|
| 58 | /*!
|
---|
| 59 | @brief IEnumeratorインターフェイスを取得する
|
---|
| 60 | @author NoWest
|
---|
| 61 | @date 2008/07/20
|
---|
| 62 | @return IEnumeratorインターフェイス
|
---|
| 63 | */
|
---|
| 64 | Override Function GetEnumerator () As IEnumerator<T>
|
---|
[607] | 65 | Return New Detail.PointerEnumerator<T>(items, count)
|
---|
[557] | 66 | End Function
|
---|
| 67 |
|
---|
| 68 | /*!
|
---|
| 69 | @brief Stackの先頭にあるオブジェクトを削除せずに返します
|
---|
| 70 | @author NoWest
|
---|
| 71 | @date 2008/07/20
|
---|
| 72 | */
|
---|
| 73 | Function Peek () As T
|
---|
[558] | 74 | Peek = This.items[ 0 ]
|
---|
[557] | 75 | End Function
|
---|
| 76 |
|
---|
| 77 | /*!
|
---|
| 78 | @brief Stackの先頭にあるオブジェクトを削除し、返します
|
---|
| 79 | @author NoWest
|
---|
| 80 | @date 2008/07/20
|
---|
| 81 | */
|
---|
| 82 | Function Pop () As T
|
---|
[558] | 83 | Pop = This.items[ 0 ]
|
---|
| 84 | memmove( items, items + SizeOf(T), (This.count - 1)*SizeOf(T) )
|
---|
[557] | 85 | This.count--
|
---|
| 86 | End Function
|
---|
| 87 |
|
---|
| 88 | /*!
|
---|
| 89 | @brief Stackの先頭にオブジェクトを挿入します
|
---|
| 90 | @author NoWest
|
---|
| 91 | @date 2008/07/20
|
---|
| 92 | */
|
---|
| 93 | Sub Push ( item As T )
|
---|
[558] | 94 | If This.size < (This.count + 1) Then
|
---|
[557] | 95 | This.size++
|
---|
| 96 | Realloc( This.size )
|
---|
| 97 | End If
|
---|
[558] | 98 | memmove( items + SizeOf(T), items, This.count * SizeOf(T) )
|
---|
| 99 | items[ 0 ] = item
|
---|
| 100 | This.count++
|
---|
[557] | 101 | End Sub
|
---|
| 102 |
|
---|
| 103 | 'Stack を新しい配列にコピーします。
|
---|
| 104 | /* ToArray*/
|
---|
| 105 |
|
---|
| 106 | /*!
|
---|
| 107 | @brief 実際の要素数が現在の容量の 90% 未満の場合は、容量をその数に設定します
|
---|
| 108 | @author NoWest
|
---|
| 109 | @date 2008/07/20
|
---|
| 110 | */
|
---|
| 111 | Sub TrimExcess ()
|
---|
| 112 | Dim pmemobj = _System_pGC->GetMemoryObjectPtr( items )
|
---|
| 113 | If ( This.size ) * 0.9 > This.count Then
|
---|
| 114 | This.size = This.count
|
---|
| 115 | Realloc( This.size )
|
---|
| 116 | End If
|
---|
| 117 | End Sub
|
---|
| 118 | End Class
|
---|
| 119 |
|
---|
| 120 | End Namespace
|
---|
| 121 | End Namespace
|
---|
| 122 | End Namespace
|
---|