FANDOM


local p = {}
 
local indices = {}
local maxIndices = {}
local elems = {}
 
function p.setup(number, items)
    if #items < 1 or number < 1 or #items < number then
        return 'err'
    end
    local i
    indices = {}
    maxIndices = {}
    elems = items
    for i=1, number do
        indices[i] = i
        maxIndices[i] = #elems + i - number
    end
end
 
function p.hasNext()
    return indices ~= -1
end
 
function createFromIndices()
    local ret = {}
    local i
    for i=1, #indices do
        table.insert(ret, elems[indices[i]])
    end
    return ret
end
 
function incrementIndices()
    if indices[1] == maxIndices[1] then
        indices = -1
        return
    end
    local i,j
    for i=#indices -1, 1, -1 do
        if indices[i] ~= maxIndices[i] then
            indices[i] = indices[i]+1
            local val = indices[i]
            for j=i+1, #indices do
                val = val + 1
                indices[j] = val
            end
            break
        end
    end
 
end
 
 
function p.getNext()
    if indices == -1 then
        return 'err'
    end
    local ret = createFromIndices()
    incrementIndices()
    return ret
end
 
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.