void untenure()

in accord-core/src/main/java/accord/utils/SearchableRangeListBuilder.java [866:922]


        void untenure(int index)
        {
            while (!isEmpty() && first().lastIndex < index)
            {
                Tenured removed = pollFirst();

                // if removed.next == null, this is not referenced by a link
                // if removed.next == removed, it is referenced by a link but does not modify the link on removal
                if (removed.next != null && removed.next != removed)
                {
                    // this is a member of a link's chain, which may serve one of two purposes:
                    // 1) it may be the entry nominated to invalidate the link, due to the link
                    //    membership shrinking below the required threshold; in which case we
                    //    must clear the chain to reactivate its members for insertion into the
                    //    next checkpoint, and remove the chain link itself
                    // 2) it may be nominated as an entry to update the chain link info, to make
                    //    it more succinct: if every entry of the chain remains active, and there
                    //    are *many* entries then we need two integers to represent the chain, but
                    //    as soon as any entry is invalid we can rely on this entry to terminate
                    //    iteration, so we update the bookkeeping on the first entry we remove in
                    //    this case

                    // first clear the chain starting at the removed entry
                    Tenured prev = removed, next = removed.next;
                    while (next.next != null)
                    {
                        prev = next;
                        next = next.next;
                        prev.next = null;
                    }
                    Invariants.checkState(next.index < 0);
                    if (prev.end == next.end)
                    {
                        // if this is the last entry in the link, the link is expired and should be removed/reused
                        remove(next);
                        if (pendingReuseTail == null)
                            pendingReuseTail = next;
                        next.next = pendingReuse;
                        pendingReuse = next;
                    }
                    else if (next.linkLength < 0)
                    {
                        // otherwise, flag the link as safely consumed without knowing the length
                        next.linkLength = next.linkLength & Integer.MAX_VALUE;
                    }
                }

                // this was not a link reference; update our bookkeeping and save it for reuse
                Invariants.checkState(removed.index >= 0);
                --directCount;
                --minSpan;
                if (pendingReuseTail == null)
                    pendingReuseTail = removed;
                removed.next = pendingReuse;
                pendingReuse = removed;
            }
        }