Tesseract  3.02
tesseract-ocr/ccutil/hashfn.cpp
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        hashfn.c  (Formerly hash.c)
00003  * Description: Simple hash function.
00004  * Author:                                      Ray Smith
00005  * Created:                                     Thu Jan 16 11:47:59 GMT 1992
00006  *
00007  * (C) Copyright 1992, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #include          "mfcpch.h"     //precompiled headers
00021 #include          "hashfn.h"
00022 
00023 /**********************************************************************
00024  * hash
00025  *
00026  * Simple hash function working on power of 2 table sizes.
00027  * Uses xor function. Needs linear rehash.
00028  **********************************************************************/
00029 
00030 inT32 hash(               //hash function
00031            inT32 bits,    //bits in hash function
00032            void *key,     //key to hash
00033            inT32 keysize  //size of key
00034           ) {
00035   inT32 bitindex;                //current bit count
00036   uinT32 keybits;                //bit buffer
00037   uinT32 hcode;                  //current hash code
00038   uinT32 mask;                   //bit mask
00039 
00040   mask = (1 << bits) - 1;
00041   keysize *= 8;                  //in bits
00042   bitindex = 0;
00043   keybits = 0;
00044   hcode = 0;
00045   do {
00046     while (keysize > 0 && bitindex <= 24) {
00047       keybits |= *((uinT8 *) key) << bitindex;
00048       key = (uinT8 *) key + 1;
00049       bitindex += 8;
00050       keysize -= 8;
00051     }
00052     hcode ^= keybits & mask;     //key new key
00053     keybits >>= bits;
00054   }
00055   while (keysize > 0);
00056   return hcode;                  //initial hash
00057 }