コード例 #1
0
    public bool IsEncapsulated(long l)
    {
        if (startingIndices.Count == 0)
        {
            return(false);
        }
        // Find earlier start
        if (!PreSortedListExt.TryGetInDirection(
                startingIndices,
                l,
                higher: false,
                result: out var startResult))
        {
            return(false);
        }
        // If start is our value, we are encapsulated
        if (startResult.Value == l)
        {
            return(true);
        }

        // Find earlier end, if found is after our earlier start
        // we aren't encapsulated
        if (PreSortedListExt.TryGetInDirection(
                endingIndices,
                l,
                higher: false,
                result: out var endEarlierResult) &&
            endEarlierResult.Value >= startResult.Value &&
            endEarlierResult.Value != l)
        {
            return(false);
        }
        return(true);
    }
コード例 #2
0
    public bool TryGetCurrentOrNextRange(long l, out RangeInt64 range)
    {
        if (startingIndices.Count == 0)
        {
            range = default(RangeInt64);
            return(false);
        }

        // Get end
        if (!PreSortedListExt.TryGetInDirection(
                endingIndices,
                item: l,
                higher: true,
                result: out var endingResult))
        {
            range = default(RangeInt64);
            return(false);
        }

        // Get earlier start, ensure earlier end comes before it
        if (PreSortedListExt.TryGetInDirection(
                startingIndices,
                item: l,
                higher: false,
                result: out var startingResult))
        {
            if (PreSortedListExt.TryGetInDirection(
                    endingIndices,
                    item: l,
                    higher: false,
                    result: out var earlierEndingResult))
            {
                if (earlierEndingResult.Value < startingResult.Value)
                {
                    range = new RangeInt64(startingResult.Value, endingResult.Value);
                    return(true);
                }
            }
            else
            {
                range = new RangeInt64(startingResult.Value, endingResult.Value);
                return(true);
            }
        }

        // Get later start
        if (!PreSortedListExt.TryGetInDirection(
                startingIndices,
                item: l,
                higher: true,
                result: out startingResult))
        {
            range = default(RangeInt64);
            return(false);
        }

        range = new RangeInt64(startingResult.Value, endingResult.Value);
        return(true);
    }
コード例 #3
0
    private void Add(long min, long max)
    {
        if (startingIndices.Count == 0)
        {
            startingIndices.Add(min);
            endingIndices.Add(max);
            return;
        }

        // ToDo
        // Can be improved by reusing binary search results that are being calculated twice
        // Or deriving later binary search results from previous similar ones

        bool shouldAddMin = !IsEncapsulated(min);
        bool shouldAddMax = !IsEncapsulated(max);

        // Remove indices inside our new added range
        if (min < max - 1 &&
            PreSortedListExt.TryGetEncapsulatedIndices(
                startingIndices,
                min + 1,
                max - 1,
                out var startingRange))
        {
            startingIndices.RemoveRange(startingRange.Min, startingRange.IntWidth);
        }

        if (min < max - 1 &&
            PreSortedListExt.TryGetEncapsulatedIndices(
                endingIndices,
                min + 1,
                max - 1,
                out var endingRange))
        {
            endingIndices.RemoveRange(endingRange.Min, endingRange.IntWidth);
        }

        if (shouldAddMin)
        {
            PreSortedListExt.Set(startingIndices, min);
        }
        if (shouldAddMax)
        {
            PreSortedListExt.Set(endingIndices, max);
        }
    }
コード例 #4
0
    public bool TryGetCurrentRange(long l, out RangeInt64 range)
    {
        if (startingIndices.Count == 0)
        {
            range = default(RangeInt64);
            return(false);
        }
        // Get start
        if (!PreSortedListExt.TryGetInDirection(
                startingIndices,
                item: l,
                higher: false,
                result: out var startingResult))
        {
            range = default(RangeInt64);
            return(false);
        }

        // Get end
        if (!PreSortedListExt.TryGetInDirection(
                endingIndices,
                item: l,
                higher: true,
                result: out var endingResult))
        {
            range = default(RangeInt64);
            return(false);
        }

        // Get start index after our located start index, confirm it's after our end, or we're not encapsulated
        if (startingIndices.Count > startingResult.Key + 1 &&
            startingIndices[startingResult.Key + 1] < endingResult.Value)
        {
            range = default(RangeInt64);
            return(false);
        }

        range = new RangeInt64(startingResult.Value, endingResult.Value);
        return(true);
    }