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

Last change on this file since 579 was 579, checked in by dai, 16 years ago
  • ForeachがIDisposableを実装したIEnumerator<T>を取り扱うように変更
  • #120(列挙体のUnicode対応)の修正
File size: 2.5 KB
Line 
1
2Namespace System
3Namespace Collections
4Namespace Generic
5
6Class 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
17Public
18 Sub Stack ()
19 This.items = GC_malloc( 1 )
20 End Sub
21
22 Sub Stack ( capacity As Long )
23 This.items = GC_malloc( 1 )
24 If capacity > 0 Then
25 This.size = capacity
26 Realloc(capacity)
27 End If
28 End Sub
29
30 /*!
31 @brief Stackに格納されている要素の数を取得
32 @author NoWest
33 @date 2008/07/20
34 */
35 Override Function Count () As Long
36 Return This.count
37 End Function
38
39Public
40 /*!
41 @brief Stackからすべての要素を削除する
42 @author NoWest
43 @date 2008/07/20
44 */
45 Sub Clear ()
46 This.count = 0
47 End Sub
48
49 'ある要素が Stack 内に存在するかどうかを判断
50/* Override Function Contains ( item As T ) As Boolean
51 TODO
52 End Function*/
53
54 '既存の 1 次元の Array に Stack をコピーします。コピー操作は、配列の指定したインデックスから始まります。
55/* CopyTo*/
56
57 /*!
58 @brief IEnumeratorインターフェイスを取得する
59 @author NoWest
60 @date 2008/07/20
61 @return IEnumeratorインターフェイス
62 */
63 Override Function GetEnumerator () As IEnumerator<T>
64 Return New Detail.PointerEnumerator(items, count)
65 End Function
66
67 /*!
68 @brief Stackの先頭にあるオブジェクトを削除せずに返します
69 @author NoWest
70 @date 2008/07/20
71 */
72 Function Peek () As T
73 Peek = This.items[ 0 ]
74 End Function
75
76 /*!
77 @brief Stackの先頭にあるオブジェクトを削除し、返します
78 @author NoWest
79 @date 2008/07/20
80 */
81 Function Pop () As T
82 Pop = This.items[ 0 ]
83 memmove( items, items + SizeOf(T), (This.count - 1)*SizeOf(T) )
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 If This.size < (This.count + 1) Then
94 This.size++
95 Realloc( This.size )
96 End If
97 memmove( items + SizeOf(T), items, This.count * SizeOf(T) )
98 items[ 0 ] = item
99 This.count++
100 End Sub
101
102 'Stack を新しい配列にコピーします。
103/* ToArray*/
104
105 /*!
106 @brief 実際の要素数が現在の容量の 90% 未満の場合は、容量をその数に設定します
107 @author NoWest
108 @date 2008/07/20
109 */
110 Sub TrimExcess ()
111 Dim pmemobj = _System_pGC->GetMemoryObjectPtr( items )
112 If ( This.size ) * 0.9 > This.count Then
113 This.size = This.count
114 Realloc( This.size )
115 End If
116 End Sub
117End Class
118
119End Namespace
120End Namespace
121End Namespace
Note: See TracBrowser for help on using the repository browser.