dict/states.h File Reference

#include "general.h"

Go to the source code of this file.

Classes

Defines

Typedefs

Functions


Define Documentation

#define MAX_NUM_CHUNKS   64

Limit on pieces, segments?, for word choices found for a word

Definition at line 32 of file states.h.

Referenced by bin_to_pieces(), improve_by_chopping(), and InitChoiceAccum().


Typedef Documentation

PIECES_STATE

Type of array of UINT8, holds array of MAX_NUM_CHUNKS pieces of state, for segmentation.

Definition at line 64 of file states.h.

SEARCH_STATE

Holds pointer to search state, integer.

Definition at line 57 of file states.h.


Function Documentation

SEARCH_STATE bin_to_chunks ( STATE state,
int  num_joints 
)

Convert a representation of the search state in "STATE" form to one in "SEARCH_STATE" form

Creates the memory required to hold the resultant state value.

Definition at line 49 of file states.cpp.

References memalloc(), and ones_in_state().

Referenced by classify_piece(), evaluate_state(), rebuild_current_state(), save_best_state(), and state_char_widths().

00049                                                          { 
00050   int x;
00051   unsigned int mask;
00052   int depth;
00053   int pieces = 0;
00054   SEARCH_STATE s;
00055 
00056   s = memalloc (sizeof (int) * (ones_in_state (state, num_joints) + 1));
00057 
00058   depth = 1;
00059   mask = 1 << (num_joints - 1 - 32);
00060   for (x = num_joints; x > 32; x--) {
00061     if (state->part1 & mask) {
00062       s[depth++] = pieces;
00063       pieces = 0;
00064     }
00065     else {
00066       pieces++;
00067     }
00068     mask >>= 1;
00069   }
00070 
00071   if (num_joints > 32)
00072     mask = 1 << 31;
00073   else
00074     mask = 1 << (num_joints - 1);
00075 
00076   while (x--) {
00077     if (state->part2 & mask) {
00078       s[depth++] = pieces;
00079       pieces = 0;
00080     }
00081     else {
00082       pieces++;
00083     }
00084     mask >>= 1;
00085   }
00086   s[0] = depth - 1;
00087 
00088   return (s);
00089 }

void bin_to_pieces ( STATE state,
int  num_joints,
PIECES_STATE  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 98 of file states.cpp.

References ASSERT_HOST, cprintf(), debug_8, FALSE, MAX_NUM_CHUNKS, new_line, num_pieces, STATE::part1, STATE::part2, print_state(), and TRUE.

Referenced by evaluate_state(), and rating_priority().

00098                                                                       { 
00099   int x;
00100   unsigned int mask;             /* Bit mask */
00101   INT16 num_pieces = 0;
00102   /* Preset mask */
00103   if (debug_8)
00104     print_state ("bin_to_pieces = ", state, num_joints);
00105 
00106   mask = ((num_joints > 32) ?
00107     (1 << (num_joints - 1 - 32)) : (1 << (num_joints - 1)));
00108 
00109   pieces[num_pieces] = 0;
00110 
00111   for (x = num_joints - 1; x >= 0; x--) {
00112                                  /* Iterate all bits */
00113     pieces[num_pieces]++;
00114 
00115     if ((x < 32) ?               /* Test for 1 bit */
00116       ((state->part2 & mask) ? TRUE : FALSE) :
00117     ((state->part1 & mask) ? TRUE : FALSE)) {
00118       pieces[++num_pieces] = 0;
00119       if (debug_8)
00120         cprintf ("[%d]=%d ", num_pieces - 1, pieces[num_pieces - 1]);
00121     }
00122     /* Next mask value */
00123     mask = ((mask == 1) ? (1 << 31) : (mask >> 1));
00124   }
00125   pieces[num_pieces]++;
00126   pieces[++num_pieces] = 0;
00127   ASSERT_HOST (num_pieces < MAX_NUM_CHUNKS + 2);
00128   if (debug_8)
00129     new_line(); 
00130 }

int compare_states ( STATE true_state,
STATE this_state,
int *  blob_index 
)

Compare the two states at the given blob index.

Parameters:
true_state State to compare
this_state State to compare
blob_index Modified to the corresponding index in the correct string
Returns:
Returns:
  • 1 if the given blob is a fragment compared to reality
  • 2 if correct
  • 4 if a join
  • 5 if both a join and a fragment
On return the blob index is set to the corresponding index in the correct string.

Definition at line 274 of file states.cpp.

References bits_in_states, blob_count, STATE::part1, and STATE::part2.

00274                                                                           { 
00275   int blob_count;                //number found
00276   int true_index;                //index of true blob
00277   int index;                     //current
00278   int result = 0;                //return value
00279   UINT32 mask;
00280 
00281   if (true_state->part1 == this_state->part1
00282     && true_state->part2 == this_state->part2)
00283     return 2;
00284   if (*blob_index == 0) {
00285     if (bits_in_states > 32) {
00286       for (mask = 1 << bits_in_states - 33; mask != 0; mask >>= 1) {
00287         if (this_state->part1 & mask) {
00288           if (true_state->part1 & mask)
00289             return 2;
00290           else
00291             return 1;
00292         }
00293         else if (true_state->part1 & mask)
00294           return 4;
00295       }
00296       index = 31;
00297     }
00298     else
00299       index = bits_in_states - 1;
00300     for (mask = 1 << index; mask != 0; mask >>= 1) {
00301       if (this_state->part2 & mask) {
00302         if (true_state->part2 & mask)
00303           return 2;
00304         else
00305           return 1;
00306       }
00307       else if (true_state->part2 & mask)
00308         return 4;
00309     }
00310     return 2;
00311   }
00312   else {
00313     blob_count = 0;
00314     true_index = 0;
00315     if (bits_in_states > 32) {
00316       for (mask = 1 << bits_in_states - 33; mask != 0; mask >>= 1) {
00317         if (true_state->part1 & mask)
00318           true_index++;
00319         if (this_state->part1 & mask) {
00320           blob_count++;
00321           if (blob_count == *blob_index) {
00322             if ((true_state->part1 & mask) == 0)
00323               result = 1;
00324             break;
00325           }
00326         }
00327       }
00328       if (blob_count == *blob_index) {
00329         for (mask >>= 1; mask != 0; mask >>= 1) {
00330           if (this_state->part1 & mask) {
00331             if ((true_state->part1 & mask) && result == 0)
00332               return 2;
00333             else
00334               return result | 1;
00335           }
00336           else if (true_state->part1 & mask)
00337             result |= 4;
00338         }
00339       }
00340       index = 31;
00341     }
00342     else
00343       index = bits_in_states - 1;
00344     mask = 1 << index;
00345     if (blob_count < *blob_index) {
00346       for (; mask != 0; mask >>= 1) {
00347         if (true_state->part2 & mask)
00348           true_index++;
00349         if (this_state->part2 & mask) {
00350           blob_count++;
00351           if (blob_count == *blob_index) {
00352             if ((true_state->part2 & mask) == 0)
00353               result = 1;
00354             break;
00355           }
00356         }
00357       }
00358       if (blob_count != *blob_index)
00359         return 2;
00360       mask >>= 1;
00361     }
00362     *blob_index = true_index;
00363     for (; mask != 0; mask >>= 1) {
00364       if (this_state->part2 & mask) {
00365         if ((true_state->part2 & mask) && result == 0)
00366           return 2;
00367         else
00368           return result | 1;
00369       }
00370       else if (true_state->part2 & mask)
00371         result |= 4;
00372     }
00373     return result == 0 ? 2 : result;
00374   }
00375 }

void free_state ( STATE  ) 

Referenced by best_first_search(), delete_search(), and save_best_state().

void insert_new_chunk ( register STATE state,
register int  index,
int  num_joints 
)

STATE* new_state ( STATE oldstate  ) 

Create a memory space for a new state variable & sets its initial value according to the parameters

Definition at line 167 of file states.cpp.

References STATE::part1, and STATE::part2.

Referenced by new_search(), number_state_change(), push_queue(), and save_best_state().

00167                                   { 
00168   STATE *this_state;
00169 
00170   this_state = newstate ();
00171   this_state->part1 = oldstate->part1;
00172   this_state->part2 = oldstate->part2;
00173   return (this_state);
00174 }

int ones_in_state ( STATE state,
int  num_joints 
)

Return the number of ones that are in this state.

Definition at line 180 of file states.cpp.

References STATE::part1, and STATE::part2.

Referenced by bin_to_chunks().

00180                                                 { 
00181   INT8 num_ones = 0;
00182   INT8 x;
00183   unsigned int mask;
00184 
00185   if (num_joints > 32)           /* Preset mask */
00186     mask = 1 << (num_joints - 1 - 32);
00187   else
00188     mask = 1 << (num_joints - 1);
00189 
00190   for (x = num_joints - 1; x >= 0; x--) {
00191                                  /* Iterate all bits */
00192 
00193     if (x < 32)
00194       num_ones += ((state->part2 & mask) ? 1 : 0);
00195     else
00196       num_ones += ((state->part1 & mask) ? 1 : 0);
00197 
00198     if (mask == 1)               /* Next mask value */
00199       mask = 1 << 31;
00200     else
00201       mask >>= 1;
00202   }
00203 
00204   return (num_ones);
00205 }

void print_state ( const char *  label,
STATE state,
int  num_joints 
)

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

Definition at line 211 of file states.cpp.

References cprintf(), new_line, STATE::part1, and STATE::part2.

Referenced by bin_to_pieces(), improve_by_chopping(), and pop_queue().

00211                                                                   { 
00212   int x;
00213   unsigned int mask;             /* Bit mask */
00214 
00215   if (num_joints > 32)           /* Preset mask */
00216     mask = 1 << (num_joints - 1 - 32);
00217   else
00218     mask = 1 << (num_joints - 1);
00219 
00220   cprintf ("%s ", label);
00221 
00222   for (x = num_joints - 1; x >= 0; x--) {
00223                                  /* Iterate all bits */
00224 
00225     if (x < 32)
00226       cprintf ("%d", ((state->part2 & mask) ? 1 : 0));
00227     else
00228       cprintf ("%d", ((state->part1 & mask) ? 1 : 0));
00229     if (x % 4 == 0)
00230       cprintf (" ");
00231 
00232     if (mask == 1)               /* Next mask value */
00233       mask = 1 << 31;
00234     else
00235       mask >>= 1;
00236   }
00237 
00238   new_line(); 
00239 }

void set_n_ones ( STATE state,
int  n 
)

Set the first n bits in a state.

Definition at line 245 of file states.cpp.

References STATE::part1, and STATE::part2.

Referenced by chop_word_main(), classify_piece(), and improve_by_chopping().

00245                                      { 
00246   if (n < 32) {
00247     state->part2 = ~0;
00248     state->part2 >>= 32 - n;
00249     state->part1 = 0;
00250   }
00251   else {
00252     state->part2 = ~0;
00253     state->part1 = ~0;
00254     state->part1 >>= 64 - n;
00255   }
00256 }


Generated on Wed Feb 28 19:49:22 2007 for Tesseract by  doxygen 1.5.1