Pixel-Composer/scripts/array_functions/array_functions.gml

367 lines
7.6 KiB
Plaintext
Raw Normal View History

function array_create_from_list(list) {
2023-02-14 07:37:13 +01:00
if(list == undefined) return [];
if(!ds_exists(list, ds_type_list)) return [];
2023-01-09 03:14:20 +01:00
var arr = array_create(ds_list_size(list));
for( var i = 0; i < ds_list_size(list); i++ )
arr[i] = list[| i];
return arr;
}
2023-01-09 03:14:20 +01:00
function array_safe_set(arr, index, value, fill = 0) {
if(!is_array(arr)) return arr;
if(is_array(index)) return arr;
2023-06-10 13:59:45 +02:00
if(index < 0) return arr;
if(index >= array_length(arr)) {
var i = array_length(arr);
for(; i <= index; i++ )
arr[i] = fill;
arr[index] = value;
return arr;
}
2022-12-21 02:30:23 +01:00
array_set(arr, index, value);
2023-06-10 13:59:45 +02:00
return arr;
}
2023-06-10 13:59:45 +02:00
function array_safe_length(arr) {
2023-11-20 05:10:55 +01:00
INLINE
return is_array(arr)? array_length(arr) : 0;
}
2023-11-20 05:10:55 +01:00
function array_fill(arr, startIndex, endIndex, value = 0) {
2023-11-17 06:24:31 +01:00
INLINE
for( var i = startIndex; i < endIndex; i++ )
arr[i] = value;
}
2023-11-17 06:24:31 +01:00
function array_resize_fill(arr, size, fill = 0) {
2023-06-10 13:59:45 +02:00
if(size < array_length(arr)) {
array_resize(arr, size);
return arr;
}
var i = array_length(arr);
for(; i < size; i++)
arr[i] = fill;
return arr;
}
2022-12-21 02:30:23 +01:00
2023-02-14 07:37:13 +01:00
enum ARRAY_OVERFLOW {
_default,
loop,
pingpong,
clamp
2023-02-14 07:37:13 +01:00
}
2023-03-13 10:45:56 +01:00
2024-06-24 13:49:54 +02:00
#macro aGetF array_safe_get_fast
function array_safe_get_fast(arr, index, def = 0) {
2024-03-31 05:36:11 +02:00
INLINE
return is_array(arr) && index >= 0 && index < array_length(arr)? arr[index] : def;
}
2024-03-31 05:36:11 +02:00
function array_safe_get(arr, index, def = 0, overflow = ARRAY_OVERFLOW._default) {
2023-11-08 08:38:04 +01:00
INLINE
2024-03-31 05:36:11 +02:00
if(!is_array(arr)) return def;
if(is_array(index)) return def;
2023-02-14 07:37:13 +01:00
var len = array_length(arr);
if(len == 0) return def;
if(overflow == ARRAY_OVERFLOW.clamp) {
index = clamp(index, 0, len - 1);
} else if(overflow == ARRAY_OVERFLOW.loop) {
2023-02-14 07:37:13 +01:00
if(index < 0)
2023-03-07 14:29:47 +01:00
index = len - safe_mod(abs(index), len);
index = index % len;
} else if(overflow == ARRAY_OVERFLOW.pingpong) {
if(index < 0) index = len - safe_mod(abs(index), len);
var _plen = len * 2 - 1;
index = index % _plen;
if(index >= len)
index = _plen - index;
2023-02-14 07:37:13 +01:00
}
2023-01-17 08:11:55 +01:00
if(index < 0) return def;
if(index >= len) return def;
2023-07-12 16:28:32 +02:00
return arr[index] == undefined? def : arr[index];
}
2022-09-27 06:37:28 +02:00
function array_push_create(arr, val) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
2023-05-30 11:09:15 +02:00
if(!is_array(arr)) return [ val ];
array_push(arr, val);
return arr;
}
2023-03-13 10:45:56 +01:00
function array_get_decimal(arr, index, color = false) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
if(!is_array(arr)) return 0;
2024-03-31 05:36:11 +02:00
if(frac(index) == 0) return array_safe_get_fast(arr, index);
2023-07-11 14:18:23 +02:00
2024-03-31 05:36:11 +02:00
var v0 = array_safe_get_fast(arr, floor(index));
var v1 = array_safe_get_fast(arr, floor(index) + 1);
2023-07-11 14:18:23 +02:00
return color?
merge_color(v0, v1, frac(index)) :
lerp(v0, v1, frac(index));
}
2023-07-11 14:18:23 +02:00
function array_exists(arr, val) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-25 20:12:40 +02:00
self.__temp_val = val;
2023-07-29 10:10:48 +02:00
if(!is_array(arr)) return false;
2023-07-25 20:12:40 +02:00
return array_any(arr, function(_val, _ind) {
return isEqual(_val, self.__temp_val);
});
}
2022-09-27 06:37:28 +02:00
function array_overlap(arr0, arr1) {
2023-11-08 08:38:04 +01:00
INLINE
self.__temp_arr = arr1;
if(!is_array(arr0)) return false;
if(!is_array(arr1)) return false;
return array_any(arr0, function(_val, _ind) {
return array_exists(self.__temp_arr, _val);
});
}
function array_empty(arr) { INLINE return is_array(arr) && array_length(arr) == 0; }
function array_valid(arr) { INLINE return is_array(arr) && array_length(arr) > 0; }
function array_invalid(arr) { INLINE return !is_array(arr) || array_length(arr) == 0; }
2023-03-28 06:58:28 +02:00
function array_find(arr, val) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-25 20:12:40 +02:00
self.__temp_val = val;
2023-07-29 10:10:48 +02:00
if(!is_array(arr)) return -1;
2024-07-03 05:16:46 +02:00
return array_find_index(arr, function(_val, _ind) { return isEqual(_val, self.__temp_val); });
}
2022-09-27 06:37:28 +02:00
function array_find_string(arr, val) {
2024-05-25 04:51:52 +02:00
INLINE
self.__temp_val = string_lower(val);
if(!is_array(arr)) return -1;
2024-10-06 11:23:58 +02:00
return array_find_index(arr, function(_val, _ind) { return string_lower(_val) == self.__temp_val; });
}
2024-05-25 04:51:52 +02:00
function array_remove(arr, val) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
if(!is_array(arr)) return;
2022-09-27 06:37:28 +02:00
if(!array_exists(arr, val)) return;
var ind = array_find(arr, val);
array_delete(arr, ind, 1);
}
2022-12-12 09:08:03 +01:00
function array_push_unique(arr, val) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
if(!is_array(arr)) return;
2022-12-12 09:08:03 +01:00
if(array_exists(arr, val)) return;
array_push(arr, val);
}
2022-12-13 14:11:39 +01:00
function array_insert_unique(arr, ind, val) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
if(!is_array(arr)) return;
2023-02-17 11:31:33 +01:00
if(array_exists(arr, val)) return;
array_insert(arr, ind, val);
}
2023-02-17 11:31:33 +01:00
function array_append(arr, arr0) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
2024-06-14 12:46:25 +02:00
if(!is_array(arr)) return arr;
if(!is_array(arr0)) { array_push(arr, arr0); return arr; }
2023-07-29 10:10:48 +02:00
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(arr0); i < n; i++ )
2022-12-13 14:11:39 +01:00
array_push(arr, arr0[i]);
2023-02-14 07:37:13 +01:00
return arr;
}
2023-02-14 07:37:13 +01:00
function array_merge() {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
2022-12-13 14:11:39 +01:00
var arr = [];
2023-07-29 10:10:48 +02:00
for( var i = 0; i < argument_count; i++ )
2022-12-13 14:11:39 +01:00
array_append(arr, argument[i]);
return arr;
}
2022-12-16 09:18:09 +01:00
function array_clone(arr, _depth = -1) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
2023-12-24 12:06:44 +01:00
if(_depth == 0) return arr;
2023-07-29 10:10:48 +02:00
if(!is_array(arr)) return arr;
2023-02-14 13:44:46 +01:00
var _res = [];
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(arr); i < n; i++ )
2023-12-24 12:06:44 +01:00
_res[i] = array_clone(arr[i], _depth--);
2022-12-16 09:18:09 +01:00
return _res;
}
2023-01-09 03:14:20 +01:00
function array_min(arr) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
if(!is_array(arr) || array_length(arr) == 0) return 0;
2023-01-09 03:14:20 +01:00
var mn = arr[0];
2023-07-29 10:10:48 +02:00
for( var i = 0, n = array_length(arr); i < n; i++ )
mn = min(mn, arr[i]);
return mn;
}
2023-01-09 03:14:20 +01:00
function array_max(arr) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
if(!is_array(arr) || array_length(arr) == 0) return 0;
2023-01-09 03:14:20 +01:00
var mx = arr[0];
2023-07-29 10:10:48 +02:00
for( var i = 0, n = array_length(arr); i < n; i++ )
mx = max(mx, arr[i]);
return mx;
}
2023-01-25 06:49:00 +01:00
function array_get_dimension(arr) {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
2023-01-25 06:49:00 +01:00
return is_array(arr)? array_length(arr) : 1;
}
2023-01-25 06:49:00 +01:00
function array_shape(arr, first = true, isSurface = false) {
2023-01-25 06:49:00 +01:00
if(!is_array(arr)) {
if(isSurface && is_surface(arr))
2023-09-08 21:37:36 +02:00
return (first? "" : " x ") + string(surface_get_width_safe(arr)) + " x " + string(surface_get_height_safe(arr)) + " px";
2023-01-25 06:49:00 +01:00
return "";
}
var dim = string(array_length(arr));
if(array_length(arr))
dim += array_shape(arr[0], false, isSurface);
return (first? "" : " x ") + dim;
}
2023-02-14 13:44:46 +01:00
function __array_get_depth(arr) { // Read all member, slower
INLINE
if(!is_array(arr)) return 0;
var dep = 0;
for (var i = 0, n = array_length(arr); i < n; i++)
dep = max(dep, __array_get_depth(arr[i]));
return 1 + dep;
}
function array_get_depth(arr) { // Read only the first member, faster
2023-11-08 08:38:04 +01:00
INLINE
2023-10-27 15:42:17 +02:00
if(!is_array(arr)) return 0;
var d = 0;
var p = arr;
while(is_array(p) && !array_empty(p)) {
d++;
p = p[0];
}
return d;
}
2023-10-27 15:42:17 +02:00
function array_spread(arr, _arr = [], _minDepth = 0) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-27 15:42:17 +02:00
2023-10-28 04:07:43 +02:00
if(array_get_depth(arr) == _minDepth) {
2023-02-14 13:44:46 +01:00
array_push(_arr, arr);
return _arr;
}
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(arr); i < n; i++ )
2023-10-27 15:42:17 +02:00
array_spread(arr[i], _arr, _minDepth);
2023-02-14 13:44:46 +01:00
return _arr;
}
function array_verify_new(arr, length) {
INLINE
if(!is_array(arr)) return array_create(length);
if(array_length(arr) == length) return arr;
arr = array_clone(arr, 1);
array_resize(arr, length);
return arr;
}
function array_verify(arr, length) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-27 15:42:17 +02:00
if(!is_array(arr)) return array_create(length);
if(array_length(arr) == length) return arr;
array_resize(arr, length);
return arr;
}
function array_verify_ext(arr, length, generator) {
INLINE
if(!is_array(arr)) return array_create_ext(length, generator);
var _len = array_length(arr);
if(_len == length) return arr;
array_resize(arr, length);
var i = _len;
repeat(length - _len)
arr[i++] = generator();
return arr;
}
function array_insert_after(arr, before, values) {
INLINE
var _ind = array_find(arr, before) + 1;
for( var i = 0, n = array_length(values); i < n; i++ )
array_insert(arr, _ind + i, values[i]);
}
function array_insert_before(arr, before, values) {
INLINE
var _ind = array_find(arr, before);
for( var i = 0, n = array_length(values); i < n; i++ )
array_insert(arr, _ind + i, values[i]);
}
2024-03-14 14:35:19 +01:00
#macro array_equals array_equals_overwrite
#macro __array_equals array_equals
function array_equals_overwrite(arr1, arr2) {
2024-03-14 14:35:19 +01:00
if(!is_array(arr1) && is_array(arr2)) return false;
if( is_array(arr1) && !is_array(arr2)) return false;
if(!is_array(arr1) && !is_array(arr2)) return arr1 == arr2;
return __array_equals(arr1, arr2)
}