1 /* System.Collections.Specialized.StringCollection.cs
\r
3 * John Barnette (jbarn@httcb.net)
\r
4 * Sean MacIsaac (macisaac@ximian.com)
\r
6 * Copyright (C) 2001 John Barnette
\r
7 * (C) Ximian, Inc. http://www.ximian.com
\r
10 * I bet Microsoft uses ArrayList as a backing store for this; I wonder what
\r
11 * the performance difference will be.
\r
16 namespace System.Collections.Specialized {
\r
17 public class StringCollection : IList, ICollection, IEnumerable {
\r
18 private static int InitialCapacity = 11;
\r
19 private static float CapacityMultiplier = 2.0f;
\r
22 private int modCount;
\r
24 private string[] entries;
\r
26 // Public Constructor
\r
27 public StringCollection() {
\r
28 entries = new string[InitialCapacity];
\r
33 // Public Instance Properties
\r
35 get { return count; }
\r
38 public bool IsFixedSize {
\r
39 get { return false; }
\r
42 public bool IsReadOnly {
\r
43 get { return false; }
\r
46 public bool IsSynchronized {
\r
47 get { return false; }
\r
50 object IList.this[int index] {
\r
51 get { return this[index]; }
\r
52 set { this[index] = value.ToString(); }
\r
55 public string this[int index] {
\r
57 if (index < 0 || index >= count) {
\r
58 throw new ArgumentOutOfRangeException("index");
\r
61 return entries[index];
\r
65 if (index < 0 || index >= count) {
\r
66 throw new ArgumentOutOfRangeException("index");
\r
70 entries[index] = value;
\r
74 public object SyncRoot {
\r
75 get { return this; }
\r
79 // Public Instance Methods
\r
81 int IList.Add(object value) {
\r
82 return Add(value.ToString());
\r
85 public int Add(string value) {
\r
88 int index = count++;
\r
89 entries[index] = value;
\r
94 public void AddRange(string[] value) {
\r
95 int numEntries = value.Length;
\r
98 Resize(count + numEntries);
\r
99 Array.Copy(value, 0, entries, count, numEntries);
\r
100 count += numEntries;
\r
103 public void Clear() {
\r
108 bool IList.Contains(object value) {
\r
109 return Contains(value.ToString());
\r
112 public bool Contains(string value) {
\r
113 foreach (string entry in entries) {
\r
114 if (value.Equals(entry)) {
\r
122 void ICollection.CopyTo(Array array, int index) {
\r
123 if (array == null) {
\r
124 throw new ArgumentNullException("array");
\r
125 } else if (index < 0) {
\r
126 throw new ArgumentOutOfRangeException("index");
\r
127 } else if (array.Rank > 1) {
\r
128 throw new ArgumentException("array");
\r
129 } else if (index >= array.Length) {
\r
130 throw new ArgumentException("index");
\r
131 } else if (array.Length - index < count) {
\r
132 throw new ArgumentException("array");
\r
135 Array.Copy(entries, 0, array, index, count);
\r
138 public void CopyTo(string[] array, int index) {
\r
139 if (array == null) {
\r
140 throw new ArgumentNullException("array");
\r
141 } else if (index < 0) {
\r
142 throw new ArgumentOutOfRangeException("index");
\r
143 } else if (array.Rank > 1) {
\r
144 throw new ArgumentException("array");
\r
145 } else if (index >= array.Length) {
\r
146 throw new ArgumentException("index");
\r
147 } else if (array.Length - index < count) {
\r
148 throw new ArgumentException("array");
\r
151 Array.Copy(entries, 0, array, index, count);
\r
154 IEnumerator IEnumerable.GetEnumerator() {
\r
155 return new InternalEnumerator(this);
\r
158 public StringEnumerator GetEnumerator() {
\r
159 return new StringEnumerator(this);
\r
162 int IList.IndexOf(object value) {
\r
163 return IndexOf(value.ToString());
\r
166 public int IndexOf(string value) {
\r
167 for (int i = 0; i < count; i++) {
\r
168 if (value.Equals(entries[i])) {
\r
176 void IList.Insert(int index, object value) {
\r
177 Insert(index, value.ToString());
\r
180 public void Insert(int index, string value) {
\r
181 if (index < 0 || index > count) {
\r
182 throw new ArgumentOutOfRangeException("index");
\r
187 Array.Copy(entries, index, entries, index + 1, count - index);
\r
188 entries[index] = value;
\r
193 void IList.Remove(object value) {
\r
194 Remove(value.ToString());
\r
197 public void Remove(string value) {
\r
198 for (int i = 0; i < count; i++) {
\r
199 if (value.Equals(entries[i])) {
\r
206 public void RemoveAt(int index) {
\r
207 if (index < 0 || index >= count) {
\r
208 throw new ArgumentOutOfRangeException("index");
\r
211 int remaining = count - index - 1;
\r
215 if (remaining > 0) {
\r
216 Array.Copy(entries, index + 1, entries, index, remaining);
\r
220 entries[count] = null;
\r
224 // Private Instance Methods
\r
226 private void Resize(int minSize) {
\r
227 int oldSize = entries.Length;
\r
229 if (minSize > oldSize) {
\r
230 string[] oldEntries = entries;
\r
231 int newSize = (int) (oldEntries.Length * CapacityMultiplier);
\r
233 if (newSize < minSize) newSize = minSize;
\r
234 entries = new string[newSize];
\r
235 Array.Copy(oldEntries, 0, entries, 0, count);
\r
242 private class InternalEnumerator : IEnumerator {
\r
243 private StringCollection data;
\r
245 private int myModCount;
\r
247 public InternalEnumerator(StringCollection data) {
\r
249 myModCount = data.modCount;
\r
254 // Public Instance Properties
\r
256 public object Current {
\r
258 if (myModCount != data.modCount) {
\r
259 throw new InvalidOperationException();
\r
260 } else if (index < 0 || index > data.count - 1) {
\r
261 throw new InvalidOperationException();
\r
264 return data[index];
\r
269 // Public Instance Methods
\r
271 public bool MoveNext() {
\r
272 if (myModCount != data.modCount) {
\r
273 throw new InvalidOperationException();
\r
276 if (++index >= data.count - 1) {
\r
283 public void Reset() {
\r
284 if (myModCount != data.modCount) {
\r
285 throw new InvalidOperationException();
\r