From f3f1c8c82fe619d492cd58024dae9fc6f914f9fd Mon Sep 17 00:00:00 2001 From: Daniel Yates Date: Thu, 2 May 2024 12:12:24 +0100 Subject: [PATCH] Compress Ring.Link implementation slightly The temporary variable names were for my 1:30am brain, but they don't actually add any clarity by virtue of only being 1 character long. However, giving them long names probably won't help either as there's just a lot of mental parsing overhead that's required when visualizing linked list operations anyway. So let's just nuke them and do it all in-line as a pair of multi-valued assignments, with slightly better comments. The first assignment is equivalent to 'b.next' and 'y.next' in the old code and covers linking self's tail node to other's head node, and other's tail node to self's head node. The second assignment is for 'a.prev' and 'x.prev' in the old code; this links our head node backwards to other's tail node, and other's head node back to our tail node. The second requires the use of a multi-value assignment, as it's effectively an exchange of values. In a multi-value assignment statement all expressions on the right of the '=' are evaluated first with their results stored on the stack, and then afterwards the actual assignments to the things on the left takes place. --- AceComm-3.0/ChatThrottleLib.lua | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/AceComm-3.0/ChatThrottleLib.lua b/AceComm-3.0/ChatThrottleLib.lua index d5e181d..89a586c 100644 --- a/AceComm-3.0/ChatThrottleLib.lua +++ b/AceComm-3.0/ChatThrottleLib.lua @@ -121,17 +121,10 @@ function Ring:Link(other) -- Move and append all contents of another ring to th self.pos = other.pos other.pos = nil elseif other.pos then - -- Link the other ring to the back of this one. - local a = self.pos - local b = self.pos.prev - local x = other.pos - local y = other.pos.prev - - a.prev = y -- My first points to their last... - y.next = a -- ...and their last to my first. - b.next = x -- My last points to their first... - x.prev = b -- ...and their first to my last. - + -- Our tail should point to their head, and their tail to our head. + self.pos.prev.next, other.pos.prev.next = other.pos, self.pos + -- Our head should point to their tail, and their head to our tail. + self.pos.prev, other.pos.prev = other.pos.prev, self.pos.prev other.pos = nil end end