source: trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Queue.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 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.Queue()
25 If capacity > 0 Then
26 This.size = capacity
27 Realloc(capacity)
28 End If
29 End Sub
30
31 /*!
32 @brief Queueに格納されている要素の数を取得
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 Queueからすべての要素を削除する
43 @author NoWest
44 @date 2008/07/20
45 */
46 Override Sub Clear ()
47 This.count = 0
48 End Sub
49
50 'ある要素が Queue内に存在するかどうかを判断
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 Queueの先頭にあるオブジェクトを削除し、返します
70 @author NoWest
71 @date 2008/07/20
72 */
73 Function Dequeue () As T
74 Dequeue = This.items[ 0 ]
75 memmove( items, items + SizeOf(T), (This.count - 1)*SizeOf(T) )
76 This.count--
77 End Function
78
79 /*!
80 @brief Queueの末尾にオブジェクトを追加します
81 @author NoWest
82 @date 2008/07/20
83 */
84 Sub Enqueue ( item As T )
85 If This.size < (This.count + 1) Then
86 This.size++
87 Realloc( This.size )
88 End If
89 This.items[ This.count ] = item
90 This.count++
91 End Sub
92
93 /*!
94 @brief Queueの先頭にあるオブジェクトを削除せずに返します
95 @author NoWest
96 @date 2008/07/20
97 */
98 Function Peek () As T
99 Peek = This.items[ 0 ]
100 End Function
101
102 'Queueを新しい配列にコピーします。
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
117
118
119 '------------------------
120 ' IEnumeratorの実装
121 '------------------------
122 currentIndexForEnumerator As Long
123
124 Override Function Current () As T
125 If This.currentIndexForEnumerator = -1 Then
126 ' MoveNextメソッドがReset後、一度も呼び出されなかった
127 Return Nothing
128 End If
129 Return This.items[ This.currentIndexForEnumerator ]
130 End Function
131
132 Override Function MoveNext() As Boolean
133 If This.currentIndexForEnumerator + 1 >= This.size Then
134 ' 上限に達した
135 Return False
136 End If
137
138 This.currentIndexForEnumerator ++
139 Return True
140 End Function
141
142 Override Sub Reset ()
143 This.currentIndexForEnumerator = -1
144 End Sub
145End Class
146
147End Namespace
148End Namespace
149End Namespace
Note: See TracBrowser for help on using the repository browser.