diff --git a/code/__HELPERS/_lists.dm b/code/__HELPERS/_lists.dm index c8163f980a..631214de98 100644 --- a/code/__HELPERS/_lists.dm +++ b/code/__HELPERS/_lists.dm @@ -9,6 +9,16 @@ * Misc */ +#define LAZYINITLIST(L) if (!L) L = list() +#define UNSETEMPTY(L) if (L && !L.len) L = null +#define LAZYREMOVE(L, I) if(L) { L -= I; if(!L.len) { L = null; } } +#define LAZYADD(L, I) if(!L) { L = list(); } L += I; +#define LAZYACCESS(L, I) (L ? (isnum(I) ? (I > 0 && I <= L.len ? L[I] : null) : L[I]) : null) +#define LAZYSET(L, K, V) if(!L) { L = list(); } L[K] = V; +#define LAZYLEN(L) length(L) +#define LAZYCLEARLIST(L) if(L) L.Cut() +#define SANITIZE_LIST(L) ( islist(L) ? L : list() ) + //Returns a list in plain english as a string /proc/english_list(list/input, nothing_text = "nothing", and_text = " and ", comma_text = ", ", final_comma_text = "" ) var/total = input.len @@ -32,7 +42,7 @@ //Returns list element or null. Should prevent "index out of bounds" error. /proc/listgetindex(list/L, index) - if(istype(L)) + if(LAZYLEN(L)) if(isnum(index) && IsInteger(index)) if(IsInRange(index,1,L.len)) return L[index] @@ -42,7 +52,7 @@ //Return either pick(list) or null if list is not of type /list or is empty /proc/safepick(list/L) - if(istype(L) && L.len) + if(LAZYLEN(L)) return pick(L) //Checks if the list is empty @@ -53,7 +63,7 @@ //Checks for specific types in a list /proc/is_type_in_list(atom/A, list/L) - if(!L || !L.len || !A) + if(!LAZYLEN(L) || !A) return FALSE for(var/type in L) if(istype(A, type)) @@ -62,7 +72,7 @@ //Checks for specific types in specifically structured (Assoc "type" = TRUE) lists ('typecaches') /proc/is_type_in_typecache(atom/A, list/L) - if(!L || !L.len || !A) + if(!LAZYLEN(L) || !A) return FALSE if(ispath(A)) @@ -72,7 +82,7 @@ //Checks for a string in a list /proc/is_string_in_list(string, list/L) - if(!L || !L.len || !string) + if(!LAZYLEN(L) || !string) return for(var/V in L) if(string == V) @@ -81,7 +91,7 @@ //Removes a string from a list /proc/remove_strings_from_list(string, list/L) - if(!L || !L.len || !string) + if(!LAZYLEN(L) || !string) return for(var/V in L) if(V == string) @@ -486,16 +496,6 @@ //Picks from the list, with some safeties, and returns the "default" arg if it fails #define DEFAULTPICK(L, default) ((islist(L) && length(L)) ? pick(L) : default) -#define LAZYINITLIST(L) if (!L) L = list() -#define UNSETEMPTY(L) if (L && !L.len) L = null -#define LAZYREMOVE(L, I) if(L) { L -= I; if(!L.len) { L = null; } } -#define LAZYADD(L, I) if(!L) { L = list(); } L += I; -#define LAZYACCESS(L, I) (L ? (isnum(I) ? (I > 0 && I <= L.len ? L[I] : null) : L[I]) : null) -#define LAZYSET(L, K, V) if(!L) { L = list(); } L[K] = V; -#define LAZYLEN(L) length(L) -#define LAZYCLEARLIST(L) if(L) L.Cut() -#define SANITIZE_LIST(L) ( islist(L) ? L : list() ) - /* Definining a counter as a series of key -> numeric value entries * All these procs modify in place.