1 |
|
---|
2 | Namespace System
|
---|
3 | Namespace Collections
|
---|
4 | Namespace Generic
|
---|
5 |
|
---|
6 | Class Stack<T>
|
---|
7 | Implements IEnumerable<T>, IEnumerator<T>, System.Collections.ICollection
|
---|
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 |
|
---|
17 | Public
|
---|
18 | Sub Stack ()
|
---|
19 | This.items = GC_malloc( 1 )
|
---|
20 | Reset()
|
---|
21 | End Sub
|
---|
22 |
|
---|
23 | Sub Stack ( capacity As Long )
|
---|
24 | This.items = GC_malloc( 1 )
|
---|
25 | Reset()
|
---|
26 | If capacity > 0 Then
|
---|
27 | This.size = capacity
|
---|
28 | Realloc(capacity)
|
---|
29 | End If
|
---|
30 | End Sub
|
---|
31 |
|
---|
32 | /*!
|
---|
33 | @brief Stackに格納されている要素の数を取得
|
---|
34 | @author NoWest
|
---|
35 | @date 2008/07/20
|
---|
36 | */
|
---|
37 | Override Function Count () As Long
|
---|
38 | Return This.count
|
---|
39 | End Function
|
---|
40 |
|
---|
41 | Public
|
---|
42 | /*!
|
---|
43 | @brief Stackからすべての要素を削除する
|
---|
44 | @author NoWest
|
---|
45 | @date 2008/07/20
|
---|
46 | */
|
---|
47 | Sub Clear ()
|
---|
48 | This.count = 0
|
---|
49 | End Sub
|
---|
50 |
|
---|
51 | 'ある要素が Stack 内に存在するかどうかを判断
|
---|
52 | /* Override Function Contains ( item As T ) As Boolean
|
---|
53 | TODO
|
---|
54 | End Function*/
|
---|
55 |
|
---|
56 | '既存の 1 次元の Array に Stack をコピーします。コピー操作は、配列の指定したインデックスから始まります。
|
---|
57 | /* CopyTo*/
|
---|
58 |
|
---|
59 | /*!
|
---|
60 | @brief IEnumeratorインターフェイスを取得する
|
---|
61 | @author NoWest
|
---|
62 | @date 2008/07/20
|
---|
63 | @return IEnumeratorインターフェイス
|
---|
64 | */
|
---|
65 | Override Function GetEnumerator () As IEnumerator<T>
|
---|
66 | Return This
|
---|
67 | End Function
|
---|
68 |
|
---|
69 | /*!
|
---|
70 | @brief Stackの先頭にあるオブジェクトを削除せずに返します
|
---|
71 | @author NoWest
|
---|
72 | @date 2008/07/20
|
---|
73 | */
|
---|
74 | Function Peek () As T
|
---|
75 | Peek = This.items[ 0 ]
|
---|
76 | End Function
|
---|
77 |
|
---|
78 | /*!
|
---|
79 | @brief Stackの先頭にあるオブジェクトを削除し、返します
|
---|
80 | @author NoWest
|
---|
81 | @date 2008/07/20
|
---|
82 | */
|
---|
83 | Function Pop () As T
|
---|
84 | Pop = This.items[ 0 ]
|
---|
85 | memmove( items, items + SizeOf(T), (This.count - 1)*SizeOf(T) )
|
---|
86 | This.count--
|
---|
87 | End Function
|
---|
88 |
|
---|
89 | /*!
|
---|
90 | @brief Stackの先頭にオブジェクトを挿入します
|
---|
91 | @author NoWest
|
---|
92 | @date 2008/07/20
|
---|
93 | */
|
---|
94 | Sub Push ( item As T )
|
---|
95 | If This.size < (This.count + 1) Then
|
---|
96 | This.size++
|
---|
97 | Realloc( This.size )
|
---|
98 | End If
|
---|
99 | memmove( items + SizeOf(T), items, This.count * SizeOf(T) )
|
---|
100 | items[ 0 ] = item
|
---|
101 | This.count++
|
---|
102 | End Sub
|
---|
103 |
|
---|
104 | 'Stack を新しい配列にコピーします。
|
---|
105 | /* ToArray*/
|
---|
106 |
|
---|
107 | /*!
|
---|
108 | @brief 実際の要素数が現在の容量の 90% 未満の場合は、容量をその数に設定します
|
---|
109 | @author NoWest
|
---|
110 | @date 2008/07/20
|
---|
111 | */
|
---|
112 | Sub TrimExcess ()
|
---|
113 | Dim pmemobj = _System_pGC->GetMemoryObjectPtr( items )
|
---|
114 | If ( This.size ) * 0.9 > This.count Then
|
---|
115 | This.size = This.count
|
---|
116 | Realloc( This.size )
|
---|
117 | End If
|
---|
118 | End Sub
|
---|
119 |
|
---|
120 |
|
---|
121 | '------------------------
|
---|
122 | ' IEnumeratorの実装
|
---|
123 | '------------------------
|
---|
124 | currentIndexForEnumerator As Long
|
---|
125 |
|
---|
126 | Override Function Current () As T
|
---|
127 | If This.currentIndexForEnumerator = -1 Then
|
---|
128 | ' MoveNextメソッドがReset後、一度も呼び出されなかった
|
---|
129 | Return Nothing
|
---|
130 | End If
|
---|
131 | Return This.items[ This.currentIndexForEnumerator ]
|
---|
132 | End Function
|
---|
133 |
|
---|
134 | Override Function MoveNext() As Boolean
|
---|
135 | If This.currentIndexForEnumerator + 1 >= This.count Then
|
---|
136 | ' 上限に達した
|
---|
137 | Return False
|
---|
138 | End If
|
---|
139 |
|
---|
140 | This.currentIndexForEnumerator ++
|
---|
141 | Return True
|
---|
142 | End Function
|
---|
143 |
|
---|
144 | Override Sub Reset ()
|
---|
145 | This.currentIndexForEnumerator = -1
|
---|
146 | End Sub
|
---|
147 | End Class
|
---|
148 |
|
---|
149 | End Namespace
|
---|
150 | End Namespace
|
---|
151 | End Namespace
|
---|