range indexing (inclusive/exclusive)

20 views
Skip to first unread message

Mark Proctor

unread,
Jan 26, 2023, 12:02:52 AM1/26/23
to fastutil
We are looking to use one of the Trees for range indexing. <, <=, >, >=.

I notice that there is no "inclusive/exclusive" option in the subMap, headMap, tailMap, so I'm not sure how create subMaps for the 4 different types of ranges. Are there workarounds for this?

Regards

Mark 

Sebastiano Vigna

unread,
Jan 26, 2023, 7:26:39 AM1/26/23
to fast...@googlegroups.com
There are internal methods. It's a nuisance for data types with no obvious successors/predecessors.

However you can find suitable starting points using an iterator with a starting point and moving backwards, for example.

Mark Proctor

unread,
Jan 26, 2023, 8:26:26 AM1/26/23
to fastutil
"However you can find suitable starting points using an iterator with a starting point and moving backwards, for example."
Thanks, I can see how it can be done with iterator, and we can do our own Compare to see if the first and last should be included.

Would you be interested in a foreach approach? that avoids the additional overhead cost of the iterator?

tree.forEach(startObject, startInclusive, endObject, endInclusive, entry -> println(entry.key() + ":" + entry.value()));
tree.forEach(startObject, endObject, entry -> println(entry.key() + ":" + entry.value())); // start and end are inclusive by default

It would use locateKey to get the start point and then another while loop to continue until it reaches the end. While it does the second loop, it calls the function for each Entry. Entry could be BasicEntry or a re-used FastEntry. Again let me know if it's worth looking into a PR for something like this.

Mark

Mark Proctor

unread,
Jan 26, 2023, 10:55:05 AM1/26/23
to fastutil
Looking over the api I can see I need to use object2ObjectEntrySet, and then it has a "from" iterator. However it expects an Entry as the argument, which I don't have yet, and then it just gets the key and does al look up anyway. As I don't yet have an Entry, only the key, I will need to make a wrapper instance, just to then be unwrapped. Probably the original intention might have been that it could continue iterating from the Entry reference, and avoid a lookup? Maybe another iterator method, that takes the key?

public ObjectBidirectionalIterator<Object2ObjectMap.Entry<K, V>> iterator(final Object2ObjectMap.Entry<K, V> from) {
return new EntryIterator(from.getKey());
}

Mark

Sebastiano Vigna

unread,
Jan 27, 2023, 3:19:20 AM1/27/23
to fast...@googlegroups.com


> On 26 Jan 2023, at 16:55, Mark Proctor <mdpr...@gmail.com> wrote:
>
> Looking over the api I can see I need to use object2ObjectEntrySet, and then it has a "from" iterator. However it expects an Entry as the argument, which I don't have yet, and then it just gets the key and does al look up anyway. As I don't yet have an Entry, only the key, I will need to make a wrapper instance, just to then be unwrapped. Probably the original intention might have been that it could continue iterating from the Entry reference, and avoid a lookup? Maybe another iterator method, that takes the key?
>

I was thinking of using an iterator over the key set, not the entry set.

Ciao,

seba

Mark Proctor

unread,
Jan 27, 2023, 9:44:09 AM1/27/23
to fastutil
I would still need to do a second look up for the value of each key, for each it.next(), so that would probably be worse than the wrap/unwrap to start the iterator from when using  object2ObjectEntrySet?



Ciao,

seba

Sebastiano Vigna

unread,
Jan 27, 2023, 10:49:16 AM1/27/23
to fast...@googlegroups.com


> On 27 Jan 2023, at 15:44, Mark Proctor <mdpr...@gmail.com> wrote:
>
> I was thinking of using an iterator over the key set, not the entry set.
> I would still need to do a second look up for the value of each key, for each it.next(), so that would probably be worse than the wrap/unwrap to start the iterator from when using object2ObjectEntrySet?

Yes, but my point is that this is just setup time. It will be amortized over the usage of the submap.

Alternatively, I'd write a subclass. Superfeaturizing classes becomes a nightmare when you have to do, e.g., refactoring.

Ciao,

seba

Reply all
Reply to author
Forward
0 new messages