Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hashfn.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: hashfn.c (Formerly hash.c)
3  * Description: Simple hash function.
4  * Author: Ray Smith
5  * Created: Thu Jan 16 11:47:59 GMT 1992
6  *
7  * (C) Copyright 1992, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include "mfcpch.h" //precompiled headers
21 #include "hashfn.h"
22 
23 /**********************************************************************
24  * hash
25  *
26  * Simple hash function working on power of 2 table sizes.
27  * Uses xor function. Needs linear rehash.
28  **********************************************************************/
29 
30 inT32 hash( //hash function
31  inT32 bits, //bits in hash function
32  void *key, //key to hash
33  inT32 keysize //size of key
34  ) {
35  inT32 bitindex; //current bit count
36  uinT32 keybits; //bit buffer
37  uinT32 hcode; //current hash code
38  uinT32 mask; //bit mask
39 
40  mask = (1 << bits) - 1;
41  keysize *= 8; //in bits
42  bitindex = 0;
43  keybits = 0;
44  hcode = 0;
45  do {
46  while (keysize > 0 && bitindex <= 24) {
47  keybits |= *((uinT8 *) key) << bitindex;
48  key = (uinT8 *) key + 1;
49  bitindex += 8;
50  keysize -= 8;
51  }
52  hcode ^= keybits & mask; //key new key
53  keybits >>= bits;
54  }
55  while (keysize > 0);
56  return hcode; //initial hash
57 }