Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
NightOwl888 opened this issue Jul 30, 2020 · 1 comment
Labels
design is:enhancement New feature or request is:feature pri:low up for grabs This issue is open to be worked on by anyone

Comments

@NightOwl888
Copy link
Owner

NightOwl888 commented Jul 30, 2020

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 NightOwl888 added is:enhancement New feature or request design is:feature pri:high up for grabs This issue is open to be worked on by anyone labels Jul 30, 2020
NightOwl888 added a commit that referenced this issue Jul 24, 2021
…thod and corresponding SubList<T> class to wrap IList<T> in cases where non-J2N lists are used. (see #9)
NightOwl888 added a commit that referenced this issue Jul 24, 2021
* J2N.Collections.List<T>: Changed implementation to be similar to SCG.List<T> rather than cascading the calls to an instance if it.

* J2N.Collections.Generic: Added List.SubList/GetView() functionality (see #9)

* J2N.Collections.Generic.Extensions.ListExtensions: Added GetView() method and corresponding SubList<T> class to wrap IList<T> in cases where non-J2N lists are used. (see #9)

* J2N.Tests.Collections: Added Support_CollectionTest from Harmony and modified other collection tests in the chain to use J2N.Numerics.Int32

* J2N.Collections.Generic.Extensions.ListExtensions: Added RemoveAll() extension method for efficiently deleting from IList<T> implementations, similar to how List<T> does it.

* BUG: J2N.Collections.Generic.Extensions.ListExtensions::Swap(): Fixed guard clauses for indicies that are out of bounds

* PERFORMANCE: J2N.Collections.Generic.Extensions.ListExtensions::BinarySearch(): Optimized common cases where the underlying is a List<T> or array

* J2N.Collections.Generic.Extensions.ListExtensions::CopyTo(): use is for null check

* J2N.Collections.Generic.Extensions.ListExtensions::BinarySearchSlow(): Fixed nullability warnings
@NightOwl888
Copy link
Owner Author

NightOwl888 commented Jul 24, 2021

Now that we have List<T>.GetView() (implemented in #40), the priority of this is low.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design is:enhancement New feature or request is:feature pri:low up for grabs This issue is open to be worked on by anyone
Projects
None yet
Development

No branches or pull requests

1 participant