Mercurial > repos > IBBoard
annotate Arrays.cs @ 100:fa8378a30ed2
* Add some Stream handling utilities
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 06 Mar 2011 14:51:05 +0000 |
parents | 1575d57a8423 |
children |
rev | line source |
---|---|
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; |
89 | 6 using System.Collections.Generic; |
37 | 7 |
8 namespace IBBoard | |
9 { | |
10 /// <summary> | |
90
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
11 /// Helper methods for doing maths set-type operations on arrays. |
37 | 12 /// </summary> |
13 public class Arrays | |
14 { | |
90
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
15 /// <summary> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
16 /// Subtract one array of items from another array of items and return them in a new array. |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
17 /// </summary> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
18 /// <param name='items'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
19 /// The array of items to subtract from |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
20 /// </param> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
21 /// <param name='subtract'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
22 /// The array of items to subtract |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
23 /// </param> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
24 /// <typeparam name='T'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
25 /// The type of the items in the arrays |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
26 /// </typeparam> |
89 | 27 public static T[] Subtract<T>(T[] items, T[] subtract) |
37 | 28 { |
89 | 29 List<T> arr = new List<T>(); |
37 | 30 arr.AddRange(items); |
31 | |
89 | 32 foreach (T obj in subtract) |
37 | 33 { |
34 arr.Remove(obj); | |
35 } | |
36 | |
37 return arr.ToArray(); | |
38 } | |
90
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
39 |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
40 /// <summary> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
41 /// Gets the objects that are not common to both arrays and returns them in a new array |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
42 /// </summary> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
43 /// <param name='items1'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
44 /// The first array of objects |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
45 /// </param> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
46 /// <param name='items2'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
47 /// The second array of objects |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
48 /// </param> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
49 /// <typeparam name='T'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
50 /// The type of the items in the arrays |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
51 /// </typeparam> |
89 | 52 public static T[] Difference<T>(T[] items1, T[] items2) |
37 | 53 { |
89 | 54 T[] diffObjs; |
37 | 55 |
56 | |
57 //Difference with as few loops as possible, so see which is shortest | |
88 | 58 if (items1.Length >= items2.Length) |
37 | 59 { |
60 //add everything from the first list | |
88 | 61 diffObjs = DoDifference(items1, items2); |
37 | 62 } |
63 else | |
64 { | |
88 | 65 diffObjs = DoDifference(items2, items1); |
66 } | |
67 | |
68 return diffObjs; | |
69 } | |
37 | 70 |
89 | 71 private static T[] DoDifference<T>(T[] longArray, T[] shortArray) |
88 | 72 { |
89 | 73 List<T> arr = new List<T>(); |
88 | 74 arr.AddRange(longArray); |
75 | |
89 | 76 foreach (T obj in shortArray) |
88 | 77 { |
78 if (arr.Contains(obj)) | |
37 | 79 { |
88 | 80 arr.Remove(obj); |
81 } | |
82 else | |
83 { | |
84 arr.Add(obj); | |
37 | 85 } |
86 } | |
88 | 87 |
37 | 88 return arr.ToArray(); |
89 } | |
88 | 90 |
90
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
91 /// <summary> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
92 /// Gets the index of an item in an array, or <code>-1</code> if the item isn't in the array |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
93 /// </summary> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
94 /// <returns> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
95 /// The index of the item, or <code>-1</code> if the item isn't in the array |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
96 /// </returns> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
97 /// <param name='items'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
98 /// The array of items to find the item in |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
99 /// </param> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
100 /// <param name='item'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
101 /// The item to find |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
102 /// </param> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
103 /// <typeparam name='T'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
104 /// The type of the items in the arrays |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
105 /// </typeparam> |
89 | 106 public static int IndexOf<T>(T[] items, T item) |
37 | 107 { |
88 | 108 return Array.IndexOf(items, item); |
37 | 109 } |
90
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
110 |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
111 /// <summary> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
112 /// Tests whether an array of items contains an item |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
113 /// </summary> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
114 /// <param name='items'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
115 /// The array of items to find the item in |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
116 /// </param> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
117 /// <param name='item'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
118 /// The item to find |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
119 /// </param> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
120 /// <typeparam name='T'> |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
121 /// The type of the items in the arrays |
1575d57a8423
Re #12: Document classes and methods
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
122 /// </typeparam> |
89 | 123 public static bool Contains<T>(T[] items, T item) |
37 | 124 { |
125 return IndexOf(items, item) != -1; | |
126 } | |
127 } | |
128 } |