Mercurial > hg > ucis.core
comparison Util/ArrayUtil.cs @ 84:146a8d224d86
Added ArrayUtil.Remove, fixed some exceptons
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Thu, 05 Jun 2014 00:21:02 +0200 |
parents | 3352f89cf6f5 |
children | ebdff34b9e4f |
comparison
equal
deleted
inserted
replaced
83:4ca44dd25a6a | 84:146a8d224d86 |
---|---|
156 array[index] = item; | 156 array[index] = item; |
157 return index; | 157 return index; |
158 } | 158 } |
159 } | 159 } |
160 public static int AddUnique<T>(ref T[] array, T item) { | 160 public static int AddUnique<T>(ref T[] array, T item) { |
161 if (array == null) { | |
162 array = new T[] { item }; | |
163 return 0; | |
164 } else { | |
165 int index = Array.IndexOf(array, item); | |
166 if (index == -1) index = Add(ref array, item); | |
167 return index; | |
168 } | |
169 } | |
170 public static Boolean Remove<T>(ref T[] array, T item) { | |
171 if (array == null) return false; | |
161 int index = Array.IndexOf(array, item); | 172 int index = Array.IndexOf(array, item); |
162 if (index == -1) index = Add(ref array, item); | 173 if (index == -1) return false; |
163 return index; | 174 T[] newarray = new T[array.Length - 1]; |
175 if (index > 0) Array.Copy(array, 0, newarray, 0, index); | |
176 if (index < array.Length - 1) Array.Copy(array, index + 1, newarray, index, array.Length - index - 1); | |
177 array = newarray; | |
178 return true; | |
164 } | 179 } |
165 } | 180 } |
166 } | 181 } |