Skip to content

Commit

Permalink
cube: Simplify new operations
Browse files Browse the repository at this point in the history
It is not necessary to check for null pointers after new.

Simplify also two delete operations which were missing
in the previous commit.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Nov 30, 2016
1 parent 23e420a commit 03eec61
Show file tree
Hide file tree
Showing 29 changed files with 65 additions and 503 deletions.
2 changes: 1 addition & 1 deletion ccmain/cube_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ bool Tesseract::init_cube_objects(bool load_combiner,
// Create the combiner object and load the combiner net for target languages.
if (load_combiner) {
tess_cube_combiner_ = new tesseract::TesseractCubeCombiner(cube_cntxt_);
if (!tess_cube_combiner_ || !tess_cube_combiner_->LoadCombinerNet()) {
if (!tess_cube_combiner_->LoadCombinerNet()) {
delete cube_cntxt_;
cube_cntxt_ = NULL;
if (tess_cube_combiner_ != NULL) {
Expand Down
10 changes: 0 additions & 10 deletions ccmain/cube_reco_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ bool CubeRecoContext::Load(TessdataManager *tessdata_manager,
lang_mod_ = new TessLangModel(lm_params, data_file_path,
tess_obj_->getDict().load_system_dawg,
tessdata_manager, this);
if (lang_mod_ == NULL) {
fprintf(stderr, "Cube ERROR (CubeRecoContext::Load): unable to create "
"TessLangModel\n");
return false;
}

// Create the optional char bigrams object.
char_bigrams_ = CharBigrams::Create(data_file_path, lang_);
Expand Down Expand Up @@ -176,11 +171,6 @@ CubeRecoContext * CubeRecoContext::Create(Tesseract *tess_obj,
UNICHARSET *tess_unicharset) {
// create the object
CubeRecoContext *cntxt = new CubeRecoContext(tess_obj);
if (cntxt == NULL) {
fprintf(stderr, "Cube ERROR (CubeRecoContext::Create): unable to create "
"CubeRecoContext object\n");
return NULL;
}
// load the necessary components
if (cntxt->Load(tessdata_manager, tess_unicharset) == false) {
fprintf(stderr, "Cube ERROR (CubeRecoContext::Create): unable to init "
Expand Down
15 changes: 0 additions & 15 deletions cube/beam_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,13 @@ WordAltList * BeamSearch::Search(SearchObject *srch_obj, LangModel *lang_mod) {

// alloc memory for columns
col_ = new SearchColumn *[col_cnt_];
if (!col_) {
fprintf(stderr, "Cube ERROR (BeamSearch::Search): could not construct "
"SearchColumn array\n");
return NULL;
}
memset(col_, 0, col_cnt_ * sizeof(*col_));

// for all possible segments
for (int end_seg = 1; end_seg <= (seg_pt_cnt_ + 1); end_seg++) {
// create a search column
col_[end_seg - 1] = new SearchColumn(end_seg - 1,
cntxt_->Params()->BeamWidth());
if (!col_[end_seg - 1]) {
fprintf(stderr, "Cube ERROR (BeamSearch::Search): could not construct "
"SearchColumn for column %d\n", end_seg - 1);
return NULL;
}

// for all possible start segments
int init_seg = MAX(0, end_seg - cntxt_->Params()->MaxSegPerChar());
Expand Down Expand Up @@ -402,11 +392,6 @@ CharSamp **BeamSearch::SplitByNode(SearchObject *srch_obj,

// Allocate memory for CharSamp array.
CharSamp **chars = new CharSamp *[*char_cnt];
if (!chars) {
if (char_boxes)
boxaDestroy(char_boxes);
return NULL;
}

int ch_idx = *char_cnt - 1;
int seg_pt_cnt = srch_obj->SegPtCnt();
Expand Down
52 changes: 1 addition & 51 deletions cube/bmp_8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,10 @@ unsigned char **Bmp8::CreateBmpBuffer(unsigned char init_val) {
stride_ = ((wid_ % 4) == 0) ? wid_ : (4 * (1 + (wid_ / 4)));

buff = (unsigned char **) new unsigned char *[hgt_ * sizeof(*buff)];
if (!buff) {
return NULL;
}

// alloc and init memory for buffer and line buffer
buff[0] = (unsigned char *)
new unsigned char[stride_ * hgt_ * sizeof(*buff[0])];
if (!buff[0]) {
delete []buff;
return NULL;
}

memset(buff[0], init_val, stride_ * hgt_ * sizeof(*buff[0]));

Expand All @@ -100,16 +93,9 @@ unsigned int ** Bmp8::CreateBmpBuffer(int wid, int hgt,

// compute stride (align on 4 byte boundries)
buff = (unsigned int **) new unsigned int *[hgt * sizeof(*buff)];
if (!buff) {
return NULL;
}

// alloc and init memory for buffer and line buffer
buff[0] = (unsigned int *) new unsigned int[wid * hgt * sizeof(*buff[0])];
if (!buff[0]) {
delete []buff;
return NULL;
}

memset(buff[0], init_val, wid * hgt * sizeof(*buff[0]));

Expand Down Expand Up @@ -172,9 +158,6 @@ bool Bmp8::LoadFromCharDumpFile(CachedFile *fp) {

// alloc memory & read the 3 channel buffer
buff = new unsigned char[buf_size];
if (buff == NULL) {
return false;
}

if (fp->Read(buff, buf_size) != buf_size) {
delete []buff;
Expand Down Expand Up @@ -213,9 +196,6 @@ bool Bmp8::LoadFromCharDumpFile(CachedFile *fp) {
Bmp8 * Bmp8::FromCharDumpFile(CachedFile *fp) {
// create a Bmp8 object
Bmp8 *bmp_obj = new Bmp8(0, 0);
if (bmp_obj == NULL) {
return NULL;
}

if (bmp_obj->LoadFromCharDumpFile(fp) == false) {
delete bmp_obj;
Expand Down Expand Up @@ -267,9 +247,6 @@ bool Bmp8::LoadFromCharDumpFile(FILE *fp) {

// alloc memory & read the 3 channel buffer
buff = new unsigned char[buf_size];
if (buff == NULL) {
return false;
}

if (fread(buff, 1, buf_size, fp) != buf_size) {
delete []buff;
Expand Down Expand Up @@ -308,9 +285,6 @@ bool Bmp8::LoadFromCharDumpFile(FILE *fp) {
Bmp8 * Bmp8::FromCharDumpFile(FILE *fp) {
// create a Bmp8 object
Bmp8 *bmp_obj = new Bmp8(0, 0);
if (bmp_obj == NULL) {
return NULL;
}

if (bmp_obj->LoadFromCharDumpFile(fp) == false) {
delete bmp_obj;
Expand Down Expand Up @@ -545,9 +519,6 @@ bool Bmp8::SaveBmp2CharDumpFile(FILE *fp) const {

// alloc memory & write the 3 channel buffer
buff = new unsigned char[buf_size];
if (buff == NULL) {
return false;
}

// copy the data
for (y = 0, pix = 0; y < hgt_; y++) {
Expand Down Expand Up @@ -699,7 +670,7 @@ ConComp ** Bmp8::FindConComps(int *concomp_cnt, int min_size) const {
// if there was no foreground pix, then create a new concomp
if (master_concomp == NULL) {
master_concomp = new ConComp();
if (master_concomp == NULL || master_concomp->Add(x, y) == false) {
if (master_concomp->Add(x, y) == false) {
fprintf(stderr, "Cube ERROR (Bmp8::FindConComps): could not "
"allocate or add a connected component\n");
FreeBmpBuffer(out_bmp_array);
Expand All @@ -711,13 +682,6 @@ ConComp ** Bmp8::FindConComps(int *concomp_cnt, int min_size) const {
if ((alloc_concomp_cnt % kConCompAllocChunk) == 0) {
ConComp **temp_con_comp =
new ConComp *[alloc_concomp_cnt + kConCompAllocChunk];
if (temp_con_comp == NULL) {
fprintf(stderr, "Cube ERROR (Bmp8::FindConComps): could not "
"extend array of connected components\n");
FreeBmpBuffer(out_bmp_array);
delete []concomp_array;
return NULL;
}

if (alloc_concomp_cnt > 0) {
memcpy(temp_con_comp, concomp_array,
Expand Down Expand Up @@ -774,9 +738,6 @@ bool Bmp8::ComputeTanTable() {
// alloc memory for tan table
delete []tan_table_;
tan_table_ = new float[kDeslantAngleCount];
if (tan_table_ == NULL) {
return false;
}

for (ang_idx = 0, ang_val = kMinDeslantAngle;
ang_idx < kDeslantAngleCount; ang_idx++) {
Expand Down Expand Up @@ -821,10 +782,6 @@ bool Bmp8::Deslant() {
int **angle_hist = new int*[kDeslantAngleCount];
for (ang_idx = 0; ang_idx < kDeslantAngleCount; ang_idx++) {
angle_hist[ang_idx] = new int[des_wid];
if (angle_hist[ang_idx] == NULL) {
delete[] angle_hist;
return false;
}
memset(angle_hist[ang_idx], 0, des_wid * sizeof(*angle_hist[ang_idx]));
}

Expand Down Expand Up @@ -1006,10 +963,6 @@ bool Bmp8::HorizontalDeslant(double *deslant_angle) {
int **angle_hist = new int*[kDeslantAngleCount];
for (ang_idx = 0; ang_idx < kDeslantAngleCount; ang_idx++) {
angle_hist[ang_idx] = new int[des_hgt];
if (angle_hist[ang_idx] == NULL) {
delete[] angle_hist;
return false;
}
memset(angle_hist[ang_idx], 0, des_hgt * sizeof(*angle_hist[ang_idx]));
}

Expand Down Expand Up @@ -1118,9 +1071,6 @@ float Bmp8::MeanHorizontalHistogramEntropy() const {

int *Bmp8::HorizontalHistogram() const {
int *hist = new int[hgt_];
if (hist == NULL) {
return NULL;
}

// compute histograms
for (int y = 0; y < hgt_; y++) {
Expand Down
3 changes: 0 additions & 3 deletions cube/cached_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ bool CachedFile::Open() {
rewind(fp_);
// alloc memory for buffer
buff_ = new unsigned char[kCacheSize];
if (buff_ == NULL) {
return false;
}
// init counters
buff_size_ = 0;
buff_pos_ = 0;
Expand Down
7 changes: 0 additions & 7 deletions cube/char_altlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,13 @@ bool CharAltList::Insert(int class_id, int cost, void *tag) {
alt_cost_ = new int[max_alt_];
alt_tag_ = new void *[max_alt_];

if (class_id_alt_ == NULL || alt_cost_ == NULL || alt_tag_ == NULL) {
return false;
}

memset(alt_tag_, 0, max_alt_ * sizeof(*alt_tag_));
}

if (class_id_cost_ == NULL) {
int class_cnt = char_set_->ClassCount();

class_id_cost_ = new int[class_cnt];
if (class_id_cost_ == NULL) {
return false;
}

for (int ich = 0; ich < class_cnt; ich++) {
class_id_cost_[ich] = WORST_COST;
Expand Down
16 changes: 0 additions & 16 deletions cube/char_bigrams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ CharBigrams *CharBigrams::Create(const string &data_file_path,

// construct a new object
CharBigrams *char_bigrams_obj = new CharBigrams();
if (char_bigrams_obj == NULL) {
fprintf(stderr, "Cube ERROR (CharBigrams::Create): could not create "
"character bigrams object.\n");
return NULL;
}
CharBigramTable *table = &char_bigrams_obj->bigram_table_;

table->total_cnt = 0;
Expand All @@ -90,11 +85,6 @@ CharBigrams *CharBigrams::Create(const string &data_file_path,
// expand the bigram table
if (ch1 > table->max_char) {
CharBigram *char_bigram = new CharBigram[ch1 + 1];
if (char_bigram == NULL) {
fprintf(stderr, "Cube ERROR (CharBigrams::Create): error allocating "
"additional memory for character bigram table.\n");
return NULL;
}

if (table->char_bigram != NULL && table->max_char >= 0) {
memcpy(char_bigram, table->char_bigram,
Expand All @@ -115,12 +105,6 @@ CharBigrams *CharBigrams::Create(const string &data_file_path,

if (ch2 > table->char_bigram[ch1].max_char) {
Bigram *bigram = new Bigram[ch2 + 1];
if (bigram == NULL) {
fprintf(stderr, "Cube ERROR (CharBigrams::Create): error allocating "
"memory for bigram.\n");
delete char_bigrams_obj;
return NULL;
}

if (table->char_bigram[ch1].bigram != NULL &&
table->char_bigram[ch1].max_char >= 0) {
Expand Down
Loading

0 comments on commit 03eec61

Please sign in to comment.