The ChainCon Software (Release 0.03)
stringhash.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// Hashing keys for std::string.
6 ///
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
10 //
11 // This file is part of my research software package. This is free software:
12 // you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation,
14 // either version 3 of the License, or (at your option) any later version.
15 //
16 // This software is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this software; see the file "license.txt". If not,
23 // please, see <http://www.gnu.org/licenses/>.
24 
25 // Started on March 24, 2009. Last revision: January 4, 2013.
26 
27 
28 #ifndef _CHAINCON_STRINGHASH_H_
29 #define _CHAINCON_STRINGHASH_H_
30 
31 
32 // include some standard C++ header files
33 #include <string>
34 
35 // include selected header files from the CHomP library
36 #include "chomp/system/config.h"
37 
38 
39 // --------------------------------------------------
40 
41 /// Creates a generic hashing key no. 1 for the standard string type,
42 /// to be used in a hashed set.
43 //inline int_t hashkey1 (const std::basic_string<char> &s)
44 inline int_t hashkey1 (const std::string &s)
45 {
46  int_t len = s. size ();
47  if (len == 0)
48  return 0;
49  else if (len == 1)
50  return static_cast<int_t> (s [0]) << 4;
51  else if (len == 2)
52  {
53  return ((static_cast<int_t> (s [0]) & 0x5555u) << 12) ^
54  ((static_cast<int_t> (s [1]) & 0xAAAAu));
55  }
56  else
57  {
58  return ((static_cast<int_t> (s [0]) & 0x9249u) << 16) ^
59  ((static_cast<int_t> (s [len >> 1]) & 0x2492u)
60  << 8) ^
61  ((static_cast<int_t> (s [len - 1]) & 0x4924u)) ^
62  (len << 12);
63  }
64 } /* hashkey1 */
65 
66 /// Creates a generic hashing key no. 1 for the standard string type,
67 /// to be used in a hashed set.
68 //inline int_t hashkey2 (const std::basic_string<char> &s)
69 inline int_t hashkey2 (const std::string &s)
70 {
71  int_t len = s. size ();
72  if (len == 0)
73  return 0;
74  else if (len == 1)
75  return static_cast<int_t> (s [0]) << 7;
76  else if (len == 2)
77  {
78  return ((static_cast<int_t> (s [0]) & 0xAAAAu) << 5) ^
79  ((static_cast<int_t> (s [1]) & 0x5555u) << 15);
80  }
81  else
82  {
83  return ((static_cast<int_t> (s [len - 1]) & 0xC30Cu) << 15) ^
84  ((static_cast<int_t> (s [0]) & 0x30C3u) << 7) ^
85  ((static_cast<int_t> (s [len >> 1]) & 0x0C30u)
86  << 1) ^
87  (len << 9);
88  }
89 } /* hashkey2 */
90 
91 
92 #endif // _CHAINCON_STRINGHASH_H_
93 
int_t hashkey1(const std::string &s)
Creates a generic hashing key no.
Definition: stringhash.h:44
int_t hashkey2(const std::string &s)
Creates a generic hashing key no.
Definition: stringhash.h:69