tesseract  5.0.0
tesseract::ELIST Class Reference

#include <elst.h>

Public Member Functions

void internal_clear (void(*zapper)(void *))
 
bool empty () const
 
bool singleton () const
 
void shallow_copy (ELIST *from_list)
 
void internal_deep_copy (ELIST_LINK *(*copier)(ELIST_LINK *), const ELIST *list)
 
void assign_to_sublist (ELIST_ITERATOR *start_it, ELIST_ITERATOR *end_it)
 
int32_t length () const
 
void sort (int comparator(const void *, const void *))
 
ELIST_LINKadd_sorted_and_find (int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
 
bool add_sorted (int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
 

Friends

class ELIST_ITERATOR
 

Detailed Description

Definition at line 111 of file elst.h.

Member Function Documentation

◆ add_sorted()

bool tesseract::ELIST::add_sorted ( int   comparatorconst void *, const void *,
bool  unique,
ELIST_LINK new_link 
)
inline

Definition at line 176 of file elst.h.

176  {
177  return (add_sorted_and_find(comparator, unique, new_link) == new_link);
178  }
ELIST_LINK * add_sorted_and_find(int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
Definition: elst.cpp:125

◆ add_sorted_and_find()

ELIST_LINK * tesseract::ELIST::add_sorted_and_find ( int   comparatorconst void *, const void *,
bool  unique,
ELIST_LINK new_link 
)

Definition at line 125 of file elst.cpp.

126  {
127  // Check for adding at the end.
128  if (last == nullptr || comparator(&last, &new_link) < 0) {
129  if (last == nullptr) {
130  new_link->next = new_link;
131  } else {
132  new_link->next = last->next;
133  last->next = new_link;
134  }
135  last = new_link;
136  } else {
137  // Need to use an iterator.
138  ELIST_ITERATOR it(this);
139  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
140  ELIST_LINK *link = it.data();
141  int compare = comparator(&link, &new_link);
142  if (compare > 0) {
143  break;
144  } else if (unique && compare == 0) {
145  return link;
146  }
147  }
148  if (it.cycled_list()) {
149  it.add_to_end(new_link);
150  } else {
151  it.add_before_then_move(new_link);
152  }
153  }
154  return new_link;
155 }
friend class ELIST_ITERATOR
Definition: elst.h:112

◆ assign_to_sublist()

void tesseract::ELIST::assign_to_sublist ( ELIST_ITERATOR start_it,
ELIST_ITERATOR end_it 
)

Definition at line 67 of file elst.cpp.

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

◆ empty()

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

Definition at line 124 of file elst.h.

124  {
125  return !last;
126  }

◆ internal_clear()

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

Definition at line 36 of file elst.cpp.

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

◆ internal_deep_copy()

void tesseract::ELIST::internal_deep_copy ( ELIST_LINK *(*)(ELIST_LINK *)  copier,
const ELIST list 
)

◆ length()

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

Definition at line 146 of file elst.h.

146  {
147  int32_t count = 0;
148  if (last != nullptr) {
149  count = 1;
150  for (auto it = last->next; it != last; it = it->next) {
151  count++;
152  }
153  }
154  return count;
155  }

◆ shallow_copy()

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

Definition at line 132 of file elst.h.

133  { // beware destructors!!
134  last = from_list->last;
135  }

◆ singleton()

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

Definition at line 128 of file elst.h.

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

◆ sort()

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

Definition at line 87 of file elst.cpp.

89  {
90  // Allocate an array of pointers, one per list element.
91  auto count = length();
92 
93  if (count > 0) {
94  // ptr array to sort
95  std::vector<ELIST_LINK *> base;
96  base.reserve(count);
97 
98  ELIST_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: elst.h:146

Friends And Related Function Documentation

◆ ELIST_ITERATOR

friend class ELIST_ITERATOR
friend

Definition at line 112 of file elst.h.


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