Update sorting routines for lists.

This commit is contained in:
Egor Pugin 2024-11-22 19:10:18 +03:00
parent 1ceb14e274
commit 1355287df1
17 changed files with 43 additions and 70 deletions

View File

@ -55,12 +55,9 @@ class ROW;
**********************************************************************/
static int c_blob_comparator( // sort blobs
const void *blob1p, // ptr to ptr to blob1
const void *blob2p // ptr to ptr to blob2
const C_BLOB *blob1,
const C_BLOB *blob2
) {
const C_BLOB *blob1 = *reinterpret_cast<const C_BLOB *const *>(blob1p);
const C_BLOB *blob2 = *reinterpret_cast<const C_BLOB *const *>(blob2p);
return blob1->bounding_box().left() - blob2->bounding_box().left();
}

View File

@ -191,9 +191,7 @@ void ParamsEditor::GetPrefixes(const char *s, std::string *level_one, std::strin
}
// Compare two VC objects by their name.
int ParamContent::Compare(const void *v1, const void *v2) {
const ParamContent *one = *static_cast<const ParamContent *const *>(v1);
const ParamContent *two = *static_cast<const ParamContent *const *>(v2);
int ParamContent::Compare(const ParamContent *one, const ParamContent *two) {
return strcmp(one->GetName(), two->GetName());
}

View File

@ -47,7 +47,7 @@ enum ParamType { VT_INTEGER, VT_BOOLEAN, VT_STRING, VT_DOUBLE };
class ParamContent : public ELIST<ParamContent>::LINK {
public:
// Compare two VC objects by their name.
static int Compare(const void *v1, const void *v2);
static int Compare(const ParamContent *v1, const ParamContent *v2);
// Gets a VC object identified by its ID.
static ParamContent *GetParamContentById(int id);

View File

@ -68,9 +68,9 @@ BLOCK::BLOCK(const char *name, ///< filename
* Sort Comparator: Return <0 if row1 top < row2 top
*/
static int decreasing_top_order(const void *row1, const void *row2) {
return (*reinterpret_cast<ROW *const *>(row2))->bounding_box().top() -
(*reinterpret_cast<ROW *const *>(row1))->bounding_box().top();
static int decreasing_top_order(const ROW *row1, const ROW *row2) {
return row2->bounding_box().top() -
row1->bounding_box().top();
}
/**

View File

@ -34,7 +34,7 @@ namespace tesseract {
#define INTERSECTING INT16_MAX
int lessthan(const void *first, const void *second);
int lessthan(const ICOORDELT *first, const ICOORDELT *second);
POLY_BLOCK::POLY_BLOCK(ICOORDELT_LIST *points, PolyBlockType t) {
ICOORDELT_IT v = &vertices;
@ -371,10 +371,7 @@ ICOORDELT_LIST *PB_LINE_IT::get_line(TDimension y) {
return result;
}
int lessthan(const void *first, const void *second) {
const ICOORDELT *p1 = *reinterpret_cast<const ICOORDELT *const *>(first);
const ICOORDELT *p2 = *reinterpret_cast<const ICOORDELT *const *>(second);
int lessthan(const ICOORDELT *p1, const ICOORDELT *p2) {
if (p1->x() < p2->x()) {
return (-1);
} else if (p1->x() > p2->x()) {

View File

@ -120,9 +120,7 @@ public:
return blob;
}
static int SortByXMiddle(const void *v1, const void *v2) {
const C_BLOB *blob1 = *static_cast<const C_BLOB *const *>(v1);
const C_BLOB *blob2 = *static_cast<const C_BLOB *const *>(v2);
static int SortByXMiddle(const C_BLOB *blob1, const C_BLOB *blob2) {
return blob1->bounding_box().x_middle() - blob2->bounding_box().x_middle();
}

View File

@ -374,9 +374,7 @@ WERD &WERD::operator=(const WERD &source) {
* order of left edge.
*/
int word_comparator(const void *word1p, const void *word2p) {
const WERD *word1 = *reinterpret_cast<const WERD *const *>(word1p);
const WERD *word2 = *reinterpret_cast<const WERD *const *>(word2p);
int word_comparator(const WERD *word1, const WERD *word2) {
return word1->bounding_box().left() - word2->bounding_box().left();
}

View File

@ -205,7 +205,7 @@ ELIST2IZEH(WERD)
namespace tesseract {
// compare words by increasing order of left edge, suitable for qsort(3)
int word_comparator(const void *word1p, const void *word2p);
int word_comparator(const WERD *word1, const WERD *word2);
} // namespace tesseract

View File

@ -117,9 +117,7 @@ public:
// Comparator function for sorting AmbigSpec_LISTs. The lists will
// be sorted by their wrong_ngram arrays. Example of wrong_ngram vectors
// in a sorted AmbigSpec_LIST: [9 1 3], [9 3 4], [9 8], [9, 8 1].
static int compare_ambig_specs(const void *spec1, const void *spec2) {
const AmbigSpec *s1 = *static_cast<const AmbigSpec *const *>(spec1);
const AmbigSpec *s2 = *static_cast<const AmbigSpec *const *>(spec2);
static int compare_ambig_specs(const AmbigSpec *s1, const AmbigSpec *s2) {
int result = UnicharIdArrayUtils::compare(s1->wrong_ngram, s2->wrong_ngram);
if (result != 0) {
return result;

View File

@ -19,7 +19,6 @@
#ifndef CLST_H
#define CLST_H
#include "list.h"
#include "lsterr.h"
#include "serialis.h"
@ -764,7 +763,7 @@ public:
**********************************************************************/
void sort( // sort elements
int comparator( // comparison routine
const void *, const void *)) {
const CLASSNAME *, const CLASSNAME *)) {
list->sort(comparator);
move_to_first();
}
@ -907,7 +906,9 @@ public:
}
// Sort the pointer array.
std::sort(base.begin(), base.end(), comparator);
std::sort(base.begin(), base.end(),
// all current comparators return -1,0,1, so we handle this correctly for std::sort
[&](auto &&l, auto &&r) {return comparator(l, r) < 0; });
// Rebuild the list from the sorted pointers.
for (auto current : base) {

View File

@ -19,7 +19,6 @@
#ifndef ELST_H
#define ELST_H
#include "list.h"
#include "lsterr.h"
#include "serialis.h"
@ -905,7 +904,7 @@ public:
**********************************************************************/
void sort( // sort elements
int comparator( // comparison routine
const void *, const void *)) {
const CLASSNAME *, const CLASSNAME *)) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::sort", ABORT);
@ -1032,7 +1031,7 @@ public:
**********************************************************************/
void sort( // sort elements
int comparator( // comparison routine
const void *, const void *)) {
const CLASSNAME *, const CLASSNAME *)) {
// Allocate an array of pointers, one per list element.
auto count = length();
@ -1049,7 +1048,9 @@ public:
}
// Sort the pointer array.
qsort(&base[0], count, sizeof(base[0]), comparator);
std::sort(base.begin(), base.end(),
// all current comparators return -1,0,1, so we handle this correctly for std::sort
[&](auto &&l, auto &&r) {return comparator(l, r) < 0; });
// Rebuild the list from the sorted pointers.
for (auto current : base) {
@ -1068,10 +1069,10 @@ public:
// list) - new_link is not added to the list and the function returns the
// pointer to the identical entry that already exists in the list
// (otherwise the function returns new_link).
CLASSNAME *add_sorted_and_find(int comparator(const void *, const void *), bool unique,
CLASSNAME *add_sorted_and_find(int comparator(const CLASSNAME *, const CLASSNAME *), bool unique,
CLASSNAME *new_link) {
// Check for adding at the end.
if (last == nullptr || comparator(&last, &new_link) < 0) {
if (last == nullptr || comparator(last, new_link) < 0) {
if (last == nullptr) {
new_link->next = new_link;
} else {
@ -1084,7 +1085,7 @@ public:
ITERATOR it(this);
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
auto *link = it.data();
int compare = comparator(&link, &new_link);
int compare = comparator(link, new_link);
if (compare > 0) {
break;
} else if (unique && compare == 0) {
@ -1102,7 +1103,7 @@ public:
// Same as above, but returns true if the new entry was inserted, false
// if the identical entry already existed in the list.
bool add_sorted(int comparator(const void *, const void *), bool unique, CLASSNAME *new_link) {
bool add_sorted(int comparator(const CLASSNAME *, const CLASSNAME *), bool unique, CLASSNAME *new_link) {
return (add_sorted_and_find(comparator, unique, new_link) == new_link);
}
};

View File

@ -19,7 +19,6 @@
#ifndef ELST2_H
#define ELST2_H
#include "list.h"
#include "lsterr.h"
#include "serialis.h"
@ -972,7 +971,7 @@ public:
**********************************************************************/
void sort( // sort elements
int comparator( // comparison routine
const void *, const void *)) {
const CLASSNAME *, const CLASSNAME *)) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST2_ITERATOR::sort", ABORT);
@ -1095,7 +1094,7 @@ public:
**********************************************************************/
void sort( // sort elements
int comparator( // comparison routine
const void *, const void *)) {
const CLASSNAME *, const CLASSNAME *)) {
// Allocate an array of pointers, one per list element.
auto count = length();
if (count > 0) {
@ -1111,7 +1110,9 @@ public:
}
// Sort the pointer array.
qsort(&base[0], count, sizeof(base[0]), comparator);
std::sort(base.begin(), base.end(),
// all current comparators return -1,0,1, so we handle this correctly for std::sort
[&](auto &&l, auto &&r) {return comparator(l, r) < 0; });
// Rebuild the list from the sorted pointers.
for (auto current : base) {
@ -1125,9 +1126,9 @@ public:
// Comparison function is the same as used by sort, i.e. uses double
// indirection. Time is O(1) to add to beginning or end.
// Time is linear to add pre-sorted items to an empty list.
void add_sorted(int comparator(const void *, const void *), CLASSNAME *new_link) {
void add_sorted(int comparator(const CLASSNAME *, const CLASSNAME *), CLASSNAME *new_link) {
// Check for adding at the end.
if (last == nullptr || comparator(&last, &new_link) < 0) {
if (last == nullptr || comparator(last, new_link) < 0) {
if (last == nullptr) {
new_link->next = new_link;
new_link->prev = new_link;
@ -1143,7 +1144,7 @@ public:
ITERATOR it(this);
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
auto link = it.data();
if (comparator(&link, &new_link) > 0) {
if (comparator(link, new_link) > 0) {
break;
}
}

View File

@ -709,9 +709,7 @@ public:
bool IsInSameColumnAs(const ColPartition &part) const;
// Sort function to sort by bounding box.
static int SortByBBox(const void *p1, const void *p2) {
const ColPartition *part1 = *static_cast<const ColPartition *const *>(p1);
const ColPartition *part2 = *static_cast<const ColPartition *const *>(p2);
static int SortByBBox(const ColPartition *part1, const ColPartition *part2) {
int mid_y1 = part1->bounding_box_.y_middle();
int mid_y2 = part2->bounding_box_.y_middle();
if ((part2->bounding_box_.bottom() <= mid_y1 &&

View File

@ -105,13 +105,8 @@ const int kMinLeaderCount = 5;
* Sort function to sort rows in y from page top.
*/
static int row_y_order( // sort function
const void *item1, // items to compare
const void *item2) {
// converted ptr
const TO_ROW *row1 = *reinterpret_cast<const TO_ROW *const *>(item1);
// converted ptr
const TO_ROW *row2 = *reinterpret_cast<const TO_ROW *const *>(item2);
const TO_ROW *row1, // items to compare
const TO_ROW *row2) {
if (row1->parallel_c() > row2->parallel_c()) {
return -1;
} else if (row1->parallel_c() < row2->parallel_c()) {
@ -2540,13 +2535,8 @@ OVERLAP_STATE most_overlapping_row( // find best row
* Sort function to sort blobs in x from page left.
*/
int blob_x_order( // sort function
const void *item1, // items to compare
const void *item2) {
// converted ptr
const BLOBNBOX *blob1 = *reinterpret_cast<const BLOBNBOX *const *>(item1);
// converted ptr
const BLOBNBOX *blob2 = *reinterpret_cast<const BLOBNBOX *const *>(item2);
const BLOBNBOX *blob1, // items to compare
const BLOBNBOX *blob2) {
if (blob1->bounding_box().left() < blob2->bounding_box().left()) {
return -1;
} else if (blob1->bounding_box().left() > blob2->bounding_box().left()) {

View File

@ -243,8 +243,8 @@ OVERLAP_STATE most_overlapping_row(TO_ROW_IT *row_it, // iterator
bool testing_blob // test stuff
);
int blob_x_order( // sort function
const void *item1, // items to compare
const void *item2);
const BLOBNBOX *item1, // items to compare
const BLOBNBOX *item2);
void mark_repeated_chars(TO_ROW *row);

View File

@ -286,9 +286,7 @@ public:
}
// Sort function for E2LIST::sort to sort by sort_key_.
static int SortVectorsByKey(const void *v1, const void *v2) {
const TabVector *tv1 = *static_cast<const TabVector *const *>(v1);
const TabVector *tv2 = *static_cast<const TabVector *const *>(v2);
static int SortVectorsByKey(const TabVector *tv1, const TabVector *tv2) {
return tv1->sort_key_ - tv2->sort_key_;
}

View File

@ -133,9 +133,7 @@ struct ViterbiStateEntry : public ELIST<ViterbiStateEntry>::LINK {
}
/// Comparator function for sorting ViterbiStateEntry_LISTs in
/// non-increasing order of costs.
static int Compare(const void *e1, const void *e2) {
const ViterbiStateEntry *ve1 = *static_cast<const ViterbiStateEntry *const *>(e1);
const ViterbiStateEntry *ve2 = *static_cast<const ViterbiStateEntry *const *>(e2);
static int Compare(const ViterbiStateEntry *ve1, const ViterbiStateEntry *ve2) {
return (ve1->cost < ve2->cost) ? -1 : 1;
}
inline bool Consistent() const {