Why SortedSet.GetViewBetween isn't O(log N)?

后端 未结 1 872
无人及你
无人及你 2020-12-01 09:52

In .NET 4.0+, a class SortedSet has a method called GetViewBetween(l, r), which returns an interface view on a tree part containing all th

相关标签:
1条回答
  • 2020-12-01 10:51

    about the version field

    UPDATE1:

    In my memory, a lot of(maybe all?) collections in BCL have the field version.

    First,about foreach:

    according to this msdn link

    The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

    In many other collections, version is protected the data is not modified during the foreach

    For example, HashTable's MoveNext():

    public virtual bool MoveNext()
    {
        if (this.version != this.hashtable.version)
        {
            throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion"));
        }
        ..........
    }
    

    But in the in the SortedSet<T>'s MoveNext() method:

    public bool MoveNext()
    {
        this.tree.VersionCheck();
        if (this.version != this.tree.version)
        {
            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
        }       
        ....
    }
    

    UPDATE2:

    But the O(N) loop maybe not only for version but also for the Count property.

    Because the MSDN of GetViewBetween said:

    This method returns a view of the range of elements that fall between lowerValue and upperValue, as defined by the comparer .... You can make changes in both the view and in the underlying SortedSet(Of T).

    So for every update it should be sync the count field (key and value are already same). To make sure the Count is correct

    There were two policies to reach the target:

    1. Microsoft's
    2. Mono's

    First.MS's,in their code, they sacrifice the GetViewBetween()'s performance and win the Count Property's performance.

    VersionCheckImpl() is one way to sync the Count property.

    Second,Mono. In mono's code,GetViewBetween() is Faster, but in their GetCount()method:

    internal override int GetCount ()
    {
        int count = 0;
        using (var e = set.tree.GetSuffixEnumerator (lower)) {
            while (e.MoveNext () && set.helper.Compare (upper, e.Current) >= 0)
                ++count;
        }
        return count;
    }
    

    It is always an O(N) operation!

    0 讨论(0)
提交回复
热议问题