diff Arrays.cs @ 0:961030992bd2

Initial commit of IBBoard libraries
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 11:13:48 +0000
parents
children 0352fa33ee8f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Arrays.cs	Fri Dec 19 11:13:48 2008 +0000
@@ -0,0 +1,90 @@
+using System;
+using System.Collections;
+
+namespace IBBoard
+{
+	/// <summary>
+	/// Summary description for Arrays.
+	/// </summary>
+	public class Arrays
+	{
+		public static object[] Subtract(object[] items, object[] subtract)
+		{
+			ArrayList arr = new ArrayList();
+			arr.AddRange(items);
+			
+			foreach (object obj in subtract)
+			{
+				arr.Remove(obj);
+			}
+
+			return arr.ToArray();
+		}
+
+		public static object[] Difference(object[] items1, object[] items2)
+		{
+			ArrayList arr = new ArrayList();
+
+
+			//Difference with as few loops as possible, so see which is shortest			
+			if (items1.Length > items2.Length)
+			{
+				//add everything from the first list
+				arr.AddRange(items1);
+
+				foreach (object obj in items2)
+				{
+					//Then for each item in the second list, if it is in the list remove it
+					if (arr.Contains(obj))
+					{
+						arr.Remove(obj);
+					}
+					else
+					{
+						//and if it isn't in the list add it
+						arr.Add(obj);
+					}
+				}
+			}
+			else
+			{
+				//add everything from the second list
+				arr.AddRange(items2);
+
+				foreach (object obj in items1)
+				{
+					//Then for each item in the first list, if it is in the list remove it
+					if (arr.Contains(obj))
+					{
+						arr.Remove(obj);
+					}
+					else
+					{
+						//and if it isn't in the list add it
+						arr.Add(obj);
+					}
+				}
+			}
+
+			return arr.ToArray();
+		}
+
+		public static int IndexOf(object[] items, object item)
+		{
+			for (int i = 0; i<items.Length; i++)
+			{
+				if (items[i].Equals(item))
+				{
+					return i;
+				}
+			}
+
+			return -1;
+		}
+
+		public static bool Contains(object[] items, object item)
+		{
+			return IndexOf(items, item) != -1;
+		}
+	}
+}