view Arrays.cs @ 34:d597feec5dd4

Closes #20 - Add rounding method with enum * Add RoundType enumeration with up, down, 'banker', up to half, down to half, 'banker' to half * Add IBBMath.Round(double, RoundType)
author IBBoard <dev@ibboard.co.uk>
date Tue, 19 May 2009 14:24:00 +0000
parents 0352fa33ee8f
children cc7fae81afec
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;
		}
	}
}