Jypeli  9
The simple game programming library
GroupCollection.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #endregion
23 
24 
25 
26 #if UseDouble
27 using Scalar = System.Double;
28 #else
29 using Scalar = System.Single;
30 #endif
31 using System;
32 using System.Collections.Generic;
33 
34 namespace Jypeli
35 {
39  public class GroupCollection : ICollection<int>
40  {
41  public static bool Intersect(GroupCollection groups1, GroupCollection groups2)
42  {
43  List<int> g1 = groups1.groups;
44  List<int> g2 = groups2.groups;
45  int index1 = 0;
46  int index2 = 0;
47  while (index1 < g1.Count && index2 < g2.Count)
48  {
49  if (g1[index1] == g2[index2])
50  {
51  return true;
52  }
53  else if (g1[index1] < g2[index2])
54  {
55  index1++;
56  }
57  else
58  {
59  index2++;
60  }
61  }
62  return false;
63  }
64 
65  List<int> groups = new List<int>();
66 
67  public GroupCollection()
68  {
69  groups = new List<int>();
70  }
72  {
73  this.groups = new List<int>(copy.groups);
74  }
75 
79  public int Count
80  {
81  get { return groups.Count; }
82  }
83  bool ICollection<int>.IsReadOnly
84  {
85  get { return false; }
86  }
87 
93  public bool Add(int item)
94  {
95  for (int index = 0; index < groups.Count; ++index)
96  {
97  if (groups[index] == item)
98  {
99  return false;
100  }
101  if (groups[index] > item)
102  {
103  groups.Insert(index, item);
104  return true;
105  }
106  }
107  groups.Add(item);
108  return true;
109  }
115  public int AddRange(int[] array)
116  {
117  if (array == null) { throw new ArgumentNullException("collection"); }
118  Array.Sort(array);
119  List<int> newGroups = new List<int>(groups.Count + array.Length);
120 
121  int newIndex = 0;
122  int oldIndex = 0;
123  int index = 0;
124  int count = 0;
125  while ((newIndex < array.Length) && (oldIndex < groups.Count))
126  {
127  if (array[newIndex] == groups[oldIndex])
128  {
129  oldIndex++;
130  }
131  else if (array[newIndex] > groups[oldIndex])
132  {
133  newGroups.Add(groups[oldIndex]);
134  oldIndex++;
135  index++;
136  }
137  else
138  {
139  newGroups.Add(array[newIndex]);
140  newIndex++;
141  index++;
142  count++;
143  }
144  }
145  for (; newIndex < array.Length; ++newIndex)
146  {
147  if (index == 0 || newGroups[index - 1] != array[newIndex])
148  {
149  newGroups.Add(array[newIndex]);
150  index++;
151  count++;
152  }
153  }
154  for (; oldIndex < groups.Count; ++oldIndex)
155  {
156  if (index == 0 || newGroups[index - 1] != groups[oldIndex])
157  {
158  newGroups.Add(groups[oldIndex]);
159  index++;
160  }
161  }
162  this.groups = newGroups;
163  return count;
164  }
170  public bool Contains(int item)
171  {
172  return groups.BinarySearch(item) >= 0;
173  }
179  public int ContainsRange(int[] array)
180  {
181  if (array == null) { throw new ArgumentNullException("collection"); }
182  Array.Sort(array);
183  int index1 = 0;
184  int index2 = 0;
185  int count = 0;
186  while (index1 < groups.Count && index2 < array.Length)
187  {
188  if (groups[index1] == array[index2])
189  {
190  count++;
191  index1++;
192  index2++;
193  }
194  else if (groups[index1] < array[index2])
195  {
196  index1++;
197  }
198  else
199  {
200  index2++;
201  }
202  }
203  return count;
204  }
210  public bool Remove(int item)
211  {
212  int index = groups.BinarySearch(item);
213  if (index < 0) { return false; }
214  groups.RemoveAt(index);
215  return true;
216  }
222  public int RemoveRange(int[] array)
223  {
224  if (array == null) { throw new ArgumentNullException("collection"); }
225  Array.Sort(array);
226  int index = 0;
227  return groups.RemoveAll(delegate(int value)
228  {
229  while (index < array.Length)
230  {
231  if (value == array[index])
232  {
233  index++;
234  return true;
235  }
236  else if (value > array[index])
237  {
238  index++;
239  }
240  else
241  {
242  return false;
243  }
244  }
245  return false;
246  });
247  }
248 
249  void ICollection<int>.Add(int item)
250  {
251  Add(item);
252  }
256  public void Clear()
257  {
258  groups.Clear();
259  }
260  public void CopyTo(int[] array, int arrayIndex)
261  {
262  groups.CopyTo(array, arrayIndex);
263  }
264  public IEnumerator<int> GetEnumerator()
265  {
266  return groups.GetEnumerator();
267  }
268  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
269  {
270  return GetEnumerator();
271  }
272  }
273 }
Jypeli.GroupCollection.Count
int Count
Gets the number of collison Groups the ignorer is part of.
Definition: GroupCollection.cs:80
Jypeli
Definition: Automobile.cs:5
Jypeli.GroupCollection.Intersect
static bool Intersect(GroupCollection groups1, GroupCollection groups2)
Definition: GroupCollection.cs:41
Jypeli.GroupCollection.Contains
bool Contains(int item)
returns true if the ignorer is part of the group.
Definition: GroupCollection.cs:170
Jypeli.GroupCollection.Remove
bool Remove(int item)
Trys to remove the ignorer from a group.
Definition: GroupCollection.cs:210
Jypeli.GroupCollection.Add
bool Add(int item)
Trys to add a group.
Definition: GroupCollection.cs:93
Jypeli.GroupCollection.groups
List< int > groups
Definition: GroupCollection.cs:65
Jypeli.GroupCollection.RemoveRange
int RemoveRange(int[] array)
Trys to remove the ignorer from a range of groups.
Definition: GroupCollection.cs:222
Jypeli.GroupCollection.GroupCollection
GroupCollection(GroupCollection copy)
Definition: GroupCollection.cs:71
Jypeli.GroupCollection.Clear
void Clear()
removes the ignorer from all groups.
Definition: GroupCollection.cs:256
Jypeli.GroupCollection.AddRange
int AddRange(int[] array)
adds an array of group ids.
Definition: GroupCollection.cs:115
Jypeli.GroupCollection.GetEnumerator
IEnumerator< int > GetEnumerator()
Definition: GroupCollection.cs:264
Scalar
System.Single Scalar
Definition: Clamped.cs:29
Jypeli.GroupCollection.ContainsRange
int ContainsRange(int[] array)
returns the number of groups in the array it is part of.
Definition: GroupCollection.cs:179
Jypeli.GroupCollection
A collection that stores ints that represent groups
Definition: GroupCollection.cs:40
System
Definition: CFFauxAttributes.cs:29
Jypeli.GroupCollection.CopyTo
void CopyTo(int[] array, int arrayIndex)
Definition: GroupCollection.cs:260
Jypeli.GroupCollection.GroupCollection
GroupCollection()
Definition: GroupCollection.cs:67