Skip to content

Commit e4f74a2

Browse files
Change LeafNode clone to copy the properties instead of using getKey() constructor (RoaringBitmap#825)
* Change LeafNode clone to copy the properties instead of using constructor * better clone
1 parent 6ff0e46 commit e4f74a2

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

roaringbitmap/src/main/java/org/roaringbitmap/art/LeafNode.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public LeafNode(long key, long containerIdx) {
4343
}
4444

4545
@Override
46-
protected LeafNode clone() {
47-
return new LeafNode(getKey(), containerIdx);
46+
public LeafNode clone() {
47+
return new LeafNode(getKey() << 16, containerIdx);
4848
}
4949

5050
@Override
@@ -99,14 +99,14 @@ public byte[] getKeyBytes() {
9999
}
100100

101101
public long getKey() {
102-
return (((long) keyHigh) & 0xFFFFFFFFL) << 16 | (((long)keyLow) & 0xFFFFL);
102+
return (((long) keyHigh) & 0xFFFFFFFFL) << 16 | (((long) keyLow) & 0xFFFFL);
103103
}
104104

105-
/**
106-
* Sets the key from a long value, only the high 48 bits are used.
107-
*
108-
* @param key the long value representing the key
109-
*/
105+
/**
106+
* Sets the key from a long value, only the high 48 bits are used.
107+
*
108+
* @param key the long value representing the key
109+
*/
110110
private void setKeyFromShifted(long key) {
111111
this.keyHigh = (int) (key >> 32);
112112
this.keyLow = (char) (key >> 16);
@@ -124,16 +124,17 @@ protected void serializeHeader(DataOutput dataOutput) throws IOException {
124124
@Override
125125
protected void serializeHeader(ByteBuffer byteBuffer) throws IOException {
126126
byteBuffer.put((byte) NodeType.LEAF_NODE.ordinal());
127-
byteBuffer.putShort((short)0);
128-
byteBuffer.put((byte)0);
127+
byteBuffer.putShort((short) 0);
128+
byteBuffer.put((byte) 0);
129129
}
130130

131131
@Override
132132
public String toString() {
133-
return "LeafNode{" +
134-
"key=" + Long.toHexString(getKey()) +
135-
", containerIdx=" + containerIdx +
136-
'}';
133+
return "LeafNode{"
134+
+ "key="
135+
+ Long.toHexString(getKey())
136+
+ ", containerIdx="
137+
+ containerIdx
138+
+ '}';
137139
}
138-
139140
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.roaringbitmap.art;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class LeafNodeTest {
7+
8+
@Test
9+
public void testClone() {
10+
LeafNode leafOne = new LeafNode(new byte[] {1, 2, 3, 4, 5, 0}, 10);
11+
LeafNode cloned = leafOne.clone();
12+
13+
Assertions.assertEquals(leafOne.toString(), cloned.toString());
14+
}
15+
}

0 commit comments

Comments
 (0)