]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Resolve tie situations in n lowest/highest element selection. 46/2746/3
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 4 Mar 2019 15:33:03 +0000 (17:33 +0200)
committerJani Simomaa <jani.simomaa@semantum.fi>
Wed, 6 Mar 2019 13:18:19 +0000 (13:18 +0000)
Pick all elements that match the value of the n'th lowest/highest one.

Change-Id: I066ae39a2f7db73db93a6c3d4c9531ba4300236f

org.simantics.district.selection/src/org/simantics/district/selection/ElementSelector.java

index 74eaa227096a06b011e230d47089328c210bacab..bf051b4c9828e3f9d4d0b08a3e8409a3d4d3c091 100644 (file)
@@ -543,13 +543,12 @@ public class ElementSelector {
                @SuppressWarnings("unchecked")
                @Override
                Collection<Resource> select(ReadGraph graph, Collection<Resource> elements) {
-                       List<Resource> result = new ArrayList<>(elements);
                        List<Tuple2> result2 = Lists.map(new FunctionImpl1<Resource, Tuple2>() {
                                @Override
                                public Tuple2 apply(Resource r) {
                                        return new Tuple2(r, getPropertyValue(graph, r, propertyName));
                                }
-                       }, result);
+                       }, new ArrayList<>(elements));
                        
                        result2 = Lists.filter(new FunctionImpl1<Tuple2, Boolean>() {
                                @Override
@@ -560,17 +559,21 @@ public class ElementSelector {
                        
                        result2.sort((t1, t2) -> smallest ? Double.compare((Double) t1.c1, (Double) t2.c1) : Double.compare((Double) t2.c1, (Double) t1.c1));
                        
-                       if (resultCount < result2.size())
-                               result2 = result2.subList(0, resultCount);
+                       if (resultCount < result2.size()) {
+                               double limitValue = (double) result2.get(resultCount-1).c1;
+                               
+                               // Expand selection to contain all items with the same value as the nth one
+                               int count = resultCount;
+                               while (count < result2.size() && (double)result2.get(count).c1 == limitValue) count++;
+                               result2 = result2.subList(0, count);
+                       }
                        
-                       result = Lists.map(new FunctionImpl1<Tuple2, Resource>() {
+                       return (List<Resource>) Lists.map(new FunctionImpl1<Tuple2, Resource>() {
                                @Override
                                public Resource apply(Tuple2 p0) {
                                        return (Resource) p0.c0;
                                }
                        }, result2);
-                       
-                       return result;
                }
        }