diff --git a/src/ccmain/control.cpp b/src/ccmain/control.cpp index b439aa649a..6d4cf4827c 100644 --- a/src/ccmain/control.cpp +++ b/src/ccmain/control.cpp @@ -982,9 +982,12 @@ void Tesseract::AssignDiacriticsToOverlappingBlobs(const std::vector *overlapped_any_blob, std::vector *target_blobs) { std::vector blob_wanted; - word_wanted->resize(outlines.size(), false); - overlapped_any_blob->resize(outlines.size(), false); - target_blobs->resize(outlines.size(), nullptr); + word_wanted->clear(); + word_wanted->resize(outlines.size()); + overlapped_any_blob->clear(); + overlapped_any_blob->resize(outlines.size()); + target_blobs->clear(); + target_blobs->resize(outlines.size()); // For each real blob, find the outlines that seriously overlap it. // A single blob could be several merged characters, so there can be quite // a few outlines overlapping, and the full engine needs to be used to chop @@ -993,7 +996,8 @@ void Tesseract::AssignDiacriticsToOverlappingBlobs(const std::vectorbounding_box(); - blob_wanted.resize(outlines.size(), false); + blob_wanted.clear(); + blob_wanted.resize(outlines.size()); int num_blob_outlines = 0; for (unsigned i = 0; i < outlines.size(); ++i) { if (blob_box.major_x_overlap(outlines[i]->bounding_box()) && !(*word_wanted)[i]) { @@ -1032,15 +1036,18 @@ void Tesseract::AssignDiacriticsToNewBlobs(const std::vector &outli std::vector *word_wanted, std::vector *target_blobs) { std::vector blob_wanted; - word_wanted->resize(outlines.size(), false); - target_blobs->resize(outlines.size(), nullptr); + word_wanted->clear(); + word_wanted->resize(outlines.size()); + target_blobs->clear(); + target_blobs->resize(outlines.size()); // Check for outlines that need to be turned into stand-alone blobs. for (unsigned i = 0; i < outlines.size(); ++i) { if (outlines[i] == nullptr) { continue; } // Get a set of adjacent outlines that don't overlap any existing blob. - blob_wanted.resize(outlines.size(), false); + blob_wanted.clear(); + blob_wanted.resize(outlines.size()); int num_blob_outlines = 0; TBOX total_ol_box(outlines[i]->bounding_box()); while (i < outlines.size() && outlines[i] != nullptr) { diff --git a/src/ccmain/linerec.cpp b/src/ccmain/linerec.cpp index 4f85fd670e..f1deced486 100644 --- a/src/ccmain/linerec.cpp +++ b/src/ccmain/linerec.cpp @@ -151,8 +151,7 @@ ImageData *Tesseract::GetLineData(const TBOX &line_box, const std::vector line_boxes.push_back(box); line_texts.push_back(texts[b]); } - std::vector page_numbers; - page_numbers.resize(line_boxes.size(), applybox_page); + std::vector page_numbers(line_boxes.size(), applybox_page); image_data->AddBoxes(line_boxes, line_texts, page_numbers); return image_data; } diff --git a/src/ccmain/paragraphs.cpp b/src/ccmain/paragraphs.cpp index f817bafdd4..b9525b9950 100644 --- a/src/ccmain/paragraphs.cpp +++ b/src/ccmain/paragraphs.cpp @@ -2314,14 +2314,14 @@ void CanonicalizeDetectionResults(std::vector *row_owners, PARA_LIST *pa void DetectParagraphs(int debug_level, std::vector *row_infos, std::vector *row_owners, PARA_LIST *paragraphs, std::vector *models) { - std::vector rows; ParagraphTheory theory(models); // Initialize row_owners to be a bunch of nullptr pointers. + row_owners->clear(); row_owners->resize(row_infos->size()); // Set up row scratch registers for the main algorithm. - rows.resize(row_infos->size(), RowScratchRegisters()); + std::vector rows(row_infos->size()); for (unsigned i = 0; i < row_infos->size(); i++) { rows[i].Init((*row_infos)[i]); } diff --git a/src/ccstruct/blobs.cpp b/src/ccstruct/blobs.cpp index c61280ff87..729d060bfd 100644 --- a/src/ccstruct/blobs.cpp +++ b/src/ccstruct/blobs.cpp @@ -558,7 +558,9 @@ void TBLOB::GetPreciseBoundingBox(TBOX *precise_box) const { // Eg x_coords[1] is a collection of the x-coords of edges at y=bottom + 1. void TBLOB::GetEdgeCoords(const TBOX &box, std::vector> &x_coords, std::vector> &y_coords) const { + x_coords.clear(); x_coords.resize(box.height()); + y_coords.clear(); y_coords.resize(box.width()); CollectEdges(box, nullptr, nullptr, &x_coords, &y_coords); // Sort the output vectors. diff --git a/src/ccstruct/fontinfo.h b/src/ccstruct/fontinfo.h index 70edfa09bd..1a84a56733 100644 --- a/src/ccstruct/fontinfo.h +++ b/src/ccstruct/fontinfo.h @@ -77,8 +77,7 @@ struct FontInfo { // Reserves unicharset_size spots in spacing_vec. void init_spacing(int unicharset_size) { - spacing_vec = new std::vector(); - spacing_vec->resize(unicharset_size); + spacing_vec = new std::vector(unicharset_size); } // Adds the given pointer to FontSpacingInfo to spacing_vec member // (FontInfo class takes ownership of the pointer). diff --git a/src/ccstruct/normalis.cpp b/src/ccstruct/normalis.cpp index 599de79c5d..077a7c75a9 100644 --- a/src/ccstruct/normalis.cpp +++ b/src/ccstruct/normalis.cpp @@ -226,7 +226,9 @@ static void ComputeEdgeDensityProfiles(const TBOX &box, const GENERIC_2D_ARRAY &hx, std::vector &hy) { int width = box.width(); int height = box.height(); + hx.clear(); hx.resize(width + 1); + hy.clear(); hy.resize(height + 1); double total = 0.0; for (int iy = 0; iy < height; ++iy) { diff --git a/src/ccstruct/pageres.cpp b/src/ccstruct/pageres.cpp index 1005dc3be3..72b6847dbd 100644 --- a/src/ccstruct/pageres.cpp +++ b/src/ccstruct/pageres.cpp @@ -901,6 +901,7 @@ void WERD_RES::FakeClassifyWord(int blob_count, BLOB_CHOICE **choices) { } FakeWordFromRatings(TOP_CHOICE_PERM); reject_map.initialise(blob_count); + best_state.clear(); best_state.resize(blob_count, 1); done = true; } diff --git a/src/ccstruct/pageres.h b/src/ccstruct/pageres.h index 96202d55c9..2258ea7fb3 100644 --- a/src/ccstruct/pageres.h +++ b/src/ccstruct/pageres.h @@ -96,6 +96,7 @@ class PAGE_RES { // page result rej_count = 0; rejected = false; prev_word_best_choice = nullptr; + blame_reasons.clear(); blame_reasons.resize(IRR_NUM_REASONS); } diff --git a/src/ccstruct/ratngs.cpp b/src/ccstruct/ratngs.cpp index 5801cd9b41..7b878b9ae5 100644 --- a/src/ccstruct/ratngs.cpp +++ b/src/ccstruct/ratngs.cpp @@ -738,6 +738,7 @@ void WERD_CHOICE::DisplaySegmentation(TWERD *word) { static std::vector prev_drawn_state; bool already_done = prev_drawn_state.size() == length_; if (!already_done) { + prev_drawn_state.clear(); prev_drawn_state.resize(length_); } for (int i = 0; i < length_; ++i) { diff --git a/src/ccstruct/stepblob.cpp b/src/ccstruct/stepblob.cpp index 513619d76c..f090e007db 100644 --- a/src/ccstruct/stepblob.cpp +++ b/src/ccstruct/stepblob.cpp @@ -431,8 +431,7 @@ int16_t C_BLOB::EstimateBaselinePosition() { return bottom; // This is only for non-CJK blobs. } // Get the minimum y coordinate at each x-coordinate. - std::vector y_mins; - y_mins.resize(width + 1, box.top()); + std::vector y_mins(width + 1, box.top()); C_OUTLINE_IT it(&outlines); for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { C_OUTLINE *outline = it.data(); diff --git a/src/ccutil/unicharcompress.cpp b/src/ccutil/unicharcompress.cpp index c947221439..9cb1e48e74 100644 --- a/src/ccutil/unicharcompress.cpp +++ b/src/ccutil/unicharcompress.cpp @@ -395,7 +395,8 @@ void UnicharCompress::ComputeCodeRange() { // Initializes the decoding hash_map from the encoding array. void UnicharCompress::SetupDecoder() { Cleanup(); - is_valid_start_.resize(code_range_, false); + is_valid_start_.clear(); + is_valid_start_.resize(code_range_); for (int c = 0; c < encoder_.size(); ++c) { const RecodedCharID &code = encoder_[c]; decoder_[code] = c; diff --git a/src/classify/intmatcher.cpp b/src/classify/intmatcher.cpp index e25b9a0950..b423ffeec8 100644 --- a/src/classify/intmatcher.cpp +++ b/src/classify/intmatcher.cpp @@ -372,6 +372,7 @@ for (int bit = 0; bit < BITS_PER_WERD/NUM_BITS_PER_CLASS; bit++) { /// Copies the pruned, sorted classes into the output results and returns /// the number of classes. int SetupResults(std::vector *results) const { + results->clear(); results->resize(num_classes_); for (int c = 0; c < num_classes_; ++c) { (*results)[c].Class = sort_index_[num_classes_ - c]; diff --git a/src/classify/shapeclassifier.cpp b/src/classify/shapeclassifier.cpp index eb247de3fa..8252cae6eb 100644 --- a/src/classify/shapeclassifier.cpp +++ b/src/classify/shapeclassifier.cpp @@ -45,8 +45,7 @@ int ShapeClassifier::UnicharClassifySample(const TrainingSample &sample, Image p std::vector shape_results; int num_shape_results = ClassifySample(sample, page_pix, debug, keep_this, &shape_results); const ShapeTable *shapes = GetShapeTable(); - std::vector unichar_map; - unichar_map.resize(shapes->unicharset().size(), -1); + std::vector unichar_map(shapes->unicharset().size(), -1); for (int r = 0; r < num_shape_results; ++r) { shapes->AddShapeToResults(shape_results[r], &unichar_map, results); } diff --git a/src/classify/shapetable.cpp b/src/classify/shapetable.cpp index f4b4e3508b..f205480cf7 100644 --- a/src/classify/shapetable.cpp +++ b/src/classify/shapetable.cpp @@ -682,6 +682,7 @@ bool ShapeTable::CommonFont(int shape_id1, int shape_id2) const { // If not nullptr, shape_map is set to map other shape_ids to this's shape_ids. void ShapeTable::AppendMasterShapes(const ShapeTable &other, std::vector *shape_map) { if (shape_map != nullptr) { + shape_map->clear(); shape_map->resize(other.NumShapes(), -1); } for (int s = 0; s < other.shape_table_.size(); ++s) { diff --git a/src/lstm/recodebeam.cpp b/src/lstm/recodebeam.cpp index 485723ea45..7861052e04 100644 --- a/src/lstm/recodebeam.cpp +++ b/src/lstm/recodebeam.cpp @@ -644,6 +644,7 @@ WERD_RES *RecodeBeamSearch::InitializeWord(bool leading_space, const TBOX &line_ // Fills top_n_flags_ with bools that are true iff the corresponding output // is one of the top_n. void RecodeBeamSearch::ComputeTopN(const float *outputs, int num_outputs, int top_n) { + top_n_flags_.clear(); top_n_flags_.resize(num_outputs, TN_ALSO_RAN); top_code_ = -1; second_code_ = -1; @@ -676,6 +677,7 @@ void RecodeBeamSearch::ComputeTopN(const float *outputs, int num_outputs, int to void RecodeBeamSearch::ComputeSecTopN(std::unordered_set *exList, const float *outputs, int num_outputs, int top_n) { + top_n_flags_.clear(); top_n_flags_.resize(num_outputs, TN_ALSO_RAN); top_code_ = -1; second_code_ = -1; diff --git a/src/training/common/ctc.cpp b/src/training/common/ctc.cpp index 41f37b2587..0cc0eb10eb 100644 --- a/src/training/common/ctc.cpp +++ b/src/training/common/ctc.cpp @@ -87,7 +87,9 @@ CTC::CTC(const std::vector &labels, int null_char, const GENERIC_2D_ARRAY layers = EnumerateLayers(); int num_layers = layers.size(); - std::vector num_weights; - num_weights.resize(num_layers, 0); + std::vector num_weights(num_layers); std::vector bad_sums[LR_COUNT]; std::vector ok_sums[LR_COUNT]; for (int i = 0; i < LR_COUNT; ++i) { @@ -1263,8 +1263,7 @@ double LSTMTrainer::ComputeWinnerError(const NetworkIO &deltas) { // Computes a very simple bag of chars char error rate. double LSTMTrainer::ComputeCharError(const std::vector &truth_str, const std::vector &ocr_str) { - std::vector label_counts; - label_counts.resize(NumOutputs(), 0); + std::vector label_counts(NumOutputs()); int truth_size = 0; for (auto ch : truth_str) { if (ch != null_char_) { diff --git a/src/wordrec/params_model.cpp b/src/wordrec/params_model.cpp index 340723e999..cda48c8c47 100644 --- a/src/wordrec/params_model.cpp +++ b/src/wordrec/params_model.cpp @@ -110,6 +110,7 @@ bool ParamsModel::LoadFromFp(const char *lang, TFile *fp) { lang_ = lang; // Load weights for passes with adaption on. std::vector &weights = weights_vec_[pass_]; + weights.clear(); weights.resize(PTRAIN_NUM_FEATURE_TYPES, 0.0f); while (fp->FGets(line, kMaxLineSize) != nullptr) { diff --git a/src/wordrec/segsearch.cpp b/src/wordrec/segsearch.cpp index 05841a28de..a534d0c52a 100644 --- a/src/wordrec/segsearch.cpp +++ b/src/wordrec/segsearch.cpp @@ -156,6 +156,7 @@ void Wordrec::InitialSegSearch(WERD_RES *word_res, LMPainPoints *pain_points, // children are considered in the non-decreasing order of their column, since // this guarantees that all the parents would be up to date before an update // of a child is done. + pending->clear(); pending->resize(word_res->ratings->dimension(), SegSearchPending()); // Search the ratings matrix for the initial best path.