Tesseract
3.02
|
00001 /* -*-C-*- 00002 ******************************************************************************** 00003 * 00004 * File: closed.c (Formerly closed.c) 00005 * Description: Hash table for closed search states. 00006 * Author: Mark Seaman, OCR Technology 00007 * Created: Fri Oct 16 14:37:00 1987 00008 * Modified: Fri May 25 11:31:16 1990 (Mark Seaman) marks@hpgrlt 00009 * Language: C 00010 * Package: N/A 00011 * Status: Reusable Software Component 00012 * 00013 * (c) Copyright 1987, Hewlett-Packard Company. 00014 ** Licensed under the Apache License, Version 2.0 (the "License"); 00015 ** you may not use this file except in compliance with the License. 00016 ** You may obtain a copy of the License at 00017 ** http://www.apache.org/licenses/LICENSE-2.0 00018 ** Unless required by applicable law or agreed to in writing, software 00019 ** distributed under the License is distributed on an "AS IS" BASIS, 00020 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00021 ** See the License for the specific language governing permissions and 00022 ** limitations under the License. 00023 * 00024 *********************************************************************************/ 00025 /*---------------------------------------------------------------------- 00026 I n c l u d e s 00027 ----------------------------------------------------------------------*/ 00028 #include "freelist.h" 00029 #include "closed.h" 00030 #include "cutil.h" 00031 #include "callcpp.h" 00032 #ifdef __UNIX__ 00033 #include <assert.h> 00034 #endif 00035 00036 /*---------------------------------------------------------------------- 00037 V a r i a b l e s 00038 ----------------------------------------------------------------------*/ 00039 #define TABLE_SIZE 2000 00040 00041 /*---------------------------------------------------------------------- 00042 F u n c t i o n s 00043 ----------------------------------------------------------------------*/ 00050 int hash_add(HASH_TABLE state_table, STATE *state) { 00051 int x; 00052 int i = 0; 00053 int table_limit = TABLE_SIZE; 00054 00055 x = state->part2 % table_limit; 00056 while (i < table_limit) { 00057 assert (0 <= x && x < table_limit); 00058 /* Found it */ 00059 if ((state_table[x].part2 == state->part2) && 00060 (state_table[x].part1 == state->part1)) { 00061 return (FALSE); 00062 } 00063 /* Not in table */ 00064 else if (state_table[x].part1 == NO_STATE) { 00065 state_table[x].part2 = state->part2; 00066 state_table[x].part1 = state->part1; 00067 return (TRUE); 00068 } 00069 i++; 00070 if (++x >= table_limit) 00071 x = 0; 00072 } 00073 cprintf("warning: hash table is full"); 00074 00075 abort(); 00076 return 0; 00077 } 00078 00079 00086 int hash_lookup(HASH_TABLE state_table, STATE *state) { 00087 int x; 00088 int i = 0; 00089 int table_limit = TABLE_SIZE; 00090 00091 x = state->part2 % table_limit; 00092 while (i < table_limit) { 00093 assert (0 <= x && x < table_limit); 00094 /* Found it */ 00095 if ((state_table[x].part2 == state->part2) && 00096 (state_table[x].part1 == state->part1)) { 00097 return (TRUE); 00098 } 00099 /* Not in table */ 00100 else if (state_table[x].part1 == NO_STATE) { 00101 return (FALSE); 00102 } 00103 00104 i++; 00105 if (++x >= table_limit) 00106 x = 0; 00107 } 00108 cprintf ("warning: fell off end of hash table (%x) %x\n", 00109 state->part2, state->part2 % table_limit); 00110 abort(); 00111 return 0; 00112 } 00113 00114 00120 HASH_TABLE new_hash_table() { 00121 HASH_TABLE ht; 00122 int x; 00123 00124 ht = (HASH_TABLE) memalloc (TABLE_SIZE * sizeof (STATE)); 00125 for (x = 0; x < TABLE_SIZE; x++) { 00126 ht[x].part1 = NO_STATE; 00127 ht[x].part2 = NO_STATE; 00128 } 00129 return (ht); 00130 }