Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CircularQueue.cs « arrays « C5 « Mono.C5 « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b33b5d7077df08b76eabfaab0f023507cee9134f (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#if NET_2_0
/*
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/

using System;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
  /// <summary>
  /// 
  /// </summary>
  /// <typeparam name="T"></typeparam>
  public class CircularQueue<T> : SequencedBase<T>, IQueue<T>, IStack<T>
  {
    #region Fields
    /*
        Invariant: the itemes in the queue ar the elements from front upwards, 
        possibly wrapping around at the end of array, to back.

        if front<=back then size = back - front + 1;
        else size = array.Length + back - front + 1;

        */
    int front, back;
    /// <summary>
    /// The internal container array is doubled when necessary, but never shrinked.
    /// </summary>
    T[] array;
    bool forwards = true, original = true;
    #endregion

    #region Events

    /// <summary>
    /// 
    /// </summary>
    /// <value></value>
    public override EventTypeEnum ListenableEvents { get { return EventTypeEnum.Basic; } }

    #endregion

    #region Util
    void expand()
    {
      int newlength = 2 * array.Length;
      T[] newarray = new T[newlength];

      if (front <= back)
        Array.Copy(array, front, newarray, 0, size);
      else
      {
        int half = array.Length - front;
        Array.Copy(array, front, newarray, 0, half);
        Array.Copy(array, 0, newarray, half, size - half);
      }

      front = 0;
      back = size;
      array = newarray;
    }

    #endregion

    #region Constructors

    /// <summary>
    /// 
    /// </summary>
    public CircularQueue() : this(8) { }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="capacity"></param>
    public CircularQueue(int capacity)
      : base(EqualityComparer<T>.Default)
    {
      int newlength = 8;
      while (newlength < capacity) newlength *= 2;
      array = new T[newlength];
    }

    #endregion

    #region IQueue<T> Members
    /// <summary>
    /// 
    /// </summary>
    /// <value></value>
    public virtual bool AllowsDuplicates { get { return true; } }

    /// <summary>
    /// Get the i'th item in the queue. The front of the queue is at index 0.
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public virtual T this[int i]
    {
      get
      {
        if (i < 0 || i >= size)
          throw new IndexOutOfRangeException();
        i = i + front;
        return array[i >= size ? i - size : i];
      }
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="item"></param>
    [Tested]
    public virtual void Enqueue(T item)
    {
      if (!original)
        throw new ReadOnlyCollectionException();
      stamp++;
      if (size == array.Length - 1) expand();
      size++;
      int oldback = back++;
      if (back == array.Length) back = 0;
      array[oldback] = item;
      if (ActiveEvents != 0)
        raiseForAdd(item);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [Tested]
    public virtual T Dequeue()
    {
      if (!original)
        throw new ReadOnlyCollectionException("Object is a non-updatable clone");
      stamp++;
      if (size == 0)
        throw new NoSuchItemException();
      size--;
      int oldfront = front++;
      if (front == array.Length) front = 0;
      T retval = array[oldfront];
      array[oldfront] = default(T);
      if (ActiveEvents != 0)
        raiseForRemove(retval);
      return retval;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="item"></param>
    public void Push(T item) //== Enqueue
    {
      if (!original)
        throw new ReadOnlyCollectionException();
      stamp++;
      if (size == array.Length - 1) expand();
      size++;
      int oldback = back++;
      if (back == array.Length) back = 0;
      array[oldback] = item;
      if (ActiveEvents != 0)
        raiseForAdd(item);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public T Pop()
    {
      if (!original)
        throw new ReadOnlyCollectionException("Object is a non-updatable clone");
      stamp++;
      if (size == 0)
        throw new NoSuchItemException();
      size--;
      back--;
      if (back == -1) back = array.Length - 1;
      T retval = array[back];
      array[back] = default(T);
      if (ActiveEvents != 0)
        raiseForRemove(retval);
      return retval;
    }
    #endregion

    #region ICollectionValue<T> Members

    //TODO: implement these with Array.Copy instead of relying on XxxBase:
    /*
        public void CopyTo(T[] a, int i)
        {
        }

        public T[] ToArray()
        {
        }*/

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [Tested]
    public override T Choose()
    {
      if (size == 0)
        throw new NoSuchItemException();
      return array[front];
    }

    #endregion

    #region IEnumerable<T> Members

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override SCG.IEnumerator<T> GetEnumerator()
    {
      int stamp = this.stamp;
      if (forwards)
      {
        int position = front;
        int end = front <= back ? back : array.Length;
        while (position < end)
        {
          if (stamp != this.stamp)
            throw new CollectionModifiedException();
          yield return array[position++];
        }
        if (front > back)
        {
          position = 0;
          while (position < back)
          {
            if (stamp != this.stamp)
              throw new CollectionModifiedException();
            yield return array[position++];
          }
        }
      }
      else
      {
        int position = back - 1;
        int end = front <= back ? front : 0;
        while (position >= end)
        {
          if (stamp != this.stamp)
            throw new CollectionModifiedException();
          yield return array[position--];
        }
        if (front > back)
        {
          position = array.Length - 1;
          while (position >= front)
          {
            if (stamp != this.stamp)
              throw new CollectionModifiedException();
            yield return array[position--];
          }
        }
      }
    }

    #endregion

    #region IDirectedCollectionValue<T> Members

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override IDirectedCollectionValue<T> Backwards()
    {
      CircularQueue<T> retval = (CircularQueue<T>)MemberwiseClone();
      retval.original = false;
      retval.forwards = !forwards;
      return retval;
    }

    #endregion

    #region IDirectedEnumerable<T> Members

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    IDirectedEnumerable<T> IDirectedEnumerable<T>.Backwards()
    {
      return Backwards();
    }

    #endregion

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public virtual bool Check()
    {
      if (front < 0 || front >= array.Length || back < 0 || back >= array.Length ||
          (front <= back && size != back - front) || (front > back && size != array.Length + back - front))
      {
        Console.WriteLine("Bad combination of (front,back,size,array.Length): ({0},{1},{2},{3})",
            front, back, size, array.Length);
        return false;
      }
      return true;
    }
  }
}
#endif