00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : Hash 00006 * Package : Zeus.ZeusBase.System 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef HashHPP 00033 #define HashHPP 00034 00035 #include <zeusbase/System/Interfaces/IValueType.hpp> 00036 #include <zeusbase/System/Int.h> 00037 00038 BEGIN_NAMESPACE_Zeus 00039 00040 /***************************************************************************/ 00043 /***************************************************************************/ 00044 template <class T> class THash 00045 { 00046 public: 00047 00048 /***********************************************************************/ 00055 /***********************************************************************/ 00056 static Int64 getHash64(const T* pBuffer, Int iSize, Int64 ldMax = TInt::MaxInt64) 00057 { 00058 //---------------------------------------------------------------------- 00059 // Source of Algorithm for C++ (Robert Sedgewick) ISBN 3-8273-7026-4 00060 //---------------------------------------------------------------------- 00061 Int64 ldH = 0; 00062 Int64 ldA = 31415; 00063 Int64 ldB = 27183; 00064 Int i = 0; 00065 for(i = 0, ldH = 0; i < iSize; i++, pBuffer++, ldA = (ldA * ldB) % (ldMax - 1)) 00066 { 00067 ldH = (ldA* ldH + *pBuffer) % ldMax; 00068 } 00069 return (ldH < 0) ? ( ldH + ldMax ) : ldH; 00070 } 00071 00072 /***********************************************************************/ 00079 /***********************************************************************/ 00080 static Int32 getHash32(const T* pBuffer, Int iSize, Int32 lMax = TInt::MaxInt32) 00081 { 00082 //---------------------------------------------------------------------- 00083 // Source of Algorithm for C++ (Robert Sedgewick) ISBN 3-8273-7026-4 00084 //---------------------------------------------------------------------- 00085 Int32 lH = 0; 00086 Int32 lA = 31415; 00087 Int32 lB = 27183; 00088 Int i = 0; 00089 for(i = 0, lH = 0; i < iSize; i++, pBuffer++, lA = (lA * lB) % (lMax - 1)) 00090 { 00091 lH = (lA* lH + *pBuffer) % lMax; 00092 } 00093 return (lH < 0) ? ( lH + lMax ) : lH; 00094 } 00095 00096 private: 00097 inline THash(){} 00098 inline virtual ~THash(){} 00099 00100 }; 00101 00102 END_NAMESPACE_Zeus 00103 00104 //--------------------------------------------------------------------------- 00105 #endif