From 433bfdba5fc9bf4b92e6a39ed3383dc3db0df1cf Mon Sep 17 00:00:00 2001 From: Remie Richards Date: Tue, 24 May 2016 19:31:56 +0100 Subject: [PATCH 1/2] Adds a DEFAULTPICK() macro, updates an old list helper * DEFAULTPICK() Picks from the list, with some safeties, and returns the "default" arg if it fails * get_key_by_index() didn't need to be as slow as it was, and now it isn't, it's also a macro now KEYBYINDEX() --- code/__HELPERS/lists.dm | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/code/__HELPERS/lists.dm b/code/__HELPERS/lists.dm index afc07638e4b..fcebf346e11 100644 --- a/code/__HELPERS/lists.dm +++ b/code/__HELPERS/lists.dm @@ -238,13 +238,7 @@ return r // Returns the key based on the index -/proc/get_key_by_index(list/L, index) - var/i = 1 - for(var/key in L) - if(index == i) - return key - i++ - return null +#define KEYBYINDEX(L, index) ((index <= L:len) && (index > 0) ? L[index] : null) /proc/count_by_type(list/L, type) var/i = 0 @@ -365,3 +359,6 @@ for(var/i = 1 to l.len) if(islist(.[i])) .[i] = .(.[i]) + +//Picks from the list, with some safeties, and returns the "default" arg if it fails +#define DEFAULTPICK(L, default) ((istype(L, /list) && L:len) ? pick(L) : default) From 20e001a8b8cfb4ae4322cfb0133f22371650317b Mon Sep 17 00:00:00 2001 From: Remie Richards Date: Tue, 24 May 2016 19:35:32 +0100 Subject: [PATCH 2/2] adds one more layer of parenthesis I forgot, cos it worries me to leave it out. --- code/__HELPERS/lists.dm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/__HELPERS/lists.dm b/code/__HELPERS/lists.dm index fcebf346e11..452d2027b5e 100644 --- a/code/__HELPERS/lists.dm +++ b/code/__HELPERS/lists.dm @@ -238,7 +238,7 @@ return r // Returns the key based on the index -#define KEYBYINDEX(L, index) ((index <= L:len) && (index > 0) ? L[index] : null) +#define KEYBYINDEX(L, index) (((index <= L:len) && (index > 0)) ? L[index] : null) /proc/count_by_type(list/L, type) var/i = 0