mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2025-01-21 08:43:03 +08:00
Replace malloc / free for ELIST
Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
parent
582260a9bf
commit
2c273c1b3b
@ -103,33 +103,29 @@ int32_t ELIST::length() const { // count elements
|
||||
void ELIST::sort( // sort elements
|
||||
int comparator( // comparison routine
|
||||
const void *, const void *)) {
|
||||
ELIST_ITERATOR it(this);
|
||||
int32_t count;
|
||||
ELIST_LINK **base; // ptr array to sort
|
||||
ELIST_LINK **current;
|
||||
int32_t i;
|
||||
// Allocate an array of pointers, one per list element.
|
||||
auto count = length();
|
||||
|
||||
/* Allocate an array of pointers, one per list element */
|
||||
count = length();
|
||||
base = static_cast<ELIST_LINK **>(malloc(count * sizeof(ELIST_LINK *)));
|
||||
if (count > 0) {
|
||||
// ptr array to sort
|
||||
std::vector<ELIST_LINK *> base;
|
||||
base.reserve(count);
|
||||
|
||||
/* Extract all elements, putting the pointers in the array */
|
||||
current = base;
|
||||
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
||||
*current = it.extract();
|
||||
current++;
|
||||
ELIST_ITERATOR it(this);
|
||||
|
||||
// Extract all elements, putting the pointers in the array.
|
||||
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
||||
base.push_back(it.extract());
|
||||
}
|
||||
|
||||
// Sort the pointer array.
|
||||
qsort(&base[0], count, sizeof(base[0]), comparator);
|
||||
|
||||
// Rebuild the list from the sorted pointers.
|
||||
for (auto current : base) {
|
||||
it.add_to_end(current);
|
||||
}
|
||||
}
|
||||
|
||||
/* Sort the pointer array */
|
||||
qsort(base, count, sizeof(*base), comparator);
|
||||
|
||||
/* Rebuild the list from the sorted pointers */
|
||||
current = base;
|
||||
for (i = 0; i < count; i++) {
|
||||
it.add_to_end(*current);
|
||||
current++;
|
||||
}
|
||||
free(base);
|
||||
}
|
||||
|
||||
// Assuming list has been sorted already, insert new_link to
|
||||
|
Loading…
Reference in New Issue
Block a user