changeset 88:3200ed24d29e

Re #2: Refactor API * Break out common code from Array Difference method * Make use of existing "index of" method
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Aug 2010 16:02:05 +0000
parents 90b9a3fe3c18
children cd96f6078edb
files Arrays.cs
diffstat 1 files changed, 24 insertions(+), 41 deletions(-) [+]
line diff
     1.1 --- a/Arrays.cs	Sat Aug 21 10:04:52 2010 +0000
     1.2 +++ b/Arrays.cs	Sat Aug 21 16:02:05 2010 +0000
     1.3 @@ -27,63 +27,46 @@
     1.4  
     1.5  		public static object[] Difference(object[] items1, object[] items2)
     1.6  		{
     1.7 -			ArrayList arr = new ArrayList();
     1.8 +			object[] diffObjs;
     1.9  
    1.10  
    1.11  			//Difference with as few loops as possible, so see which is shortest			
    1.12 -			if (items1.Length > items2.Length)
    1.13 +			if (items1.Length >= items2.Length)
    1.14  			{
    1.15  				//add everything from the first list
    1.16 -				arr.AddRange(items1);
    1.17 -
    1.18 -				foreach (object obj in items2)
    1.19 -				{
    1.20 -					//Then for each item in the second list, if it is in the list remove it
    1.21 -					if (arr.Contains(obj))
    1.22 -					{
    1.23 -						arr.Remove(obj);
    1.24 -					}
    1.25 -					else
    1.26 -					{
    1.27 -						//and if it isn't in the list add it
    1.28 -						arr.Add(obj);
    1.29 -					}
    1.30 -				}
    1.31 +				diffObjs = DoDifference(items1, items2);
    1.32  			}
    1.33  			else
    1.34  			{
    1.35 -				//add everything from the second list
    1.36 -				arr.AddRange(items2);
    1.37 +				diffObjs = DoDifference(items2, items1);
    1.38 +			}
    1.39  
    1.40 -				foreach (object obj in items1)
    1.41 +			return diffObjs;
    1.42 +		}
    1.43 +
    1.44 +		private static object[] DoDifference(object[] longArray, object[] shortArray)
    1.45 +		{
    1.46 +			ArrayList arr = new ArrayList();
    1.47 +			arr.AddRange(longArray);
    1.48 +
    1.49 +			foreach (object obj in shortArray)
    1.50 +			{
    1.51 +				if (arr.Contains(obj))
    1.52  				{
    1.53 -					//Then for each item in the first list, if it is in the list remove it
    1.54 -					if (arr.Contains(obj))
    1.55 -					{
    1.56 -						arr.Remove(obj);
    1.57 -					}
    1.58 -					else
    1.59 -					{
    1.60 -						//and if it isn't in the list add it
    1.61 -						arr.Add(obj);
    1.62 -					}
    1.63 +					arr.Remove(obj);
    1.64 +				}
    1.65 +				else
    1.66 +				{
    1.67 +					arr.Add(obj);
    1.68  				}
    1.69  			}
    1.70 -
    1.71 +			
    1.72  			return arr.ToArray();
    1.73  		}
    1.74 -
    1.75 +		
    1.76  		public static int IndexOf(object[] items, object item)
    1.77  		{
    1.78 -			for (int i = 0; i<items.Length; i++)
    1.79 -			{
    1.80 -				if (items[i].Equals(item))
    1.81 -				{
    1.82 -					return i;
    1.83 -				}
    1.84 -			}
    1.85 -
    1.86 -			return -1;
    1.87 +			return Array.IndexOf(items, item);
    1.88  		}
    1.89  
    1.90  		public static bool Contains(object[] items, object item)