|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | + |
| 5 | +public class AList<T> : ICollection<T> |
| 6 | +{ |
| 7 | + // Our implementation of list is so that |
| 8 | + // the alist array is always of the "right |
| 9 | + // size", so we don't need to keep track |
| 10 | + // of a separate counter for the size of the |
| 11 | + // list. This is more memory- |
| 12 | + // intensive (because arrays get created |
| 13 | + // frequently), but lighter to implement. |
| 14 | + private T[] alist; |
| 15 | + |
| 16 | + public AList() |
| 17 | + { |
| 18 | + alist = new T[0]; |
| 19 | + } |
| 20 | + |
| 21 | + public bool IsEmpty() |
| 22 | + { |
| 23 | + return alist.Length == 0; |
| 24 | + } |
| 25 | + |
| 26 | + public void Clear() |
| 27 | + { |
| 28 | + alist = new T[0]; |
| 29 | + } |
| 30 | + |
| 31 | + // Attribute and property for the |
| 32 | + // isReadOnly required by ICollection. |
| 33 | + public bool isReadonly = false; |
| 34 | + public bool IsReadOnly |
| 35 | + { |
| 36 | + get { return isReadonly; } |
| 37 | + set { isReadonly = value; } |
| 38 | + } |
| 39 | + |
| 40 | + // Add is adding "to the left". |
| 41 | + public void Add(T value) |
| 42 | + { |
| 43 | + if (isReadonly) |
| 44 | + { |
| 45 | + throw new InvalidOperationException( |
| 46 | + "List is read-only, you cannot add to it." |
| 47 | + ); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + // We create a larger array |
| 52 | + // using the length of the existing |
| 53 | + // array. |
| 54 | + T[] newlist = new T[alist.Length + 1]; |
| 55 | + // We insert the new value "to the left". |
| 56 | + newlist[0] = value; |
| 57 | + // We copy the remaining values, |
| 58 | + // shifting them by one. |
| 59 | + for (int i = 0; i < alist.Length; i++) |
| 60 | + newlist[i + 1] = alist[i]; |
| 61 | + // Finally, our new array becomes our |
| 62 | + // alist attribute. |
| 63 | + alist = newlist; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + // Remove the first node containing the value |
| 69 | + // if it exists and returns true, |
| 70 | + // returns false otherwise. |
| 71 | + public bool Remove(T value) |
| 72 | + { |
| 73 | + if (isReadonly) |
| 74 | + { |
| 75 | + throw new InvalidOperationException( |
| 76 | + "List is read-only, you cannot remove from it." |
| 77 | + ); |
| 78 | + } |
| 79 | + bool removed = false; |
| 80 | + if (!IsEmpty()) |
| 81 | + { |
| 82 | + int i = 0; |
| 83 | + while (i < alist.Length && !removed) |
| 84 | + { |
| 85 | + if (alist[i].Equals(value)) |
| 86 | + { |
| 87 | + removed = true; |
| 88 | + |
| 89 | + // We create the array that will replace |
| 90 | + // the current one. |
| 91 | + |
| 92 | + T[] newList = new T[alist.Length - 1]; |
| 93 | + // We first copy the values |
| 94 | + // before the value we found: |
| 95 | + for (int j = 0; j < i; j++) |
| 96 | + newList[j] = alist[j]; |
| 97 | + // We now copy the values *after* |
| 98 | + // the value we found. |
| 99 | + for (int j = i; j < newList.Length; j++) |
| 100 | + newList[j] = alist[j + 1]; |
| 101 | + alist = newList; |
| 102 | + } |
| 103 | + i++; |
| 104 | + } |
| 105 | + } |
| 106 | + return removed; |
| 107 | + } |
| 108 | + |
| 109 | + public bool Contains(T value) |
| 110 | + { |
| 111 | + // We start by assuming we did not find |
| 112 | + // the target value. |
| 113 | + bool found = false; |
| 114 | + // We go through the array. |
| 115 | + for (int i = 0; i < alist.Length; i++) |
| 116 | + { |
| 117 | + if (alist[i].Equals(value)) found = true; |
| 118 | + } |
| 119 | + return found; |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + // Adding "to the right" (as the last value). |
| 124 | + public void AddR(T value) |
| 125 | + { |
| 126 | + if (isReadonly) |
| 127 | + { |
| 128 | + throw new InvalidOperationException( |
| 129 | + "List is read-only, you cannot add to it." |
| 130 | + ); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + // We resize the array using |
| 135 | + // built-in method. |
| 136 | + Array.Resize(ref alist, alist.Length + 1); |
| 137 | + // We insert the new value "at the end". |
| 138 | + alist[alist.Length - 1] = value; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Count property simply returns |
| 143 | + // the length of the alist array |
| 144 | + // if it is not null. |
| 145 | + public int Count |
| 146 | + { |
| 147 | + get |
| 148 | + { |
| 149 | + return alist.Length; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Copies the elements of the ICollection to an Array, starting at a particular Array index. |
| 154 | + public void CopyTo(T[] array, int arrayIndex) |
| 155 | + { |
| 156 | + if (array == null) |
| 157 | + // The array into which we copy |
| 158 | + // cannot be null. |
| 159 | + throw new ArgumentNullException(); |
| 160 | + if (arrayIndex < 0) |
| 161 | + // The index must be strictly positive. |
| 162 | + throw new ArgumentOutOfRangeException(); |
| 163 | + if (Count > array.Length - arrayIndex) |
| 164 | + // If there is not enough room in the |
| 165 | + // array given as argument to copy |
| 166 | + // our list, we throw an exception. |
| 167 | + throw new ArgumentException( |
| 168 | + $"The destination array does not have enough space ({array.Length}) to copy the list ({Count}) starting at the given index ({arrayIndex})." |
| 169 | + ); |
| 170 | + // If none of the exceptions were triggered, |
| 171 | + // we can copy the values at the right index. |
| 172 | + for (int i = 0; i < alist.Length; i++) |
| 173 | + { |
| 174 | + array[i + arrayIndex] = alist[i]; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /* The following is "bureaucracy" to realize the |
| 179 | + * ICollection interface. */ |
| 180 | + |
| 181 | + public IEnumerator<T> GetEnumerator() |
| 182 | + { |
| 183 | + if (!IsEmpty()) |
| 184 | + { |
| 185 | + for (int i = 0; i < alist.Length; i++) |
| 186 | + { |
| 187 | + yield return alist[i]; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + IEnumerator IEnumerable.GetEnumerator() |
| 193 | + { |
| 194 | + return this.GetEnumerator(); // call the generic version of the method |
| 195 | + } |
| 196 | + |
| 197 | + /* We are done realizing the ICollection interface. */ |
| 198 | +} |
0 commit comments