1
- use bit_vec :: BitVec ;
1
+ use fixedbitset :: FixedBitSet ;
2
2
3
- pub fn tanimoto_vec ( a : & BitVec , b : & BitVec ) -> f32 {
3
+ pub fn tanimoto_bitset ( a : & FixedBitSet , b : & FixedBitSet ) -> f32 {
4
4
let mut and_ = a. clone ( ) ;
5
- let mut or_ = a. clone ( ) ;
6
- and_. and ( b) ;
7
- or_. or ( b) ;
8
-
9
- let mut dividend: u32 = 0 ;
10
- for b in and_. blocks ( ) {
11
- dividend += b. count_ones ( ) ;
12
- }
13
- let mut divisor: u32 = 0 ;
14
- for b in or_. blocks ( ) {
15
- divisor += b. count_ones ( ) ;
16
- }
17
-
18
- return dividend as f32 / divisor as f32 ;
19
- }
20
-
21
- pub unsafe fn tanimoto_array ( a : & [ u64 ; 4 ] , b : & [ u64 ; 4 ] ) -> f32 {
22
- let mut dividend: u32 = 0 ;
23
- let mut divisor: u32 = 0 ;
24
-
25
- for i in 0 ..4 {
26
- dividend += ( ( a[ i] & b[ i] ) as i64 ) . count_ones ( ) ;
27
- divisor += ( ( a[ i] | b[ i] ) as i64 ) . count_ones ( ) ;
28
- }
29
- return dividend as f32 / divisor as f32 ;
5
+ and_. intersect_with ( b) ;
6
+ return and_. count_ones ( ..) as f32 / ( a. count_ones ( ..) + b. count_ones ( ..) - and_. count_ones ( ..) ) as f32 ;
30
7
}
31
8
32
9
#[ cfg( test) ]
33
10
mod tests {
34
- use bit_vec :: BitVec ;
35
- use crate :: ringo:: math:: similarity:: tanimoto:: { tanimoto_array , tanimoto_vec } ;
11
+ use fixedbitset :: FixedBitSet ;
12
+ use crate :: ringo:: math:: similarity:: tanimoto:: { tanimoto_bitset } ;
36
13
37
14
#[ test]
38
- fn test_tanimoto_vec_033 ( ) {
39
- let a: BitVec = BitVec :: from_bytes ( & [ 0b00000101 ] ) ;
40
- let b = BitVec :: from_bytes ( & [ 0b00000011 ] ) ;
41
-
42
- assert_eq ! ( tanimoto_vec( & a, & b) , 0.33333334 ) ;
15
+ fn test_tanimoto_bitset_033 ( ) {
16
+ let mut a = FixedBitSet :: with_capacity ( 8 ) ;
17
+ a. insert ( 0 ) ;
18
+ a. insert ( 2 ) ;
19
+ let mut b = FixedBitSet :: with_capacity ( 8 ) ;
20
+ b. insert ( 0 ) ;
21
+ b. insert ( 1 ) ;
22
+ assert_eq ! ( tanimoto_bitset( & a, & b) , 0.33333334 ) ;
43
23
}
44
24
45
25
#[ test]
46
- fn test_tanimoto_vec_05 ( ) {
47
- let a: BitVec = BitVec :: from_bytes ( & [ 0b0000001 ] ) ;
48
- let b = BitVec :: from_bytes ( & [ 0b00000011 ] ) ;
49
-
50
- assert_eq ! ( tanimoto_vec( & a, & b) , 0.5 ) ;
26
+ fn test_tanimoto_bitset_05 ( ) {
27
+ let mut a = FixedBitSet :: with_capacity ( 8 ) ;
28
+ a. insert ( 0 ) ;
29
+ let mut b = FixedBitSet :: with_capacity ( 8 ) ;
30
+ b. insert ( 0 ) ;
31
+ b. insert ( 1 ) ;
32
+ assert_eq ! ( tanimoto_bitset( & a, & b) , 0.5 ) ;
51
33
}
52
-
53
- #[ test]
54
- fn test_tanimoto_array_033 ( ) {
55
- let a: [ u64 ; 4 ] = [ 0b00000101 , 0 , 0 , 0 ] ;
56
- let b = [ 0b00000011 , 0 , 0 , 0 ] ;
57
-
58
- unsafe {
59
- assert_eq ! ( tanimoto_array( & a, & b) , 0.33333334 ) ;
60
- }
61
- }
62
-
63
- #[ test]
64
- fn test_tanimoto_array_05 ( ) {
65
- let a: [ u64 ; 4 ] = [ 0b00000001 , 0 , 0 , 0 ] ;
66
- let b = [ 0b00000011 , 0 , 0 , 0 ] ;
67
-
68
- unsafe {
69
- assert_eq ! ( tanimoto_array( & a, & b) , 0.5 ) ;
70
- }
71
- }
72
- }
34
+ }
0 commit comments