ifesdjeen commented on code in PR #179:
URL: https://github.com/apache/cassandra-accord/pull/179#discussion_r1988910940


##########
accord-core/src/main/java/accord/utils/SortedArrays.java:
##########
@@ -447,6 +447,93 @@ else if (cmp < 0)
         }
     }
 
+    // applies the update function to all entries in left, and any matching 
entries in right
+    public static <O, T extends O> O[] linearUpdate(T[] left, int leftStart, 
int leftEnd, T[] right, int rightStart, int rightEnd, AsymmetricComparator<? 
super T, ? super T> comparator, BiFunction<? super T, ? super T, ? extends T> 
update, ObjectBuffers<O> buffers)
+    {
+        int leftIdx = leftStart;
+        int rightIdx = rightStart;
+
+        T nextKey = null;
+        while (leftIdx < leftEnd && rightIdx < rightEnd)
+        {
+            T leftKey = left[leftIdx];
+            T rightKey = right[rightIdx];
+            int cmp = leftKey == rightKey ? 0 : comparator.compare(leftKey, 
rightKey);
+            if (cmp > 0)
+            {
+                ++rightIdx;
+                continue;
+            }
+
+            if (cmp < 0) rightKey = null;
+            else ++rightIdx;
+
+            T testKey = update.apply(leftKey, rightKey);
+            if (testKey != leftKey)
+            {
+                nextKey = testKey;
+                break;
+            }
+            ++leftIdx;
+        }
+
+        if (nextKey == null)
+        {
+            while (leftIdx < leftEnd)
+            {
+                T leftKey = left[leftIdx];
+                T testKey = update.apply(left[leftIdx], null);
+                if (testKey != leftKey)
+                {
+                    nextKey = testKey;
+                    break;
+                }
+                ++leftIdx;
+            }
+
+            if (nextKey == null)
+                return left;
+        }
+
+        O[] result = buffers.get(leftEnd - leftStart);
+        int resultSize = leftIdx - leftStart;
+        System.arraycopy(left, leftStart, result, 0, resultSize);
+        result[resultSize++] = nextKey;
+        ++leftIdx;
+
+        try
+        {
+            while (leftIdx < leftEnd && rightIdx < rightEnd)
+            {
+                T leftKey = left[leftIdx];
+                T rightKey = right[rightIdx];
+                int cmp = leftKey == rightKey ? 0 : 
comparator.compare(leftKey, rightKey);
+
+                if (cmp > 0)
+                {
+                    ++rightIdx;
+                    continue;
+                }
+
+                if (cmp < 0) rightKey = null;
+                else ++rightIdx;
+                ++leftIdx;
+
+                result[resultSize++] = update.apply(leftKey, rightKey);
+            }
+
+            while (leftIdx < leftEnd)

Review Comment:
   Just to make sure: do we always know that left is longer than right?



-- 
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