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