1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Collections.ObjectModel;
|
---|
6 | using System.Collections;
|
---|
7 | using System.Collections.Specialized;
|
---|
8 |
|
---|
9 | namespace Microsoft.Research.DynamicDataDisplay.Common
|
---|
10 | {
|
---|
11 | public class RingArray<T> : INotifyCollectionChanged, IList<T>
|
---|
12 | {
|
---|
13 | public RingArray(int capacity)
|
---|
14 | {
|
---|
15 | this.capacity = capacity;
|
---|
16 | array = new T[capacity];
|
---|
17 | }
|
---|
18 |
|
---|
19 | public void Add(T item)
|
---|
20 | {
|
---|
21 | int index = (startIndex + count) % capacity;
|
---|
22 | if (startIndex + count >= capacity)
|
---|
23 | {
|
---|
24 | startIndex++;
|
---|
25 | }
|
---|
26 | else
|
---|
27 | {
|
---|
28 | count++;
|
---|
29 | }
|
---|
30 |
|
---|
31 | array[index] = item;
|
---|
32 |
|
---|
33 | CollectionChanged.Raise(this);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public T this[int index]
|
---|
37 | {
|
---|
38 | get { return array[(startIndex + index) % capacity]; }
|
---|
39 | set
|
---|
40 | {
|
---|
41 | array[(startIndex + index) % capacity] = value;
|
---|
42 | CollectionChanged.Raise(this);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void Clear()
|
---|
47 | {
|
---|
48 | count = 0;
|
---|
49 | startIndex = 0;
|
---|
50 | array = new T[capacity];
|
---|
51 | }
|
---|
52 |
|
---|
53 | public IEnumerator<T> GetEnumerator()
|
---|
54 | {
|
---|
55 | for (int i = 0; i < count; i++)
|
---|
56 | {
|
---|
57 | yield return this[i];
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private int count;
|
---|
62 | public int Count
|
---|
63 | {
|
---|
64 | get { return count; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | private T[] array;
|
---|
68 |
|
---|
69 | private int capacity;
|
---|
70 | public int Capacity
|
---|
71 | {
|
---|
72 | get { return capacity; }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private int startIndex = 0;
|
---|
76 |
|
---|
77 | #region INotifyCollectionChanged Members
|
---|
78 |
|
---|
79 | public event NotifyCollectionChangedEventHandler CollectionChanged;
|
---|
80 |
|
---|
81 | #endregion
|
---|
82 |
|
---|
83 | #region IList<T> Members
|
---|
84 |
|
---|
85 | public int IndexOf(T item)
|
---|
86 | {
|
---|
87 | int index = Array.IndexOf(array, item);
|
---|
88 |
|
---|
89 | if (index == -1)
|
---|
90 | return -1;
|
---|
91 |
|
---|
92 | return (index - startIndex + count) % capacity;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void Insert(int index, T item)
|
---|
96 | {
|
---|
97 | throw new NotImplementedException();
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void RemoveAt(int index)
|
---|
101 | {
|
---|
102 | throw new NotImplementedException();
|
---|
103 | }
|
---|
104 |
|
---|
105 | #endregion
|
---|
106 |
|
---|
107 | #region ICollection<T> Members
|
---|
108 |
|
---|
109 | public bool Contains(T item)
|
---|
110 | {
|
---|
111 | return Array.IndexOf(array, item) > -1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void CopyTo(T[] array, int arrayIndex)
|
---|
115 | {
|
---|
116 | throw new NotImplementedException();
|
---|
117 | }
|
---|
118 |
|
---|
119 | public bool IsReadOnly
|
---|
120 | {
|
---|
121 | get { throw new NotImplementedException(); }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public bool Remove(T item)
|
---|
125 | {
|
---|
126 | throw new NotImplementedException();
|
---|
127 | }
|
---|
128 |
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | #region IEnumerable Members
|
---|
132 |
|
---|
133 | IEnumerator IEnumerable.GetEnumerator()
|
---|
134 | {
|
---|
135 | return GetEnumerator();
|
---|
136 | }
|
---|
137 |
|
---|
138 | #endregion
|
---|
139 | }
|
---|
140 | }
|
---|