Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Algo/Hash.h
Go to the documentation of this file.
00001 /*
00002     This file is part of Simple Application Framework (Saf) library.
00003     Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com>
00004 
00005     This library is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published 
00007     by the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     Saf is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Simple Application Framework library. If not, 
00017     see <http://www.gnu.org/licenses/>.
00018 */
00019 
00028 #ifndef SAF_ALGO_HASH_H
00029 #define SAF_ALGO_HASH_H
00030 
00031 #include "../Core.h"
00032 #include "../Type.h"
00033 #include "Function.h"
00034 
00035 namespace Saf
00036 {
00037     namespace Algo
00038     {
00043         template <class T>
00044         struct Hash
00045             : public UnaryFunction<T,Size>
00046         {
00047             Size operator()(const T& val) const;
00048         };
00049 
00054         SAF_DLL_EXPORT Size HashBytes(const Uint8 *data, Size sz);
00055 
00057         // Explicit Hash instantiations
00058         template <> inline Size Hash<Uint8>::operator()(const Uint8 &val) const
00059         {
00060             return (Size)val;
00061         }
00062 
00063         template <> inline Size Hash<Uint16>::operator()(const Uint16 &val) const
00064         {
00065             return (Size)val;
00066         }
00067 
00068         template <> inline Size Hash<Uint32>::operator()(const Uint32 &val) const
00069         {
00070             return (Size)val;
00071         }
00072 
00073         template <> inline Size Hash<Uint64>::operator()(const Uint64 &val) const
00074         {
00075             return sizeof(Uint64) > sizeof(Size) ? Size((val & 0xffffffff) ^ (val >> 32)) : (Size)val;
00076         }
00077 
00078         template <> inline Size Hash<Int8>::operator()(const Int8 &val) const
00079         {
00080             return (Size)val;
00081         }
00082 
00083         template <> inline Size Hash<Int16>::operator()(const Int16 &val) const
00084         {
00085             return (Size)val;
00086         }
00087 
00088         template <> inline Size Hash<Int32>::operator()(const Int32 &val) const
00089         {
00090             return (Size)val;
00091         }
00092 
00093         template <> inline Size Hash<Int64>::operator()(const Int64 &val) const
00094         {
00095             return Hash<Uint64>()(Uint64(val));
00096         }
00097 
00098         template <> inline Size Hash<Float32>::operator()(const Float32 &val) const
00099         {
00100             return (val == -0.0f || val == 0.0f) ? Size(0) : Hash<Uint32>()(*((Uint32*)&val));
00101         }
00102 
00103         template <> inline Size Hash<Float64>::operator()(const Float64 &val) const
00104         {
00105             return (val == -0.0 || val == 0.0) ? Size(0) : Hash<Uint64>()(*((Uint64*)&val));
00106         }
00108     }
00109 }
00110 
00111 #endif