function getItemFromRL()

in packages/core/micro/src/permutation/index.ts [20:57]


function getItemFromRL(segment: RunLengthSegment, offset: number, fresh?: () => number) {
    const content = segment.content;
    const size = content.length;
    let startIndex = 0;
    let end = offset;
    while (startIndex < size) {
        const len = content[startIndex];
        if (end < len) {
            break;
        }
        end -= len;
        startIndex += 2;
    }
    const base = content[startIndex + 1];
    if (base === UNALLOCATED) {
        if (fresh === undefined) {
            return UNALLOCATED;
        }
        const n = fresh();
        const run = content[startIndex];
        if (end === 0) {
            if (run === 1) {
                content[startIndex + 1] = n;
            }
            else {
                content[startIndex]--;
                content.splice(startIndex, 0, 1, n);
            }
        }
        else {
            // can offset === run?
            content[startIndex] -= end;
            content.splice(startIndex, 0, end, UNALLOCATED, 1, n);
        }
        return n;
    }
    return end + content[startIndex + 1];
}