wordrec/matchtab.cpp

Go to the documentation of this file.
00001 
00020 /*----------------------------------------------------------------------
00021               I n c l u d e s
00022 ----------------------------------------------------------------------*/
00023 #include "matchtab.h"
00024 #include "freelist.h"
00025 #include "callcpp.h"
00026 #include "blobs.h"
00027 
00028 /*----------------------------------------------------------------------
00029               T y p e s
00030 ----------------------------------------------------------------------*/
00036 typedef struct _MATCH_
00037 {
00038   int topleft;
00039   int botright;
00040   LIST rating;
00041 } MATCH;
00042 
00043 /*----------------------------------------------------------------------
00044               V a r i a b l e s
00045 ----------------------------------------------------------------------*/
00049 MATCH *match_table;
00050 //?int   missed_count = 0;
00051 
00052 /*----------------------------------------------------------------------
00053               M a c r o s
00054 ----------------------------------------------------------------------*/
00058 #define NUM_MATCH_ENTRIES 500
00059 
00060 /* ================== */
00066 #define blank_entry(match_table,x)  \
00067 (! (match_table[x].topleft | match_table[x].botright))
00068 
00069 /*----------------------------------------------------------------------
00070           Public Function Code
00071 ----------------------------------------------------------------------*/
00072 /* ================== */
00076   static int been_initialized = 0;
00077 void init_match_table() {
00078   int x;
00079 
00080   if (been_initialized) {
00081     /* Reclaim old choices */
00082     for (x = 0; x < NUM_MATCH_ENTRIES; x++) {
00083       if ((!blank_entry (match_table, x)) && match_table[x].rating)
00084         destroy_nodes (match_table[x].rating, free_choice);
00085     }
00086   }
00087   else {
00088     /* Allocate memory once */
00089     been_initialized = 1;
00090     match_table = (MATCH *) memalloc (sizeof (MATCH) * NUM_MATCH_ENTRIES);
00091   }
00092   /* Initialize the table */
00093   for (x = 0; x < NUM_MATCH_ENTRIES; x++) {
00094     match_table[x].topleft = 0;
00095     match_table[x].botright = 0;
00096     match_table[x].rating = NULL;
00097   }
00098 }
00099 
00100 void end_match_table() {
00101   if (been_initialized) {
00102     init_match_table();
00103     memfree(match_table);
00104     match_table = NULL;
00105     been_initialized = 0;
00106   }
00107 }
00108 
00109 
00110 /* ================== */
00114 void put_match(TBLOB *blob, CHOICES ratings) {
00115   unsigned int topleft;
00116   unsigned int botright;
00117   unsigned int start;
00118   TPOINT tp_topleft;
00119   TPOINT tp_botright;
00120   int x;
00121   /* Hash into table */
00122   blob_bounding_box(blob, &tp_topleft, &tp_botright);
00123   topleft = *(unsigned int *) &tp_topleft;
00124   botright = *(unsigned int *) &tp_botright;
00125   start = (topleft * botright) % NUM_MATCH_ENTRIES;
00126 
00127   /* Look for empty */
00128   x = start;
00129   do {
00130     if (blank_entry (match_table, x)) {
00131       /* Add this entry */
00132       match_table[x].topleft = topleft;
00133       match_table[x].botright = botright;
00134       match_table[x].rating = copy_choices (ratings);
00135       return;
00136     }
00137     if (++x >= NUM_MATCH_ENTRIES)
00138       x = 0;
00139   }
00140   while (x != start);
00141 
00142   cprintf ("error: Match table is full\n");
00143 }
00144 
00145 
00146 /* ================== */
00153 CHOICES get_match(TBLOB *blob) {
00154   unsigned int topleft;
00155   unsigned int botright;
00156   TPOINT tp_topleft;
00157   TPOINT tp_botright;
00158   /* Do starting hash */
00159   blob_bounding_box(blob, &tp_topleft, &tp_botright);
00160   topleft = *(unsigned int *) &tp_topleft;
00161   botright = *(unsigned int *) &tp_botright;
00162   return (get_match_by_bounds (topleft, botright));
00163 }
00164 
00165 
00166 /* ================== */
00173 CHOICES get_match_by_bounds(unsigned int topleft, unsigned int botright) { 
00174   unsigned int start;
00175   int x;
00176   /* Do starting hash */
00177   start = (topleft * botright) % NUM_MATCH_ENTRIES;
00178   /* Search for match */
00179   x = start;
00180   do {
00181     /* Not found when blank */
00182     if (blank_entry (match_table, x))
00183       break;
00184     /* Is this the match ? */
00185     if (match_table[x].topleft == topleft &&
00186     match_table[x].botright == botright) {
00187       return (copy_choices (match_table[x].rating));
00188     }
00189     if (++x >= NUM_MATCH_ENTRIES)
00190       x = 0;
00191   }
00192   while (x != start);
00193 
00194   return (NIL);
00195 }

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