[
https://issues.apache.org/jira/browse/GROOVY-10964?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17702270#comment-17702270
]
Ingo Wilms commented on GROOVY-10964:
-------------------------------------
Based on the observation above, here is an example with comparable objects, but
not Numbers, where the new PR-version (without {{{}removeAll{}}}) behaves
differently than the current version (with {{{}removeAll{}}}). I like the new
version better, but I have no idea if anyone relies on this behavior of
{{{}removeAll{}}}.
{code:groovy}
List a = [1, 2]
List b1 = [2.0]
List b2 = [2.0, 3]
// old version
assert Num.listOf(a) - Num.listOf(b1) == Num.listOf([1])
assert Num.listOf(a) - Num.listOf(b2) == Num.listOf([1, 2])
// new version
assert DefaultGroovyMethods.minus(Num.listOf(a), Num.listOf(b1)) ==
Num.listOf([1])
assert DefaultGroovyMethods.minus(Num.listOf(a), Num.listOf(b2)) ==
Num.listOf([1])
@Canonical
class Num implements Comparable<Num>{
Number val
static listOf(List<Number> list){
return list.collect{ new Num(it) }
}
@Override
int compareTo(Num other) {
new NumberAwareComparator<>().compare(val, other.val)
}
@Override
boolean equals(Object other) {
return super.equals(other)
}
}
{code}
> List.minus() slow for Numbers
> -----------------------------
>
> Key: GROOVY-10964
> URL: https://issues.apache.org/jira/browse/GROOVY-10964
> Project: Groovy
> Issue Type: Question
> Components: groovy-jdk
> Affects Versions: 2.4.0, 3.0.9, 4.0.9
> Reporter: Ingo Wilms
> Priority: Minor
> Labels: Collections, Groovy, List, Number
> Original Estimate: 1h
> Remaining Estimate: 1h
>
> In List.minus() is a n*LOG\(n) version for comparable objects. Only for
> numbers, there is a dedicated slower n^2*LOG\(n) version. Is there a reason
> for this? It exists since 2.4.0 and hasn't changed much since then. Here is
> part of the code from version 4.0.9:
>
> {code:java}
> // if (nlgnSort && (head instanceof Comparable)) {
> //n*LOG(n) version
> Set<T> answer;
> if (head instanceof Number) {
> answer = new TreeSet<>(comparator);
> answer.addAll(self1);
> for (T t : self1) {
> if (t instanceof Number) {
> for (Object t2 : removeMe1) {
> if (t2 instanceof Number) {
> if (comparator.compare(t, (T) t2) == 0)
> answer.remove(t);
> }
> }
> } else {
> if (removeMe1.contains(t))
> answer.remove(t);
> }
> }
> } else {
> answer = new TreeSet<>(comparator);
> answer.addAll(self1);
> answer.removeAll(removeMe1);
> }
> for (T o : self1) {
> if (answer.contains(o))
> ansCollection.add(o);
> }
> } else {
> //n*n version {code}
> I fail to see why the whole extra block for numbers beginning with
> {code:java}
> if (head instanceof Number) { {code}
> is necessary.
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)