-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_utility_array.gsc
More file actions
228 lines (192 loc) · 4.91 KB
/
_utility_array.gsc
File metadata and controls
228 lines (192 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Adds the element to the end of the array.
* @param arr The array to add the element to.
* @param element The element to add to the array.
* @return The array with the element added to it.
*/
array_push_back(arr, element)
{
arr[arr.size] = element;
return arr;
}
/**
* Removes the last element in the array.
* @param arr The array to remove the element from.
* @return The array with the element removed from it
*/
array_pop_back(arr)
{
assert(arr.size > 0);
arr[arr.size - 1] = undefined;
return arr;
}
/**
* Adds the element to the front of the array.
* @param arr The array to add the element to.
* @param element The element to add to the array.
* @return The array with the element added to it.
*/
array_push_front(arr, element)
{
for (i = arr.size; i > 0; i--)
{
arr[i] = arr[i - 1];
}
arr[0] = element;
return arr;
}
/**
* Removes the first element in the array.
* @param arr The array to remove the element from.
* @return The array with the element removed from it.
*/
array_pop_front(arr)
{
for (i = 1; i < arr.size; i++)
{
arr[i - 1] = arr[i];
}
arr[arr.size - 1] = undefined;
return arr;
}
/**
* Combines the elements of both arrays into a single array.
* @param arrA The array forming the first half of the result.
* @param arrB The array forming the last half of the result.
* @return An array holding all the elements of both arrays.
*/
array_concat(arrA, arrB)
{
for (i = 0; i < arrB.size; i++)
{
arrA[arrA.size] = arrB[i];
}
return arrA;
}
/**
* Inserts the given element into the specified position.
* @param arr The array to add the element into.
* @param element The element to add to the array.
* @param pos The position within the array to add the element.
* @return The array with the element added into the specified position.
*/
array_insert_element(arr, element, pos)
{
for (i = arr.size; i > pos; i--)
{
arr[i] = arr[i - 1];
}
arr[pos] = element;
return arr;
}
/**
* Removes the matching element from the array.
* @param arr The array to remove the element from.
* @param element The element to remove from the array.
* @return The array with the element removed.
*/
array_remove_element(arr, element)
{
for (i = 0; i < arr.size; i++)
{
if (arr[i] == element)
{
for (j = i + 1; j < arr.size; j++)
{
arr[j - 1] = arr[j];
}
arr[arr.size - 1] = undefined;
// NOTE: uncomment to remove the element only once.
//break;
}
}
return arr;
}
/**
* Removes the element at the specified index.
* @param arr The array to remove an element from.
* @param index The index of the element to be removed.
* @return The array with the element at the specified index removed.
*/
array_remove_at(arr, index)
{
assert(index >= 0 && arr.size < index);
for (i = index + 1; i < arr.size; i++)
{
arr[i - 1] = arr[i];
}
arr[arr.size - 1] = undefined;
return arr;
}
/**
* Inserts array B into the specified position within array A.
* @param arrA The array to add to.
* @param arrB The array to insert.
* @param pos The position in array A to insert array B into.
* @return An array with the elements of array B insert into array A at the specified position.
*/
array_insert_array(arrA, arrB, pos)
{
assert(isDefined(pos) && pos >= 0 && pos < arrA.length);
output = [];
for (i = 0; i < pos; i++)
{
output[output.size] = arrA[i];
}
for (i = 0; i < arrB; i++)
{
output[output.size] = arrB[i];
}
for (i = pos; i < arrA.size; i++)
{
output[output.size] = arrA[i];
}
return output;
}
/**
* Get the index of the specified element within the array.
* @param arr The array that holds the element.
* @param element The element to look for in the array.
* @return -1 if not found; otherwise, the index of the element within the array.
*/
array_index_of(arr, element)
{
for (i = 0; i < arr.size; i++)
{
if (arr[i] == element)
return i;
}
return -1;
}
//==============================================================================
// SORTING - SPECIAL CASES
//==============================================================================
/#
array_sort(arr)
{
assert(arr.size > 0);
output = [];
output[0] = arr[0];
for (i = 1; i < arr.size; i++)
{
inserted = false;
for (j = 0; j < output.size && !inserted; j++)
{
if (output[j] > arr[i])
{
for (k = output.size; k > j; k--)
{
output[k] = output[k - 1];
}
output[j] = arr[i];
inserted = true;
}
}
if (!inserted)
{
output[output.size] = arr[i];
}
}
return output;
}
#/