-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircleCollection.cs
203 lines (160 loc) · 5.36 KB
/
CircleCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Text;
namespace LumiSoft.Net
{
/// <summary>
/// Circle collection. Elements will be circled clockwise.
/// </summary>
public class CircleCollection<T>
{
private List<T> m_pItems = null;
private int m_Index = 0;
/// <summary>
/// Default constructor.
/// </summary>
public CircleCollection()
{
m_pItems = new List<T>();
}
#region methd Add
/// <summary>
/// Adds specified items to the collection.
/// </summary>
/// <param name="items">Items to add.</param>
/// <exception cref="ArgumentNullException">Is raised when <b>items</b> is null.</exception>
public void Add(T[] items)
{
if(items == null){
throw new ArgumentNullException("items");
}
foreach(T item in items){
Add(item);
}
}
/// <summary>
/// Adds specified item to the collection.
/// </summary>
/// <param name="item">Item to add.</param>
/// <exception cref="ArgumentNullException">Is raised when <b>item</b> is null.</exception>
public void Add(T item)
{
if(item == null){
throw new ArgumentNullException("item");
}
m_pItems.Add(item);
// Reset loop index.
m_Index = 0;
}
#endregion
#region method Remove
/// <summary>
/// Removes specified item from the collection.
/// </summary>
/// <param name="item">Item to remove.</param>
/// <exception cref="ArgumentNullException">Is raised when <b>item</b> is null.</exception>
public void Remove(T item)
{
if(item == null){
throw new ArgumentNullException("item");
}
m_pItems.Remove(item);
// Reset loop index.
m_Index = 0;
}
#endregion
#region method Clear
/// <summary>
/// Clears all items from collection.
/// </summary>
public void Clear()
{
m_pItems.Clear();
// Reset loop index.
m_Index = 0;
}
#endregion
#region method Contains
/// <summary>
/// Gets if the collection contain the specified item.
/// </summary>
/// <param name="item">Item to check.</param>
/// <returns>Returns true if the collection contain the specified item, otherwise false.</returns>
public bool Contains(T item)
{
return m_pItems.Contains(item);
}
#endregion
#region method Next
/// <summary>
/// Gets next item from the collection. This method is thread-safe.
/// </summary>
/// <exception cref="InvalidOperationException">Is raised when thre is no items in the collection.</exception>
public T Next()
{
if(m_pItems.Count == 0){
throw new InvalidOperationException("There is no items in the collection.");
}
lock(m_pItems){
T item = m_pItems[m_Index];
m_Index++;
if(m_Index >= m_pItems.Count){
m_Index = 0;
}
return item;
}
}
#endregion
#region method ToArray
/// <summary>
/// Copies all elements to new array, all elements will be in order they added. This method is thread-safe.
/// </summary>
/// <returns>Returns elements in a new array.</returns>
public T[] ToArray()
{
lock(m_pItems){
return m_pItems.ToArray();
}
}
#endregion
#region method ToCurrentOrderArray
/// <summary>
/// Copies all elements to new array, all elements will be in current circle order. This method is thread-safe.
/// </summary>
/// <returns>Returns elements in a new array.</returns>
public T[] ToCurrentOrderArray()
{
lock(m_pItems){
int index = m_Index;
T[] retVal = new T[m_pItems.Count];
for(int i=0;i<m_pItems.Count;i++){
retVal[i] = m_pItems[index];
index++;
if(index >= m_pItems.Count){
index = 0;
}
}
return retVal;
}
}
#endregion
#region Properties Implementation
/// <summary>
/// Gets number of items in the collection.
/// </summary>
public int Count
{
get{ return m_pItems.Count; }
}
/// <summary>
/// Gets item at the specified index.
/// </summary>
/// <param name="index">Item zero based index.</param>
/// <returns>Returns item at the specified index.</returns>
public T this[int index]
{
get{ return m_pItems[index]; }
}
#endregion
}
}