/* * Holds procs to help with list operations * Contains groups: * Misc * Sorting */ /* * Misc */ /* * ## Lazylists * * * What is a lazylist? * * True to its name a lazylist is a lazy instantiated list. * It is a list that is only created when necessary (when it has elements) and is null when empty. * * * Why use a lazylist? * * Lazylists save memory - an empty list that is never used takes up more memory than just `null`. * * * When to use a lazylist? * * Lazylists are best used on hot types when making lists that are not always used. * * For example, if you were adding a list to all atoms that tracks the names of people who touched it, * you would want to use a lazylist because most atoms will never be touched by anyone. * * * How do I use a lazylist? * * A lazylist is just a list you defined as `null` rather than `list()`. * Then, you use the LAZY* macros to interact with it, which are essentially null-safe ways to interact with a list. * * Note that you probably should not be using these macros if your list is not a lazylist. * This will obfuscate the code and make it a bit harder to read and debug. * * Generally speaking you shouldn't be checking if your lazylist is `null` yourself, the macros will do that for you. * Remember that LAZYLEN (and by extension, length) will return 0 if the list is null. */ ///Initialize the lazylist #define LAZYINITLIST(L) if (!L) { L = list(); } ///Returns the key of the submitted item in the list #define LAZYFIND(L, V) (L ? L.Find(V) : 0) /// Returns the top (last) element from the list, does not remove it from the list. Stack functionality. /proc/peek(list/target_list) var/list_length = length(target_list) if(list_length != 0) return target_list[list_length] //Returns a list in plain english as a string /proc/english_list(var/list/input, nothing_text = "nothing", and_text = " and ", comma_text = ", ", final_comma_text = ",") // this proc cannot be merged with counting_english_list to maintain compatibility // with shoddy use of this proc for code logic and for cases that require original order switch(input.len) if(0) return nothing_text if(1) return "[input[1]]" if(2) return "[input[1]][and_text][input[2]]" else return "[jointext(input, comma_text, 1, -1)][final_comma_text][and_text][input[input.len]]" // Determiner constants #define DET_NONE 0x00 #define DET_DEFINITE 0x01 // the #define DET_INDEFINITE 0x02 // a, an, some #define DET_AUTO 0x04 //Returns a newline-separated list that counts equal-ish items, outputting count and item names, optionally with icons and specific determiners /proc/counting_english_list(var/list/input, var/mob/user, output_icons = TRUE, determiners = DET_NONE, nothing_text = "nothing", line_prefix = "\t", first_item_prefix = "\n", last_item_suffix = "\n", and_text = "\n", comma_text = "\n", final_comma_text = ",") //CHOMPEdit var/list/counts = list() // counted input items var/list/items = list() // actual objects for later reference (for icons and formatting) // count items for(var/item in input) var/name = "[item]" // index items by name; usually works fairly well for loose equality if(name in counts) counts[name]++ else counts[name] = 1 items.Add(item) // assemble the output list var/list/out = list() var/i = 0 for(var/item in items) var/name = "[item]" var/count = counts[name] var/item_str = line_prefix if(count > 1) item_str += "[count]x " if(isatom(item)) // atoms/items/objects can be pretty and whatnot var/atom/A = item if(output_icons && isicon(A.icon) && !ismob(A)) // mobs tend to have unusable icons item_str += "[icon2html(A,user)] " //CHOMPEdit switch(determiners) if(DET_NONE) item_str += A.name if(DET_DEFINITE) item_str += "\the [A]" if(DET_INDEFINITE) item_str += "\a [A]" else item_str += name else // non-atoms use plain string conversion item_str += name if(i == 0) item_str = first_item_prefix + item_str if(i == items.len - 1) item_str = item_str + last_item_suffix out.Add(item_str) i++ // finally return the list using regular english_list builder return english_list(out, nothing_text, and_text, comma_text, final_comma_text) //A "preset" for counting_english_list that displays the list "inline" (comma separated) /proc/inline_counting_english_list(var/list/input, output_icons = TRUE, determiners = DET_NONE, nothing_text = "nothing", and_text = " and ", comma_text = ", ", final_comma_text = "", line_prefix = "", first_item_prefix = "", last_item_suffix = "") return counting_english_list(input, output_icons, determiners, nothing_text, and_text, comma_text, final_comma_text) //Returns list element or null. Should prevent "index out of bounds" error. /proc/listgetindex(var/list/list,index) if(istype(list) && list.len) if(isnum(index)) if(InRange(index,1,list.len)) return list[index] else if(index in list) return list[index] return //Return either pick(list) or null if list is not of type /list or is empty /proc/safepick(list/list) if(!islist(list) || !list.len) return return pick(list) //Checks if the list is empty /proc/isemptylist(list/list) if(!list.len) return 1 return 0 //Checks for specific types in a list /proc/is_type_in_list(var/atom/A, var/list/L) for(var/type in L) if(istype(A, type)) return 1 return 0 //Checks for specific paths in a list /proc/is_path_in_list(var/atom/A, var/list/L) for(var/path in L) if(ispath(A, path)) return 1 return 0 ////////////////////////////////////////////////////// // "typecache" utilities - Making and searching them ////////////////////////////////////////////////////// //Checks for specific types in specifically structured (Assoc "type" = TRUE) lists ('typecaches') /proc/is_type_in_typecache(atom/A, list/L) if(!LAZYLEN(L) || !A) return FALSE return L[A.type] //returns a new list with only atoms that are in typecache L /proc/typecache_filter_list(list/atoms, list/typecache) . = list() for(var/atom/A as anything in atoms) if(typecache[A.type]) . += A /proc/typecache_filter_list_reverse(list/atoms, list/typecache) . = list() for(var/atom/A as anything in atoms) if(!typecache[A.type]) . += A /proc/typecache_filter_multi_list_exclusion(list/atoms, list/typecache_include, list/typecache_exclude) . = list() for(var/atom/A as anything in atoms) if(typecache_include[A.type] && !typecache_exclude[A.type]) . += A //Like typesof() or subtypesof(), but returns a typecache instead of a list /proc/typecacheof(path, ignore_root_path, only_root_path = FALSE) if(ispath(path)) var/list/types = list() if(only_root_path) types = list(path) else types = ignore_root_path ? subtypesof(path) : typesof(path) var/list/L = list() for(var/T in types) L[T] = TRUE return L else if(islist(path)) var/list/pathlist = path var/list/L = list() if(ignore_root_path) for(var/P in pathlist) for(var/T in subtypesof(P)) L[T] = TRUE else for(var/P in pathlist) if(only_root_path) L[P] = TRUE else for(var/T in typesof(P)) L[T] = TRUE return L ////////////////////////////////////////////////////// //Empties the list by setting the length to 0. Hopefully the elements get garbage collected /proc/clearlist(list/list) if(istype(list)) list.len = 0 return //Removes any null entries from the list /proc/listclearnulls(list/list) if(istype(list)) while(null in list) list -= null return /* * Returns list containing all the entries from first list that are not present in second. * If skiprep = 1, repeated elements are treated as one. * If either of arguments is not a list, returns null */ /proc/difflist(var/list/first, var/list/second, var/skiprep=0) if(!islist(first) || !islist(second)) return var/list/result = list() if(skiprep) for(var/e in first) if(!(e in result) && !(e in second)) result += e else result = first - second return result /** * Removes any null entries from the list * Returns TRUE if the list had nulls, FALSE otherwise **/ /proc/list_clear_nulls(list/list_to_clear) return (list_to_clear.RemoveAll(null) > 0) /* Two lists may be different (A!=B) even if they have the same elements. This actually tests if they have the same entries and values. */ /proc/same_entries(var/list/first, var/list/second) if(!islist(first) || !islist(second)) return 0 if(length(first) != length(second)) return 0 for(var/entry in first) if(!(entry in second) || (first[entry] != second[entry])) return 0 return 1 /* Checks if a list has the same entries and values as an element of big. */ /proc/in_as_list(var/list/little, var/list/big) if(!islist(big)) return 0 for(var/element in big) if(same_entries(little, element)) return 1 return 0 /* * Returns list containing entries that are in either list but not both. * If skipref = 1, repeated elements are treated as one. * If either of arguments is not a list, returns null */ /proc/uniquemergelist(var/list/first, var/list/second, var/skiprep=0) if(!islist(first) || !islist(second)) return var/list/result = list() if(skiprep) result = difflist(first, second, skiprep)+difflist(second, first, skiprep) else result = first ^ second return result //Pretends to pick an element based on its weight but really just seems to pick a random element. /proc/pickweight(list/L) var/total = 0 var/item for (item in L) if (!L[item]) L[item] = 1 total += L[item] total = rand(1, total) for (item in L) total -=L [item] if (total <= 0) return item return null //Pick a random element from the list and remove it from the list. /proc/pick_n_take(list/listfrom) if (listfrom.len > 0) var/picked = pick(listfrom) listfrom -= picked return picked return null //Returns the top(last) element from the list and removes it from the list (typical stack function) /proc/pop(list/listfrom) if (listfrom.len > 0) var/picked = listfrom[listfrom.len] listfrom.len-- return picked return null //Returns the next element in parameter list after first appearance of parameter element. If it is the last element of the list or not present in list, returns first element. /proc/next_in_list(element, list/L) for(var/i=1, i= 1; i--) output += L[i] return output //Randomize: Return the list in a random order /proc/shuffle(var/list/L) if(!L) return L = L.Copy() for(var/i=1; i current_index) current_index++ current_item = sorted_list[current_index] current_item_value = current_item:dd_SortValue() current_sort_object_value = current_sort_object:dd_SortValue() if (current_sort_object_value < current_item_value) high_index = current_index - 1 else if (current_sort_object_value > current_item_value) low_index = current_index + 1 else // current_sort_object == current_item low_index = current_index break // Insert before low_index. insert_index = low_index // Special case adding to end of list. if (insert_index > sorted_list.len) sorted_list += current_sort_object continue // Because BYOND lists don't support insert, have to do it by: // 1) taking out bottom of list, 2) adding item, 3) putting back bottom of list. list_bottom = sorted_list.Copy(insert_index) sorted_list.Cut(insert_index) sorted_list += current_sort_object sorted_list += list_bottom return sorted_list */ /proc/dd_sortedtextlist(list/incoming, case_sensitive = 0) // Returns a new list with the text values sorted. // Use binary search to order by sortValue. // This works by going to the half-point of the list, seeing if the node in question is higher or lower cost, // then going halfway up or down the list and checking again. // This is a very fast way to sort an item into a list. var/list/sorted_text = new() var/low_index var/high_index var/insert_index var/midway_calc var/current_index var/current_item var/list/list_bottom var/sort_result var/current_sort_text for (current_sort_text in incoming) low_index = 1 high_index = sorted_text.len while (low_index <= high_index) // Figure out the midpoint, rounding up for fractions. (BYOND rounds down, so add 1 if necessary.) midway_calc = (low_index + high_index) / 2 current_index = round(midway_calc) if (midway_calc > current_index) current_index++ current_item = sorted_text[current_index] if (case_sensitive) sort_result = sorttextEx(current_sort_text, current_item) else sort_result = sorttext(current_sort_text, current_item) switch(sort_result) if (1) high_index = current_index - 1 // current_sort_text < current_item if (-1) low_index = current_index + 1 // current_sort_text > current_item if (0) low_index = current_index // current_sort_text == current_item break // Insert before low_index. insert_index = low_index // Special case adding to end of list. if (insert_index > sorted_text.len) sorted_text += current_sort_text continue // Because BYOND lists don't support insert, have to do it by: // 1) taking out bottom of list, 2) adding item, 3) putting back bottom of list. list_bottom = sorted_text.Copy(insert_index) sorted_text.Cut(insert_index) sorted_text += current_sort_text sorted_text += list_bottom return sorted_text /proc/dd_sortedTextList(list/incoming) var/case_sensitive = 1 return dd_sortedtextlist(incoming, case_sensitive) /datum/proc/dd_SortValue() return "[src]" /obj/machinery/dd_SortValue() return "[sanitize_old(name)]" /obj/machinery/camera/dd_SortValue() return "[c_tag]" /datum/alarm/dd_SortValue() return "[sanitize_old(last_name)]" /proc/subtypesof(prototype) return (typesof(prototype) - prototype) //creates every subtype of prototype (excluding prototype) and adds it to list L. //if no list/L is provided, one is created. /proc/init_subtypes(prototype, list/L) if(!istype(L)) L = list() for(var/path in subtypesof(prototype)) L += new path() return L //creates every subtype of prototype (excluding prototype) and adds it to list L as a type/instance pair. //if no list/L is provided, one is created. /proc/init_subtypes_assoc(prototype, list/L) if(!istype(L)) L = list() for(var/path in subtypesof(prototype)) L[path] = new path() return L //Move a single element from position fromIndex within a list, to position toIndex //All elements in the range [1,toIndex) before the move will be before the pivot afterwards //All elements in the range [toIndex, L.len+1) before the move will be after the pivot afterwards //In other words, it's as if the range [fromIndex,toIndex) have been rotated using a <<< operation common to other languages. //fromIndex and toIndex must be in the range [1,L.len+1] //This will preserve associations ~Carnie /proc/moveElement(list/L, fromIndex, toIndex) if(fromIndex == toIndex || fromIndex+1 == toIndex) //no need to move return if(fromIndex > toIndex) ++fromIndex //since a null will be inserted before fromIndex, the index needs to be nudged right by one L.Insert(toIndex, null) L.Swap(fromIndex, toIndex) L.Cut(fromIndex, fromIndex+1) //Move elements [fromIndex,fromIndex+len) to [toIndex-len, toIndex) //Same as moveElement but for ranges of elements //This will preserve associations ~Carnie /proc/moveRange(list/L, fromIndex, toIndex, len=1) var/distance = abs(toIndex - fromIndex) if(len >= distance) //there are more elements to be moved than the distance to be moved. Therefore the same result can be achieved (with fewer operations) by moving elements between where we are and where we are going. The result being, our range we are moving is shifted left or right by dist elements if(fromIndex <= toIndex) return //no need to move fromIndex += len //we want to shift left instead of right for(var/i=0, i toIndex) fromIndex += len for(var/i=0, i> 1;\ var ##TYPECONT/__BIN_ITEM;\ while(__BIN_LEFT < __BIN_RIGHT) {\ __BIN_ITEM = COMPTYPE;\ if(__BIN_ITEM.##COMPARISON <= COMPARE.##COMPARISON) {\ __BIN_LEFT = __BIN_MID + 1;\ } else {\ __BIN_RIGHT = __BIN_MID;\ };\ __BIN_MID = (__BIN_LEFT + __BIN_RIGHT) >> 1;\ };\ __BIN_ITEM = COMPTYPE;\ __BIN_MID = __BIN_ITEM.##COMPARISON > COMPARE.##COMPARISON ? __BIN_MID : __BIN_MID + 1;\ __BIN_LIST.Insert(__BIN_MID, INPUT);\ };\ } while(FALSE) /// A version of deep_copy_list that actually supports associative list nesting: list(list(list("a" = "b"))) will actually copy correctly. /proc/deep_copy_list_alt(list/inserted_list) if(!islist(inserted_list)) return inserted_list var/copied_list = inserted_list.Copy() . = copied_list for(var/key_or_value in inserted_list) if(isnum_safe(key_or_value) || !inserted_list[key_or_value]) continue var/value = inserted_list[key_or_value] var/new_value = value if(islist(value)) new_value = deep_copy_list_alt(value) copied_list[key_or_value] = new_value /// Turns an associative list into a flat list of keys /proc/assoc_to_keys(list/input) var/list/keys = list() for(var/key in input) UNTYPED_LIST_ADD(keys, key) return keys ///compare two lists, returns TRUE if they are the same /proc/compare_list(list/l,list/d) if(!islist(l) || !islist(d)) return FALSE if(l.len != d.len) return FALSE for(var/i in 1 to l.len) if(l[i] != d[i]) return FALSE return TRUE //TG sort_list ///uses sort_list() but uses the var's name specifically. This should probably be using mergeAtom() instead /proc/sort_names(list/list_to_sort, order=1) return sortTim(list_to_sort.Copy(), order >= 0 ? GLOBAL_PROC_REF(cmp_name_asc) : GLOBAL_PROC_REF(cmp_name_dsc)) /// Compares 2 lists, returns TRUE if they are the same /proc/deep_compare_list(list/list_1, list/list_2) if(list_1 == list_2) return TRUE if(!islist(list_1) || !islist(list_2)) return FALSE if(list_1.len != list_2.len) return FALSE for(var/i in 1 to list_1.len) var/key_1 = list_1[i] var/key_2 = list_2[i] if (islist(key_1) && islist(key_2)) if(!deep_compare_list(key_1, key_2)) return FALSE else if(key_1 != key_2) return FALSE if(istext(key_1) || islist(key_1) || ispath(key_1) || isdatum(key_1) || key_1 == world) var/value_1 = list_1[key_1] var/value_2 = list_2[key_1] if (islist(value_1) && islist(value_2)) if(!deep_compare_list(value_1, value_2)) return FALSE else if(value_1 != value_2) return FALSE return TRUE /** * Attempts to convert a numeric keyed alist of (2=second, 1=first) to a list of (first, second). * * If you instead want to discard values and keep only keys, just do list + alist. * * Arguments: * * to_flatten - The alist with sequential numeric keys to extract values from into a normal list. * * assert - Whether to assert every key is numeric and in bounds. */ /proc/flatten_numeric_alist(alist/to_flatten, assert=TRUE) RETURN_TYPE(/list) var/count = length(to_flatten) if(assert) for(var/key in to_flatten) if(!isnum(key) || key < 1 || key > count) CRASH("flatten_numeric_alist not possible for alist: [json_encode(to_flatten)]") var/list/retval = list() for(var/i in 1 to count) retval += to_flatten[i] return retval //CHOMPAdd start /proc/pick_weight(list/list_to_pick) var/total = 0 var/item for(item in list_to_pick) if(!list_to_pick[item]) list_to_pick[item] = 0 total += list_to_pick[item] total = rand(1, total) for(item in list_to_pick) total -= list_to_pick[item] if(total <= 0 && list_to_pick[item]) return item return null ///Converts a bitfield to a list of numbers (or words if a wordlist is provided) /proc/bitfield_to_list(bitfield = 0, list/wordlist) var/list/return_list = list() if(islist(wordlist)) var/max = min(wordlist.len, 24) var/bit = 1 for(var/i in 1 to max) if(bitfield & bit) return_list += wordlist[i] bit = bit << 1 else for(var/bit_number = 0 to 23) var/bit = 1 << bit_number if(bitfield & bit) return_list += bit return return_list //CHOMPAdd end