comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:961030992bd2
1 using System;
2 using System.Collections;
3
4 namespace IBBoard
5 {
6 /// <summary>
7 /// Summary description for Arrays.
8 /// </summary>
9 public class Arrays
10 {
11 public static object[] Subtract(object[] items, object[] subtract)
12 {
13 ArrayList arr = new ArrayList();
14 arr.AddRange(items);
15
16 foreach (object obj in subtract)
17 {
18 arr.Remove(obj);
19 }
20
21 return arr.ToArray();
22 }
23
24 public static object[] Difference(object[] items1, object[] items2)
25 {
26 ArrayList arr = new ArrayList();
27
28
29 //Difference with as few loops as possible, so see which is shortest
30 if (items1.Length > items2.Length)
31 {
32 //add everything from the first list
33 arr.AddRange(items1);
34
35 foreach (object obj in items2)
36 {
37 //Then for each item in the second list, if it is in the list remove it
38 if (arr.Contains(obj))
39 {
40 arr.Remove(obj);
41 }
42 else
43 {
44 //and if it isn't in the list add it
45 arr.Add(obj);
46 }
47 }
48 }
49 else
50 {
51 //add everything from the second list
52 arr.AddRange(items2);
53
54 foreach (object obj in items1)
55 {
56 //Then for each item in the first list, if it is in the list remove it
57 if (arr.Contains(obj))
58 {
59 arr.Remove(obj);
60 }
61 else
62 {
63 //and if it isn't in the list add it
64 arr.Add(obj);
65 }
66 }
67 }
68
69 return arr.ToArray();
70 }
71
72 public static int IndexOf(object[] items, object item)
73 {
74 for (int i = 0; i<items.Length; i++)
75 {
76 if (items[i].Equals(item))
77 {
78 return i;
79 }
80 }
81
82 return -1;
83 }
84
85 public static bool Contains(object[] items, object item)
86 {
87 return IndexOf(items, item) != -1;
88 }
89 }
90 }