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