Skip to content

Commit fd974be

Browse files
committed
Change remove to its previous implementation, use JDK's copyOf for removeFrom
1 parent 1d983d3 commit fd974be

File tree

3 files changed

+28
-47
lines changed

3 files changed

+28
-47
lines changed

src/main/java/btree4j/BTree.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,15 @@
3939
import btree4j.utils.io.FastMultiByteArrayOutputStream;
4040
import btree4j.utils.lang.ArrayUtils;
4141
import btree4j.utils.lang.Primitives;
42-
43-
import java.io.DataInputStream;
44-
import java.io.DataOutputStream;
45-
import java.io.File;
46-
import java.io.IOException;
47-
import java.io.RandomAccessFile;
48-
import java.nio.ByteBuffer;
49-
import java.util.Arrays;
50-
51-
import javax.annotation.CheckForNull;
52-
5342
import com.google.common.annotations.Beta;
5443
import org.apache.commons.logging.Log;
5544
import org.apache.commons.logging.LogFactory;
5645

46+
import javax.annotation.CheckForNull;
47+
import java.io.*;
48+
import java.nio.ByteBuffer;
49+
import java.util.Arrays;
50+
5751
/**
5852
* BTree represents a Variable Magnitude Simple-Prefix B+Tree File.
5953
*/
@@ -669,20 +663,20 @@ void removeFrom(Value searchKey) throws BTreeException {
669663
getChildNode(leftIdx).removeFrom(searchKey);
670664

671665
if (leftIdx < keys.length && leftIdx + 1 < ptrs.length)
672-
set(ArrayUtils.removeFrom(keys, leftIdx), ArrayUtils.removeFrom(ptrs, leftIdx + 1));
666+
set(ArrayUtils.copyOf(keys, leftIdx), ArrayUtils.copyOf(ptrs, leftIdx + 1));
673667
break;
674668
case LEAF:
675669
leftIdx = (leftIdx < 0) ? -(leftIdx + 1) : leftIdx;
676670
if (leftIdx < keys.length && leftIdx < ptrs.length)
677-
set(ArrayUtils.removeFrom(keys, leftIdx), ArrayUtils.removeFrom(ptrs, leftIdx));
671+
set(ArrayUtils.copyOf(keys, leftIdx), ArrayUtils.copyOf(ptrs, leftIdx));
678672
this._next = -1;
679673
break;
680674
default:
681675
throw new BTreeCorruptException(
682676
"Invalid page type '" + ph.getStatus() + "' in removeValue");
683677
}
684678

685-
if (getParent() == null)
679+
if (getParent() == null)
686680
while (_rootNode.ptrs.length == 1) {
687681
_rootNode = _rootNode.getChildNode(0);
688682
}

src/main/java/btree4j/utils/lang/ArrayUtils.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -301,29 +301,21 @@ public static int[] append(final int[] left, final int[] right) {
301301
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >=
302302
* array.length), or if the array is <code>null</code>.
303303
* @since 2.1
304-
* @implNote Its dependance on removeFrom may slow down the function.
305304
*/
306305
@SuppressWarnings("unchecked")
307306
public static <T> T[] remove(final T[] array, final int index) {
308-
int length = getLength(array);
309-
Object result = Arrays.copyOf(removeFrom(array, index), length - 1);
310-
if (index < length - 1) {
311-
System.arraycopy(array, index + 1, result, index, length - index - 1);
312-
}
313-
return (T[]) result;
314-
}
315-
316-
public static <T> T[] removeFrom(final T[] array, final int index) {
317307
int length = getLength(array);
318308
if (index < 0 || index >= length) {
319309
throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
320310
}
321-
Object result = Array.newInstance(array.getClass().getComponentType(), index);
311+
Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
322312
System.arraycopy(array, 0, result, 0, index);
313+
if (index < length - 1) {
314+
System.arraycopy(array, index + 1, result, index, length - index - 1);
315+
}
323316
return (T[]) result;
324317
}
325318

326-
327319
@SuppressWarnings("unchecked")
328320
public static <T> T[] remove(final T[] array, final int from, final int to) {
329321
assert (to >= from) : to + " - " + from;
@@ -341,22 +333,14 @@ public static <T> T[] remove(final T[] array, final int from, final int to) {
341333
return (T[]) result;
342334
}
343335

344-
/**
345-
* @implNote Its dependance on removeFrom may slow down the function.
346-
*/
347336
public static long[] remove(final long[] vals, final int idx) {
348-
long[] newVals = Arrays.copyOf(removeFrom(vals, idx), vals.length - 1);
349-
if (idx < newVals.length) {
350-
System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx);
351-
}
352-
return newVals;
353-
}
354-
355-
public static long[] removeFrom(final long[] vals, final int idx) {
356-
long[] newVals = new long[idx];
337+
long[] newVals = new long[vals.length - 1];
357338
if (idx > 0) {
358339
System.arraycopy(vals, 0, newVals, 0, idx);
359340
}
341+
if (idx < newVals.length) {
342+
System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx);
343+
}
360344
return newVals;
361345
}
362346

@@ -482,6 +466,11 @@ public static int[] copyOf(final int[] original, final int newLength) {
482466
return copy;
483467
}
484468

469+
public static long[] copyOf(final long[] vals, final int idx) {
470+
return Arrays.copyOf(vals, idx);
471+
472+
}
473+
485474
public static char[] copyOf(final char[] original, final int newLength) {
486475
final char[] copy = new char[newLength];
487476
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));

src/test/java/btree4j/ArrayUtilsTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import org.junit.Assert;
55
import org.junit.Test;
66

7-
import java.util.ArrayList;
8-
97
public class ArrayUtilsTest {
108
// --------------- remove
119
@Test
@@ -50,46 +48,46 @@ public void shouldRemoveMiddleValuePrimitive() {
5048
Assert.assertArrayEquals(new long[]{1, 3}, a);
5149
}
5250

53-
// --------------- removeFrom
51+
// --------------- copyOf
5452
@Test
5553
public void shouldRemoveFromFirstValue() {
5654
Integer[] a = {1, 2, 3};
57-
a = ArrayUtils.removeFrom(a, 0);
55+
a = ArrayUtils.copyOf(a, 0);
5856
Assert.assertArrayEquals(new Integer[]{}, a);
5957
}
6058

6159
@Test
6260
public void shouldRemoveFromLastValue() {
6361
Integer[] a = {1, 2, 3};
64-
a = ArrayUtils.removeFrom(a, 2);
62+
a = ArrayUtils.copyOf(a, 2);
6563
Assert.assertArrayEquals(new Integer[]{1, 2}, a);
6664
}
6765

6866
@Test
6967
public void shouldRemoveFromMiddleValue() {
7068
Integer[] a = {1, 2, 3};
71-
a = ArrayUtils.removeFrom(a, 1);
69+
a = ArrayUtils.copyOf(a, 1);
7270
Assert.assertArrayEquals(new Integer[]{1}, a);
7371
}
7472

7573
@Test
7674
public void shouldRemoveFromFirstValuePrimitive() {
7775
long[] a = {1, 2, 3};
78-
a = ArrayUtils.removeFrom(a, 0);
76+
a = ArrayUtils.copyOf(a, 0);
7977
Assert.assertArrayEquals(new long[]{}, a);
8078
}
8179

8280
@Test
8381
public void shouldRemoveFromLastValuePrimitive() {
8482
long[] a = {1, 2, 3};
85-
a = ArrayUtils.removeFrom(a, 2);
83+
a = ArrayUtils.copyOf(a, 2);
8684
Assert.assertArrayEquals(new long[]{1, 2}, a);
8785
}
8886

8987
@Test
9088
public void shouldRemoveFromMiddleValuePrimitive() {
9189
long[] a = {1, 2, 3};
92-
a = ArrayUtils.removeFrom(a, 1);
90+
a = ArrayUtils.copyOf(a, 1);
9391
Assert.assertArrayEquals(new long[]{1}, a);
9492
}
9593
}

0 commit comments

Comments
 (0)