belliottsmith commented on code in PR #7:
URL: https://github.com/apache/cassandra-accord/pull/7#discussion_r1013062031


##########
accord-core/src/main/java/accord/primitives/KeyRanges.java:
##########
@@ -116,44 +119,64 @@ public KeyRanges select(int[] indexes)
         return ofSortedAndDeoverlapped(selection);
     }
 
-    public boolean intersects(Keys keys)
+    public boolean intersects(AbstractKeys<?, ?> keys)
     {
         return findNextIntersection(0, keys, 0) >= 0;
     }
 
+    public <K extends RoutingKey> boolean intersects(AbstractKeys<K, ?> keys, 
Predicate<? super K> matches)
+    {
+        int ri = 0, ki = 0;
+        while (true)
+        {
+            long rki = findNextIntersection(ri, keys, ki);
+            if (rki < 0)
+                return false;
+
+            ri = (int) (rki >>> 32);
+            ki = (int) (rki);
+
+            if (matches.test(keys.get(ki)))
+                return true;
+
+            ki++;
+        }
+    }
+
     public boolean intersects(KeyRanges that)
     {
         return SortedArrays.findNextIntersection(this.ranges, 0, that.ranges, 
0, KeyRange::compareIntersecting) >= 0;
     }
 
-    public int findFirstKey(Keys keys)
+    public int findFirstKey(AbstractKeys<?, ?> keys)
     {
         return findNextKey(0, keys, 0);
     }
 
-    public int findNextKey(int ri, Keys keys, int ki)
+    public int findNextKey(int ri, AbstractKeys<?, ?> keys, int ki)
     {
-        return (int) (findNextIntersection(ri, keys, ki) >> 32);
+        return (int) findNextIntersection(ri, keys, ki);
     }
 
-    // returns ki in top 32 bits, ri in bottom, or -1 if no match found
-    public long findNextIntersection(int ri, Keys keys, int ki)
+    // returns ri in top 32 bits, ki in bottom, or -1 if no match found
+    // TODO (now): inconsistent bits order vs SortedArrays
+    public long findNextIntersection(int ri, AbstractKeys<?, ?> keys, int ki)
     {
-        return SortedArrays.findNextIntersectionWithMultipleMatches(keys.keys, 
ki, ranges, ri);
+        return 
swapHighLow32b(SortedArrays.findNextIntersectionWithMultipleMatches(keys.keys, 
ki, ranges, ri));
     }
 
-    public int findFirstKey(Key[] keys)
+    public int findFirstKey(RoutingKey[] keys)

Review Comment:
   Let's leave cleaning this up to routables, if necessary



##########
accord-core/src/main/java/accord/primitives/Keys.java:
##########
@@ -166,181 +137,100 @@ public Keys with(Key key)
         return new Keys(trg);
     }
 
-    public Stream<Key> stream()
+    public static Keys of(Key key)
     {
-        return Stream.of(keys);
-    }
-
-    @Override
-    public Iterator<Key> iterator()
-    {
-        return new Iterator<Key>()
-        {
-            int i = 0;
-            @Override
-            public boolean hasNext()
-            {
-                return i < keys.length;
-            }
-
-            @Override
-            public Key next()
-            {
-                return keys[i++];
-            }
-        };
+        return new Keys(new Key[] { key });
     }
 
     public static Keys of(Key ... keys)
     {
-        Arrays.sort(keys);
-        return new Keys(keys);
+        return dedupSorted(sort(keys));
     }
 
-    public static Keys of(List<Key> keys)
+    public static Keys copyOf(Key[] keys)
     {
-        Key[] akeys = new Key[keys.size()];
-        for (int i=0; i<akeys.length; i++)
-            akeys[i] = keys.get(i);
-
-        return of(akeys);
+        return dedupSorted(sort(keys.clone()));
     }
 
-    public static Keys ofSorted(Key ... keys)
+    public static Keys ofUnique(Key ... keys)
     {
-        for (int i = 1 ; i < keys.length ; ++i)
-        {
-            if (keys[i - 1].compareTo(keys[i]) >= 0)
-                throw new IllegalArgumentException(Arrays.toString(keys) + " 
is not sorted");
-        }
-        return new Keys(keys);
+        // check unique
+        return ofSorted(sort(keys));
     }
 
-    static Keys ofSortedUnchecked(Key ... keys)
+    public static Keys of(Collection<? extends Key> keys)
     {
-        return new Keys(keys);
+        return of(keys.toArray(new Key[0]));
     }
 
-    public boolean any(KeyRanges ranges, Predicate<Key> predicate)
+    public static <V> Keys of(Collection<V> input, Function<? super V, ? 
extends Key> transform)
     {
-        return 1 == foldl(ranges, (i1, key, i2, i3) -> predicate.test(key) ? 1 
: 0, 0, 0, 1);
+        Key[] array = new Key[input.size()];
+        int i = 0;
+        for (V v : input)
+            array[i++] = transform.apply(v);
+        return of(array);
     }
 
-    public boolean any(IndexedPredicate<Key> predicate)
+    public static <V> Keys of(List<V> input, Function<? super V, ? extends 
Key> transform)
     {
-        return 1 == foldl((i, key, p, v) -> predicate.test(i, key) ? 1 : 0, 0, 
0, 1);
+        Key[] array = new Key[input.size()];
+        for (int i = 0, mi = input.size() ; i < mi ; ++i)
+            array[i] = transform.apply(input.get(i));
+        return of(array);
     }
 
-    /**
-     * Count the number of keys matching the predicate and intersecting with 
the given ranges.
-     * If terminateAfter is greater than 0, the method will return once 
terminateAfter matches are encountered
-     */
-    @Inline
-    public <V> V foldl(KeyRanges rs, IndexedFold<Key, V> fold, V accumulator)
+    public static Keys ofUnique(Collection<? extends Key> keys)

Review Comment:
   Let's leave cleaning this up to routables, if necessary



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to