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

Last change on this file since 558 was 558, checked in by NoWest, 16 years ago

Queueジェネリッククラスを追加
Listジェネリッククラスの内部でメモリの再確保が頻繁に行われないように改良

File size: 3.0 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[ 0 ]
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[ 0 ]
84 memmove( items, items + SizeOf(T), (This.count - 1)*SizeOf(T) )
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 )
94 If This.size < (This.count + 1) Then
95 This.size++
96 Realloc( This.size )
97 End If
98 memmove( items + SizeOf(T), items, This.count * SizeOf(T) )
99 items[ 0 ] = item
100 This.count++
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
119
120 '------------------------
121 ' IEnumeratorの実装
122 '------------------------
123 currentIndexForEnumerator As Long
124
125 Override Function Current () As T
126 If This.currentIndexForEnumerator = -1 Then
127 ' MoveNextメソッドがReset後、一度も呼び出されなかった
128 Return Nothing
129 End If
130 Return This.items[ This.currentIndexForEnumerator ]
131 End Function
132
133 Override Function MoveNext() As Boolean
134 If This.currentIndexForEnumerator + 1 >= This.count Then
135 ' 上限に達した
136 Return False
137 End If
138
139 This.currentIndexForEnumerator ++
140 Return True
141 End Function
142
143 Override Sub Reset ()
144 This.currentIndexForEnumerator = -1
145 End Sub
146End Class
147
148End Namespace
149End Namespace
150End Namespace
Note: See TracBrowser for help on using the repository browser.