AshanGimhana's picture
Upload folder using huggingface_hub
9375c9a verified
raw
history blame
7.96 kB
<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 ([email protected])
</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'>&lt;</font>climits<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>namespace</font> dlib
<b>{</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
<font color='#0000FF'>typename</font> T
<font color='#5555FF'>&gt;</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'>&lt;</font>T<font color='#5555FF'>&gt;</font>::value <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>T<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</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'>&gt;</font><font color='#5555FF'>&gt;</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</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'>&amp;</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'>&gt;</font><font color='#5555FF'>&gt;</font> <font color='#979000'>2</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</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'>&gt;</font><font color='#5555FF'>&gt;</font> <font color='#979000'>4</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</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'>&gt;</font><font color='#5555FF'>&gt;</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'>&lt;</font>
<font color='#0000FF'>typename</font> T
<font color='#5555FF'>&gt;</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'>&amp;</font> a,
<font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</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'>&lt;</font>
<font color='#0000FF'>typename</font> T
<font color='#5555FF'>&gt;</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'>&lt;</font>T,T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> a,
<font color='#0000FF'>const</font> std::pair<font color='#5555FF'>&lt;</font>T,T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</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>