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

Static Public Member Functions

static int RandomIndex< T > (this T[] array)
 Get a random index of the array More...
 
static T RandomValue< T > (this T[] array)
 Get a random value from the array More...
 
static bool Contains< T > (this T[] array, T target)
 Check if an array contains a target More...
 
static T [] Add< T > (this T[] array, T item)
 add element to array. Extends array length by 1. More...
 
static T [] GetRange< T > (this T[] array, int startIndex, int endIndex=-1)
 Get a Range of Elements in an Array. endIndex is NOT INCLUSIVE More...
 
static int [] Indexes< T > (this T[] arr)
 Gets an array of all the indexes More...
 
static int BinarySearchLowerBound (this float[] array, float value)
 Search a (ordered) float array for the first value exceeding or equal to the argument More...
 
static int BinarySearchLowerBound (this double[] array, double value)
 Search a (ordered) float array for the first value exceeding or equal to the argument More...
 

Detailed Description

Definition at line 6 of file ArrayExtensions.cs.

Member Function Documentation

◆ Add< T >()

static T [] BGC.Extensions.ArrayExtensions.Add< T > ( this T []  array,
item 
)
inlinestatic

add element to array. Extends array length by 1.

Template Parameters
T
Parameters
array
item

Definition at line 55 of file ArrayExtensions.cs.

56  {
57  T[] newArray = new T[array.Length + 1];
58  newArray[array.Length] = item;
59 
60  for (int i = 0; i < array.Length; ++i)
61  {
62  newArray[i] = array[i];
63  }
64 
65  return newArray;
66  }

◆ BinarySearchLowerBound() [1/2]

static int BGC.Extensions.ArrayExtensions.BinarySearchLowerBound ( this float []  array,
float  value 
)
inlinestatic

Search a (ordered) float array for the first value exceeding or equal to the argument

Definition at line 120 of file ArrayExtensions.cs.

121  {
122  int min = 0;
123  int max = array.Length;
124  int guess;
125 
126  if (value < array[0])
127  {
128  return -1;
129  }
130 
131  while (true)
132  {
133  guess = (max + min) / 2;
134 
135  if (guess == min)
136  {
137  return guess;
138  }
139  else if (array[guess] == value)
140  {
141  return guess;
142  }
143  else if (value > array[guess])
144  {
145  min = guess;
146  }
147  else
148  {
149  max = guess;
150  }
151  }
152  }

◆ BinarySearchLowerBound() [2/2]

static int BGC.Extensions.ArrayExtensions.BinarySearchLowerBound ( this double []  array,
double  value 
)
inlinestatic

Search a (ordered) float array for the first value exceeding or equal to the argument

Definition at line 157 of file ArrayExtensions.cs.

158  {
159  int min = 0;
160  int max = array.Length;
161  int guess;
162 
163  if (value < array[0])
164  {
165  return -1;
166  }
167 
168  while (true)
169  {
170  guess = (max + min) / 2;
171 
172  if (guess == min)
173  {
174  return guess;
175  }
176  else if (array[guess] == value)
177  {
178  return guess;
179  }
180  else if (value > array[guess])
181  {
182  min = guess;
183  }
184  else
185  {
186  max = guess;
187  }
188  }
189  }

◆ Contains< T >()

static bool BGC.Extensions.ArrayExtensions.Contains< T > ( this T []  array,
target 
)
inlinestatic

Check if an array contains a target

Template Parameters
T
Parameters
array
target
Returns

Definition at line 37 of file ArrayExtensions.cs.

38  {
39  for (int i = 0; i < array.Length; i++)
40  {
41  if (Equals(array[i], target))
42  {
43  return true;
44  }
45  }
46  return false;
47  }

◆ GetRange< T >()

static T [] BGC.Extensions.ArrayExtensions.GetRange< T > ( this T []  array,
int  startIndex,
int  endIndex = -1 
)
inlinestatic

Get a Range of Elements in an Array. endIndex is NOT INCLUSIVE

Template Parameters
T
Parameters
array
startIndex
endIndex
Returns

Definition at line 77 of file ArrayExtensions.cs.

78  {
79  if (endIndex == -1)
80  {
81  endIndex = array.Length;
82  }
83 
84  Assert.IsNotNull(array);
85  Assert.IsTrue(startIndex > -1);
86  Assert.IsTrue(endIndex >= startIndex);
87  Assert.IsFalse(startIndex > array.Length);
88  Assert.IsFalse(endIndex > array.Length);
89 
90  T[] arr = new T[endIndex - startIndex];
91  for (int i = startIndex; i < endIndex; ++i)
92  {
93  arr[i - startIndex] = array[i];
94  }
95 
96  return arr;
97  }

◆ Indexes< T >()

static int [] BGC.Extensions.ArrayExtensions.Indexes< T > ( this T []  arr)
inlinestatic

Gets an array of all the indexes

Template Parameters
T
Parameters
arr
Returns

Definition at line 105 of file ArrayExtensions.cs.

106  {
107  int[] indexes = new int[arr.Length];
108 
109  for (int i = 0; i < arr.Length; ++i)
110  {
111  indexes[i] = i;
112  }
113 
114  return indexes;
115  }

◆ RandomIndex< T >()

static int BGC.Extensions.ArrayExtensions.RandomIndex< T > ( this T []  array)
inlinestatic

Get a random index of the array

Template Parameters
T
Parameters
array
Returns

Definition at line 14 of file ArrayExtensions.cs.

15  {
16  return Random.Range(0, array.Length);
17  }

◆ RandomValue< T >()

static T BGC.Extensions.ArrayExtensions.RandomValue< T > ( this T []  array)
inlinestatic

Get a random value from the array

Template Parameters
T
Parameters
array
Returns

Definition at line 25 of file ArrayExtensions.cs.

26  {
27  return array[array.RandomIndex()];
28  }

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