tesseract  5.0.0
tesseract::ELIST2 Class Reference

#include <elst2.h>

Public Member Functions

void internal_clear (void(*zapper)(void *))
 
bool empty () const
 
bool singleton () const
 
void shallow_copy (ELIST2 *from_list)
 
void internal_deep_copy (ELIST2_LINK *(*copier)(ELIST2_LINK *), const ELIST2 *list)
 
void assign_to_sublist (ELIST2_ITERATOR *start_it, ELIST2_ITERATOR *end_it)
 
int32_t length () const
 
void sort (int comparator(const void *, const void *))
 
void add_sorted (int comparator(const void *, const void *), ELIST2_LINK *new_link)
 

Friends

class ELIST2_ITERATOR
 

Detailed Description

Definition at line 86 of file elst2.h.

Member Function Documentation

◆ add_sorted()

void tesseract::ELIST2::add_sorted ( int   comparatorconst void *, const void *,
ELIST2_LINK new_link 
)

Definition at line 120 of file elst2.cpp.

120  {
121  // Check for adding at the end.
122  if (last == nullptr || comparator(&last, &new_link) < 0) {
123  if (last == nullptr) {
124  new_link->next = new_link;
125  new_link->prev = new_link;
126  } else {
127  new_link->next = last->next;
128  new_link->prev = last;
129  last->next = new_link;
130  new_link->next->prev = new_link;
131  }
132  last = new_link;
133  } else {
134  // Need to use an iterator.
135  ELIST2_ITERATOR it(this);
136  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
137  ELIST2_LINK *link = it.data();
138  if (comparator(&link, &new_link) > 0) {
139  break;
140  }
141  }
142  if (it.cycled_list()) {
143  it.add_to_end(new_link);
144  } else {
145  it.add_before_then_move(new_link);
146  }
147  }
148 }
friend class ELIST2_ITERATOR
Definition: elst2.h:87

◆ assign_to_sublist()

void tesseract::ELIST2::assign_to_sublist ( ELIST2_ITERATOR start_it,
ELIST2_ITERATOR end_it 
)

Definition at line 68 of file elst2.cpp.

70  { // from list end
71  constexpr ERRCODE LIST_NOT_EMPTY("Destination list must be empty before extracting a sublist");
72 
73  if (!empty()) {
74  LIST_NOT_EMPTY.error("ELIST2.assign_to_sublist", ABORT, nullptr);
75  }
76 
77  last = start_it->extract_sublist(end_it);
78 }
@ ABORT
Definition: errcode.h:31
bool empty() const
Definition: elst2.h:99

◆ empty()

bool tesseract::ELIST2::empty ( ) const
inline

Definition at line 99 of file elst2.h.

99  { // is list empty?
100  return !last;
101  }

◆ internal_clear()

void tesseract::ELIST2::internal_clear ( void(*)(void *)  zapper)

Definition at line 37 of file elst2.cpp.

38  {
39  // ptr to zapper functn
40  ELIST2_LINK *ptr;
41  ELIST2_LINK *next;
42 
43  if (!empty()) {
44  ptr = last->next; // set to first
45  last->next = nullptr; // break circle
46  last = nullptr; // set list empty
47  while (ptr) {
48  next = ptr->next;
49  zapper(ptr);
50  ptr = next;
51  }
52  }
53 }

◆ internal_deep_copy()

void tesseract::ELIST2::internal_deep_copy ( ELIST2_LINK *(*)(ELIST2_LINK *)  copier,
const ELIST2 list 
)

◆ length()

int32_t tesseract::ELIST2::length ( ) const
inline

Definition at line 121 of file elst2.h.

121  {
122  int32_t count = 0;
123  if (last != nullptr) {
124  count = 1;
125  for (auto it = last->next; it != last; it = it->next) {
126  count++;
127  }
128  }
129  return count;
130  }

◆ shallow_copy()

void tesseract::ELIST2::shallow_copy ( ELIST2 from_list)
inline

Definition at line 107 of file elst2.h.

108  { // beware destructors!!
109  last = from_list->last;
110  }

◆ singleton()

bool tesseract::ELIST2::singleton ( ) const
inline

Definition at line 103 of file elst2.h.

103  {
104  return last ? (last == last->next) : false;
105  }
LIST last(LIST var_list)
Definition: oldlist.cpp:153
list_rec * next
Definition: oldlist.h:105

◆ sort()

void tesseract::ELIST2::sort ( int   comparator const void *, const void *)

Definition at line 88 of file elst2.cpp.

90  {
91  // Allocate an array of pointers, one per list element.
92  auto count = length();
93  if (count > 0) {
94  // ptr array to sort
95  std::vector<ELIST2_LINK *> base;
96  base.reserve(count);
97 
98  ELIST2_ITERATOR it(this);
99 
100  // Extract all elements, putting the pointers in the array.
101  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
102  base.push_back(it.extract());
103  }
104 
105  // Sort the pointer array.
106  qsort(&base[0], count, sizeof(base[0]), comparator);
107 
108  // Rebuild the list from the sorted pointers.
109  for (auto current : base) {
110  it.add_to_end(current);
111  }
112  }
113 }
int32_t length() const
Definition: elst2.h:121

Friends And Related Function Documentation

◆ ELIST2_ITERATOR

friend class ELIST2_ITERATOR
friend

Definition at line 87 of file elst2.h.


The documentation for this class was generated from the following files: