1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml">
4
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5
<title>BitMagic: sample9.cpp Source File</title>
6
<link href="tabs.css" rel="stylesheet" type="text/css"/>
7
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
10
<!-- Generated by Doxygen 1.6.1 -->
11
<div class="navigation" id="top">
14
<li><a href="index.html"><span>Main Page</span></a></li>
15
<li><a href="modules.html"><span>Modules</span></a></li>
16
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
17
<li><a href="annotated.html"><span>Data Structures</span></a></li>
18
<li class="current"><a href="files.html"><span>Files</span></a></li>
19
<li><a href="examples.html"><span>Examples</span></a></li>
24
<li><a href="files.html"><span>File List</span></a></li>
25
<li><a href="globals.html"><span>Globals</span></a></li>
28
<h1>sample9.cpp</h1><a href="a00109.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
29
<a name="l00002"></a>00002 <span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
30
<a name="l00003"></a>00003 <span class="comment"></span>
31
<a name="l00004"></a>00004 <span class="comment">Permission is hereby granted, free of charge, to any person </span>
32
<a name="l00005"></a>00005 <span class="comment">obtaining a copy of this software and associated documentation </span>
33
<a name="l00006"></a>00006 <span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
34
<a name="l00007"></a>00007 <span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
35
<a name="l00008"></a>00008 <span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
36
<a name="l00009"></a>00009 <span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
37
<a name="l00010"></a>00010 <span class="comment">subject to the following conditions:</span>
38
<a name="l00011"></a>00011 <span class="comment"></span>
39
<a name="l00012"></a>00012 <span class="comment">The above copyright notice and this permission notice shall be included </span>
40
<a name="l00013"></a>00013 <span class="comment">in all copies or substantial portions of the Software.</span>
41
<a name="l00014"></a>00014 <span class="comment"></span>
42
<a name="l00015"></a>00015 <span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
43
<a name="l00016"></a>00016 <span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
44
<a name="l00017"></a>00017 <span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
45
<a name="l00018"></a>00018 <span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
46
<a name="l00019"></a>00019 <span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
47
<a name="l00020"></a>00020 <span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
48
<a name="l00021"></a>00021 <span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
49
<a name="l00022"></a>00022 <span class="comment">*/</span>
50
<a name="l00023"></a>00023 <span class="comment"></span>
51
<a name="l00024"></a>00024 <span class="comment">/** \example sample9.cpp</span>
52
<a name="l00025"></a>00025 <span class="comment"></span>
53
<a name="l00026"></a>00026 <span class="comment"> Example demonstrates binary distance metrics.</span>
54
<a name="l00027"></a>00027 <span class="comment"> </span>
55
<a name="l00028"></a>00028 <span class="comment"> \sa bm::count_and </span>
56
<a name="l00029"></a>00029 <span class="comment"> \sa bm::count_xor</span>
57
<a name="l00030"></a>00030 <span class="comment"> \sa bm::count_sub</span>
58
<a name="l00031"></a>00031 <span class="comment"> \sa bm::count_or</span>
59
<a name="l00032"></a>00032 <span class="comment"> \sa bm::distance_operation</span>
60
<a name="l00033"></a>00033 <span class="comment"></span>
61
<a name="l00034"></a>00034 <span class="comment"> For more information please visit: http://bmagic.sourceforge.net</span>
62
<a name="l00035"></a>00035 <span class="comment"></span>
63
<a name="l00036"></a>00036 <span class="comment">*/</span>
64
<a name="l00037"></a>00037
65
<a name="l00038"></a>00038 <span class="preprocessor">#include <iostream></span>
66
<a name="l00039"></a>00039
67
<a name="l00040"></a>00040 <span class="preprocessor">#include "<a class="code" href="a00084.html">bm.h</a>"</span>
68
<a name="l00041"></a>00041 <span class="preprocessor">#include "<a class="code" href="a00085.html">bmalgo.h</a>"</span>
69
<a name="l00042"></a>00042
70
<a name="l00043"></a>00043
71
<a name="l00044"></a>00044 <span class="keyword">using namespace </span>std;
72
<a name="l00045"></a>00045
73
<a name="l00046"></a>00046
74
<a name="l00047"></a><a class="code" href="a00109.html#a840291bc02cba5474a4cb46a9b9566fe">00047</a> <span class="keywordtype">int</span> <a class="code" href="a00101.html#a840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)
75
<a name="l00048"></a>00048 {
76
<a name="l00049"></a>00049 <a class="code" href="a00043.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv1;
77
<a name="l00050"></a>00050 <a class="code" href="a00043.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv2;
78
<a name="l00051"></a>00051
79
<a name="l00052"></a>00052 bv1[10] = <span class="keyword">true</span>;
80
<a name="l00053"></a>00053 bv1[100] = <span class="keyword">true</span>;
81
<a name="l00054"></a>00054 bv1[10000] = <span class="keyword">true</span>;
82
<a name="l00055"></a>00055
83
<a name="l00056"></a>00056 bv2[10] = <span class="keyword">true</span>;
84
<a name="l00057"></a>00057 bv2[100] = <span class="keyword">true</span>;
85
<a name="l00058"></a>00058 bv2[20000] = <span class="keyword">true</span>;
86
<a name="l00059"></a>00059 bv2[30000] = <span class="keyword">true</span>;
87
<a name="l00060"></a>00060
88
<a name="l00061"></a>00061 <span class="comment">// Hamming distance: </span>
89
<a name="l00062"></a>00062
90
<a name="l00063"></a>00063 <span class="keywordtype">unsigned</span> hamming = <a class="code" href="a00120.html#gafc64a082dcaee07f70f7faf3e24f844e" title="Computes bitcount of XOR operation of two bitsets.">bm::count_xor</a>(bv1, bv2);
91
<a name="l00064"></a>00064
92
<a name="l00065"></a>00065 cout << <span class="stringliteral">"Hamming distance = "</span> << hamming << endl;
93
<a name="l00066"></a>00066
94
<a name="l00067"></a>00067 <span class="comment">// Dice distance using basic distance functions</span>
95
<a name="l00068"></a>00068
96
<a name="l00069"></a>00069 <span class="keywordtype">double</span> dice =
97
<a name="l00070"></a>00070 double(2 * <a class="code" href="a00120.html#gae451cb4b87e8c0859b0614d2ffb99737" title="Computes bitcount of AND operation of two bitsets.">bm::count_and</a>(bv1, bv2))/double(bv1.<a class="code" href="a00043.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() + bv2.<a class="code" href="a00043.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>());
98
<a name="l00071"></a>00071
99
<a name="l00072"></a>00072 cout << <span class="stringliteral">"Dice distance = "</span> << dice << endl;
100
<a name="l00073"></a>00073
101
<a name="l00074"></a>00074
102
<a name="l00075"></a>00075 <span class="comment">// Dice distance, can be computed using "metric pipeline"</span>
103
<a name="l00076"></a>00076
104
<a name="l00077"></a>00077 <a class="code" href="a00060.html" title="Distance metric descriptor, holds metric code and result.">bm::distance_metric_descriptor</a> dmd[3];
105
<a name="l00078"></a>00078 dmd[0].<a class="code" href="a00060.html#a3ece2ccdb7a6616f06a5a15679ccabb1">metric</a> = <a class="code" href="a00120.html#ggac38d64c8b27b652413a664cddd4d40abacb39376469ba778e9a9099ddbae5e7ce" title="(A &amp; B).count()">bm::COUNT_AND</a>;
106
<a name="l00079"></a>00079 dmd[1].<a class="code" href="a00060.html#a3ece2ccdb7a6616f06a5a15679ccabb1">metric</a> = <a class="code" href="a00120.html#ggac38d64c8b27b652413a664cddd4d40abaac7947d53eb3acf81251ee4130e6a4a2" title="A.count().">bm::COUNT_A</a>;
107
<a name="l00080"></a>00080 dmd[2].<a class="code" href="a00060.html#a3ece2ccdb7a6616f06a5a15679ccabb1">metric</a> = <a class="code" href="a00120.html#ggac38d64c8b27b652413a664cddd4d40abac6dbadf2ab9405daa47dc0f10d082516" title="B.count().">bm::COUNT_B</a>;
108
<a name="l00081"></a>00081
109
<a name="l00082"></a>00082 <a class="code" href="a00120.html#ga815fef76f02576ab9ab58de0a45d8a4b" title="Distance computing template function.">bm::distance_operation</a>(bv1, bv2, dmd, dmd+3);
110
<a name="l00083"></a>00083
111
<a name="l00084"></a>00084 <span class="keywordtype">double</span> dice_p =
112
<a name="l00085"></a>00085 double(2 * dmd[0].result) / double(dmd[1].result + dmd[2].result);
113
<a name="l00086"></a>00086
114
<a name="l00087"></a>00087 cout << <span class="stringliteral">"Dice distance (pipeline) = "</span> << dice_p << endl;
115
<a name="l00088"></a>00088
116
<a name="l00089"></a>00089 <span class="keywordflow">return</span> 0;
117
<a name="l00090"></a>00090 }
118
<a name="l00091"></a>00091
120
<hr size="1"/><address style="text-align: right;"><small>Generated on Sun Nov 22 10:49:36 2009 for BitMagic by
121
<a href="http://www.doxygen.org/index.html">
122
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>