Debian Codegraph @

core/templates/sort_list.h

raw symbols SCIP
1 /**************************************************************************/
2 /* sort_list.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/typedefs.h"
34
35 template <typename Element, typename T, T Element::*value, Element *Element::*prev, Element *Element::*next, typename Comparator = Comparator<T>>
36 class SortList {
37 public:
38 Comparator compare;
39
40 void sort(Element *&r_head, Element *&r_tail) {
41 Element *sorted_until;
42 if (_is_sorted(r_head, r_tail, sorted_until)) {
43 return;
44 }
45
46 // In case we're sorting only part of a larger list.
47 Element *head_prev = r_head->*prev;
48 r_head->*prev = nullptr;
49 Element *tail_next = r_tail->*next;
50 r_tail->*next = nullptr;
51
52 // Sort unsorted section and merge.
53 Element *head2 = sorted_until->*next;
54 _split(sorted_until, head2);
55 _merge_sort(head2, r_tail);
56 _merge(r_head, sorted_until, head2, r_tail, r_head, r_tail);
57
58 // Reconnect to larger list if needed.
59 if (head_prev) {
60 _connect(head_prev, r_head);
61 }
62 if (tail_next) {
63 _connect(r_tail, tail_next);
64 }
65 }
66
67 private:
68 bool _is_sorted(Element *p_head, Element *p_tail, Element *&r_sorted_until) {
69 r_sorted_until = p_head;
70 while (r_sorted_until != p_tail) {
71 if (compare(r_sorted_until->*next->*value, r_sorted_until->*value)) {
72 return false;
73 }
74
75 r_sorted_until = r_sorted_until->*next;
76 }
77
78 return true;
79 }
80
81 void _merge_sort(Element *&r_head, Element *&r_tail) {
82 if (r_head == r_tail) {
83 return;
84 }
85
86 Element *tail1 = _get_mid(r_head);
87 Element *head2 = tail1->*next;
88 _split(tail1, head2);
89
90 _merge_sort(r_head, tail1);
91 _merge_sort(head2, r_tail);
92 _merge(r_head, tail1, head2, r_tail, r_head, r_tail);
93 }
94
95 void _merge(
96 Element *p_head1, Element *p_tail1,
97 Element *p_head2, Element *p_tail2,
98 Element *&r_head, Element *&r_tail) {
99 if (compare(p_head2->*value, p_head1->*value)) {
100 r_head = p_head2;
101 p_head2 = p_head2->*next;
102 } else {
103 r_head = p_head1;
104 p_head1 = p_head1->*next;
105 }
106
107 Element *curr = r_head;
108 while (p_head1 && p_head2) {
109 if (compare(p_head2->*value, p_head1->*value)) {
110 _connect(curr, p_head2);
111 p_head2 = p_head2->*next;
112 } else {
113 _connect(curr, p_head1);
114 p_head1 = p_head1->*next;
115 }
116 curr = curr->*next;
117 }
118
119 if (p_head1) {
120 _connect(curr, p_head1);
121 r_tail = p_tail1;
122 } else {
123 _connect(curr, p_head2);
124 r_tail = p_tail2;
125 }
126 }
127
128 Element *_get_mid(Element *p_head) {
129 Element *end = p_head;
130 Element *mid = p_head;
131 while (end->*next && end->*next->*next) {
132 end = end->*next->*next;
133 mid = mid->*next;
134 }
135
136 return mid;
137 }
138
139 _FORCE_INLINE_ void _connect(Element *p_a, Element *p_b) {
140 p_a->*next = p_b;
141 p_b->*prev = p_a;
142 }
143
144 _FORCE_INLINE_ void _split(Element *p_a, Element *p_b) {
145 p_a->*next = nullptr;
146 p_b->*prev = nullptr;
147 }
148 };