J2N icon indicating copy to clipboard operation
J2N copied to clipboard

Implement SubList(), SubMap(), Head() and Tail() functionality in List<T>, SortedSet<t>, and SortedDictionary<TKey, TValue>

Open NightOwl888 opened this issue 3 years ago • 1 comments

Java has the ability to get a view of sorted collections without allocating additional memory. Unlike LINQ, these are not cut down interfaces that are read-only, but can be edited (which modifies the original collection). .NET has such functionality, but only on SortedSet<T> with the GetViewBetween(T, T) method.

So far, the GetViewBetween(T, T) method of SortedSet<T> has been duplicated along with a second overload that accepts boolean parameters indicating whether the upper and lower bounds should be inclusive. This allows us to match the behavior of Java by making the upper bound exclusive, but still keep compatibility with .NET where both bounds are inclusive.

public virtual J2N.Collections.Generic.SortedSet<T> GetViewBetween (T lowerValue, T upperValue);
public virtual J2N.Collections.Generic.SortedSet<T> GetViewBetween (T lowerValue, bool lowerValueInclusive, T upperValue, bool upperValueInclusive);

However, we are missing several members from Java that can be useful, some of which are used by Lucene.NET. In particular, SubList is used frequently, but currently the implementation has no tests and is only partially implemented.

SortedMap<K,V> headMap(K toKey)
SortedMap<K,V> subMap(K fromKey, K toKey)
SortedMap<K,V> tailMap(K fromKey)

public List<E> subList(int fromIndex, int toIndex)

SortedSet<E> headSet(E toElement)
SortedSet<E> tailSet(E fromElement)

Proposed API

public class SortedDictionary<TKey, TValue>
{
    public virtual J2N.Collections.Generic.SortedDictionary<T> GetHeadView (T upperKey);
    public virtual J2N.Collections.Generic.SortedDictionary<T> GetHeadView (T upperKey, bool upperKeyInclusive);
    public virtual J2N.Collections.Generic.SortedDictionary<T> GetViewBetween (T lowerKey, T upperKey);
    public virtual J2N.Collections.Generic.SortedDictionary<T> GetViewBetween (T lowerKey, bool lowerKeyInclusive, T upperKey, bool upperKeyInclusive);
    public virtual J2N.Collections.Generic.SortedDictionary<T> GetTailView (T lowerKey);
    public virtual J2N.Collections.Generic.SortedDictionary<T> GetTailView (T lowerKey, bool lowerKeyInclusive);
}
public class SortedSet<T>
{
    public virtual J2N.Collections.Generic.SortedSet<T> GetHeadView (T upperValue);
    public virtual J2N.Collections.Generic.SortedSet<T> GetHeadView (T upperValue, bool upperValueInclusive);
    public virtual J2N.Collections.Generic.SortedSet<T> GetViewBetween (T lowerValue, T upperValue); (already exists)
    public virtual J2N.Collections.Generic.SortedSet<T> GetViewBetween (T lowerValue, bool lowerValueInclusive, T upperValue, bool upperValueInclusive); (already exists)
    public virtual J2N.Collections.Generic.SortedSet<T> GetTailView (T lowerValue);
    public virtual J2N.Collections.Generic.SortedSet<T> GetTailView (T lowerValue, bool lowerValueInclusive);
}
public class List<T>
{
    public virtual J2N.Collections.Generic.List<T> GetView (int index, int count); (completed)
}

NightOwl888 avatar Jul 30 '20 20:07 NightOwl888