Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
states.h File Reference
#include "host.h"
#include "strngs.h"

Go to the source code of this file.

Classes

struct  STATE
 

Macros

#define MAX_NUM_CHUNKS   64 /* Limit on pieces */
 

Typedefs

typedef int * SEARCH_STATE
 
typedef uinT8 PIECES_STATE [MAX_NUM_CHUNKS+2]
 

Functions

SEARCH_STATE bin_to_chunks (STATE *state, int num_joints)
 
void bin_to_pieces (STATE *state, int num_joints, PIECES_STATE pieces)
 
void insert_new_chunk (register STATE *state, register int index, int num_joints)
 
STATEnew_state (STATE *oldstate)
 
int ones_in_state (STATE *state, int num_joints)
 
void print_state (const char *label, STATE *state, int num_joints)
 
void print_state (STATE *state, int num_joints, STRING *toappend)
 
void set_n_ones (STATE *state, int n)
 
void free_state (STATE *)
 

Macro Definition Documentation

#define MAX_NUM_CHUNKS   64 /* Limit on pieces */

Definition at line 37 of file states.h.

Typedef Documentation

typedef uinT8 PIECES_STATE[MAX_NUM_CHUNKS+2]

State variable for search

Definition at line 49 of file states.h.

typedef int* SEARCH_STATE

State variable for search

Definition at line 46 of file states.h.

Function Documentation

SEARCH_STATE bin_to_chunks ( STATE state,
int  num_joints 
)

Definition at line 49 of file states.cpp.

49  {
50  int x;
51  unsigned int mask;
52  int depth;
53  int pieces = 0;
54  SEARCH_STATE s;
55 
56  s = memalloc (sizeof (int) * (ones_in_state (state, num_joints) + 1));
57 
58  depth = 1;
59  mask = 1 << (num_joints - 1 - 32);
60  for (x = num_joints; x > 32; x--) {
61  if (state->part1 & mask) {
62  s[depth++] = pieces;
63  pieces = 0;
64  }
65  else {
66  pieces++;
67  }
68  mask >>= 1;
69  }
70 
71  if (num_joints > 32)
72  mask = 1 << 31;
73  else
74  mask = 1 << (num_joints - 1);
75 
76  while (x--) {
77  if (state->part2 & mask) {
78  s[depth++] = pieces;
79  pieces = 0;
80  }
81  else {
82  pieces++;
83  }
84  mask >>= 1;
85  }
86  s[0] = depth - 1;
87 
88  return (s);
89 }
int * memalloc(int size)
Definition: freelist.cpp:22
uinT32 part1
Definition: states.h:41
int ones_in_state(STATE *state, int num_joints)
Definition: states.cpp:181
uinT32 part2
Definition: states.h:42
int * SEARCH_STATE
Definition: states.h:46
void bin_to_pieces ( STATE state,
int  num_joints,
PIECES_STATE  pieces 
)

bin_to_pieces

Convert the binary (bit vector) format of a search state to an array of piece counts. This array has a zero element after the last valid character.

Definition at line 99 of file states.cpp.

99  {
100  int x;
101  unsigned int mask; /* Bit mask */
102  inT16 num_pieces = 0;
103  /* Preset mask */
104  mask = ((num_joints > 32) ?
105  (1 << (num_joints - 1 - 32)) : (1 << (num_joints - 1)));
106 
107  pieces[num_pieces] = 0;
108 
109  for (x = num_joints - 1; x >= 0; x--) {
110  /* Iterate all bits */
111  pieces[num_pieces]++;
112 
113  if ((x < 32) ? /* Test for 1 bit */
114  ((state->part2 & mask) ? TRUE : FALSE) :
115  ((state->part1 & mask) ? TRUE : FALSE)) {
116  pieces[++num_pieces] = 0;
117  }
118  /* Next mask value */
119  mask = ((mask == 1) ? (1 << 31) : (mask >> 1));
120  }
121  pieces[num_pieces]++;
122  pieces[++num_pieces] = 0;
123  ASSERT_HOST (num_pieces < MAX_NUM_CHUNKS + 2);
124 }
uinT32 part1
Definition: states.h:41
#define FALSE
Definition: capi.h:28
short inT16
Definition: host.h:100
uinT32 part2
Definition: states.h:42
#define ASSERT_HOST(x)
Definition: errcode.h:84
#define MAX_NUM_CHUNKS
Definition: states.h:37
#define TRUE
Definition: capi.h:27
void free_state ( STATE )
void insert_new_chunk ( register STATE state,
register int  index,
int  num_joints 
)
STATE* new_state ( STATE oldstate)

new_state

Create a memory space for a new state variable. Set its initial value according to the parameters.

Definition at line 166 of file states.cpp.

166  {
167  STATE *this_state;
168 
169  this_state = newstate ();
170  this_state->part1 = oldstate->part1;
171  this_state->part2 = oldstate->part2;
172  return (this_state);
173 }
uinT32 part1
Definition: states.h:41
uinT32 part2
Definition: states.h:42
Definition: states.h:39
int ones_in_state ( STATE state,
int  num_joints 
)

ones_in_state

Return the number of ones that are in this state.

Definition at line 181 of file states.cpp.

181  {
182  inT8 num_ones = 0;
183  inT8 x;
184  unsigned int mask;
185 
186  if (num_joints > 32) /* Preset mask */
187  mask = 1 << (num_joints - 1 - 32);
188  else
189  mask = 1 << (num_joints - 1);
190 
191  for (x = num_joints - 1; x >= 0; x--) {
192  /* Iterate all bits */
193 
194  if (x < 32)
195  num_ones += ((state->part2 & mask) ? 1 : 0);
196  else
197  num_ones += ((state->part1 & mask) ? 1 : 0);
198 
199  if (mask == 1) /* Next mask value */
200  mask = 1 << 31;
201  else
202  mask >>= 1;
203  }
204 
205  return (num_ones);
206 }
uinT32 part1
Definition: states.h:41
uinT32 part2
Definition: states.h:42
SIGNED char inT8
Definition: host.h:98
void print_state ( const char *  label,
STATE state,
int  num_joints 
)

print_state

Print out the current state variable on a line with a label.

Definition at line 214 of file states.cpp.

214  {
215  int x;
216  unsigned int mask; /* Bit mask */
217 
218  if (num_joints > 32) /* Preset mask */
219  mask = 1 << (num_joints - 1 - 32);
220  else
221  mask = 1 << (num_joints - 1);
222 
223  cprintf ("%s ", label);
224 
225  for (x = num_joints - 1; x >= 0; x--) {
226  /* Iterate all bits */
227 
228  if (x < 32)
229  cprintf ("%d", ((state->part2 & mask) ? 1 : 0));
230  else
231  cprintf ("%d", ((state->part1 & mask) ? 1 : 0));
232  if (x % 4 == 0)
233  cprintf (" ");
234 
235  if (mask == 1) /* Next mask value */
236  mask = 1 << 31;
237  else
238  mask >>= 1;
239  }
240 
241  new_line();
242 }
uinT32 part1
Definition: states.h:41
uinT32 part2
Definition: states.h:42
#define new_line()
Definition: cutil.h:83
void cprintf(const char *format,...)
Definition: callcpp.cpp:41
void print_state ( STATE state,
int  num_joints,
STRING toappend 
)

Definition at line 246 of file states.cpp.

246  {
247  PIECES_STATE pieces;
248  bin_to_pieces(state, num_joints, pieces);
249  for (int i = 0; pieces[i] > 0; i++) {
250  if (i > 0) {
251  toappend->add_str_int(" ", pieces[i]);
252  } else {
253  toappend->add_str_int("", pieces[i]);
254  }
255  }
256 }
void add_str_int(const char *str, int number)
Definition: strngs.cpp:334
uinT8 PIECES_STATE[MAX_NUM_CHUNKS+2]
Definition: states.h:49
void bin_to_pieces(STATE *state, int num_joints, PIECES_STATE pieces)
Definition: states.cpp:99
void set_n_ones ( STATE state,
int  n 
)

set_n_ones

Set the first n bits in a state.

Definition at line 263 of file states.cpp.

263  {
264  if (n < 32) {
265  state->part2 = ~0;
266  state->part2 >>= 32 - n;
267  state->part1 = 0;
268  }
269  else {
270  state->part2 = ~0;
271  state->part1 = ~0;
272  state->part1 >>= 64 - n;
273  }
274 }
uinT32 part1
Definition: states.h:41
uinT32 part2
Definition: states.h:42