Pixel-Composer/scripts/array_functions/array_functions.gml

271 lines
5.7 KiB
Text
Raw Normal View History

2023-11-17 06:24:31 +01:00
function array_create_from_list(list) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-01-09 03:14:20 +01:00
2023-11-17 06:24:31 +01:00
function array_safe_set(arr, index, value, fill = 0) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-06-10 13:59:45 +02:00
2023-11-20 05:10:55 +01:00
function array_safe_length(arr) { #region
INLINE
return is_array(arr)? array_length(arr) : 0;
} #endregion
2023-11-17 06:24:31 +01:00
function array_fill(arr, startIndex, endIndex, value = 0) { #region
INLINE
for( var i = startIndex; i < endIndex; i++ )
arr[i] = value;
} #endregion
function array_resize_fill(arr, size, fill = 0) { #region
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;
2023-11-17 06:24:31 +01:00
} #endregion
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-11-17 06:24:31 +01:00
function array_safe_get(arr, index, def = 0, overflow = ARRAY_OVERFLOW._default) { #region
2023-11-08 08:38:04 +01:00
INLINE
if(!is_array(arr)) return def;
if(is_array(index)) return def;
2023-02-14 07:37:13 +01:00
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];
2023-11-17 06:24:31 +01:00
} #endregion
2022-09-27 06:37:28 +02:00
2023-11-17 06:24:31 +01:00
function array_push_create(arr, val) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-03-13 10:45:56 +01:00
2023-11-17 06:24:31 +01:00
function array_get_decimal(arr, index, color = false) { #region
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
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));
2023-11-17 06:24:31 +01:00
} #endregion
2023-07-11 14:18:23 +02:00
2023-11-17 06:24:31 +01:00
function array_exists(arr, val) { #region
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);
});
2023-11-17 06:24:31 +01:00
} #endregion
2022-09-27 06:37:28 +02:00
2023-11-17 06:24:31 +01:00
function array_overlap(arr0, arr1) { #region
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);
});
2023-11-17 06:24:31 +01:00
} #endregion
2023-11-17 06:24:31 +01:00
function array_empty(arr) { #region
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
return is_array(arr) && array_length(arr) == 0;
2023-11-17 06:24:31 +01:00
} #endregion
2023-03-28 06:58:28 +02:00
2023-11-17 06:24:31 +01:00
function array_find(arr, val) { #region
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;
2023-07-25 20:12:40 +02:00
return array_find_index(arr, function(_val, _ind) {
return isEqual(_val, self.__temp_val);
});
2023-11-17 06:24:31 +01:00
} #endregion
2022-09-27 06:37:28 +02:00
2023-11-17 06:24:31 +01:00
function array_remove(arr, val) { #region
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);
2023-11-17 06:24:31 +01:00
} #endregion
2022-12-12 09:08:03 +01:00
2023-11-17 06:24:31 +01:00
function array_push_unique(arr, val) { #region
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);
2023-11-17 06:24:31 +01:00
} #endregion
2022-12-13 14:11:39 +01:00
2023-11-17 06:24:31 +01:00
function array_insert_unique(arr, ind, val) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-02-17 11:31:33 +01:00
2023-11-17 06:24:31 +01:00
function array_append(arr, arr0) { #region
2023-11-08 08:38:04 +01:00
INLINE
2023-07-29 10:10:48 +02:00
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;
2023-11-17 06:24:31 +01:00
} #endregion
2023-02-14 07:37:13 +01:00
2023-11-17 06:24:31 +01:00
function array_merge() { #region
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;
2023-11-17 06:24:31 +01:00
} #endregion
2022-12-16 09:18:09 +01:00
2023-12-24 12:06:44 +01:00
function array_clone(arr, _depth = -1) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-01-09 03:14:20 +01:00
2023-11-17 06:24:31 +01:00
function array_min(arr) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-01-09 03:14:20 +01:00
2023-11-17 06:24:31 +01:00
function array_max(arr) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-01-25 06:49:00 +01:00
2023-11-17 06:24:31 +01:00
function array_get_dimension(arr) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-01-25 06:49:00 +01:00
2023-11-17 06:24:31 +01:00
function array_shape(arr, first = true, isSurface = false) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-02-14 13:44:46 +01:00
2023-11-17 06:24:31 +01:00
function array_get_depth(arr) { #region
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-11-17 06:24:31 +01:00
} #endregion
2023-10-27 15:42:17 +02:00
2023-11-17 06:24:31 +01:00
function array_spread(arr, _arr = [], _minDepth = 0) { #region
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;
2023-11-17 06:24:31 +01:00
} #endregion
2023-11-17 06:24:31 +01:00
function array_verify(arr, length) { #region
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;
2023-11-17 06:24:31 +01:00
} #endregion