Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Hydrogen.Array Class Reference

Additional static functions used to extend existing Array support inside of Unity. More...

Static Public Member Functions

static bool Add< T > (ref T[] array, T newObject, bool forceUnique)
 Add a unique item to an array. More...
 
static bool Add< T > (ref T[] array, T newObject)
 Add an item to an array. More...
 
static bool AddAt< T > (ref T[] array, int position, T newObject, bool forceUnique)
 Add a unique item to an array. More...
 
static bool Contains< T > (ref T[] array, T targetObject)
 Determine if the specified array contains the targetObject. More...
 
static bool Remove< T > (ref T[] array, T oldObject)
 Removes all references for an object from array. More...
 
static bool RemoveAt< T > (ref T[] array, int index)
 Removes an object in an array at the provided index. More...
 

Detailed Description

Additional static functions used to extend existing Array support inside of Unity.

Definition at line 36 of file Array.cs.

Member Function Documentation

static bool Hydrogen.Array.Add< T > ( ref T[]  array,
newObject,
bool  forceUnique 
)
static

Add a unique item to an array.

Returns
true, if something was added, false otherwise.
Parameters
arrayThe source array
newObjectObject to be added.
forceUniqueShould it check to see if there is an existing reference in the array?
Template Parameters
TObject Type.

Definition at line 46 of file Array.cs.

47  {
48  return AddAt (ref array, array.Length, newObject, forceUnique);
49  }
static bool Hydrogen.Array.Add< T > ( ref T[]  array,
newObject 
)
static

Add an item to an array.

Returns
Was anything added?
Parameters
arrayThe source array
newObjectObject to be added.
Template Parameters
TObject Type.

Definition at line 58 of file Array.cs.

59  {
60  return AddAt (ref array, array.Length, newObject, false);
61  }
static bool Hydrogen.Array.AddAt< T > ( ref T[]  array,
int  position,
newObject,
bool  forceUnique 
)
static

Add a unique item to an array.

Returns
true, if something was added, false otherwise.
Parameters
arrayThe source array.
positionThe position (index) where to insert the object.
newObjectObject to be added.
forceUniqueShould it check to see if there is an existing reference in the array?
Template Parameters
TObject Type.

Definition at line 72 of file Array.cs.

73  {
74  // First Addition - Just Quick Add
75  if (array.Length == 0) {
76  array = new T[1];
77  array [0] = newObject;
78  return true;
79  }
80 
81  // Check if we already have this target and unique is required
82  if (forceUnique) {
83  if (array.Any (t => t.Equals (newObject))) {
84  return false;
85  }
86  }
87 
88  // Create new array
89  var newArray = new T[array.Length + 1];
90 
91  for (var x = 0; x <= array.Length; x++) {
92  if (x < position) {
93  newArray [x] = array [x];
94  } else if (x == position) {
95  newArray [x] = newObject;
96  } else if (x > position) {
97  newArray [x] = array [x - 1];
98  }
99  }
100 
101  // Assign array
102  array = newArray;
103 
104  return true;
105  }
static bool Hydrogen.Array.Contains< T > ( ref T[]  array,
targetObject 
)
static

Determine if the specified array contains the targetObject.

Returns
true, if something was found, false otherwise.
Parameters
arrayThe source array.
targetObjectObject to test for.
Template Parameters
TObject Type

Definition at line 114 of file Array.cs.

115  {
116  return array.Any (t => t.Equals (targetObject));
117  }
static bool Hydrogen.Array.Remove< T > ( ref T[]  array,
oldObject 
)
static

Removes all references for an object from array.

Returns
true, if something was removed, false otherwise.
Parameters
arrayThe source array.
oldObjectObject to be removed.
Template Parameters
TObject Type.

Definition at line 126 of file Array.cs.

127  {
128 
129  // Make sure we're not trying to remove nothing
130  if (oldObject.Equals (null)) {
131  return false;
132  }
133 
134  var newArray = new T[array.Length - 1];
135  var counter = 0;
136  var found = false;
137 
138  foreach (var singleObject in array) {
139  if (!singleObject.Equals (oldObject)) {
140  newArray [counter] = singleObject;
141  counter++;
142  } else {
143  found = true;
144  }
145  }
146 
147  if (found) {
148  array = newArray;
149  }
150 
151  return found;
152  }
static bool Hydrogen.Array.RemoveAt< T > ( ref T[]  array,
int  index 
)
static

Removes an object in an array at the provided index.

Returns
true, if something was removed, false otherwise.
Parameters
arrayThe source array.
indexThe index of the item to be removed.
Template Parameters
TObject Type.

Definition at line 161 of file Array.cs.

162  {
163  // Failsafe
164  if (index >= array.Length || index < 0)
165  return false;
166 
167  var newArray = new T[array.Length - 1];
168  var counter = 0;
169 
170  for (int x = 0; x < array.Length; x++) {
171  if (x != index) {
172  newArray [counter] = array [x];
173  counter++;
174  }
175  }
176  array = newArray;
177  return true;
178  }