Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use linkedlists for queue #1767

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions MainModule/Server/Shared/DoubleLinkedList.luau
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function LinkNode:setprev(p)
end

function LinkNode:Destroy()
for i,_ in self do
for i, _ in self do
self[i] = nil
end
setmetatable(self, nil)
Expand Down Expand Up @@ -57,34 +57,37 @@ function LinkedList:AddStart(data)
local old = self.snode
local newNode = LinkNode.new(data)
newNode:setprev(nil)

if not old then
self.enode = newNode
else
old:setprev(newNode)
end

newNode:setnext(old)
self.snode = newNode

self.count += 1
end

function LinkedList:AddEnd(data)
local old = self.enode
local newNode = LinkNode.new(data)
newNode:setnext(nil)

if not old then
self.snode = newNode
else
old:setNext(newNode)
old:setnext(newNode)
end

newNode:setprev(old)
self.enode = newNode

self.count += 1
end

function LinkedList:AddToStartAndRemoveEndIfEnd(data, limit)
self:AddStart(data)

if self.count > limit and self.enode then
self:RemoveNode(self.enode)
end
Expand All @@ -96,23 +99,22 @@ function LinkedList:AddBetweenNodes(data, left, right)
end

local newNode = LinkNode.new(data)

newNode:setnext()
newNode:setprev()

left:setnext(newNode)
right:setprev(newNode)

self.count += 1
end

function LinkedList:RemoveNode(node)
local data = node.data
local prev = node.prev
local nextN = node.next

if self.snode == node then
self.snode = nextN
end

if self.enode == node then
self.enode = prev
end
Expand All @@ -126,10 +128,10 @@ function LinkedList:RemoveNode(node)

node:setnext(nil)
node:setprev(nil)

node:Destroy()

self.count -= 1

return data
end

function LinkedList:Get(val: any)
Expand Down Expand Up @@ -161,8 +163,10 @@ function LinkedList:Get(val: any)
else
table.insert(nodes, curr)
end

curr = curr.next
end

return nodes
end

Expand Down Expand Up @@ -196,14 +200,16 @@ function LinkedList:GetAsTable(val: any)
else
table.insert(nodes, curr.data)
end

curr = curr.next
end

return nodes
end

function LinkedList:Destroy()
for i, _ in self do
self[i] = nil
for k, _ in self do
self[k] = nil
end
setmetatable(self, nil)
end
Expand Down
Loading
Loading