@@ -769,6 +769,7 @@ cdef class AbstractBitMap64:
769
769
An efficient and light-weight ordered set of 64 bits integers.
770
770
"""
771
771
cdef croaring.roaring64_bitmap_t* _c_bitmap
772
+ cdef int64_t _h_val
772
773
773
774
def __cinit__ (self , values = None , optimize = True , no_init = False ):
774
775
if no_init:
@@ -780,6 +781,7 @@ cdef class AbstractBitMap64:
780
781
self ._c_bitmap = croaring.roaring64_bitmap_create()
781
782
elif isinstance (values, AbstractBitMap64):
782
783
self ._c_bitmap = croaring.roaring64_bitmap_copy((< AbstractBitMap64?> values)._c_bitmap)
784
+ self ._h_val = (< AbstractBitMap64?> values)._h_val
783
785
elif isinstance (values, range ):
784
786
_, (start, stop, step) = values.__reduce__()
785
787
if step < 0 :
@@ -806,6 +808,8 @@ cdef class AbstractBitMap64:
806
808
if size > 0 :
807
809
buff_vect = values
808
810
croaring.roaring64_bitmap_add_many(self ._c_bitmap, size, & buff_vect[0 ])
811
+ if not isinstance (values, AbstractBitMap64):
812
+ self ._h_val = 0
809
813
if optimize:
810
814
self .run_optimize()
811
815
@@ -891,6 +895,31 @@ cdef class AbstractBitMap64:
891
895
raise AssertionError (' range_end must not be lower than range_start' )
892
896
return croaring.roaring64_bitmap_range_cardinality(self ._c_bitmap, range_start, range_end)
893
897
898
+ cdef compute_hash(self ):
899
+ cdef int64_t h_val = 0
900
+ cdef uint32_t i, count, max_count= 256
901
+ cdef croaring.roaring64_iterator_t * iterator = croaring.roaring64_iterator_create(self ._c_bitmap)
902
+ cdef uint64_t * buff = < uint64_t* > malloc(max_count* 8 )
903
+ while True :
904
+ count = croaring.roaring64_iterator_read(iterator, buff, max_count)
905
+ i = 0
906
+ while i < count:
907
+ h_val += buff[i]
908
+ # TODO find a good hash formula
909
+ i += 1
910
+ if count != max_count:
911
+ break
912
+ croaring.roaring64_iterator_free(iterator)
913
+ free(buff)
914
+ if not self :
915
+ return - 1
916
+ return h_val
917
+
918
+ def __hash__ (self ):
919
+ if self ._h_val == 0 :
920
+ self ._h_val = self .compute_hash()
921
+ return self ._h_val
922
+
894
923
def iter_equal_or_larger (self , uint64_t val ):
895
924
"""
896
925
Iterate over items in the bitmap equal or larger than a given value.
0 commit comments