Skip to content

Commit

Permalink
Added toArray method to PGbit
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 23, 2024
1 parent b7bb910 commit 44637d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/main/java/com/pgvector/PGbit.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ public void toBytes(byte[] bytes, int offset) {
}
}

/**
* Returns an array
*
* @return an array
*/
public boolean[] toArray() {
boolean[] bits = new boolean[length];
for (int i = 0; i < length; i++) {
bits[i] = ((data[i / 8] >> (7 - (i % 8))) & 1) == 1;
}
return bits;
}

/**
* Registers the bit type
*
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/com/pgvector/PGbitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ public class PGbitTest {
@Test
void testArrayConstructor() {
PGbit vec = new PGbit(new boolean[] {true, false, true});
assertEquals("101", vec.getValue());
assertArrayEquals(new boolean[] {true, false, true}, vec.toArray());
}

@Test
void testStringConstructor() throws SQLException {
PGbit vec = new PGbit("101");
assertArrayEquals(new boolean[] {true, false, true}, vec.toArray());
}

@Test
void testGetValue() {
PGbit vec = new PGbit(new boolean[] {true, false, true});
assertEquals("101", vec.getValue());
}
}

0 comments on commit 44637d9

Please sign in to comment.