BitMagic-C++
sample9.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16For 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
44using namespace std;
45
46
47int 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
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
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Algorithms for bvector<> (main include)
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition bm.h:2194
void distance_operation(const BV &bv1, const BV &bv2, distance_metric_descriptor *dmit, distance_metric_descriptor *dmit_end) BMNOEXCEPT
Distance computing template function.
@ COUNT_A
A.count()
Definition bmalgo_impl.h:64
@ COUNT_B
B.count()
Definition bmalgo_impl.h:65
@ COUNT_AND
(A & B).count()
Definition bmalgo_impl.h:59
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_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
int main(void)
Definition sample9.cpp:47
Distance metric descriptor, holds metric code and result.
Definition bmalgo_impl.h:88