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