Skip to content

Commit 00eb189

Browse files
committed
clean up fixed bitfields
1 parent 5400e0b commit 00eb189

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
lines changed

iabgpp-encoder/src/main/java/com/iab/gpp/encoder/bitstring/BitString.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.iab.gpp.encoder.bitstring;
22

33
import java.util.BitSet;
4+
import java.util.PrimitiveIterator.OfInt;
5+
import java.util.stream.IntStream;
6+
import com.iab.gpp.encoder.datatype.encoder.BaseIntegerSet;
47
import com.iab.gpp.encoder.datatype.encoder.IntegerBitSet;
58
import com.iab.gpp.encoder.datatype.encoder.IntegerSet;
69
import com.iab.gpp.encoder.error.DecodingException;
@@ -46,7 +49,7 @@ public static final BitString of(String str) {
4649
}
4750

4851
public IntegerSet toIntegerSet() {
49-
return new IntegerBitSet(bitSet, from, to);
52+
return new IntegerBitSet(bitSet, from, to, 1);
5053
}
5154

5255
@Override

iabgpp-encoder/src/main/java/com/iab/gpp/encoder/datatype/EncodableFixedBitfield.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public final class EncodableFixedBitfield extends AbstractEncodableBitStringData
1616
public EncodableFixedBitfield(int numElements) {
1717
super(true);
1818
this.numElements = numElements;
19-
this.value = IntegerBitSet.withLimit(numElements);
19+
this.value = new IntegerBitSet(numElements);
2020
}
2121

2222
public void encode(BitStringBuilder builder) {

iabgpp-encoder/src/main/java/com/iab/gpp/encoder/datatype/encoder/IntegerBitSet.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,21 @@ public final class IntegerBitSet extends BaseIntegerSet {
1717
protected final BitSet bitSet;
1818
protected final int from;
1919
protected final int to;
20+
private final int adjustment;
2021

21-
public IntegerBitSet(BitSet bitSet, int from, int to) {
22+
public IntegerBitSet(BitSet bitSet, int from, int to, int adjustment) {
2223
this.bitSet = bitSet;
2324
this.from = from;
2425
this.to = to;
26+
this.adjustment = adjustment;
2527
}
2628

27-
public static final IntegerBitSet withLimit(int limit) {
28-
return new IntegerBitSet(new BitSet(0), 0, limit);
29-
}
30-
31-
public IntegerBitSet(int size) {
32-
this(new BitSet(size), 0, MAX_COLLECTION_SIZE);
29+
public IntegerBitSet(int limit) {
30+
this(new BitSet(0), 0, limit, 0);
3331
}
3432

3533
public IntegerBitSet() {
36-
this(0);
34+
this(MAX_COLLECTION_SIZE);
3735
}
3836

3937
@Override
@@ -47,6 +45,14 @@ public int size() {
4745
return count;
4846
}
4947

48+
private int getOffset(int value) {
49+
int offset = from - adjustment + value;
50+
if (offset < from) {
51+
throw new IndexOutOfBoundsException();
52+
}
53+
return offset;
54+
}
55+
5056
@Override
5157
public void clear() {
5258
bitSet.clear(from, to);
@@ -59,7 +65,7 @@ public boolean isEmpty() {
5965

6066
@Override
6167
public boolean containsInt(int value) {
62-
int offset = from + value;
68+
int offset = getOffset(value);
6369
return offset < to && bitSet.get(offset);
6470
}
6571

@@ -85,7 +91,7 @@ public int nextInt() {
8591
}
8692
int next = cursor;
8793
cursor = bitSet.nextSetBit(cursor + 1);
88-
return next - from;
94+
return next - from + adjustment;
8995
}
9096
};
9197
}
@@ -110,8 +116,8 @@ public void addRange(int start, int end) {
110116
if (end < start) {
111117
throw new IllegalArgumentException("Negative length range");
112118
}
113-
int realStart = from + start;
114-
int realEnd = from + end;
119+
int realStart = getOffset(start);
120+
int realEnd = getOffset(end);
115121
if (realStart >= to) {
116122
logOutOfRange(start);
117123
return;
@@ -124,7 +130,7 @@ public void addRange(int start, int end) {
124130
}
125131

126132
public boolean addInt(int value) {
127-
int offset = from + value;
133+
int offset = getOffset(value);
128134
if (offset >= to) {
129135
logOutOfRange(value);
130136
return false;
@@ -138,7 +144,7 @@ public boolean addInt(int value) {
138144
}
139145

140146
public boolean removeInt(int value) {
141-
int offset = from + value;
147+
int offset = getOffset(value);
142148
if (offset >= to) {
143149
logOutOfRange(value);
144150
return false;

iabgpp-encoder/src/main/java/com/iab/gpp/encoder/datatype/encoder/OptimizedFibonacciRangeEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static IntegerSet decode(BitString bitString) throws DecodingException {
3838
} else {
3939
BitString bits = bitString.substring(17);
4040
int length = bits.length();
41-
IntegerBitSet value = new IntegerBitSet(length + 1);
41+
IntegerBitSet value = new IntegerBitSet();
4242
for (int i = 0; i < length; i++) {
4343
if (bits.getValue(i)) {
4444
value.addInt(i + 1);

iabgpp-encoder/src/main/java/com/iab/gpp/encoder/datatype/encoder/OptimizedFixedRangeEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static IntegerSet decode(BitString bitString) throws DecodingException {
3737
} else {
3838
BitString bits = bitString.substring(17);
3939
int length = bits.length();
40-
IntegerBitSet value = new IntegerBitSet(length + 1);
40+
IntegerBitSet value = new IntegerBitSet();
4141
for (int i = 0; i < length; i++) {
4242
if (bits.getValue(i)) {
4343
value.addInt(i + 1);

iabgpp-encoder/src/test/java/com/iab/gpp/encoder/datatype/encoder/FixedBitfieldEncoderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void testEncode7() {
7373

7474
@Test
7575
public void testEncode8() {
76-
IntegerSet set = IntegerBitSet.withLimit(5);
76+
IntegerSet set = new IntegerBitSet(5);
7777
for(int i = 0; i <= 10; i++) {
7878
set.addInt(i);
7979
}
@@ -82,7 +82,7 @@ public void testEncode8() {
8282

8383
@Test
8484
public void testEncode9() {
85-
IntegerBitSet set = IntegerBitSet.withLimit(5);
85+
IntegerBitSet set = new IntegerBitSet(5);
8686
set.addRange(0,10);
8787
Assertions.assertEquals(Set.of(0,1,2,3,4), set);
8888
}

0 commit comments

Comments
 (0)