mirror of https://github.com/octoleo/lsyncd.git
add Queue.inject, test Queue, bugfixes in remove()
This commit is contained in:
parent
daa8abb4cf
commit
cda98d6ba9
54
lsyncd.lua
54
lsyncd.lua
|
@ -578,31 +578,30 @@ Queue = ( function
|
|||
error( 'Removing nonexisting item in Queue', 2 )
|
||||
end
|
||||
|
||||
nt[ pos ] = nil
|
||||
|
||||
-- if removing first or last element,
|
||||
-- the queue limits are adjusted.
|
||||
if pos == nt.first
|
||||
then
|
||||
local last = nt.last
|
||||
|
||||
while nt[ pos ] == nil and pos <= last
|
||||
do
|
||||
pos = pos + 1
|
||||
end
|
||||
|
||||
nt.first = pos
|
||||
|
||||
nt[ pos ] = nil
|
||||
nt.first = pos + 1
|
||||
elseif pos == nt.last
|
||||
then
|
||||
nt[ pos ] = nil
|
||||
nt.last = nt.last - 1
|
||||
else
|
||||
local first = nt.first
|
||||
local last = nt.first
|
||||
|
||||
while nt[ pos ] == nil and pos >= first
|
||||
do
|
||||
pos = pos - 1
|
||||
local i = pos
|
||||
|
||||
while i < last do
|
||||
if i == pos then
|
||||
nt[ i ] = nt[ i + 1 ]
|
||||
i = i + 1
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
|
||||
nt.last = pos
|
||||
nt.last = last - 1
|
||||
end
|
||||
|
||||
-- reset the indizies if the queue is empty
|
||||
|
@ -616,6 +615,28 @@ Queue = ( function
|
|||
nt.size = nt.size - 1
|
||||
end
|
||||
|
||||
--
|
||||
-- Injects a value in front of the Queue.
|
||||
--
|
||||
local function inject
|
||||
(
|
||||
self, -- the queue
|
||||
value -- position to remove
|
||||
)
|
||||
local nt = self[ k_nt ]
|
||||
|
||||
local pos = nt.last
|
||||
while pos >= nt.first
|
||||
do
|
||||
nt[pos + 1] = nt[pos]
|
||||
pos = pos - 1
|
||||
end
|
||||
|
||||
nt[nt.first] = value
|
||||
nt.size = nt.size + 1
|
||||
nt.last = nt.last + 1
|
||||
end
|
||||
|
||||
--
|
||||
-- Replaces a value.
|
||||
--
|
||||
|
@ -721,6 +742,7 @@ Queue = ( function
|
|||
first = first,
|
||||
last = last,
|
||||
push = push,
|
||||
inject = inject,
|
||||
qpairs = qpairs,
|
||||
qpairsReverse = qpairsReverse,
|
||||
remove = remove,
|
||||
|
|
|
@ -28,4 +28,52 @@ assert(
|
|||
|
||||
assert(type(lsyncd.get_free_port()) == "number")
|
||||
|
||||
local function testQueue()
|
||||
local q = Queue.new()
|
||||
q:push(1)
|
||||
q:push(2)
|
||||
q:push(3)
|
||||
q:push(4)
|
||||
assert(q:size(), 4)
|
||||
assert(q[1], 1)
|
||||
assert(q[4], 4)
|
||||
|
||||
q:remove(4)
|
||||
assert(q:size(), 3)
|
||||
assert(q[3], 3)
|
||||
assert(q[1], 1)
|
||||
|
||||
q:remove(1)
|
||||
assert(q:size(), 2)
|
||||
assert(q[3], 3)
|
||||
assert(q[2], 2)
|
||||
assert(q.first, 2)
|
||||
assert(q.last, 3)
|
||||
|
||||
q:push(5)
|
||||
assert(q:size(), 3)
|
||||
assert(q.last, 4)
|
||||
assert(q.first, 2)
|
||||
assert(q[4], 5)
|
||||
assert(q[3], 3)
|
||||
assert(q[2], 2)
|
||||
|
||||
q:remove(3)
|
||||
assert(q:size(), 2)
|
||||
assert(q.last, 3)
|
||||
assert(q.first, 2)
|
||||
assert(q[2], 2)
|
||||
assert(q[3], 5)
|
||||
|
||||
q:inject(23)
|
||||
assert(q:size(), 3)
|
||||
assert(q.last, 3)
|
||||
assert(q.first, 1)
|
||||
assert(q[1], 23)
|
||||
assert(q[2], 2)
|
||||
assert(q[3], 5)
|
||||
end
|
||||
|
||||
testQueue()
|
||||
|
||||
os.exit(0)
|
Loading…
Reference in New Issue