void setupLinkChain()

in accord-core/src/main/java/accord/utils/SearchableRangeListBuilder.java [1038:1085]


        void setupLinkChain(TenuredSet tenured, int startIndex, int endIndex)
        {
            int minSizeToReference = openIndirectCount + MIN_INDIRECT_LINK_LENGTH;
            if (openDirectCount >= minSizeToReference)
            {
                int i = firstOpenDirect;
                Tenured prev = get(i++);

                while (openDirectCount > minSizeToReference)
                {
                    Tenured e = get(i++);
                    if (e.index < 0)
                    {
                        --minSizeToReference;
                        continue;
                    }

                    Invariants.checkState(prev.next == null);
                    prev.next = prev;
                    prev = e;
                    --openDirectCount;
                }

                while (i < count)
                {
                    Tenured next = get(i++);
                    if (next.index < 0)
                        continue;

                    Invariants.checkState(prev.next == null);
                    prev.next = next;
                    prev = next;
                }

                // may be more than one entry per item (though usually not)
                int length = endIndex - startIndex;
                Tenured chainEntry = tenured.addLinkEntry(prev.end, BIT31 | startIndex, prev.lastIndex, length);
                prev.next = chainEntry;
                if (hasClosedDirect && (startIndex > 0xfffff || (length > 0xff)))
                {
                    // TODO (expected, testing): make sure this is tested, as not a common code path (may never be executed in normal operation)
                    // we have no closed ranges so iteration needs to know the end bound, but we cannot encode our bounds cheaply
                    // so link the first bound to the chain entry, so that on removal it triggers an update of endIndex to note
                    // that it can be iterated safely without an end bound
                    get(firstOpenDirect).next = chainEntry;
                }
            }
        }