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
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
34 CharAltList::~CharAltList() {
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 if (class_id_alt_ == NULL || alt_cost_ == NULL || alt_tag_ == NULL) {
60 return false;
61 }
62
63 memset(alt_tag_, 0, max_alt_ * sizeof(*alt_tag_));
64 }
65
66 if (class_id_cost_ == NULL) {
67 int class_cnt = char_set_->ClassCount();
68
69 class_id_cost_ = new int[class_cnt];
70 if (class_id_cost_ == NULL) {
71 return false;
72 }
73
74 for (int ich = 0; ich < class_cnt; ich++) {
75 class_id_cost_[ich] = WORST_COST;
76 }
77 }
78
79 if (class_id < 0 || class_id >= char_set_->ClassCount()) {
80 return false;
81 }
82
83 // insert the alternate
84 class_id_alt_[alt_cnt_] = class_id;
85 alt_cost_[alt_cnt_] = cost;
86 alt_tag_[alt_cnt_] = tag;
87
88 alt_cnt_++;
89
90 class_id_cost_[class_id] = cost;
91
92 return true;
93 }
94
95 // sort the alternate Desc. based on prob
96 void CharAltList::Sort() {
97 for (int alt_idx = 0; alt_idx < alt_cnt_; alt_idx++) {
98 for (int alt = alt_idx + 1; alt < alt_cnt_; alt++) {
99 if (alt_cost_[alt_idx] > alt_cost_[alt]) {
100 int temp = class_id_alt_[alt_idx];
101 class_id_alt_[alt_idx] = class_id_alt_[alt];
102 class_id_alt_[alt] = temp;
103
104 temp = alt_cost_[alt_idx];
105 alt_cost_[alt_idx] = alt_cost_[alt];
106 alt_cost_[alt] = temp;
107
108 void *tag = alt_tag_[alt_idx];
109 alt_tag_[alt_idx] = alt_tag_[alt];
110 alt_tag_[alt] = tag;
111 }
112 }
113 }
114 }
115 }