view Arrays.cs @ 64:70d6c2a5d99e

* Move schema from "dtds" folder to "schemas" folder to match warfoundry:429 no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Wed, 16 Dec 2009 20:48:10 +0000
parents cc7fae81afec
children 3200ed24d29e
line wrap: on
line source

// This file (Array.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
//
// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.

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;
		}
	}
}