Pixel-Composer/scripts/array_functions/array_functions.gml

217 lines
4.5 KiB
Text
Raw Normal View History

2023-01-09 03:14:20 +01:00
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-06-10 13:59:45 +02:00
function array_safe_set(arr, index, value, fill = 0) {
if(!is_array(arr)) return arr;
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;
}
function array_resize_fill(arr, size, fill = 0) {
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
}
2023-03-13 10:45:56 +01:00
2023-02-14 07:37:13 +01:00
function array_safe_get(arr, index, def = 0, overflow = ARRAY_OVERFLOW._default) {
2023-04-16 16:24:17 +02:00
gml_pragma("forceinline");
2023-02-14 07:37:13 +01:00
if(!is_array(arr)) return def;
if(overflow == ARRAY_OVERFLOW.loop) {
2023-03-07 14:29:47 +01:00
var len = array_length(arr);
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 = safe_mod(index, len);
2023-02-14 07:37:13 +01:00
}
2023-01-17 08:11:55 +01:00
if(index < 0) return def;
2022-09-27 06:37:28 +02:00
if(index >= array_length(arr)) 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
}
2023-05-30 11:09:15 +02:00
function array_push_create(arr, val) {
2023-07-29 10:10:48 +02:00
gml_pragma("forceinline");
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
2023-07-11 14:18:23 +02:00
function array_get_decimal(arr, index, color = false) {
2023-07-29 10:10:48 +02:00
gml_pragma("forceinline");
if(!is_array(arr)) return 0;
2023-07-11 14:18:23 +02:00
if(frac(index) == 0) return array_safe_get(arr, index);
var v0 = array_safe_get(arr, floor(index));
var v1 = array_safe_get(arr, floor(index) + 1);
return color?
merge_color(v0, v1, frac(index)) :
lerp(v0, v1, frac(index));
}
2022-09-27 06:37:28 +02:00
function array_exists(arr, val) {
2023-07-25 20:12:40 +02:00
gml_pragma("forceinline");
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
}
2023-03-28 06:58:28 +02:00
function array_empty(arr) {
2023-07-25 20:12:40 +02:00
gml_pragma("forceinline");
2023-07-29 10:10:48 +02:00
return is_array(arr) && array_length(arr) == 0;
2023-03-28 06:58:28 +02:00
}
2022-09-27 06:37:28 +02:00
function array_find(arr, val) {
2023-07-25 20:12:40 +02:00
gml_pragma("forceinline");
self.__temp_val = val;
2023-07-29 10:10:48 +02:00
if(!is_array(arr)) return -1;
2023-07-25 20:12:40 +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_remove(arr, val) {
2023-07-29 10:10:48 +02:00
gml_pragma("forceinline");
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-07-29 10:10:48 +02:00
gml_pragma("forceinline");
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
}
2023-02-17 11:31:33 +01:00
function array_insert_unique(arr, ind, val) {
2023-07-29 10:10:48 +02:00
gml_pragma("forceinline");
if(!is_array(arr)) return;
2023-02-17 11:31:33 +01:00
if(array_exists(arr, val)) return;
array_insert(arr, ind, val);
}
2022-12-13 14:11:39 +01:00
function array_append(arr, arr0) {
2023-07-29 10:10:48 +02:00
gml_pragma("forceinline");
if(!is_array(arr)) return arr;
if(!is_array(arr0)) return arr;
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;
}
2022-12-13 14:11:39 +01:00
function array_merge() {
2023-07-29 10:10:48 +02:00
gml_pragma("forceinline");
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) {
2023-07-29 10:10:48 +02:00
gml_pragma("forceinline");
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-02-14 13:44:46 +01:00
_res[i] = array_clone(arr[i]);
2022-12-16 09:18:09 +01:00
return _res;
2023-01-09 03:14:20 +01:00
}
function array_min(arr) {
2023-07-29 10:10:48 +02:00
gml_pragma("forceinline");
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-07-29 10:10:48 +02:00
gml_pragma("forceinline");
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-07-29 10:10:48 +02:00
gml_pragma("forceinline");
2023-01-25 06:49:00 +01:00
return is_array(arr)? array_length(arr) : 1;
}
function array_shape(arr, first = true, isSurface = false) {
if(!is_array(arr)) {
if(isSurface && is_surface(arr))
return (first? "" : " x ") + string(surface_get_width(arr)) + " x " + string(surface_get_height(arr)) + " px";
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_spread(arr, _arr = []) {
if(!is_array(arr)) {
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-02-14 13:44:46 +01:00
array_spread(arr[i], _arr);
return _arr;
2022-01-13 05:24:03 +01:00
}