|
<html><head><title>dlib C++ Library - general_hash.h</title></head><body bgcolor='white'><pre> |
|
<font color='#009900'>// Copyright (C) 2003 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_GENERAL_HASh_ |
|
<font color='#0000FF'>#define</font> DLIB_GENERAL_HASh_ |
|
|
|
|
|
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>string<font color='#5555FF'>></font> |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='hash.h.html'>hash.h</a>" |
|
|
|
<font color='#0000FF'>namespace</font> dlib |
|
<b>{</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font><font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font><font color='#009900'>// ----------------------- provide a general hashing function object ---------------------- |
|
</font><font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font><font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> T |
|
<font color='#5555FF'>></font> |
|
<font color='#0000FF'>class</font> <b><a name='general_hash'></a>general_hash</b> |
|
<b>{</b> |
|
<font color='#0000FF'>public</font>: |
|
<font color='#0000FF'>inline</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>; |
|
<b>}</b>; |
|
<font color='#009900'>/*! |
|
Note that the default behavior of general hash is to attempt to cast |
|
an object of type T to an unsigned long and use that as the hash. |
|
|
|
REQUIREMENTS ON general_hash |
|
- must have a default constructor |
|
- must be a function object which overloads operator() as follows: |
|
unsigned long operator()(const T& item) |
|
- must take item, compute a hash number and return it |
|
- must not throw |
|
- must define the hash in such a way that all equivalent objects have |
|
the same hash. where equivalent means the following: |
|
definition of equivalent: |
|
a is equivalent to b if |
|
a < b == false and |
|
b < a == false |
|
!*/</font> |
|
|
|
<font color='#009900'>// --------------- |
|
</font> |
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> T |
|
<font color='#5555FF'>></font> |
|
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> general_hash<font color='#5555FF'><</font>T<font color='#5555FF'>></font>:: |
|
<b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#009900'>// hash any types that have a conversion to uint64 |
|
</font> <font color='#0000FF'>return</font> <font color='#BB00BB'>hash</font><font face='Lucida Console'>(</font><font color='#0000FF'>static_cast</font><font color='#5555FF'><</font>uint64<font color='#5555FF'>></font><font face='Lucida Console'>(</font>item<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
|
|
<font color='#009900'>// --------------- |
|
</font> |
|
<font color='#009900'>// std::string hash |
|
</font> <font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#5555FF'>></font> |
|
<font color='#0000FF'>inline</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> general_hash<font color='#5555FF'><</font>std::string<font color='#5555FF'>></font>:: |
|
<b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> <font color='#BB00BB'>hash</font><font face='Lucida Console'>(</font>item<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font><font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_GENERAL_HASh_ |
|
</font> |
|
|
|
</pre></body></html> |