diff --git a/code/__HELPERS/_lists.dm b/code/__HELPERS/_lists.dm index 156432f74dc..81b329ec03e 100644 --- a/code/__HELPERS/_lists.dm +++ b/code/__HELPERS/_lists.dm @@ -38,6 +38,7 @@ * TYPECONT: The typepath of the contents of the list * COMPARE: The object to compare against, usualy the same as INPUT * COMPARISON: The variable on the objects to compare + * COMPTYPE: How should the values be compared? Either COMPARE_KEY or COMPARE_VALUE. */ #define BINARY_INSERT(INPUT, LIST, TYPECONT, COMPARE, COMPARISON, COMPTYPE) \ do {\ @@ -49,7 +50,7 @@ var/__BIN_LEFT = 1;\ var/__BIN_RIGHT = __BIN_CTTL;\ var/__BIN_MID = (__BIN_LEFT + __BIN_RIGHT) >> 1;\ - var/##TYPECONT/__BIN_ITEM;\ + var ##TYPECONT/__BIN_ITEM;\ while(__BIN_LEFT < __BIN_RIGHT) {\ __BIN_ITEM = COMPTYPE;\ if(__BIN_ITEM.##COMPARISON <= COMPARE.##COMPARISON) {\ diff --git a/code/controllers/subsystem/runechat.dm b/code/controllers/subsystem/runechat.dm index f590674bde2..c0471a7a089 100644 --- a/code/controllers/subsystem/runechat.dm +++ b/code/controllers/subsystem/runechat.dm @@ -158,7 +158,7 @@ SUBSYSTEM_DEF(runechat) // Handle insertion into the secondary queue if the required time is outside our tracked amounts if (scheduled_destruction >= BUCKET_LIMIT) - BINARY_INSERT(src, SSrunechat.second_queue, datum/chatmessage, src, scheduled_destruction, COMPARE_KEY) + BINARY_INSERT(src, SSrunechat.second_queue, /datum/chatmessage, src, scheduled_destruction, COMPARE_KEY) return // Get bucket position and a local reference to the datum var, it's faster to access this way diff --git a/code/controllers/subsystem/timer.dm b/code/controllers/subsystem/timer.dm index 414c8b3c071..1315388e26e 100644 --- a/code/controllers/subsystem/timer.dm +++ b/code/controllers/subsystem/timer.dm @@ -122,7 +122,7 @@ SUBSYSTEM_DEF(timer) if(ctime_timer.flags & TIMER_LOOP) ctime_timer.spent = 0 ctime_timer.timeToRun = REALTIMEOFDAY + ctime_timer.wait - BINARY_INSERT(ctime_timer, clienttime_timers, datum/timedevent, ctime_timer, timeToRun, COMPARE_KEY) + BINARY_INSERT(ctime_timer, clienttime_timers, /datum/timedevent, ctime_timer, timeToRun, COMPARE_KEY) else qdel(ctime_timer) @@ -525,7 +525,7 @@ SUBSYSTEM_DEF(timer) else if (timeToRun >= TIMER_MAX) L = SStimer.second_queue if(L) - BINARY_INSERT(src, L, datum/timedevent, src, timeToRun, COMPARE_KEY) + BINARY_INSERT(src, L, /datum/timedevent, src, timeToRun, COMPARE_KEY) return // Get a local reference to the bucket list, this is faster than referencing the datum diff --git a/code/modules/actionspeed/_actionspeed_modifier.dm b/code/modules/actionspeed/_actionspeed_modifier.dm index fe244e66532..a2870be6f46 100644 --- a/code/modules/actionspeed/_actionspeed_modifier.dm +++ b/code/modules/actionspeed/_actionspeed_modifier.dm @@ -69,7 +69,7 @@ GLOBAL_LIST_EMPTY(actionspeed_modification_cache) return TRUE remove_actionspeed_modifier(existing, FALSE) if(length(actionspeed_modification)) - BINARY_INSERT(type_or_datum.id, actionspeed_modification, datum/actionspeed_modifier, type_or_datum, priority, COMPARE_VALUE) + BINARY_INSERT(type_or_datum.id, actionspeed_modification, /datum/actionspeed_modifier, type_or_datum, priority, COMPARE_VALUE) LAZYSET(actionspeed_modification, type_or_datum.id, type_or_datum) if(update) update_actionspeed() diff --git a/code/modules/movespeed/_movespeed_modifier.dm b/code/modules/movespeed/_movespeed_modifier.dm index 195b3f67518..3897a7fa997 100644 --- a/code/modules/movespeed/_movespeed_modifier.dm +++ b/code/modules/movespeed/_movespeed_modifier.dm @@ -80,7 +80,7 @@ GLOBAL_LIST_EMPTY(movespeed_modification_cache) return TRUE remove_movespeed_modifier(existing, FALSE) if(length(movespeed_modification)) - BINARY_INSERT(type_or_datum.id, movespeed_modification, datum/movespeed_modifier, type_or_datum, priority, COMPARE_VALUE) + BINARY_INSERT(type_or_datum.id, movespeed_modification, /datum/movespeed_modifier, type_or_datum, priority, COMPARE_VALUE) LAZYSET(movespeed_modification, type_or_datum.id, type_or_datum) if(update) update_movespeed() diff --git a/code/modules/unit_tests/_unit_tests.dm b/code/modules/unit_tests/_unit_tests.dm index ec3ef3d3760..3cd8587d99e 100644 --- a/code/modules/unit_tests/_unit_tests.dm +++ b/code/modules/unit_tests/_unit_tests.dm @@ -12,6 +12,7 @@ #include "anchored_mobs.dm" #include "bespoke_id.dm" +#include "binary_insert.dm" #include "card_mismatch.dm" #include "chain_pull_through_space.dm" #include "component_tests.dm" diff --git a/code/modules/unit_tests/binary_insert.dm b/code/modules/unit_tests/binary_insert.dm new file mode 100644 index 00000000000..ac7f58208e6 --- /dev/null +++ b/code/modules/unit_tests/binary_insert.dm @@ -0,0 +1,26 @@ +/// A test to ensure the sanity of BINARY_INSERT +/datum/unit_test/binary_insert/Run() + var/list/datum/binary_insert_node/nodes = list() + + var/datum/binary_insert_node/node_a = new /datum/binary_insert_node(10) + BINARY_INSERT(node_a, nodes, /datum/binary_insert_node, node_a, x, COMPARE_KEY) + TEST_ASSERT_EQUAL(nodes.len, 1, "List should have one node") + + var/datum/binary_insert_node/node_b = new /datum/binary_insert_node(5) + BINARY_INSERT(node_b, nodes, /datum/binary_insert_node, node_b, x, COMPARE_KEY) + TEST_ASSERT_EQUAL(nodes.len, 2, "List should have two nodes") + TEST_ASSERT_EQUAL(nodes[1].x, 5, "The first node should be the one with 5") + TEST_ASSERT_EQUAL(nodes[2].x, 10, "The second node should be the one with 10") + + var/datum/binary_insert_node/node_c = new /datum/binary_insert_node(15) + BINARY_INSERT(node_c, nodes, /datum/binary_insert_node, node_c, x, COMPARE_KEY) + TEST_ASSERT_EQUAL(nodes.len, 3, "List should have three nodes") + TEST_ASSERT_EQUAL(nodes[1].x, 5, "The first node should be the one with 5") + TEST_ASSERT_EQUAL(nodes[2].x, 10, "The second node should be the one with 10") + TEST_ASSERT_EQUAL(nodes[3].x, 15, "The third node should be the one with 15") + +/datum/binary_insert_node + var/x + +/datum/binary_insert_node/New(_x) + x = _x