Skip to content

Commit e064e67

Browse files
committed
Revert "Revert "static functions""
This reverts commit 7c5ad5f.
1 parent 7c5ad5f commit e064e67

File tree

3 files changed

+53
-39
lines changed

3 files changed

+53
-39
lines changed

ArrayClass.yymps

240 Bytes
Binary file not shown.

Sample+Tests/scripts/ArrayClass/ArrayClass.gml

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Array() constructor {
2828
///@function append(value, value2, ..)
2929
///@description Adds (a) value(s) to the end of the array
3030
///@param {any} value
31-
append = function(value) {
31+
static append = function(value) {
3232
for(var i = 0; i < argument_count; ++i) {
3333
var val = argument[i]
3434
content[size] = val;
@@ -41,7 +41,7 @@ function Array() constructor {
4141
///@function add(value, value2, ..)
4242
///@description Mirrors append() method
4343
///@param {any} value
44-
add = function(value) {
44+
static add = function(value) {
4545
for(var i = 0; i < argument_count; ++i) {
4646
var val = argument[i]
4747
content[size] = val;
@@ -54,7 +54,7 @@ function Array() constructor {
5454
///@function concat(other)
5555
///@description Adds every element of the second array to this array
5656
///@param {Array/array} other
57-
concat = function(_other) {
57+
static concat = function(_other) {
5858
if(!is_Array(_other)) {
5959
if is_array(_other) {
6060
_other = array_to_Array(_other)
@@ -74,7 +74,7 @@ function Array() constructor {
7474

7575
///@function copy()
7676
///@description Returns a copy of the array object
77-
copy = function() {
77+
static copy = function() {
7878
ans = new Array();
7979

8080
forEach(function(el) {
@@ -86,7 +86,7 @@ function Array() constructor {
8686

8787
///@function clear()
8888
///@description clears an array object
89-
clear = function() {
89+
static clear = function() {
9090
content = [];
9191
size = 0;
9292

@@ -96,7 +96,7 @@ function Array() constructor {
9696
///@function remove(pos)
9797
///@description removes the value at given position
9898
///@param {real} pos
99-
remove = function(pos) {
99+
static remove = function(pos) {
100100
if(pos < 0)
101101
pos += size;
102102

@@ -122,13 +122,13 @@ function Array() constructor {
122122

123123
///@function empty()
124124
///@description Returns true if the array is empty and false otherwise
125-
empty = function() {
125+
static empty = function() {
126126
return size == 0;
127127
}
128128

129129
///@function equal(other)
130130
///@description Returns true if arrays are equal and false otherwise
131-
equal = function(_other) {
131+
static equal = function(_other) {
132132
if(!is_Array(_other)) {
133133
__throw( "TypeError: trying to compare "+typeof(_other)+" with Array");
134134
return false;
@@ -163,7 +163,7 @@ function Array() constructor {
163163

164164
///@function exists(value)
165165
///@description Returns true if the value exists in the array and false otherwise
166-
exists = function(_val) {
166+
static exists = function(_val) {
167167
val = _val;
168168
ans = false;
169169

@@ -183,7 +183,7 @@ function Array() constructor {
183183
/// Function func gets (x, *pos) as input
184184
/// Note: Clean function. Does not affect the original array!
185185
///@param {function} func
186-
filter = function(_func) {
186+
static filter = function(_func) {
187187
func = _func;
188188
ans = new Array();
189189

@@ -200,7 +200,7 @@ function Array() constructor {
200200
///@function find(value)
201201
///@description finds a value and returns its position. -1 if not found
202202
///@param {any} value
203-
find = function(_val) {
203+
static find = function(_val) {
204204
val = _val;
205205
ans = -1;
206206

@@ -217,7 +217,7 @@ function Array() constructor {
217217
///@function findAll(value)
218218
///@description finds all places a value appears and returns an Array with all the positions. empty set if not found
219219
///@param {any} value
220-
findAll = function(_val) {
220+
static findAll = function(_val) {
221221
val = _val;
222222
ans = new Array();
223223

@@ -231,7 +231,7 @@ function Array() constructor {
231231

232232
///@function first()
233233
///@description Returns the first value of the array
234-
first = function() {
234+
static first = function() {
235235
return get(0);
236236
}
237237

@@ -240,7 +240,7 @@ function Array() constructor {
240240
/// Function func gets (x, *pos) as arguments
241241
/// Note: Loop will stop immediately if the function returns anything but zero or undefined
242242
///@param {function} func(x, *pos)
243-
forEach = function(func) {
243+
static forEach = function(func) {
244244
for(var i = 0; i < size; i++) {
245245
var res = func(get(i), i)
246246
if(!is_undefined(res) and res != 0) {
@@ -254,7 +254,7 @@ function Array() constructor {
254254
///@function get(pos)
255255
///@description Returns value at given pos
256256
///@param {real} pos
257-
get = function(pos) {
257+
static get = function(pos) {
258258
if(pos < 0)
259259
pos += size; //i.e. Array.get(-1) = Array.last()
260260

@@ -275,7 +275,7 @@ function Array() constructor {
275275
///@description inserts a value into the array at given position
276276
///@param {real} pos
277277
///@param {any} value
278-
insert = function(pos, value) {
278+
static insert = function(pos, value) {
279279
if(pos < 0)
280280
pos += size;
281281

@@ -299,7 +299,7 @@ function Array() constructor {
299299
///@function lambda(func)
300300
///@description Loops through the array and applies the function to each element
301301
///@param {function} func(x, *pos)
302-
lambda = function(func) {
302+
static lambda = function(func) {
303303
for(var i = 0; i < size; i++) {
304304
set(i, func(get(i), i) );
305305
}
@@ -309,13 +309,13 @@ function Array() constructor {
309309

310310
///@function last()
311311
///@description Returns the last value of the array
312-
last = function() {
312+
static last = function() {
313313
return get(-1);
314314
}
315315

316316
///@function _max()
317317
///@description Returns a maximum of the array. Only works with numbers
318-
_max = function() {
318+
static _max = function() {
319319
ans = get(0);
320320

321321
forEach(function(x) {
@@ -334,7 +334,7 @@ function Array() constructor {
334334

335335
///@function _min()
336336
///@description Returns a minimum of the array. Only works with numbers
337-
_min = function() {
337+
static _min = function() {
338338
ans = content[0];
339339

340340
forEach(function(x) {
@@ -356,7 +356,7 @@ function Array() constructor {
356356
///@note IMPORTANT! Don't try to use this with data structures, as results may be unpredictable
357357
/// (Use forEach() with your own logic instead)
358358
///@param {any} value
359-
number = function(_val) {
359+
static number = function(_val) {
360360
val = _val;
361361
ans = 0;
362362

@@ -370,7 +370,7 @@ function Array() constructor {
370370

371371
///@function pop()
372372
///@description removes a value from the end of the array and returns it
373-
pop = function() {
373+
static pop = function() {
374374
ans = last();
375375
if(empty()) {
376376
__throw( "Error: trying to pop value from empty Array");
@@ -384,7 +384,7 @@ function Array() constructor {
384384

385385
///@function popBack()
386386
///@description removes a value from the beginning of the array and returns it
387-
popBack = function() {
387+
static popBack = function() {
388388
ans = first();
389389
remove(0);
390390

@@ -394,13 +394,13 @@ function Array() constructor {
394394
///@function pushBack(value)
395395
///@description inserts a value to the beginning of the array
396396
///@param {any} value
397-
pushBack = function(val) {
397+
static pushBack = function(val) {
398398
insert(0, val);
399399
}
400400

401401
///@function getRandom()
402402
///@description Returns a random element from the array
403-
getRandom = function() {
403+
static getRandom = function() {
404404
var idx = irandom(size-1)
405405
if empty() {
406406
var ans = undefined
@@ -415,7 +415,7 @@ function Array() constructor {
415415
///@function resize(size)
416416
///@description resizes the array. Sizing up leads to filling the empty spots with zeros
417417
///@param {real} size
418-
resize = function(size) {
418+
static resize = function(size) {
419419
if(size < 0) {
420420
__throw( "Error: array size cannot be negative");
421421
return self;
@@ -433,7 +433,7 @@ function Array() constructor {
433433

434434
///@function reverse()
435435
///@description reverses the array, affecting it
436-
reverse = function() {
436+
static reverse = function() {
437437
ans = new Array();
438438
forEach(function(element, pos) {
439439
ans.set(size-pos-1, element);
@@ -445,7 +445,7 @@ function Array() constructor {
445445

446446
///@function reversed()
447447
///@description Returns reversed version of the array, without affecting the original
448-
reversed = function() {
448+
static reversed = function() {
449449
ans = new Array();
450450
forEach(function(element, pos) {
451451
ans.set(size-pos-1, element);
@@ -458,7 +458,7 @@ function Array() constructor {
458458
///@description sets value in the array at given index
459459
///@param {real} pos
460460
///@param {any} item
461-
set = function(pos, value) {
461+
static set = function(pos, value) {
462462
if(pos < 0)
463463
pos += size;
464464

@@ -475,7 +475,7 @@ function Array() constructor {
475475
///@description Returns a slice from the array with given boundaries. If begin > end - returns reversed version
476476
///@param {real} begin
477477
///@param {real} end
478-
slice = function(_begin, _end) {
478+
static slice = function(_begin, _end) {
479479
if(is_undefined(_begin))
480480
_begin = 0;
481481

@@ -506,7 +506,7 @@ function Array() constructor {
506506
///@param {function} func
507507
///@param {real} *startpos Default - 0
508508
///@param {real} *endpos Default - size
509-
sort = function(compare, _begin, _end) {
509+
static sort = function(compare, _begin, _end) {
510510
if(is_undefined(_begin))
511511
_begin = 0;
512512

@@ -536,14 +536,14 @@ function Array() constructor {
536536

537537
///@function sorted(func, *startpos, *endpos)
538538
///@description Mirrors .sort() function, but doesn't affect the original Array
539-
sorted = function(compare, _begin, _end) {
539+
static sorted = function(compare, _begin, _end) {
540540
var ans = copy() // self.copy()
541541
return ans.sort(compare, _begin, _end)
542542
}
543543

544544
///@function shuffle()
545545
///@description shuffles the array (randomly replaces every element)
546-
shuffle = function() {
546+
static shuffle = function() {
547547
// Knuth shuffle implementation
548548
for(var i = size-1; i > 0; --i) {
549549
var j = irandom_range(0, i)
@@ -556,15 +556,15 @@ function Array() constructor {
556556

557557
///@function shuffled()
558558
///@description clean version of .shuffle()
559-
shuffled = function() {
559+
static shuffled = function() {
560560
var ans = copy();
561561
return ans.shuffle();
562562
}
563563

564564
///@function sum()
565565
///@description Returns the sum of all the elements of the array. concats strings.
566566
///NOTE: Works only with strings or numbars and only if all the elements are the same type.
567-
sum = function() {
567+
static sum = function() {
568568
if(is_string(get(0)))
569569
ans = "";
570570
else if(is_numeric(get(0)))
@@ -588,7 +588,7 @@ function Array() constructor {
588588
///@description swaps 2 values at given positions
589589
///@param {real} pos1
590590
///@param {real} pos2
591-
swap = function(pos1, pos2) {
591+
static swap = function(pos1, pos2) {
592592
var temp = get(pos1);
593593
set(pos1, get(pos2));
594594
set(pos2, temp);
@@ -598,7 +598,7 @@ function Array() constructor {
598598

599599
///@function unique()
600600
///@description Returns a copy of this Array object, deleting all duplicates
601-
unique = function() {
601+
static unique = function() {
602602
ans = new Array();
603603

604604
forEach(function(x) {
@@ -613,12 +613,12 @@ function Array() constructor {
613613
append(argument[i])
614614

615615

616-
toString = function() {
616+
static toString = function() {
617617
str = "[";
618618

619619
forEach(function(el, i) {
620620
str += string(el);
621-
if(i < size-1)
621+
if(i < size-1)
622622
str += ", ";
623623
});
624624

Sample+Tests/scripts/Tests/Tests.gml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55
function __Array_test() {
66
show_debug_message("#### STARTING TESTS ####")
77

8+
9+
//gc_enable(false)
10+
11+
812
// Creating
913
var array = new Array(0)
14+
global.array2 = new Array(0, 2, 3, 4, pi, "string")
15+
16+
repeat(10000) {
17+
global.array2.add(1000000)
18+
}
19+
20+
//gc_collect()
1021

1122

1223
// Adding new stuff
@@ -46,6 +57,9 @@ function __Array_test() {
4657

4758
// Chaining methods
4859
show_debug_message("#### CHAINING METHODS ####")
60+
61+
array = new Array(1, "string", pi)
62+
4963
array = array.insert(0, "first")
5064
.insert(1, "second")
5165
.append("last", "last2")

0 commit comments

Comments
 (0)