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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
1 // This file (Array.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
2 //
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
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.
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
4
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
5 using System;
89
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
6 using System.Collections.Generic;
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
7
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
8 namespace IBBoard
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
9 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
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
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
12 /// </summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
13 public class Arrays
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
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
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
27 public static T[] Subtract<T>(T[] items, T[] subtract)
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
28 {
89
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
29 List<T> arr = new List<T>();
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
30 arr.AddRange(items);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
31
89
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
32 foreach (T obj in subtract)
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
33 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
34 arr.Remove(obj);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
35 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
36
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
37 return arr.ToArray();
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
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
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
52 public static T[] Difference<T>(T[] items1, T[] items2)
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
53 {
89
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
54 T[] diffObjs;
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
55
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
56
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
57 //Difference with as few loops as possible, so see which is shortest
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
58 if (items1.Length >= items2.Length)
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
59 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
60 //add everything from the first list
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
61 diffObjs = DoDifference(items1, items2);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
62 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
63 else
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
64 {
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
65 diffObjs = DoDifference(items2, items1);
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
66 }
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
67
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
68 return diffObjs;
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
69 }
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
70
89
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
71 private static T[] DoDifference<T>(T[] longArray, T[] shortArray)
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
72 {
89
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
73 List<T> arr = new List<T>();
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
74 arr.AddRange(longArray);
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
75
89
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
76 foreach (T obj in shortArray)
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
77 {
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
78 if (arr.Contains(obj))
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
79 {
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
80 arr.Remove(obj);
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
81 }
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
82 else
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
83 {
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
84 arr.Add(obj);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
85 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
86 }
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
87
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
88 return arr.ToArray();
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
89 }
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
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
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
106 public static int IndexOf<T>(T[] items, T item)
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
107 {
88
3200ed24d29e Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
108 return Array.IndexOf(items, item);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
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
cd96f6078edb Re #2: Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 88
diff changeset
123 public static bool Contains<T>(T[] items, T item)
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
124 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
125 return IndexOf(items, item) != -1;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
126 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
127 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
128 }