aweisberg commented on code in PR #26:
URL: https://github.com/apache/cassandra-accord/pull/26#discussion_r1093723225
##########
accord-core/src/main/java/accord/utils/ReducingRangeMap.java:
##########
@@ -0,0 +1,131 @@
+package accord.utils;
+
+import accord.api.RoutingKey;
+import accord.primitives.*;
+import com.google.common.annotations.VisibleForTesting;
+
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+
+import static accord.utils.SortedArrays.Search.FAST;
+
+public class ReducingRangeMap<V> extends ReducingIntervalMap<RoutingKey, V>
+{
+ final RoutingKeys endKeys;
+
+ public ReducingRangeMap(V value)
+ {
+ super(value);
+ this.endKeys = RoutingKeys.EMPTY;
+ }
+
+ ReducingRangeMap(boolean inclusiveEnds, RoutingKey[] ends, V[] values)
+ {
+ super(inclusiveEnds, ends, values);
+ this.endKeys = RoutingKeys.ofSortedUnique(ends);
+ }
+
+ public <V2> V2 foldl(Routables<?, ?> routables, BiFunction<V, V2, V2>
fold, V2 initialValue, Predicate<V2> terminate)
+ {
+ switch (routables.domain())
+ {
+ default: throw new AssertionError();
+ case Key: return foldl((AbstractKeys<?, ?>) routables, fold,
initialValue, terminate);
+ case Range: return Routables.foldl(endKeys, (AbstractRanges<?>)
routables, (f, vs, routingKey, v, index) -> f.apply(vs[index], v), fold,
values, initialValue, terminate);
+ }
+ }
+
+ public <V2> V2 foldl(AbstractKeys<?, ?> keys, BiFunction<V, V2, V2>
reduce, V2 accumulator, Predicate<V2> terminate)
+ {
+ int i = 0, j = 0;
+ while (j < keys.size())
+ {
+ i = endKeys.findNext(i, keys.get(j), FAST);
+ if (i < 0) i = -1 - i;
+ else if (inclusiveEnds) ++i;
+
+ accumulator = reduce.apply(values[i], accumulator);
+ if (i == endKeys.size() || terminate.test(accumulator))
+ return accumulator;
+
+ j = keys.findNext(j + 1, endKeys.get(i), FAST);
+ if (j < 0) j = -1 - j;
+ else if (inclusiveEnds) ++j;
+ }
+ return accumulator;
+ }
+
+ /**
+ * returns a copy of this LinearRangeMap limited to the ranges supplied,
with all other ranges reporting Ballot.none()
Review Comment:
It's not a ballot anymore
--
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]