| 1 | /* DoublyLinkedList.cpp |
| 2 | * |
| 3 | * Copyright (C) 2011-2019 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "DoublyLinkedList.h"cxx . . $ `<file>/dwsys/DoublyLinkedList.h`/ |
| 20 | |
| 21 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (DoublyLinkedNode, Daata, 0); |
| 22 | |
| 23 | void structDoublyLinkedNodecxx . . $ structDoublyLinkedNode# :: v1_copycxx . . $ structDoublyLinkedNode#v1_copy(42659c2335f2eda7). (Daatacxx . . $ Daata# thee_Daata) const { |
| 24 | DoublyLinkedNodecxx . . $ DoublyLinkedNode# thee = static_cast <DoublyLinkedNodecxx . . $ DoublyLinkedNode#> (thee_Daata); |
| 25 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structDoublyLinkedNode#data. =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (ourcxx . . $ `melder/melder.h:42:9`! datacxx . . $ structDoublyLinkedNode#data..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 26 | } |
| 27 | |
| 28 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (DoublyLinkedList, Thing, 0); |
| 29 | |
| 30 | void structDoublyLinkedListcxx . . $ structDoublyLinkedList# :: v9_destroycxx . . $ structDoublyLinkedList#v9_destroy(ced63f7c635d850d). () noexcept { |
| 31 | DoublyLinkedNodecxx . . $ DoublyLinkedNode# v = frontcxx . . $ structDoublyLinkedList#front.; |
| 32 | while (v) { |
| 33 | DoublyLinkedNodecxx . . $ DoublyLinkedNode# cur = v; |
| 34 | v = v -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 35 | forgetcxx . . $ `sys/Thing.h:158:9`! (cur); |
| 36 | } |
| 37 | DoublyLinkedList_Parentcxx . . $ DoublyLinkedList_Parent# :: v9_destroycxx . . $ structThing#v9_destroy(ced63f7c635d850d). (); |
| 38 | } |
| 39 | |
| 40 | int structDoublyLinkedListcxx . . $ structDoublyLinkedList# :: s_compareHookcxx . . $ structDoublyLinkedList#s_compareHook(6db7cd0ff86649e8). (Daatacxx . . $ Daata# /* node1 */, Daatacxx . . $ Daata# /* node2 */) noexcept { |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | autoDoublyLinkedNodecxx . . $ autoDoublyLinkedNode# DoublyLinkedNode_createcxx . . $ DoublyLinkedNode_create(d0f338d5367a7b54). (autoDaatacxx . . $ autoDaata# data) { |
| 45 | autoDoublyLinkedNodecxx . . $ autoDoublyLinkedNode# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (DoublyLinkedNode); |
| 46 | mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structDoublyLinkedNode#data. =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). data.movecxx . . $ autoSomeThing#move(25acc3554b7d1642).(); |
| 47 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 48 | } |
| 49 | |
| 50 | void DoublyLinkedList_initcxx . . $ DoublyLinkedList_init(d44afe7b578c9278). (DoublyLinkedListcxx . . $ DoublyLinkedList#) { |
| 51 | } |
| 52 | |
| 53 | autoDoublyLinkedListcxx . . $ autoDoublyLinkedList# DoublyLinkedList_createcxx . . $ DoublyLinkedList_create(d35e9a1b7ea31d63).() { |
| 54 | try { |
| 55 | autoDoublyLinkedListcxx . . $ autoDoublyLinkedList# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (DoublyLinkedList); |
| 56 | DoublyLinkedList_initcxx . . $ DoublyLinkedList_init(d44afe7b578c9278). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 57 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 58 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 59 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"DoublyLinkedList not created."); |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | |
| 64 | void DoublyLinkedList_addFrontcxx . . $ DoublyLinkedList_addFront(a2c897d9bfcc8e6a). (DoublyLinkedListcxx . . $ DoublyLinkedList# me, DoublyLinkedNodecxx . . $ DoublyLinkedNode# node) { |
| 65 | if (mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front.) { |
| 66 | DoublyLinkedList_addBeforecxx . . $ DoublyLinkedList_addBefore(df52792679f85443). (me, mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front., node); |
| 67 | } else { // empty list |
| 68 | mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front. = node; |
| 69 | mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back. = node; |
| 70 | node -> nextcxx . . $ structDoublyLinkedNode#next. = nullptr; |
| 71 | node -> prevcxx . . $ structDoublyLinkedNode#prev. = nullptr; |
| 72 | mycxx . . $ `melder/melder.h:44:9`! numberOfNodescxx . . $ structDoublyLinkedList#numberOfNodes. ++; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void DoublyLinkedList_addBackcxx . . $ DoublyLinkedList_addBack(a2c897d9bfcc8e6a). (DoublyLinkedListcxx . . $ DoublyLinkedList# me, DoublyLinkedNodecxx . . $ DoublyLinkedNode# node) { |
| 77 | if (mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back.) |
| 78 | DoublyLinkedList_addAftercxx . . $ DoublyLinkedList_addAfter(df52792679f85443). (me, mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back., node); |
| 79 | else |
| 80 | DoublyLinkedList_addFrontcxx . . $ DoublyLinkedList_addFront(a2c897d9bfcc8e6a). (me, node); // empty list |
| 81 | } |
| 82 | |
| 83 | void DoublyLinkedList_addBeforecxx . . $ DoublyLinkedList_addBefore(df52792679f85443). (DoublyLinkedListcxx . . $ DoublyLinkedList# me, DoublyLinkedNodecxx . . $ DoublyLinkedNode# pos, DoublyLinkedNodecxx . . $ DoublyLinkedNode# node) { |
| 84 | node -> prevcxx . . $ structDoublyLinkedNode#prev. = pos -> prevcxx . . $ structDoublyLinkedNode#prev.; |
| 85 | node -> nextcxx . . $ structDoublyLinkedNode#next. = pos; |
| 86 | if (pos -> prevcxx . . $ structDoublyLinkedNode#prev. == nullptr) |
| 87 | mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front. = node; |
| 88 | else |
| 89 | pos -> prevcxx . . $ structDoublyLinkedNode#prev. -> nextcxx . . $ structDoublyLinkedNode#next. = node; |
| 90 | pos -> prevcxx . . $ structDoublyLinkedNode#prev. = node; |
| 91 | mycxx . . $ `melder/melder.h:44:9`! numberOfNodescxx . . $ structDoublyLinkedList#numberOfNodes. ++; |
| 92 | } |
| 93 | |
| 94 | void DoublyLinkedList_addAftercxx . . $ DoublyLinkedList_addAfter(df52792679f85443). (DoublyLinkedListcxx . . $ DoublyLinkedList# me, DoublyLinkedNodecxx . . $ DoublyLinkedNode# pos, DoublyLinkedNodecxx . . $ DoublyLinkedNode# node) { |
| 95 | node -> prevcxx . . $ structDoublyLinkedNode#prev. = pos; |
| 96 | node -> nextcxx . . $ structDoublyLinkedNode#next. = pos -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 97 | if (pos -> nextcxx . . $ structDoublyLinkedNode#next. == nullptr) |
| 98 | mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back. = node; |
| 99 | else |
| 100 | pos -> nextcxx . . $ structDoublyLinkedNode#next. -> prevcxx . . $ structDoublyLinkedNode#prev. = node; |
| 101 | pos -> nextcxx . . $ structDoublyLinkedNode#next. = node; |
| 102 | mycxx . . $ `melder/melder.h:44:9`! numberOfNodescxx . . $ structDoublyLinkedList#numberOfNodes. ++; |
| 103 | } |
| 104 | |
| 105 | void DoublyLinkedList_removecxx . . $ DoublyLinkedList_remove(a2c897d9bfcc8e6a). (DoublyLinkedListcxx . . $ DoublyLinkedList# me, DoublyLinkedNodecxx . . $ DoublyLinkedNode# node) { |
| 106 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfNodescxx . . $ structDoublyLinkedList#numberOfNodes. == 0) |
| 107 | return; |
| 108 | if (node == mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front.) { |
| 109 | mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front. = mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front. -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 110 | mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front. -> prevcxx . . $ structDoublyLinkedNode#prev. = nullptr; |
| 111 | } else if (node == mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back.) { |
| 112 | mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back. = mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back. -> prevcxx . . $ structDoublyLinkedNode#prev.; |
| 113 | mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back. -> nextcxx . . $ structDoublyLinkedNode#next. = nullptr; |
| 114 | } else { |
| 115 | node -> prevcxx . . $ structDoublyLinkedNode#prev. -> nextcxx . . $ structDoublyLinkedNode#next. = node -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 116 | node -> nextcxx . . $ structDoublyLinkedNode#next. -> prevcxx . . $ structDoublyLinkedNode#prev. = node -> prevcxx . . $ structDoublyLinkedNode#prev.; |
| 117 | } |
| 118 | forgetcxx . . $ `sys/Thing.h:158:9`! (node); |
| 119 | mycxx . . $ `melder/melder.h:44:9`! numberOfNodescxx . . $ structDoublyLinkedList#numberOfNodes. --; |
| 120 | } |
| 121 | |
| 122 | // Preconditions: |
| 123 | // from and to should be part of the list |
| 124 | // from must occur before to |
| 125 | void DoublyLinkedList_sortPartcxx . . $ DoublyLinkedList_sortPart(df52792679f85443). (DoublyLinkedListcxx . . $ DoublyLinkedList# me, DoublyLinkedNodecxx . . $ DoublyLinkedNode# from, DoublyLinkedNodecxx . . $ DoublyLinkedNode# to) { |
| 126 | // Save data |
| 127 | if (from == to) |
| 128 | return; // nothing to do |
| 129 | |
| 130 | const DoublyLinkedNodecxx . . $ DoublyLinkedNode# from_prev = from -> prevcxx . . $ structDoublyLinkedNode#prev.; |
| 131 | const DoublyLinkedNodecxx . . $ DoublyLinkedNode# to_next = to -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 132 | const DoublyLinkedNodecxx . . $ DoublyLinkedNode# my_front = mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front.; |
| 133 | const DoublyLinkedNodecxx . . $ DoublyLinkedNode# my_back = mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back.; |
| 134 | |
| 135 | from -> prevcxx . . $ structDoublyLinkedNode#prev. = to -> nextcxx . . $ structDoublyLinkedNode#next. = nullptr; |
| 136 | mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front. = from; |
| 137 | mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back. = to; |
| 138 | DoublyLinkedList_sortcxx . . $ DoublyLinkedList_sort(d44afe7b578c9278). (me); |
| 139 | // restore complete list |
| 140 | mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front. -> prevcxx . . $ structDoublyLinkedNode#prev. = from_prev; |
| 141 | if (from_prev) |
| 142 | from_prev -> nextcxx . . $ structDoublyLinkedNode#next. = mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front.; |
| 143 | |
| 144 | mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back. -> nextcxx . . $ structDoublyLinkedNode#next. = to_next; |
| 145 | if (to_next) |
| 146 | to_next -> prevcxx . . $ structDoublyLinkedNode#prev. = mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back.; |
| 147 | |
| 148 | if (my_front != from) |
| 149 | mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front. = my_front; |
| 150 | |
| 151 | if (my_back != to) |
| 152 | mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back. = my_back; |
| 153 | |
| 154 | } |
| 155 | |
| 156 | void DoublyLinkedList_sortcxx . . $ DoublyLinkedList_sort(d44afe7b578c9278). (DoublyLinkedListcxx . . $ DoublyLinkedList# me) { |
| 157 | Data_CompareHookcxx . . $ Data_CompareHook#::FunctionTypecxx . . $ MelderCompareHook#FunctionType# compare = mycxx . . $ `melder/melder.h:44:9`! v_getCompareHookcxx . . $ structDoublyLinkedList#v_getCompareHook(595dba4c91681740). ().getcxx . . $ MelderCompareHook#get(b7681fab4ac30f5a).(); |
| 158 | integercxx . . $ integer# increment = 1; |
| 159 | DoublyLinkedNodecxx . . $ DoublyLinkedNode# front = mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front., back; |
| 160 | for (;;) { |
| 161 | DoublyLinkedNodecxx . . $ DoublyLinkedNode# node1 = front; |
| 162 | front = nullptr; |
| 163 | back = nullptr; |
| 164 | |
| 165 | integercxx . . $ integer# numberOfMerges = 0; |
| 166 | |
| 167 | while (node1) { |
| 168 | DoublyLinkedNodecxx . . $ DoublyLinkedNode# node2 = node1, node; |
| 169 | integercxx . . $ integer# node1size = 0; |
| 170 | numberOfMerges ++; |
| 171 | |
| 172 | for (integercxx . . $ integer# i = 1; i <= increment; i++) { |
| 173 | node1size ++; |
| 174 | node2 = node2 -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 175 | if (! node2) |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | integercxx . . $ integer# node2size = increment; |
| 180 | |
| 181 | while (node1size > 0 || (node2size > 0 && node2)) { // merge node1 and node2 |
| 182 | if (node1size == 0) { |
| 183 | node2size --; |
| 184 | node = node2; |
| 185 | node2 = node2 -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 186 | } else if (node2size == 0 || ! node2) { |
| 187 | node1size --; |
| 188 | node = node1; |
| 189 | node1 = node1 -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 190 | } else if (compare (node1, node2) <= 0) { |
| 191 | node1size --; |
| 192 | node = node1; |
| 193 | node1 = node1 -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 194 | } else { |
| 195 | node2size --; |
| 196 | node = node2; |
| 197 | node2 = node2 -> nextcxx . . $ structDoublyLinkedNode#next.; |
| 198 | } |
| 199 | |
| 200 | if (back) |
| 201 | back -> nextcxx . . $ structDoublyLinkedNode#next. = node; |
| 202 | else |
| 203 | front = node; |
| 204 | |
| 205 | node -> prevcxx . . $ structDoublyLinkedNode#prev. = back; |
| 206 | back = node; |
| 207 | } |
| 208 | node1 = node2; |
| 209 | } |
| 210 | back -> nextcxx . . $ structDoublyLinkedNode#next. = nullptr; |
| 211 | if (numberOfMerges <= 1) |
| 212 | break; |
| 213 | |
| 214 | increment *= 2; |
| 215 | } |
| 216 | // |
| 217 | mycxx . . $ `melder/melder.h:44:9`! frontcxx . . $ structDoublyLinkedList#front. = front; |
| 218 | mycxx . . $ `melder/melder.h:44:9`! backcxx . . $ structDoublyLinkedList#back. = back; |
| 219 | } |
| 220 | |
| 221 | /* end of file DoublyLinkedList.cpp */ |