Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
closed.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2 ********************************************************************************
3 *
4 * File: closed.c (Formerly closed.c)
5 * Description: Hash table for closed search states.
6 * Author: Mark Seaman, OCR Technology
7 * Created: Fri Oct 16 14:37:00 1987
8 * Modified: Fri May 25 11:31:16 1990 (Mark Seaman) marks@hpgrlt
9 * Language: C
10 * Package: N/A
11 * Status: Reusable Software Component
12 *
13 * (c) Copyright 1987, Hewlett-Packard Company.
14 ** Licensed under the Apache License, Version 2.0 (the "License");
15 ** you may not use this file except in compliance with the License.
16 ** You may obtain a copy of the License at
17 ** http://www.apache.org/licenses/LICENSE-2.0
18 ** Unless required by applicable law or agreed to in writing, software
19 ** distributed under the License is distributed on an "AS IS" BASIS,
20 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 ** See the License for the specific language governing permissions and
22 ** limitations under the License.
23 *
24 *********************************************************************************/
25 /*----------------------------------------------------------------------
26 I n c l u d e s
27 ----------------------------------------------------------------------*/
28 #include "freelist.h"
29 #include "closed.h"
30 #include "cutil.h"
31 #include "callcpp.h"
32 #ifdef __UNIX__
33 #include <assert.h>
34 #endif
35
36 /*----------------------------------------------------------------------
37 V a r i a b l e s
38 ----------------------------------------------------------------------*/
39 #define TABLE_SIZE 2000
40
41 /*----------------------------------------------------------------------
42 F u n c t i o n s
43 ----------------------------------------------------------------------*/
50 int hash_add(HASH_TABLE state_table, STATE *state) {
51 int x;
52 int i = 0;
53 int table_limit = TABLE_SIZE;
54
55 x = state->part2 % table_limit;
56 while (i < table_limit) {
57 assert (0 <= x && x < table_limit);
58 /* Found it */
59 if ((state_table[x].part2 == state->part2) &&
60 (state_table[x].part1 == state->part1)) {
61 return (FALSE);
62 }
63 /* Not in table */
64 else if (state_table[x].part1 == NO_STATE) {
65 state_table[x].part2 = state->part2;
66 state_table[x].part1 = state->part1;
67 return (TRUE);
68 }
69 i++;
70 if (++x >= table_limit)
71 x = 0;
72 }
73 cprintf("warning: hash table is full");
74
75 abort();
76 return 0;
77 }
78
79
86 int hash_lookup(HASH_TABLE state_table, STATE *state) {
87 int x;
88 int i = 0;
89 int table_limit = TABLE_SIZE;
90
91 x = state->part2 % table_limit;
92 while (i < table_limit) {
93 assert (0 <= x && x < table_limit);
94 /* Found it */
95 if ((state_table[x].part2 == state->part2) &&
96 (state_table[x].part1 == state->part1)) {
97 return (TRUE);
98 }
99 /* Not in table */
100 else if (state_table[x].part1 == NO_STATE) {
101 return (FALSE);
102 }
103
104 i++;
105 if (++x >= table_limit)
106 x = 0;
107 }
108 cprintf ("warning: fell off end of hash table (%x) %x\n",
109 state->part2, state->part2 % table_limit);
110 abort();
111 return 0;
112 }
113
114
120 HASH_TABLE new_hash_table() {
121 HASH_TABLE ht;
122 int x;
123
124 ht = (HASH_TABLE) memalloc (TABLE_SIZE * sizeof (STATE));
125 for (x = 0; x < TABLE_SIZE; x++) {
126 ht[x].part1 = NO_STATE;
127 ht[x].part2 = NO_STATE;
128 }
129 return (ht);
130 }