Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2ce3f43

Browse files
committed
Improved internal representation of Bit class
1 parent2d1b754 commit2ce3f43

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

‎pgvector/bit.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,58 @@
55

66
classBit:
77
def__init__(self,value):
8-
ifisinstance(value,str):
9-
self._value=self.from_text(value)._value
10-
elifisinstance(value,bytes):
11-
self._value=np.unpackbits(np.frombuffer(value,dtype=np.uint8)).astype(bool)
8+
ifisinstance(value,bytes):
9+
self._len=8*len(value)
10+
self._data=value
1211
else:
13-
value=np.asarray(value)
12+
ifisinstance(value,str):
13+
value= [v!='0'forvinvalue]
14+
else:
15+
value=np.asarray(value)
1416

15-
ifvalue.dtype!=np.bool:
16-
warn('expected elements to be boolean',stacklevel=2)
17-
value=value.astype(bool)
17+
ifvalue.dtype!=np.bool:
18+
warn('expected elements to be boolean',stacklevel=2)
19+
value=value.astype(bool)
1820

19-
ifvalue.ndim!=1:
20-
raiseValueError('expected ndim to be 1')
21+
ifvalue.ndim!=1:
22+
raiseValueError('expected ndim to be 1')
2123

22-
self._value=value
24+
self._len=len(value)
25+
self._data=np.packbits(value).tobytes()
2326

2427
def__repr__(self):
2528
returnf'Bit({self.to_text()})'
2629

2730
def__eq__(self,other):
2831
ifisinstance(other,self.__class__):
29-
returnnp.array_equal(self.to_numpy(),other.to_numpy())
32+
returnself._len==other._lenandself._data==other._data
3033
returnFalse
3134

3235
defto_list(self):
33-
returnself._value.tolist()
36+
returnself.to_numpy().tolist()
3437

3538
defto_numpy(self):
36-
returnself._value
39+
returnnp.unpackbits(np.frombuffer(self._data,dtype=np.uint8),count=self._len).astype(bool)
3740

3841
defto_text(self):
39-
return''.join(self._value.astype(np.uint8).astype(str))
42+
return''.join(format(v,'08b')forvinself._data)[:self._len]
4043

4144
defto_binary(self):
42-
returnpack('>i',len(self._value))+np.packbits(self._value).tobytes()
45+
returnpack('>i',self._len)+self._data
4346

4447
@classmethod
4548
deffrom_text(cls,value):
46-
returncls(np.asarray([v!='0'forvinvalue],dtype=bool))
49+
returncls(str(value))
4750

4851
@classmethod
4952
deffrom_binary(cls,value):
50-
count=unpack_from('>i',value)[0]
51-
buf=np.frombuffer(value,dtype=np.uint8,offset=4)
52-
returncls(np.unpackbits(buf,count=count).astype(bool))
53+
ifnotisinstance(value,bytes):
54+
raiseValueError('expected bytes')
55+
56+
bit=cls.__new__(cls)
57+
bit._len=unpack_from('>i',value)[0]
58+
bit._data=value[4:]
59+
returnbit
5360

5461
@classmethod
5562
def_to_db(cls,value):

‎tests/test_bit.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def test_bytes(self):
2525
assertBit(b'\xff\x00').to_list()== [True,True,True,True,True,True,True,True,False,False,False,False,False,False,False,False]
2626
assertBit(b'\xfe\x07').to_list()== [True,True,True,True,True,True,True,False,False,False,False,False,False,True,True,True]
2727

28+
deftest_ndarray(self):
29+
arr=np.array([True,False,True])
30+
assertBit(arr).to_list()== [True,False,True]
31+
assertnp.array_equal(Bit(arr).to_numpy(),arr)
32+
2833
deftest_ndarray_uint8(self):
2934
arr=np.array([254,7,0],dtype=np.uint8)
3035
withpytest.warns(UserWarning,match='expected elements to be boolean'):
@@ -35,11 +40,6 @@ def test_ndarray_uint16(self):
3540
withpytest.warns(UserWarning,match='expected elements to be boolean'):
3641
assertBit(arr).to_text()=='110'
3742

38-
deftest_ndarray_same_object(self):
39-
arr=np.array([True,False,True])
40-
assertBit(arr).to_list()== [True,False,True]
41-
assertBit(arr).to_numpy()isarr
42-
4343
deftest_ndim_two(self):
4444
withpytest.raises(ValueError)aserror:
4545
Bit([[True,False], [True,False]])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp