• Main Page
  • Classes
  • Files
  • File List
  • File Members

chaincon/stringhash.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 ///
00003 /// \file
00004 ///
00005 /// Hashing keys for std::string.
00006 ///
00007 /////////////////////////////////////////////////////////////////////////////
00008 
00009 // Copyright (C) 2009-2011 by Pawel Pilarczyk.
00010 //
00011 // This file is part of my research software package. This is free software:
00012 // you can redistribute it and/or modify it under the terms of the GNU
00013 // General Public License as published by the Free Software Foundation,
00014 // either version 3 of the License, or (at your option) any later version.
00015 //
00016 // This software is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU General Public License
00022 // along with this software; see the file "license.txt". If not,
00023 // please, see <http://www.gnu.org/licenses/>.
00024 
00025 // Started on March 24, 2009. Last revision: March 4, 2011.
00026 
00027 
00028 #ifndef _CHAINCON_STRINGHASH_H_
00029 #define _CHAINCON_STRINGHASH_H_
00030 
00031 
00032 // include some standard C++ header files
00033 #include <string>
00034 
00035 // include selected header files from the CHomP library
00036 #include "chomp/system/config.h"
00037 
00038 
00039 // --------------------------------------------------
00040 
00041 /// Creates a generic hashing key no. 1 for the standard string type,
00042 /// to be used in a hashed set.
00043 inline int_t hashkey1 (const std::string &s)
00044 {
00045         int_t len = s. size ();
00046         if (len == 0)
00047                 return 0;
00048         else if (len == 1)
00049                 return static_cast<int_t> (s [0]) << 4;
00050         else if (len == 2)
00051         {
00052                 return ((static_cast<int_t> (s [0]) & 0x5555u) << 12) ^
00053                         ((static_cast<int_t> (s [1]) & 0xAAAAu));
00054         }
00055         else
00056         {
00057                 return ((static_cast<int_t> (s [0]) & 0x9249u) << 16) ^
00058                         ((static_cast<int_t> (s [len >> 1]) & 0x2492u)
00059                         << 8) ^
00060                         ((static_cast<int_t> (s [len - 1]) & 0x4924u)) ^
00061                         (len << 12);
00062         }
00063 } /* hashkey1 */
00064 
00065 /// Creates a generic hashing key no. 1 for the standard string type,
00066 /// to be used in a hashed set.
00067 inline int_t hashkey2 (const std::string &s)
00068 {
00069         int_t len = s. size ();
00070         if (len == 0)
00071                 return 0;
00072         else if (len == 1)
00073                 return static_cast<int_t> (s [0]) << 7;
00074         else if (len == 2)
00075         {
00076                 return ((static_cast<int_t> (s [0]) & 0xAAAAu) << 5) ^
00077                         ((static_cast<int_t> (s [1]) & 0x5555u) << 15);
00078         }
00079         else
00080         {
00081                 return ((static_cast<int_t> (s [len - 1]) & 0xC30Cu) << 15) ^
00082                         ((static_cast<int_t> (s [0]) & 0x30C3u) << 7) ^
00083                         ((static_cast<int_t> (s [len >> 1]) & 0x0C30u)
00084                         << 1) ^
00085                         (len << 9);
00086         }
00087 } /* hashkey2 */
00088 
00089 
00090 #endif // _CHAINCON_STRINGHASH_H_
00091 

Generated on Tue Apr 5 2011 00:06:32 for Chain Contraction Software by  doxygen 1.7.2