Change signature of BINARY_INSERT to require the full type path, add test (#53217)

BINARY_INSERT used to only take typepaths like/this. Now, it expects them to be /like/this, to be more consistent with ther est of the code.

Adds documentation to COMPTYPE.

Adds a test for BINARY_INSERT.
This commit is contained in:
Jared-Fogle
2020-09-01 00:40:52 -07:00
committed by GitHub
parent 78a9b76410
commit 09b9ad869e
7 changed files with 34 additions and 6 deletions
+2 -1
View File
@@ -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) {\
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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
@@ -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()
@@ -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()
+1
View File
@@ -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"
+26
View File
@@ -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