vigna / fastutil

fastutil extends the Java™ Collections Framework by providing type-specific maps, sets, lists and queues.
Apache License 2.0
1.81k stars 199 forks source link

Primitive Comparator.comparing(keyExtractor), like standrad Comparator #313

Closed barakugav closed 4 months ago

barakugav commented 10 months ago

https://github.com/vigna/fastutil/issues/312

vigna commented 10 months ago

Mmmmhh no this will generate a copy of all functions in each generated file.

barakugav commented 10 months ago

That is the intention.

java.util.Comparator define 5 'comparing' static functions:

static <T, U extends Comparable<U>> Comparator<T> comparing(Function<T, U> keyExtractor);
static <T, U> Comparator<T> comparing(Function<T, U> keyExtractor, Comparator<U> keyComparator);
static <T> Comparator<T> comparingInt(ToIntFunction<T> keyExtractor);
static <T> Comparator<T> comparingLong(ToLongFunction<T> keyExtractor);
static <T> Comparator<T> comparingDouble(ToDoubleFunction<T> keyExtractor);

These methods create a comparator that extract comparable/obj/int/long/double from the T instances and compare them.

I want to provide 5 such functions for each primitive comparator:

IntComparator comparingObj(Int2ObjFunction<Comparable> keyExtractor);
IntComparator comparingObj(Int2ObjFunction<U> keyExtractor, Comparator<U> keyComparator);
IntComparator comparingInt(Int2IntFunction keyExtractor);
IntComparator comparingLong(Int2LongFunction keyExtractor);
IntComparator comparingDouble(Int2DoubleFunction keyExtractor);
ShortComparator comparingObj(Short2ObjFunction<Comparable> keyExtractor);
ShortComparator comparingObj(Short2ObjFunction<U> keyExtractor, Comparator<U> keyComparator);
ShortComparator comparingInt(Short2IntFunction keyExtractor);
...
barakugav commented 10 months ago

@vigna what you think?

vigna commented 10 months ago

It seems Ok, but I'm in the middle of a deadline and unfortunately I don't have any bandwidth left. I'll be able to check this in a couple of weeks...

vigna commented 9 months ago

Ok, but is it necessary to name it comparingObj? Can't it just be named comparing? Or some ambiguity arises?

vigna commented 9 months ago

And, it would be nice to have a few unit tests covering the code.

barakugav commented 4 months ago

@vigna Renamed comparingObj to comparing and added a few tests