BGC Tools
Static Public Member Functions
BGC.Extensions.ListExtension Class Reference

Set of extensions for a list for easier use of ILists More...

Static Public Member Functions

static int RandomIndex (this IList list)
 Get a random index from a list More...
 
static T RandomValue< T > (this IList list)
 Get a random value from the list More...
 
static T RandomValue< T > (this IList list, params int[] excludeIndicies)
 Get a random value from the list More...
 
static void Shuffle (this IList list)
 Randomize a list More...
 
static int LastIndex (this IList list)
 Get last index of the list More...
 
static T LastVal< T > (this IList list)
 Get the last element in the list More...
 
static void Swap< T > (this IList list, int indexA, int indexB)
 swap elements in a list More...
 
static bool ListsEquivalent< T > (List< T > a, List< T > b)
 Test if list a and list b are exactly equal More...
 
static int GetSequenceHashCode< T > (this IList< T > sequence)
 https://stackoverflow.com/questions/8094867/good-gethashcode-override-for-list-of-foo-objects-respecting-the-order More...
 
static int [] Indexes (this IList list)
 Get a list of all the valid indexes in the list More...
 
static bool SetAdd< T > (this List< T > list, T item)
 Add an item to a list if it is not already in the list. More...
 
static bool SetSub< T > (this List< T > list, T item)
 Remove all occurrences of item in list More...
 
static T Max< T > (this List< T > list, bool verbose=false)
 Get the maximum value of a list. Best to only use with a list of numbers More...
 
static T Min< T > (this List< T > list, bool verbose=false)
 Get the minimum value of a list. Best to only use with a list of numbers More...
 
static List< T > ShallowClone< T > (this List< T > list)
 Create a shallow copy of the list More...
 
static void PrintSelf< T > (this List< T > list)
 Debug.Log to print out all values of the lists= More...
 
static List< T > FindAllInstancesOf< T > (this List< T > list, T value)
 Finds all instances of an object within a list and returns them More...
 
static string Join< T > (this List< T > list, string separator=",")
 Join a list into a string with a separator of the users choice More...
 
static string Join< T > (this T[] arr, string separator)
 Join an array into a string with a separator of the users choice More...
 
static bool TryGetElement< T > (this List< T > list, int index, out T value)
 Attempts to get an element from an array and returns it. If not, returns default. Outputs the result to the out parameter More...
 
static bool Conains< T > (IList list, T val)
 

Detailed Description

Set of extensions for a list for easier use of ILists

Definition at line 11 of file ListExtensions.cs.

Member Function Documentation

◆ Conains< T >()

static bool BGC.Extensions.ListExtension.Conains< T > ( IList  list,
val 
)
inlinestatic

Checks if the list contains the defined value

Template Parameters
T
Parameters
list
val
Returns
True if the list has the defined value

Definition at line 443 of file ListExtensions.cs.

444  {
445  bool found = false;
446  int length = list.Count;
447  for (int i = 0; i < length; ++i)
448  {
449  if (list[i].Equals(val))
450  {
451  found = true;
452  break;
453  }
454  }
455 
456  return found;
457  }

◆ FindAllInstancesOf< T >()

static List<T> BGC.Extensions.ListExtension.FindAllInstancesOf< T > ( this List< T >  list,
value 
)
inlinestatic

Finds all instances of an object within a list and returns them

Template Parameters
T
Parameters
list
value
Returns

Definition at line 342 of file ListExtensions.cs.

343  {
344  List<T> instances = new List<T>();
345 
346  int length = list.Count;
347  for(int i = 0; i < length; ++i)
348  {
349  if(list[i].Equals(value))
350  {
351  instances.Add(list[i]);
352  }
353  }
354 
355  return instances;
356  }

◆ GetSequenceHashCode< T >()

static int BGC.Extensions.ListExtension.GetSequenceHashCode< T > ( this IList< T >  sequence)
inlinestatic

https://stackoverflow.com/questions/8094867/good-gethashcode-override-for-list-of-foo-objects-respecting-the-order

Template Parameters
T
Parameters
sequence
Returns

Definition at line 169 of file ListExtensions.cs.

170  {
171  unchecked
172  {
173  int hash = 19;
174  foreach (var foo in sequence)
175  {
176  hash = hash * 31 + foo.GetHashCode();
177  }
178 
179  return hash;
180  }
181  }

◆ Indexes()

static int [] BGC.Extensions.ListExtension.Indexes ( this IList  list)
inlinestatic

Get a list of all the valid indexes in the list

Parameters
list
Returns

Definition at line 188 of file ListExtensions.cs.

189  {
190  int[] indexes = new int[list.Count];
191 
192  int length = list.Count;
193  for (int i = 0; i < length; ++i)
194  {
195  indexes[i] = i;
196  }
197 
198  return indexes;
199  }

◆ Join< T >() [1/2]

static string BGC.Extensions.ListExtension.Join< T > ( this List< T >  list,
string  separator = "," 
)
inlinestatic

Join a list into a string with a separator of the users choice

Template Parameters
T
Parameters
list
separatordefaults to a comma
Returns

Definition at line 365 of file ListExtensions.cs.

366  {
367  Assert.IsNotNull(list);
368  Assert.IsFalse(string.IsNullOrEmpty(separator));
369 
370  int length = list.Count;
371  string result = "";
372 
373  if (length != 0)
374  {
375  result = list[0].ToString();
376 
377  for (int i = 1; i < length; ++i)
378  {
379  result = $"{result}{separator}{list[i].ToString()}";
380  }
381  }
382 
383  return result;
384  }

◆ Join< T >() [2/2]

static string BGC.Extensions.ListExtension.Join< T > ( this T []  arr,
string  separator 
)
inlinestatic

Join an array into a string with a separator of the users choice

Template Parameters
T
Parameters
arr
separator
Returns

Definition at line 393 of file ListExtensions.cs.

394  {
395  int length = arr.Length;
396  if(length == 0)
397  {
398  return "";
399  }
400 
401  string result = arr[0].ToString();
402  for (int i = 1; i < length; ++i)
403  {
404  result = $"{result}{separator}{arr[i].ToString()}";
405  }
406 
407  return result;
408  }

◆ LastIndex()

static int BGC.Extensions.ListExtension.LastIndex ( this IList  list)
inlinestatic

Get last index of the list

Parameters
list
Returns

Definition at line 108 of file ListExtensions.cs.

109  {
110  return list.Count - 1;
111  }

◆ LastVal< T >()

static T BGC.Extensions.ListExtension.LastVal< T > ( this IList  list)
inlinestatic

Get the last element in the list

Template Parameters
T
Parameters
list
Returns

Definition at line 119 of file ListExtensions.cs.

120  {
121  return (T)list[list.LastIndex()];
122  }

◆ ListsEquivalent< T >()

static bool BGC.Extensions.ListExtension.ListsEquivalent< T > ( List< T >  a,
List< T >  b 
)
inlinestatic

Test if list a and list b are exactly equal

Parameters
a
b
Returns

Definition at line 144 of file ListExtensions.cs.

145  {
146  int length = a.Count;
147  if (length != b.Count)
148  {
149  return false;
150  }
151 
152  for (int i = 0; i < length; ++i)
153  {
154  if (a[i].Equals(b[i]) == false)
155  {
156  return false;
157  }
158  }
159 
160  return true;
161  }

◆ Max< T >()

static T BGC.Extensions.ListExtension.Max< T > ( this List< T >  list,
bool  verbose = false 
)
inlinestatic

Get the maximum value of a list. Best to only use with a list of numbers

Template Parameters
T
Parameters
list
verbose
Returns

Definition at line 248 of file ListExtensions.cs.

249  {
250  if (list.Count <= 0)
251  {
252  if (verbose)
253  {
254  Debug.LogError("List size is less than or equal to 0.");
255  }
256 
257  return default(T);
258  }
259 
260  T max = list[0];
261  int length = list.Count;
262  for (int i = 1; i < length; ++i)
263  {
264  if (Comparer<T>.Default.Compare(max, list[i]) < 0)
265  {
266  max = list[i];
267  }
268  }
269 
270  return max;
271  }

◆ Min< T >()

static T BGC.Extensions.ListExtension.Min< T > ( this List< T >  list,
bool  verbose = false 
)
inlinestatic

Get the minimum value of a list. Best to only use with a list of numbers

Template Parameters
T
Parameters
list
verbose
Returns

Definition at line 280 of file ListExtensions.cs.

281  {
282  if (list.Count <= 0)
283  {
284  if (verbose)
285  {
286  Debug.LogError("List size is less than or equal to 0.");
287  }
288 
289  return default(T);
290  }
291 
292  T min = list[0];
293  int length = list.Count;
294  for (int i = 1; i < length; ++i)
295  {
296  if (Comparer<T>.Default.Compare(min, list[i]) > 0)
297  {
298  min = list[i];
299  }
300  }
301 
302  return min;
303  }

◆ PrintSelf< T >()

static void BGC.Extensions.ListExtension.PrintSelf< T > ( this List< T >  list)
inlinestatic

Debug.Log to print out all values of the lists=

Template Parameters
T
Parameters
list

Definition at line 326 of file ListExtensions.cs.

327  {
328  int length = list.Count;
329  for (int i = 0; i < length; ++i)
330  {
331  Debug.Log($"{i}) {list[i]}");
332  }
333  }

◆ RandomIndex()

static int BGC.Extensions.ListExtension.RandomIndex ( this IList  list)
inlinestatic

Get a random index from a list

Template Parameters
T
Parameters
list
Returns

Definition at line 19 of file ListExtensions.cs.

20  {
21  if (list.Count <= 0)
22  {
23  return -1;
24  }
25 
26  return Random.Range(0, list.Count);
27  }

◆ RandomValue< T >() [1/2]

static T BGC.Extensions.ListExtension.RandomValue< T > ( this IList  list)
inlinestatic

Get a random value from the list

Template Parameters
T
Parameters
list
Returns

Definition at line 35 of file ListExtensions.cs.

36  {
37  if (list.Count == 0)
38  {
39  Debug.LogError(
40  "Received list of length 0 which doesn't allow for random value, " +
41  "returning default value");
42  return default(T);
43  }
44 
45  return (T)list[list.RandomIndex()];
46  }

◆ RandomValue< T >() [2/2]

static T BGC.Extensions.ListExtension.RandomValue< T > ( this IList  list,
params int []  excludeIndicies 
)
inlinestatic

Get a random value from the list

Template Parameters
T
Parameters
list
excludeIndicies
Returns

Definition at line 55 of file ListExtensions.cs.

56  {
57  int length = list.Count;
58  if (length == 0)
59  {
60  Debug.LogError(
61  "Received list of length 0 which doesn't allow for random value, " +
62  "returning default value");
63  return default(T);
64  }
65 
66  List<int> indexes = new List<int>();
67  for (int i = 0; i < length; ++i)
68  {
69  if (excludeIndicies.Contains(i) == false)
70  {
71  indexes.Add(i);
72  }
73  }
74 
75  if(indexes.Count == 0)
76  {
77  Debug.LogError(
78  "Recieved array of excludedIndicies that does not allow for any values to be returned, " +
79  "returning default value");
80 
81  return default(T);
82  }
83 
84  return (T)list[indexes.RandomValue<int>()];
85  }

◆ SetAdd< T >()

static bool BGC.Extensions.ListExtension.SetAdd< T > ( this List< T >  list,
item 
)
inlinestatic

Add an item to a list if it is not already in the list.

Template Parameters
T
Parameters
list
item
Returns
True if the item was not present and added to the list

Definition at line 208 of file ListExtensions.cs.

209  {
210  bool added = false;
211 
212  if (list.Contains(item) == false)
213  {
214  list.Add(item);
215  added = true;
216  }
217 
218  return added;
219  }

◆ SetSub< T >()

static bool BGC.Extensions.ListExtension.SetSub< T > ( this List< T >  list,
item 
)
inlinestatic

Remove all occurrences of item in list

Template Parameters
T
Parameters
list
item
Returns
if item was found at all in list and removed

Definition at line 228 of file ListExtensions.cs.

229  {
230  bool removed = false;
231 
232  while (list.Contains(item))
233  {
234  list.Remove(item);
235  removed = true;
236  }
237 
238  return removed;
239  }

◆ ShallowClone< T >()

static List<T> BGC.Extensions.ListExtension.ShallowClone< T > ( this List< T >  list)
inlinestatic

Create a shallow copy of the list

src: https://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c

Template Parameters
T
Parameters
list
Returns

Definition at line 313 of file ListExtensions.cs.

314  {
315  List<T> newList = new List<T>(list.Capacity);
316  newList.AddRange(list);
317 
318  return newList;
319  }

◆ Shuffle()

static void BGC.Extensions.ListExtension.Shuffle ( this IList  list)
inlinestatic

Randomize a list

Parameters
list

Definition at line 91 of file ListExtensions.cs.

92  {
93  int length = list.Count;
94  for (int i = 0; i < length; ++i)
95  {
96  int randomIndex = Random.Range(i, list.Count);
97  object temp = list[i];
98  list[i] = list[randomIndex];
99  list[randomIndex] = temp;
100  }
101  }

◆ Swap< T >()

static void BGC.Extensions.ListExtension.Swap< T > ( this IList  list,
int  indexA,
int  indexB 
)
inlinestatic

swap elements in a list

Template Parameters
T
Parameters
list
indexA
indexB

Definition at line 131 of file ListExtensions.cs.

132  {
133  T tmp = (T)list[indexA];
134  list[indexA] = list[indexB];
135  list[indexB] = tmp;
136  }

◆ TryGetElement< T >()

static bool BGC.Extensions.ListExtension.TryGetElement< T > ( this List< T >  list,
int  index,
out T  value 
)
inlinestatic

Attempts to get an element from an array and returns it. If not, returns default. Outputs the result to the out parameter

Template Parameters
T
Parameters
list
index
result
Returns

Definition at line 419 of file ListExtensions.cs.

420  {
421  Assert.IsNotNull(list);
422  bool result = false;
423 
424  if (index < list.Count && index > -1)
425  {
426  value = list[index];
427  result = true;
428  }
429  else
430  {
431  value = default(T);
432  }
433 
434  return result;
435  }

The documentation for this class was generated from the following file: