source: trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Stack.ab@ 557

Last change on this file since 557 was 557, checked in by NoWest, 16 years ago
File size: 2.9 KB
Line 
1
2Namespace System
3Namespace Collections
4Namespace Generic
5
6Class Stack<T>
7 Inherits 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
17Public
18 Sub Stack ()
19 This.items = GC_malloc( 1 )
20 Reset()
21 End Sub
22
23 Sub Stack ( capacity As Long )
24 Stack()
25 If capacity > 0 Then
26 This.size = capacity
27 Realloc(capacity)
28 End If
29 End Sub
30
31 /*!
32 @brief Stackに格納されている要素の数を取得
33 @author NoWest
34 @date 2008/07/20
35 */
36 Override Function Count () As Long
37 Return This.count
38 End Function
39
40Public
41 /*!
42 @brief Stackからすべての要素を削除する
43 @author NoWest
44 @date 2008/07/20
45 */
46 Override Sub Clear ()
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>
65 Return This
66 End Function
67
68 /*!
69 @brief Stackの先頭にあるオブジェクトを削除せずに返します
70 @author NoWest
71 @date 2008/07/20
72 */
73 Function Peek () As T
74 Peek = This.items[ This.count -1 ]
75 End Function
76
77 /*!
78 @brief Stackの先頭にあるオブジェクトを削除し、返します
79 @author NoWest
80 @date 2008/07/20
81 */
82 Function Pop () As T
83 Pop = This.items[ This.count -1 ]
84 This.count--
85 End Function
86
87 /*!
88 @brief Stackの先頭にオブジェクトを挿入します
89 @author NoWest
90 @date 2008/07/20
91 */
92 Sub Push ( item As T )
93 This.count++
94 If This.size < This.count Then
95 This.size++
96 Realloc( This.size )
97 End If
98 This.items[ This.count -1 ] = item
99 End Sub
100
101 'Stack を新しい配列にコピーします。
102/* ToArray*/
103
104 /*!
105 @brief 実際の要素数が現在の容量の 90% 未満の場合は、容量をその数に設定します
106 @author NoWest
107 @date 2008/07/20
108 */
109 Sub TrimExcess ()
110 Dim pmemobj = _System_pGC->GetMemoryObjectPtr( items )
111 If ( This.size ) * 0.9 > This.count Then
112 This.size = This.count
113 Realloc( This.size )
114 End If
115 End Sub
116
117
118 '------------------------
119 ' IEnumeratorの実装
120 '------------------------
121 currentIndexForEnumerator As Long
122
123 Override Function Current () As T
124 If This.currentIndexForEnumerator = -1 Then
125 ' MoveNextメソッドがReset後、一度も呼び出されなかった
126 Return Nothing
127 End If
128 Return This.items[ This.currentIndexForEnumerator ]
129 End Function
130
131 Override Function MoveNext() As Boolean
132 If This.currentIndexForEnumerator + 1 >= This.count Then
133 ' 上限に達した
134 Return False
135 End If
136
137 This.currentIndexForEnumerator ++
138 Return True
139 End Function
140
141 Override Sub Reset ()
142 This.currentIndexForEnumerator = -1
143 End Sub
144End Class
145
146End Namespace
147End Namespace
148End Namespace
Note: See TracBrowser for help on using the repository browser.