StringCollection.cs 6.3 KB

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