From 5bf1af257c932e4d29784ad4825d218099cb9dd5 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 28 Mar 2021 12:36:18 +0200 Subject: [PATCH] Use std::vector for CLASS_STRUCT::Configurations Signed-off-by: Stefan Weil --- src/classify/protos.cpp | 22 +++++----------------- src/classify/protos.h | 19 ++++++------------- src/training/common/commontraining.cpp | 2 +- 3 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/classify/protos.cpp b/src/classify/protos.cpp index 6fb31fadb..71f192077 100644 --- a/src/classify/protos.cpp +++ b/src/classify/protos.cpp @@ -60,9 +60,7 @@ int AddConfigToClass(CLASS_TYPE Class) { NewNumConfigs = (((Class->MaxNumConfigs + CONFIG_INCREMENT) / CONFIG_INCREMENT) * CONFIG_INCREMENT); - Class->Configurations = - static_cast(realloc(Class->Configurations, sizeof(BIT_VECTOR) * NewNumConfigs)); - + Class->Configurations.resize(NewNumConfigs); Class->MaxNumConfigs = NewNumConfigs; } NewConfig = Class->NumConfigs++; @@ -131,14 +129,9 @@ void FreeClass(CLASS_TYPE Class) { * Deallocate the memory consumed by subfields of the specified class. **********************************************************************/ void FreeClassFields(CLASS_TYPE Class) { - int i; - if (Class) { - if (Class->MaxNumConfigs > 0) { - for (i = 0; i < Class->NumConfigs; i++) { - FreeBitVector(Class->Configurations[i]); - } - free(Class->Configurations); + for (int i = 0; i < Class->NumConfigs; i++) { + FreeBitVector(Class->Configurations[i]); } } } @@ -154,13 +147,8 @@ CLASS_TYPE NewClass(int NumProtos, int NumConfigs) { Class = new CLASS_STRUCT; - if (NumProtos > 0) { - Class->Prototypes.resize(NumProtos); - } - - if (NumConfigs > 0) { - Class->Configurations = static_cast(malloc(NumConfigs * sizeof(BIT_VECTOR))); - } + Class->Prototypes.resize(NumProtos); + Class->Configurations.resize(NumConfigs); Class->MaxNumProtos = NumProtos; Class->MaxNumConfigs = NumConfigs; Class->NumProtos = 0; diff --git a/src/classify/protos.h b/src/classify/protos.h index 15e7ab1ba..fdabcd41a 100644 --- a/src/classify/protos.h +++ b/src/classify/protos.h @@ -27,8 +27,6 @@ namespace tesseract { -using CONFIGS = BIT_VECTOR *; - struct PROTO_STRUCT { float A; float B; @@ -40,18 +38,13 @@ struct PROTO_STRUCT { }; struct CLASS_STRUCT { - CLASS_STRUCT() - : NumProtos(0) - , MaxNumProtos(0) - , NumConfigs(0) - , MaxNumConfigs(0) - , Configurations(nullptr) {} - int16_t NumProtos; - int16_t MaxNumProtos; - int16_t NumConfigs; - int16_t MaxNumConfigs; + CLASS_STRUCT() = default; + int16_t NumProtos = 0; + int16_t MaxNumProtos = 0; + int16_t NumConfigs = 0; + int16_t MaxNumConfigs = 0; std::vector Prototypes; - CONFIGS Configurations; + std::vector Configurations; UnicityTable font_set; }; using CLASS_TYPE = CLASS_STRUCT *; diff --git a/src/training/common/commontraining.cpp b/src/training/common/commontraining.cpp index b1424fd75..294044c3e 100644 --- a/src/training/common/commontraining.cpp +++ b/src/training/common/commontraining.cpp @@ -730,7 +730,7 @@ CLASS_STRUCT *SetUpForFloat2Int(const UNICHARSET &unicharset, LIST LabeledClassL Class->NumConfigs = NumConfigs; Class->MaxNumConfigs = NumConfigs; Class->font_set.move(&font_set); - Class->Configurations = static_cast(malloc(sizeof(BIT_VECTOR) * NumConfigs)); + Class->Configurations.resize(NumConfigs); NumWords = WordsInVectorOfSize(NumProtos); for (i = 0; i < NumConfigs; i++) { NewConfig = NewBitVector(NumProtos);