38 void (*zapper)(
void *)) {
71 constexpr
ERRCODE LIST_NOT_EMPTY(
"Destination list must be empty before extracting a sublist");
74 LIST_NOT_EMPTY.error(
"ELIST2.assign_to_sublist",
ABORT,
nullptr);
77 last = start_it->extract_sublist(end_it);
90 const void *,
const void *)) {
95 std::vector<ELIST2_LINK *> base;
106 qsort(&base[0], count,
sizeof(base[0]), comparator);
109 for (
auto current : base) {
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;
127 new_link->next = last->next;
128 new_link->prev = last;
129 last->next = new_link;
130 new_link->next->prev = new_link;
138 if (comparator(&link, &new_link) > 0) {
174 started_cycling =
true;
176 current = current->next;
178 if (ex_current_was_cycle_pt) {
189 next = current->next;
219 started_cycling =
true;
221 current = current->prev;
223 if (ex_current_was_cycle_pt) {
237 prev = current->prev;
260 for (ptr = current ? current : next; offset++ < 0; ptr = ptr->prev) {
264 for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next) {
289 constexpr
ERRCODE DONT_EXCHANGE_DELETED(
"Can't exchange deleted elements of lists");
298 if (!(other_it->list))
305 if ((list->
empty()) || (other_it->list->
empty()) || (current == other_it->current)) {
311 if (!current || !other_it->current) {
312 DONT_EXCHANGE_DELETED.error(
"ELIST2_ITERATOR.exchange",
ABORT,
nullptr);
320 if ((next == other_it->current) || (other_it->next == current)) {
322 if ((next == other_it->current) && (other_it->next == current)) {
323 prev = next = current;
324 other_it->prev = other_it->next = other_it->current;
328 if (other_it->next == current) {
329 other_it->prev->next = current;
330 other_it->current->next = next;
331 other_it->current->prev = current;
332 current->next = other_it->current;
333 current->prev = other_it->prev;
334 next->prev = other_it->current;
336 other_it->next = other_it->current;
339 prev->next = other_it->current;
340 current->next = other_it->next;
341 current->prev = other_it->current;
342 other_it->current->next = current;
343 other_it->current->prev = prev;
344 other_it->next->prev = current;
347 other_it->prev = other_it->current;
351 prev->next = other_it->current;
352 current->next = other_it->next;
353 current->prev = other_it->prev;
354 next->prev = other_it->current;
355 other_it->prev->next = current;
356 other_it->current->next = next;
357 other_it->current->prev = prev;
358 other_it->next->prev = current;
364 if (list->last == current) {
365 list->last = other_it->current;
367 if (other_it->list->last == other_it->current) {
368 other_it->list->last = current;
371 if (current == cycle_pt) {
372 cycle_pt = other_it->cycle_pt;
374 if (other_it->current == other_it->cycle_pt) {
375 other_it->cycle_pt = cycle_pt;
380 old_current = current;
381 current = other_it->current;
382 other_it->current = old_current;
398 constexpr
ERRCODE BAD_EXTRACTION_PTS(
"Can't extract sublist from points on different lists");
399 constexpr
ERRCODE DONT_EXTRACT_DELETED(
"Can't extract a sublist marked by deleted points");
401 constexpr
ERRCODE BAD_SUBLIST(
"Can't find sublist end point in original list");
411 if (list != other_it->list)
412 BAD_EXTRACTION_PTS.error(
"ELIST2_ITERATOR.extract_sublist",
ABORT,
nullptr);
416 if (!current || !other_it->current)
417 DONT_EXTRACT_DELETED.error(
"ELIST2_ITERATOR.extract_sublist",
ABORT,
nullptr);
420 ex_current_was_last = other_it->ex_current_was_last =
false;
421 ex_current_was_cycle_pt =
false;
422 other_it->ex_current_was_cycle_pt =
false;
427 BAD_SUBLIST.error(
"ELIST2_ITERATOR.extract_sublist",
ABORT,
nullptr);
432 ex_current_was_last = other_it->ex_current_was_last =
true;
435 if (temp_it.current == cycle_pt) {
436 ex_current_was_cycle_pt =
true;
439 if (temp_it.current == other_it->cycle_pt) {
440 other_it->ex_current_was_cycle_pt =
true;
446 while (temp_it.prev != other_it->current);
449 other_it->current->next = current;
451 current->prev = other_it->current;
452 end_of_new_list = other_it->current;
455 if (prev == other_it->current) {
456 list->last =
nullptr;
457 prev = current = next =
nullptr;
458 other_it->prev = other_it->current = other_it->next =
nullptr;
460 prev->next = other_it->next;
461 other_it->next->prev = prev;
463 current = other_it->current =
nullptr;
464 next = other_it->next;
465 other_it->prev = prev;
467 return end_of_new_list;
constexpr ERRCODE BAD_PARAMETER("List parameter error")
constexpr ERRCODE NULL_PREV("Previous element on the list is nullptr")
constexpr ERRCODE NO_LIST("Iterator not set to a list")
constexpr ERRCODE NULL_NEXT("Next element on the list is nullptr")
constexpr ERRCODE EMPTY_LIST("List is empty")
constexpr ERRCODE NULL_DATA("List would have returned a nullptr data pointer")
void sort(int comparator(const void *, const void *))
void assign_to_sublist(ELIST2_ITERATOR *start_it, ELIST2_ITERATOR *end_it)
void internal_clear(void(*zapper)(void *))
void add_sorted(int comparator(const void *, const void *), ELIST2_LINK *new_link)
void exchange(ELIST2_ITERATOR *other_it)
void add_before_then_move(ELIST2_LINK *new_link)
void add_to_end(ELIST2_LINK *new_link)
ELIST2_LINK * data_relative(int8_t offset)
void error(const char *caller, TessErrorLogCode action, const char *format,...) const __attribute__((format(printf