StringCollection.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* System.Collections.Specialized.StringCollection.cs
  2. * Authors:
  3. * John Barnette ([email protected])
  4. * Sean MacIsaac ([email protected])
  5. *
  6. * Copyright (C) 2001 John Barnette
  7. * (C) Ximian, Inc. http://www.ximian.com
  8. *
  9. * NOTES:
  10. * I bet Microsoft uses ArrayList as a backing store for this; I wonder what
  11. * the performance difference will be.
  12. */
  13. using System;
  14. namespace System.Collections.Specialized {
  15. public class StringCollection : IList, ICollection, IEnumerable {
  16. private static int InitialCapacity = 11;
  17. private static float CapacityMultiplier = 2.0f;
  18. private int count;
  19. private int modCount;
  20. private string[] entries;
  21. // Public Constructor
  22. public StringCollection() {
  23. entries = new string[InitialCapacity];
  24. count = 0;
  25. modCount = 0;
  26. }
  27. // Public Instance Properties
  28. public int Count {
  29. get { return count; }
  30. }
  31. public bool IsFixedSize {
  32. get { return false; }
  33. }
  34. public bool IsReadOnly {
  35. get { return false; }
  36. }
  37. public bool IsSynchronized {
  38. get { return false; }
  39. }
  40. object IList.this[int index] {
  41. get { return this[index]; }
  42. set { this[index] = value.ToString(); }
  43. }
  44. public string this[int index] {
  45. get {
  46. if (index < 0 || index >= count) {
  47. throw new ArgumentOutOfRangeException("index");
  48. }
  49. return entries[index];
  50. }
  51. set {
  52. if (index < 0 || index >= count) {
  53. throw new ArgumentOutOfRangeException("index");
  54. }
  55. modCount++;
  56. entries[index] = value;
  57. }
  58. }
  59. public object SyncRoot {
  60. get { return this; }
  61. }
  62. // Public Instance Methods
  63. int IList.Add(object value) {
  64. return Add(value.ToString());
  65. }
  66. public int Add(string value) {
  67. modCount++;
  68. Resize(count + 1);
  69. int index = count++;
  70. entries[index] = value;
  71. return index;
  72. }
  73. public void AddRange(string[] value) {
  74. int numEntries = value.Length;
  75. modCount++;
  76. Resize(count + numEntries);
  77. Array.Copy(value, 0, entries, count, numEntries);
  78. count += numEntries;
  79. }
  80. public void Clear() {
  81. modCount++;
  82. count = 0;
  83. }
  84. bool IList.Contains(object value) {
  85. return Contains(value.ToString());
  86. }
  87. public bool Contains(string value) {
  88. foreach (string entry in entries) {
  89. if (value.Equals(entry)) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. void ICollection.CopyTo(Array array, int index) {
  96. if (array == null) {
  97. throw new ArgumentNullException("array");
  98. } else if (index < 0) {
  99. throw new ArgumentOutOfRangeException("index");
  100. } else if (array.Rank > 1) {
  101. throw new ArgumentException("array");
  102. } else if (index >= array.Length) {
  103. throw new ArgumentException("index");
  104. } else if (array.Length - index < count) {
  105. throw new ArgumentException("array");
  106. }
  107. Array.Copy(entries, 0, array, index, count);
  108. }
  109. public void CopyTo(string[] array, int index) {
  110. if (array == null) {
  111. throw new ArgumentNullException("array");
  112. } else if (index < 0) {
  113. throw new ArgumentOutOfRangeException("index");
  114. } else if (array.Rank > 1) {
  115. throw new ArgumentException("array");
  116. } else if (index >= array.Length) {
  117. throw new ArgumentException("index");
  118. } else if (array.Length - index < count) {
  119. throw new ArgumentException("array");
  120. }
  121. Array.Copy(entries, 0, array, index, count);
  122. }
  123. IEnumerator IEnumerable.GetEnumerator() {
  124. return new InternalEnumerator(this);
  125. }
  126. public StringEnumerator GetEnumerator() {
  127. return new StringEnumerator(this);
  128. }
  129. int IList.IndexOf(object value) {
  130. return IndexOf(value.ToString());
  131. }
  132. public int IndexOf(string value) {
  133. for (int i = 0; i < count; i++) {
  134. if (value.Equals(entries[i])) {
  135. return i;
  136. }
  137. }
  138. return -1;
  139. }
  140. void IList.Insert(int index, object value) {
  141. Insert(index, value.ToString());
  142. }
  143. public void Insert(int index, string value) {
  144. if (index < 0 || index > count) {
  145. throw new ArgumentOutOfRangeException("index");
  146. }
  147. modCount++;
  148. Resize(count + 1);
  149. Array.Copy(entries, index, entries, index + 1, count - index);
  150. entries[index] = value;
  151. count++;
  152. }
  153. void IList.Remove(object value) {
  154. Remove(value.ToString());
  155. }
  156. public void Remove(string value) {
  157. for (int i = 0; i < count; i++) {
  158. if (value.Equals(entries[i])) {
  159. RemoveAt(i);
  160. return;
  161. }
  162. }
  163. }
  164. public void RemoveAt(int index) {
  165. if (index < 0 || index >= count) {
  166. throw new ArgumentOutOfRangeException("index");
  167. }
  168. int remaining = count - index - 1;
  169. modCount++;
  170. if (remaining > 0) {
  171. Array.Copy(entries, index + 1, entries, index, remaining);
  172. }
  173. count--;
  174. entries[count] = null;
  175. }
  176. // Private Instance Methods
  177. private void Resize(int minSize) {
  178. int oldSize = entries.Length;
  179. if (minSize > oldSize) {
  180. string[] oldEntries = entries;
  181. int newSize = (int) (oldEntries.Length * CapacityMultiplier);
  182. if (newSize < minSize) newSize = minSize;
  183. entries = new string[newSize];
  184. Array.Copy(oldEntries, 0, entries, 0, count);
  185. }
  186. }
  187. // Private classes
  188. private class InternalEnumerator : IEnumerator {
  189. private StringCollection data;
  190. private int index;
  191. private int myModCount;
  192. public InternalEnumerator(StringCollection data) {
  193. this.data = data;
  194. myModCount = data.modCount;
  195. index = -1;
  196. }
  197. // Public Instance Properties
  198. public object Current {
  199. get {
  200. if (myModCount != data.modCount) {
  201. throw new InvalidOperationException();
  202. } else if (index < 0 || index > data.count - 1) {
  203. throw new InvalidOperationException();
  204. }
  205. return data[index];
  206. }
  207. }
  208. // Public Instance Methods
  209. public bool MoveNext() {
  210. if (myModCount != data.modCount) {
  211. throw new InvalidOperationException();
  212. }
  213. if (++index >= data.count - 1) {
  214. return false;
  215. }
  216. return true;
  217. }
  218. public void Reset() {
  219. if (myModCount != data.modCount) {
  220. throw new InvalidOperationException();
  221. }
  222. index = -1;
  223. }
  224. }
  225. }
  226. }