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 )
|
error( 'Removing nonexisting item in Queue', 2 )
|
||||||
end
|
end
|
||||||
|
|
||||||
nt[ pos ] = nil
|
|
||||||
|
|
||||||
-- if removing first or last element,
|
-- if removing first or last element,
|
||||||
-- the queue limits are adjusted.
|
-- the queue limits are adjusted.
|
||||||
if pos == nt.first
|
if pos == nt.first
|
||||||
then
|
then
|
||||||
local last = nt.last
|
nt[ pos ] = nil
|
||||||
|
nt.first = pos + 1
|
||||||
while nt[ pos ] == nil and pos <= last
|
|
||||||
do
|
|
||||||
pos = pos + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
nt.first = pos
|
|
||||||
|
|
||||||
elseif pos == nt.last
|
elseif pos == nt.last
|
||||||
then
|
then
|
||||||
|
nt[ pos ] = nil
|
||||||
|
nt.last = nt.last - 1
|
||||||
|
else
|
||||||
local first = nt.first
|
local first = nt.first
|
||||||
|
local last = nt.first
|
||||||
|
|
||||||
while nt[ pos ] == nil and pos >= first
|
local i = pos
|
||||||
do
|
|
||||||
pos = pos - 1
|
while i < last do
|
||||||
|
if i == pos then
|
||||||
|
nt[ i ] = nt[ i + 1 ]
|
||||||
|
i = i + 1
|
||||||
|
end
|
||||||
|
i = i + 1
|
||||||
end
|
end
|
||||||
|
nt.last = last - 1
|
||||||
nt.last = pos
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- reset the indizies if the queue is empty
|
-- reset the indizies if the queue is empty
|
||||||
|
@ -616,6 +615,28 @@ Queue = ( function
|
||||||
nt.size = nt.size - 1
|
nt.size = nt.size - 1
|
||||||
end
|
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.
|
-- Replaces a value.
|
||||||
--
|
--
|
||||||
|
@ -721,6 +742,7 @@ Queue = ( function
|
||||||
first = first,
|
first = first,
|
||||||
last = last,
|
last = last,
|
||||||
push = push,
|
push = push,
|
||||||
|
inject = inject,
|
||||||
qpairs = qpairs,
|
qpairs = qpairs,
|
||||||
qpairsReverse = qpairsReverse,
|
qpairsReverse = qpairsReverse,
|
||||||
remove = remove,
|
remove = remove,
|
||||||
|
|
|
@ -28,4 +28,52 @@ assert(
|
||||||
|
|
||||||
assert(type(lsyncd.get_free_port()) == "number")
|
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)
|
os.exit(0)
|
Loading…
Reference in New Issue