tesseract  3.05.02
char_altlist.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: char_altlist.cpp
3  * Description: Implementation of a Character Alternate List Class
4  * Author: Ahmad Abdulkader
5  * Created: 2007
6  *
7  * (C) Copyright 2008, Google Inc.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include "char_altlist.h"
21 
22 namespace tesseract {
23 
24 // The CharSet is not class owned and must exist for
25 // the life time of this class
26 CharAltList::CharAltList(const CharSet *char_set, int max_alt)
27  : AltList(max_alt) {
28  char_set_ = char_set;
29  max_alt_ = max_alt;
30  class_id_alt_ = NULL;
31  class_id_cost_ = NULL;
32 }
33 
35  if (class_id_alt_ != NULL) {
36  delete []class_id_alt_;
37  class_id_alt_ = NULL;
38  }
39 
40  if (class_id_cost_ != NULL) {
41  delete []class_id_cost_;
42  class_id_cost_ = NULL;
43  }
44 }
45 
46 // Insert a new char alternate
47 bool CharAltList::Insert(int class_id, int cost, void *tag) {
48  // validate class ID
49  if (class_id < 0 || class_id >= char_set_->ClassCount()) {
50  return false;
51  }
52 
53  // allocate buffers if nedded
54  if (class_id_alt_ == NULL || alt_cost_ == NULL) {
55  class_id_alt_ = new int[max_alt_];
56  alt_cost_ = new int[max_alt_];
57  alt_tag_ = new void *[max_alt_];
58 
59  memset(alt_tag_, 0, max_alt_ * sizeof(*alt_tag_));
60  }
61 
62  if (class_id_cost_ == NULL) {
63  int class_cnt = char_set_->ClassCount();
64 
65  class_id_cost_ = new int[class_cnt];
66 
67  for (int ich = 0; ich < class_cnt; ich++) {
68  class_id_cost_[ich] = WORST_COST;
69  }
70  }
71 
72  if (class_id < 0 || class_id >= char_set_->ClassCount()) {
73  return false;
74  }
75 
76  // insert the alternate
77  class_id_alt_[alt_cnt_] = class_id;
78  alt_cost_[alt_cnt_] = cost;
79  alt_tag_[alt_cnt_] = tag;
80 
81  alt_cnt_++;
82 
83  class_id_cost_[class_id] = cost;
84 
85  return true;
86 }
87 
88 // sort the alternate Desc. based on prob
90  for (int alt_idx = 0; alt_idx < alt_cnt_; alt_idx++) {
91  for (int alt = alt_idx + 1; alt < alt_cnt_; alt++) {
92  if (alt_cost_[alt_idx] > alt_cost_[alt]) {
93  int temp = class_id_alt_[alt_idx];
94  class_id_alt_[alt_idx] = class_id_alt_[alt];
95  class_id_alt_[alt] = temp;
96 
97  temp = alt_cost_[alt_idx];
98  alt_cost_[alt_idx] = alt_cost_[alt];
99  alt_cost_[alt] = temp;
100 
101  void *tag = alt_tag_[alt_idx];
102  alt_tag_[alt_idx] = alt_tag_[alt];
103  alt_tag_[alt] = tag;
104  }
105  }
106  }
107 }
108 }
bool Insert(int class_id, int cost, void *tag=NULL)
CharAltList(const CharSet *char_set, int max_alt=kMaxCharAlt)
void ** alt_tag_
Definition: altlist.h:57
#define WORST_COST
Definition: cube_const.h:30
int ClassCount() const
Definition: char_set.h:111