Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide ssz bitvector OR function #8302

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ default boolean isWritableSupported() {

SszBitvector withBit(int i);

SszBitvector or(SszBitvector other);

/** Returns individual bit value */
boolean getBit(int i);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public List<Integer> getSetBitIndices() {
return data.stream().boxed().toList();
}

public BitvectorImpl or(final BitvectorImpl other) {
if (other.getSize() > getSize()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd think we want to make sure the sizes match exactly if we're doing a bitwise or, otherwise we'll be needing to decide if the stragglers are left or right etc... other.size() != getSize

Copy link
Contributor Author

@tbenr tbenr May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm yes but this mimics the BitlistImpl or which does the same. We may say that this is a vector so it could be more strict but I'm more on the side to have a similar behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its probably bad behaviour to mimic... you can mis-match and do bitwise or partially.... future @tbenr problem to find i guess...

Copy link
Contributor

@zilm13 zilm13 May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this feature really used somewhere? (I mean or with smaller list/vector)
If yes we could split it to several safer steps, but it could affect performance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k lets be more stringent and maybe open in the future.
@zilm13 we won't (at least for 7549)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i really do think we're better off making this one right rather than copying a bad idea - especially on a bitwise or... at least then we know why its a greater than if it causes a break...

throw new IllegalArgumentException(
"Argument bitfield size is greater: " + other.getSize() + " > " + getSize());
}
BitSet newData = (BitSet) this.data.clone();
tbenr marked this conversation as resolved.
Show resolved Hide resolved
newData.or(other.data);
return new BitvectorImpl(newData, size);
}

public BitvectorImpl withBit(final int i) {
checkElementIndex(i, size);
BitSet newSet = (BitSet) data.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public SszBitvector withBit(final int i) {
return new SszBitvectorImpl(getSchema(), value.withBit(i));
}

@Override
public SszBitvector or(SszBitvector other) {
tbenr marked this conversation as resolved.
Show resolved Hide resolved
return new SszBitvectorImpl(getSchema(), value.or(toBitvectorImpl(other)));
}

@Override
protected int sizeImpl() {
return getSchema().getLength();
Expand All @@ -108,4 +113,8 @@ public boolean isWritableSupported() {
public String toString() {
return "SszBitvector{size=" + this.size() + ", " + value.toString() + "}";
}

private BitvectorImpl toBitvectorImpl(final SszBitvector bv) {
return ((SszBitvectorImpl) bv).value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ void testTreeRoundtrip(SszBitvector bitvector1) {
SszDataAssert.assertThatSszData(bitvector2).isEqualByAllMeansTo(bitvector1);
}

@ParameterizedTest
@MethodSource("bitvectorArgs")
void or_testEqualList(SszBitvector bitvector) {
SszBitvector res = bitvector.or(bitvector);
assertThat(res).isEqualTo(bitvector);
}

@ParameterizedTest
@MethodSource("bitvectorArgs")
void or_shouldThrowIfBitvectorSizeIsLarger(SszBitvector bitvector) {
SszBitvectorSchema<SszBitvector> largerSchema =
SszBitvectorSchema.create(bitvector.getSchema().getMaxLength() + 1);
SszBitvector largerBitvector = largerSchema.ofBits(bitvector.size() - 1, bitvector.size());
assertThatThrownBy(() -> bitvector.or(largerBitvector))
.isInstanceOf(IllegalArgumentException.class);
}

@ParameterizedTest
@MethodSource("bitvectorArgs")
void getBitCount_shouldReturnCorrectCount(SszBitvector bitvector) {
Expand Down Expand Up @@ -177,6 +194,25 @@ void testBitMethodsAreConsistent(SszBitvector vector) {
assertThat(vector.getBitCount()).isEqualTo(bitsIndices.size());
}

@ParameterizedTest
@MethodSource("bitvectorArgs")
void testOr(SszBitvector bitvector) {
SszBitvector orVector = random(bitvector.getSchema());
SszBitvector res = bitvector.or(orVector);
assertThat(res.size()).isEqualTo(bitvector.size());
assertThat(res.getSchema()).isEqualTo(bitvector.getSchema());
for (int i = 0; i < bitvector.size(); i++) {
assertThat(res.getBit(i)).isEqualTo(bitvector.getBit(i) || orVector.getBit(i));
}
}

@ParameterizedTest
@MethodSource("bitvectorArgs")
void testOrWithEmptyBitvector(SszBitvector bitvector) {
SszBitvector empty = bitvector.getSchema().ofBits();
assertThat(bitvector.or(empty)).isEqualTo(bitvector);
}

@ParameterizedTest
@MethodSource("bitvectorArgs")
void get_shouldThrowIndexOutOfBounds(SszBitvector vector) {
Expand Down