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

StringCollection.cs « System.Collections.Specialized « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 120468812f2aca13e96839428ffdf6795c4407c5 (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
/* System.Collections.Specialized.StringCollection.cs
 * Authors:
 *   John Barnette (jbarn@httcb.net)
 *   Sean MacIsaac (macisaac@ximian.com)
 *
 *  Copyright (C) 2001 John Barnette
 * (C) Ximian, Inc.  http://www.ximian.com
 *
 * NOTES:
 * I bet Microsoft uses ArrayList as a backing store for this; I wonder what
 * the performance difference will be.
*/

using System;

namespace System.Collections.Specialized {
	[Serializable]
	public class StringCollection : IList, ICollection, IEnumerable {
		private static int   InitialCapacity    = 11;
		private static float CapacityMultiplier = 2.0f;
		
		private int count;
		private int modCount;
		
		private string[] entries;
		
		// Public Constructor
		public StringCollection() {
			entries  = new string[InitialCapacity];
			count    = 0;
			modCount = 0;
		}
		
		// Public Instance Properties
		public int Count {
			get { return count; }
		}
		
		public bool IsFixedSize {
			get { return false; }
		}
		
		public bool IsReadOnly {
			get { return false; }
		}
		
		public bool IsSynchronized {
			get { return false; }
		}
		
		object IList.this[int index] {
			get { return this[index]; }
			set { this[index] = value.ToString(); } 
		}
		
		public string this[int index] {
			get {
				if (index < 0 || index >= count) {
					throw new ArgumentOutOfRangeException("index");
				}
				
				return entries[index];
			}
			
			set {
				if (index < 0 || index >= count) {
					throw new ArgumentOutOfRangeException("index");
				}
				
				modCount++;
				entries[index] = value;
			}
		}
		
		public object SyncRoot {
			get { return this; }
		}
		
		
		// Public Instance Methods
		
		int IList.Add(object value) {
			return Add(value.ToString());
		}
		
		public int Add(string value) {
			modCount++;
			Resize(count + 1);
			int index = count++;
			entries[index] = value;
			
			return index;
		}
		
		public void AddRange(string[] value) {
			int numEntries = value.Length;
			
			modCount++;
			Resize(count + numEntries);
			Array.Copy(value, 0, entries, count, numEntries);
			count += numEntries;
		}
		
		public void Clear() {
			modCount++;
			count = 0;
		}
		
		bool IList.Contains(object value) {
			return Contains(value.ToString());
		}
		
		public bool Contains(string value) {
			foreach (string entry in entries) {
				if (value.Equals(entry)) {
					return true;
				}
			}
			
			return false;
		}
		
		void ICollection.CopyTo(Array array, int index) {
			if (array == null) {
				throw new ArgumentNullException("array");
			} else if (index < 0) {
				throw new ArgumentOutOfRangeException("index");
			} else if (array.Rank > 1) {
				throw new ArgumentException("array");
			} else if (index >= array.Length) {
				throw new ArgumentException("index");
			} else if (array.Length - index < count) {
				throw new ArgumentException("array");
			}
			
			Array.Copy(entries, 0, array, index, count);
		}
		
		public void CopyTo(string[] array, int index) {
			if (array == null) {
				throw new ArgumentNullException("array");
			} else if (index < 0) {
				throw new ArgumentOutOfRangeException("index");
			} else if (array.Rank > 1) {
				throw new ArgumentException("array");
			} else if (index >= array.Length) {
				throw new ArgumentException("index");
			} else if (array.Length - index < count) {
				throw new ArgumentException("array");
			}
			
			Array.Copy(entries, 0, array, index, count);
		}
		
		IEnumerator IEnumerable.GetEnumerator() {
			return new InternalEnumerator(this);
		}
		
		public StringEnumerator GetEnumerator() {
			return new StringEnumerator(this);
		}
		
		int IList.IndexOf(object value) {
			return IndexOf(value.ToString());
		}
		
		public int IndexOf(string value) {
			for (int i = 0; i < count; i++) {
				if (value.Equals(entries[i])) {
					return i;
				}
			}
			
			return -1;
		}
		
		void IList.Insert(int index, object value) {
			Insert(index, value.ToString());
		}
		
		public void Insert(int index, string value) {
			if (index < 0 || index > count) {
				throw new ArgumentOutOfRangeException("index");
			}
			
			modCount++;
			Resize(count + 1);
			Array.Copy(entries, index, entries, index + 1, count - index);
			entries[index] = value;
			count++;
		}

		
		void IList.Remove(object value) {
			Remove(value.ToString());
		}
		
		public void Remove(string value) {
			for (int i = 0; i < count; i++) {
				if (value.Equals(entries[i])) {
					RemoveAt(i);
					return;
				}
			}
		}
		
		public void RemoveAt(int index) {
			if (index < 0 || index >= count) {
				throw new ArgumentOutOfRangeException("index");
			}
			
			int remaining = count - index - 1;
			
			modCount++;
			
			if (remaining > 0) {
				Array.Copy(entries, index + 1, entries, index, remaining);
			}
			
			count--;
			entries[count] = null;
		}
		
		
		// Private Instance Methods
		
		private void Resize(int minSize) {
			int oldSize = entries.Length;
			
			if (minSize > oldSize) {
				string[] oldEntries = entries;
				int newSize = (int) (oldEntries.Length * CapacityMultiplier);
				
				if (newSize < minSize) newSize = minSize;
				entries = new string[newSize];
				Array.Copy(oldEntries, 0, entries, 0, count);
			}
		}
		
		
		// Private classes
		
		private class InternalEnumerator : IEnumerator {
			private StringCollection data;
			private int index;
			private int myModCount;
			
			public InternalEnumerator(StringCollection data) {
				this.data  = data;
				myModCount = data.modCount;
				index      = -1;
			}
			
			
			// Public Instance Properties
			
			public object Current {
				get {
					if (myModCount != data.modCount) {
						throw new InvalidOperationException();
					} else if (index < 0 || index > data.count - 1) {
						throw new InvalidOperationException();
					}
					
					return data[index];
				}
			}
			
			
			// Public Instance Methods
			
			public bool MoveNext() {
				if (myModCount != data.modCount) {
					throw new InvalidOperationException();
				}
				
				if (++index >= data.count - 1) {
					return false;
				}
				
				return true;
			}
			
			public void Reset() {
				if (myModCount != data.modCount) {
					throw new InvalidOperationException();
				}
				
				index = -1;
			}
		}
	}
}