-- Generic utlity functions we want everywehre.

-- Dump from stack overflow:
function dump(o, maxdepth)
    maxdepth = maxdepth or 100

    if type(o) == 'table' then
	if maxdepth <= 0 then
	    return "{ .. } "
	end
	local s = '{ '
	for k,v in pairs(o) do
	    if type(k) == 'table' then k = '"'..dump(k,maxdepth-1)..'"'
	    elseif type(k) ~= 'number' then k = '"'..k..'"' end
	    s = s .. '['..k..'] = '..dump(v, maxdepth-1) ..','
	end
	return s .. '} '
    else
	return tostring(o)
    end
end

function shuffleInPlace(list)
    for i = #list, 2, -1 do
	local j = math.random(i)
	list[i], list[j] = list[j], list[i]
    end
end

Queue = {}
Queue.__index = Queue

function Queue.new()
    local v = { }
    v.q = {}
    v.first = 1
    v.last = 0
    setmetatable(v, Queue)
    return v
end

function Queue:top()
    return self.q[self.first]
end

function Queue:pop()
    local v = self:top()
    if self.first >= self.last then
	self.first = self.first + 1
    end
    return v
end

function Queue:push(v)
    self.last = self.last + 1
    self.q[self.last] = v
end

function Queue:entries()
    return self.last - self.first + 1
end

function Queue:isEmpty()
    return self:entries() == 0
end



