BitMagic-C++: sample9.cpp Source File

BitMagic-C++

sample9.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 For more information please visit: http://bitmagic.io
17 */
18
19 /** \example sample9.cpp
20
21 Example demonstrates binary distance metrics. BM library can compute
22 various distances without materializing a product set.
23
24 \sa bm::count_and
25 \sa bm::count_xor
26 \sa bm::count_sub
27 \sa bm::count_or
28 \sa bm::distance_operation
29
30 For more information please visit: http://bmagic.sourceforge.net
31 */
32
33 /*! \file sample9.cpp
34 \brief Example: bvector<> binary similarity / distance algorithms
35 */
36
37
38 #include <iostream>
39
40 #include "bm.h"
41 #include "bmalgo.h"
42
43
44 using namespace std;
45
46
47 int main(void)
48 {
49 try
50 {
51 bm::bvector<> bv1;
52 bm::bvector<> bv2;
53
54 bv1[10] = true;
55 bv1[100] = true;
56 bv1[10000] = true;
57
58 bv2[10] = true;
59 bv2[100] = true;
60 bv2[20000] = true;
61 bv2[30000] = true;
62
63 // Hamming distance:
64
65 auto hamming = bm::count_xor(bv1, bv2);
66
67 cout << "Hamming distance = " << hamming << endl;
68
69 // Dice distance using basic distance functions
70
71 double dice =
72 double(2 * bm::count_and(bv1, bv2))/double(bv1.count() + bv2.count());
73
74 cout << "Dice distance = " << dice << endl;
75
76
77 // Dice distance, can be computed using "metric pipeline"
78
79 bm::distance_metric_descriptor dmd[3];
80 dmd[0].metric = bm::COUNT_AND;
81 dmd[1].metric = bm::COUNT_A;
82 dmd[2].metric = bm::COUNT_B;
83
84 bm::distance_operation(bv1, bv2, dmd, dmd+3);
85
86 double dice_p =
87 double(2 * dmd[0].result) / double(dmd[1].result + dmd[2].result);
88
89 cout << "Dice distance (pipeline) = " << dice_p << endl;
90 }
91 catch(std::exception& ex)
92 {
93 std::cerr << ex.what() << std::endl;
94 return 1;
95 }
96 return 0;
97 }
98
bm::distance_metric_descriptor
Distance metric descriptor, holds metric code and result.
Definition: bmalgo_impl.h:87
bmalgo.h
Algorithms for bvector<> (main include)
bm::bvector<>
bm::bvector::count
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition: bm.h:2194
bm::COUNT_A
@ COUNT_A
A.count()
Definition: bmalgo_impl.h:64
bm::distance_metric_descriptor::metric
distance_metric metric
Definition: bmalgo_impl.h:95
bm::count_xor
bm::distance_metric_descriptor::size_type count_xor(const BV &bv1, const BV &bv2) BMNOEXCEPT
Computes bitcount of XOR operation of two bitsets.
Definition: bmalgo.h:81
bm::count_and
BV::size_type count_and(const BV &bv1, const BV &bv2) BMNOEXCEPT
Computes bitcount of AND operation of two bitsets.
Definition: bmalgo.h:49
bm::distance_operation
void distance_operation(const BV &bv1, const BV &bv2, distance_metric_descriptor *dmit, distance_metric_descriptor *dmit_end) BMNOEXCEPT
Distance computing template function.
Definition: bmalgo_impl.h:702
bm::COUNT_B
@ COUNT_B
B.count()
Definition: bmalgo_impl.h:65
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bm::COUNT_AND
@ COUNT_AND
(A & B).count()
Definition: bmalgo_impl.h:59
main
int main(void)
Definition: sample9.cpp:47