File size: 7,958 Bytes
9375c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - count_bits.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2013 Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_COUNT_BiTS_Hh_
<font color='#0000FF'>#define</font> DLIB_COUNT_BiTS_Hh_
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>climits<font color='#5555FF'>></font>
<font color='#0000FF'>namespace</font> dlib
<b>{</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>template</font> <font color='#5555FF'><</font>
<font color='#0000FF'>typename</font> T
<font color='#5555FF'>></font>
T <b><a name='count_bits'></a>count_bits</b> <font face='Lucida Console'>(</font>
T v
<font face='Lucida Console'>)</font>
<font color='#009900'>/*!
requires
- T is an unsigned integral type
ensures
- returns the number of bits in v which are set to 1.
!*/</font>
<b>{</b>
<font color='#BB00BB'>COMPILE_TIME_ASSERT</font><font face='Lucida Console'>(</font>is_unsigned_type<font color='#5555FF'><</font>T<font color='#5555FF'>></font>::value <font color='#5555FF'>&</font><font color='#5555FF'>&</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'>=</font> <font color='#979000'>8</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// This bit of bit trickery is from:
</font> <font color='#009900'>// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSet64
</font>
v <font color='#5555FF'>=</font> v <font color='#5555FF'>-</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>v <font color='#5555FF'>></font><font color='#5555FF'>></font> <font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&</font> <font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font>~<font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font><font color='#979000'>0</font><font color='#5555FF'>/</font><font color='#979000'>3</font><font face='Lucida Console'>)</font>;
v <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>v <font color='#5555FF'>&</font> <font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font>~<font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font><font color='#979000'>0</font><font color='#5555FF'>/</font><font color='#979000'>15</font><font color='#5555FF'>*</font><font color='#979000'>3</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>v <font color='#5555FF'>></font><font color='#5555FF'>></font> <font color='#979000'>2</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&</font> <font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font>~<font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font><font color='#979000'>0</font><font color='#5555FF'>/</font><font color='#979000'>15</font><font color='#5555FF'>*</font><font color='#979000'>3</font><font face='Lucida Console'>)</font>;
v <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>v <font color='#5555FF'>+</font> <font face='Lucida Console'>(</font>v <font color='#5555FF'>></font><font color='#5555FF'>></font> <font color='#979000'>4</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&</font> <font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font>~<font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font><font color='#979000'>0</font><font color='#5555FF'>/</font><font color='#979000'>255</font><font color='#5555FF'>*</font><font color='#979000'>15</font>;
<font color='#0000FF'>return</font> <font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font><font face='Lucida Console'>(</font>v <font color='#5555FF'>*</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font>~<font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font><font color='#979000'>0</font><font color='#5555FF'>/</font><font color='#979000'>255</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>></font><font color='#5555FF'>></font> <font face='Lucida Console'>(</font><font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font> <font color='#5555FF'>-</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> CHAR_BIT;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>template</font> <font color='#5555FF'><</font>
<font color='#0000FF'>typename</font> T
<font color='#5555FF'>></font>
T <b><a name='hamming_distance'></a>hamming_distance</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> a,
<font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> b
<font face='Lucida Console'>)</font>
<font color='#009900'>/*!
requires
- T is an unsigned integral type
ensures
- returns the number of bits which differ between a and b.
!*/</font>
<b>{</b>
<font color='#0000FF'>return</font> <font color='#BB00BB'>count_bits</font><font face='Lucida Console'>(</font>a^b<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>template</font> <font color='#5555FF'><</font>
<font color='#0000FF'>typename</font> T
<font color='#5555FF'>></font>
T <b><a name='hamming_distance'></a>hamming_distance</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> std::pair<font color='#5555FF'><</font>T,T<font color='#5555FF'>></font><font color='#5555FF'>&</font> a,
<font color='#0000FF'>const</font> std::pair<font color='#5555FF'><</font>T,T<font color='#5555FF'>></font><font color='#5555FF'>&</font> b
<font face='Lucida Console'>)</font>
<font color='#009900'>/*!
requires
- T is an unsigned integral type or a std::pair that, recursively, eventually
contains unsigned integral types.
ensures
- returns the number of bits which differ between a and b.
!*/</font>
<b>{</b>
<font color='#0000FF'>return</font> <font color='#BB00BB'>hamming_distance</font><font face='Lucida Console'>(</font>a.first,b.first<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> <font color='#BB00BB'>hamming_distance</font><font face='Lucida Console'>(</font>a.second, b.second<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_COUNT_BiTS_Hh_
</font>
</pre></body></html> |