source: trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Queue.ab@ 560

Last change on this file since 560 was 560, checked in by dai, 16 years ago

#183への対応。コンストラクタ、デストラクタが直接呼び出された場合はエラーとして扱うようにした。
(64bit版は後ほどコミットします)

File size: 3.0 KB
Line 
1
2Namespace System
3Namespace Collections
4Namespace Generic
5
6Class Queue<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 Queue ()
19 This.items = GC_malloc( 1 )
20 Reset()
21 End Sub
22
23 Sub Queue ( 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 Queueに格納されている要素の数を取得
34 @author NoWest
35 @date 2008/07/20
36 */
37 Override Function Count () As Long
38 Return This.count
39 End Function
40
41Public
42 /*!
43 @brief Queueからすべての要素を削除する
44 @author NoWest
45 @date 2008/07/20
46 */
47 Override Sub Clear ()
48 This.count = 0
49 End Sub
50
51 'ある要素が Queue内に存在するかどうかを判断
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 Queueの先頭にあるオブジェクトを削除し、返します
71 @author NoWest
72 @date 2008/07/20
73 */
74 Function Dequeue () As T
75 Dequeue = This.items[ 0 ]
76 memmove( items, items + SizeOf(T), (This.count - 1)*SizeOf(T) )
77 This.count--
78 End Function
79
80 /*!
81 @brief Queueの末尾にオブジェクトを追加します
82 @author NoWest
83 @date 2008/07/20
84 */
85 Sub Enqueue ( item As T )
86 If This.size < (This.count + 1) Then
87 This.size++
88 Realloc( This.size )
89 End If
90 This.items[ This.count ] = item
91 This.count++
92 End Sub
93
94 /*!
95 @brief Queueの先頭にあるオブジェクトを削除せずに返します
96 @author NoWest
97 @date 2008/07/20
98 */
99 Function Peek () As T
100 Peek = This.items[ 0 ]
101 End Function
102
103 'Queueを新しい配列にコピーします。
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.size 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.