Skip to content

Commit

Permalink
Compress Ring.Link implementation slightly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Meorawr committed May 2, 2024
1 parent 6ebd34c commit f3f1c8c
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions AceComm-3.0/ChatThrottleLib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f3f1c8c

Please sign in to comment.