rhp.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867
  1. /*
  2. * Copyright 2021
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * SPDX-License-Identifier: GPL-3.0+
  18. * License-Filename: LICENSE
  19. */
  20. /* updated: it now allows empty levels, see if(0) { rhp_log() } */
  21. /* modified from GNU GCC versions splay tree database */
  22. /* A splay-tree datatype.
  23. Copyright (C) 1998 Free Software Foundation, Inc.
  24. Contributed by Mark Mitchell
  25. This file is part of GNU CC.
  26. GNU CC is free software; you can redistribute it and/or modify it
  27. under the terms of the GNU General Public License as published by
  28. the Free Software Foundation; either version 2, or (at your option)
  29. any later version.
  30. GNU CC is distributed in the hope that it will be useful, but
  31. WITHOUT ANY WARRANTY; without even the implied warranty of
  32. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. General Public License for more details.
  34. You should have received a copy of the GNU General Public License
  35. along with GNU CC; see the file COPYING. If not, write to
  36. the Free Software Foundation, 59 Temple Place - Suite 330,
  37. Boston, MA 02111-1307, USA. */
  38. /* For an easily readable description of splay-trees, see:
  39. Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
  40. Algorithms. Harper-Collins, Inc. 1991.
  41. verified it is no problem to run this splay tree nested
  42. inside another splay tree, nested inside a splay tree etc.
  43. splay is fast because it is based on the real-world observation
  44. that a search in data is done with a key the next search is often
  45. with a similar key value so splay changes root of the tree everytime.
  46. */
  47. /*
  48. * relative horizontal positioning:
  49. * based on GNU GPL source from Matthias Stallman
  50. * api is described in rhp.h
  51. * demo program how to use is in mainrhp.c
  52. * Copyright (C) 2008, 2011 Matthias Stallmann, Saurabh Gupta.
  53. * See also https://github.com/mfms-ncsu
  54. * "Crossing Minimization in K-layer Graphs"
  55. * Saurabh Gupta 2008 - 74 pagina's
  56. * This source can be "compiled" to javascript using llvm emscripten
  57. * and that resuls in working running javascript. tested and verified.
  58. */
  59. /*
  60. * core edge crossing reduction graph barycenter in c in a single file.
  61. * this is a barycenter algo as in graph layout not as in astronomy.
  62. * This rhp relative-horizontal-positioning is the part which uses
  63. * temporary so much extra memory to save a copy of the graph positions
  64. * and so much cpu usage to try different graph layout configurations.
  65. * This rhp is a new experimental idea to do it this way which has few
  66. * benefits from other ways how it is done and currently as simple as
  67. * possible as a start, so it has toedoe to improve it.
  68. *
  69. * limits:
  70. * because it depends on datatype (signed int) the maximum value for
  71. * number of nodes/edges/levels is for each (2^31 - 1) is 2147483647
  72. * the number of edge crossings in the graph depends on the datatype
  73. * (signed long int) maximum of (2^63 -1) that is 9223372036854775807
  74. *
  75. * This is approx. 3000 lines text and 2000 lines program source code.
  76. * The global routines available are in rhp.h
  77. * All routines are static and this needs only the c-lib functions:
  78. * fclose, fflush, fopen, free, malloc, qsort, stderr/stdout, vfprintf
  79. *
  80. * The global routines are pedantic about the data supplied.
  81. * The input are nodes+edges in vertical relative levels
  82. * and a pointer to a struct with more data.
  83. * At top of drawing must be level 0 and downward the other
  84. * levels n. The edges must point downward and exactly span
  85. * one (1) level, that is the to-node must be one level higher
  86. * then the from node. The graph can have zero edges but not
  87. * zero nodes. In every level is a node supposed to be normally.
  88. * First init() must be done, then filling the nodes and then
  89. * the edges, after that running the layout() routine and using
  90. * node_foreach() with a callback() function the relative
  91. * x position of the nodes within the level are available.
  92. * When that is done the data is not needed anymore and with
  93. * a deinit() routine all memory is free()'ed and there are
  94. * no memory leaks.
  95. *
  96. * When init() has a filename supplied then a log file is written
  97. * to that file and errors are printed with a '!' also on stderr.
  98. *
  99. * To fprintf() 64bits in c99 mode inttypes.h is used with these defs:
  100. * #define PRIu64 "I64u" // unsigned int
  101. * #define PRIi64 "I64i" // signed int
  102. * #define PRIx64 "I64x" // hexadecimal
  103. * This seems to be the preferred portable way to print 64bits ints.
  104. * This has a internal splay database with the nodes+edges data.
  105. *
  106. This algorithm is for routing hierarchies of elements. A "good route" is
  107. one that has a minimum number of link crossings. An algorithm that was
  108. truly optimal (for minimizing link crossings) would be combinatorial and
  109. therefore cost prohibitive; therefore, this algorithm uses a heuristic
  110. approach that finds a route with close to the minimum number of crossings
  111. in a reasonable amount of time.
  112. This algorithm assumes that all the elements form a directed acyclic graph
  113. (DAG), which means (1) that links flow one way between elements and (2) for
  114. any given node there is no way to get back to the node if, starting at the
  115. node, you traverse the links going from node to node. This algorithm also
  116. assumes that AT MOST only ONE link may exist between a pair of nodes.
  117. -------------------------------------------------------------------------------
  118. OVERVIEW OF ALGORITHM
  119. All elements that do not have any parents are placed in the first row (row 0).
  120. Elements are assigned to rows, where the row number for each child is equal to
  121. the [maximum(row number of all its parents) + 1]. Crossovers are determined
  122. by examining links between elements on adjacent rows, so if a parent is in a
  123. row that is not adjacent to its child's row, "dummy" nodes are created on the
  124. rows in between the parent and child, and the parent and child are connected
  125. via these dummy nodes.
  126. Once the elements (now called nodes) are assigned to individual rows, the
  127. rows are sorted (repeatedly) in order to minimize link crossings. The
  128. sort criteria involves attempting to both center children under parents and
  129. to center parents over children. The sort orders are then tweaked by swapping
  130. nodes that have the same sort value.
  131. After the column orders are finalized, the nodes are spread out so they are
  132. more or less centered above their children and below their parents. When
  133. centering children below parents, a row of children is sorted by which node
  134. has the greatest number of parents. These get first choice of where to be
  135. placed under the parents (actually, dummy nodes get first preference, then
  136. all of the others). Centering parents above children is analogous.
  137. When done with node placement, there may be some empty columns, and the
  138. numbering scheme may not start at 0. Therefore, the empty columns must
  139. be eliminated and every node needs its column renumbered, starting at 0.
  140. Then you are done.
  141. -------------------------------------------------------------------------------
  142. REALIZATION MATRIX
  143. When it comes to both sorting nodes and horizontally spacing the nodes, two
  144. adjacent rows are always involved. For example, if we are sorting row[i]
  145. based on the children of row[i]'s nodes, then row[i+1] is also involved
  146. at this step. These two rows are called the "i-th realization", and form
  147. the "i-th realization matrix". A realization matrix shows the parent-child
  148. relationships between adjacent rows, with the parents on the rows and the
  149. children on the columns. If there is a parent-child relationship, a 1 is
  150. stored in the matrix at the position, else a 0 is stored.
  151. An example:
  152. A B C D
  153. \ \ / \ / / |
  154. \ /\ / \ / |
  155. /\ / \ / \ |
  156. / /\ /\ \ |
  157. // \ / \ \|
  158. E F G H
  159. E F G H
  160. A 0 1 1 0 In this example, parent 'A' has children 'F' and 'G',
  161. B 1 0 0 1 parent 'B' has children 'E' and 'H',
  162. C 1 1 0 0 parent 'C' has children 'E' and 'F',
  163. D 0 0 0 1 and parent 'D' has child 'H'.
  164. -------------------------------------------------------------------------------
  165. ROW AND COLUMN BARYCENTERS
  166. Two other important concepts are the "row barycenter" and the "column
  167. barycenter" for a node. The "row barycenter" is the basically the average
  168. of the positions of a node's children. The "column barycenter" is the average
  169. of the positions of a node's parents. These numbers tell us where a node
  170. would like to be positioned in its row, depending whether we are positioning
  171. relative to children or parents.
  172. For example, using the above realization matrix, we can calculate the row
  173. barycenters for A, B, C, and D, and the column barycenters for E, F, G, and H.
  174. Since the row barycenter of a node is equal to the sum of the positions of
  175. the node's children divided by the number of children of the node, the row
  176. barycenter for A is (1 + 2)/2 = 1.5. This assumes that we start numbering
  177. rows and columns at 0. Similarly, the column barycenter of a node is equal
  178. to the sum of the positions of the node's parents divided by the number of
  179. parents of the node. So, the column barycenter of F is (0 + 2)/2 = 1.0.
  180. The complete example is as follows:
  181. Row
  182. | E F G H | Barys
  183. ------+--------------------+-----
  184. A | 0 1 1 0 | 1.5
  185. B | 1 0 0 1 | 1.5
  186. C | 1 1 0 0 | 0.5
  187. D | 0 0 0 1 | 3.0
  188. ------+--------------------+-----
  189. Col | 1.5 1.0 0.0 2.0 |
  190. Barys | |
  191. If we were to sort the child nodes here by their column barycenters, the new
  192. order would be G, F, E, H. If we were to sort the parent nodes here by their
  193. row barycenters, then the order would be C, A, B, D (if two or more nodes have
  194. the same value, be sure to keep the same precedence that already exists
  195. between them, e.g., make sure that order after sorting is not C, B, A, D).
  196. If a node has no parents then it can't have a column barycenter. This case
  197. should never happen, as all nodes that have no parents should be in the first
  198. level of the hierarchy, and these nodes would only be represented in
  199. realization matrix 0, and they would only have row barycenters.
  200. If a node has no children then it can't have a row barycenter. In this case,
  201. while sorting based on row barycenters, sort AROUND these nodes, i.e., do
  202. not change their positions at all. For example, if we had the following:
  203. Row
  204. | W X Y Z | Barys
  205. ------+--------------------+-----
  206. Q | 0 1 1 1 | 2.0
  207. R | 0 0 0 0 | ???
  208. S | 1 0 0 0 | 0.0
  209. T | 0 1 0 1 | 2.0 ,
  210. ------+--------------------+-----
  211. Col | 2.0 1.5 0.0 1.5 |
  212. Barys | |
  213. and we were to sort by row barycenters, the resulting order should be S, R,
  214. Q, T. Notice how R stayed in its position, and even though Q and T had the
  215. same barycentric value, Q stayed before T.
  216. The whole reason for sorting rows and columns by their barycenters is to
  217. decrease the number of crossovers.
  218. -------------------------------------------------------------------------------
  219. CROSSOVERS
  220. The realization matrix is also used to count the number of crossovers between
  221. two adjacent rows of nodes. For each row, starting with the second row, if
  222. a row element has a 1, then sum up all of the matrix elements that are above
  223. AND to the right of this element. Looking again at the first example:
  224. A B C D
  225. \ \ / \ / / |
  226. \ /\ / \ / |
  227. /\ / \ / \ |
  228. / /\ /\ \ |
  229. // \ / \ \|
  230. E F G H
  231. Row
  232. | E F G H | Barys
  233. ------+--------------------+-----
  234. A | 0 1 1 0 | 1.5
  235. B | 1 0 0 1 | 1.5
  236. C | 1 1 0 0 | 0.5
  237. D | 0 0 0 1 | 3.0
  238. ------+--------------------+-----
  239. Col | 1.5 1.0 0.0 2.0 |
  240. Barys | |
  241. Starting with the second row (parent B's row), position B-E has a 1. Looking
  242. at positions above and to the right, we see positions A-F and A-G both have
  243. a 1, so the number of crossovers is currently = 2. Position B-H has a 1, but
  244. there are no elements above and to the right, so crossovers is still = 2.
  245. For parent row of C, position C-E crosses over with B-H, A-F, and A-G, so
  246. crossovers = 5. C-F crosses over with B-H and A-G, so crossovers = 7. For
  247. parent row D, position D-H doesn't cross over with any other link. So for
  248. this realization matrix representing these two rows, the number of crossovers
  249. is 7.
  250. The total number of crossovers for the whole graph would be the sum of the
  251. crossovers from each matrix realization.
  252. -------------------------------------------------------------------------------
  253. NODE CENTERING
  254. After the nodes for each row have their final sort order, the nodes need to
  255. be assigned to grid positions. Their initial grid position will be their
  256. column position, by which we mean their array position in the row. From now
  257. on, when we take a row or column barycenter, we will be using grid positions
  258. instead of column positions.
  259. Note: The following examples will be based on centering children with respect
  260. to their parents' positions. Centering parents based on their children's
  261. positions is analogous.
  262. When positioning the nodes on a row based on their parents' positions, the
  263. nodes must be initially sorted to see which nodes get first choice. The dummy
  264. nodes go first, and the rest of nodes are sorted in descending order based on
  265. the number of parents the node has. If a dummy node has a parent that has
  266. multiple dummy nodes, all of these dummy nodes are again sorted by how close
  267. to the center of the parent's children they are. This is a confusing
  268. statement, best illustrated by example:
  269. P1 P2
  270. \ |
  271. \ __________^__________
  272. \| | | | |
  273. C1 D1 D2 C2 D3
  274. Here, parent P1 has one child, C1. Parent P2 has five children, and three of
  275. the child nodes are dummy nodes: D1, D2, and D3. C1 is child 0 of P2, D1 is
  276. child 1 of P2, D2 is child 2 of P2, C2 is child 3 of P2, and D3 is child 4 of
  277. P2. The child midpoint underneath the parent is equal to
  278. (the number of children - 1) / 2, so (5 - 1) / 2 = 2. Since the dummy nodes
  279. go first, the initial order is D1, D2, D3, C1 (because it has 2 parents), and
  280. finally C2. All of the dummy nodes have the same parent, so we will sort them
  281. based on how far away they are from the parent's child midpoint. D1 is child
  282. 1 of P2, so it is 1 away. D2 is child 2 of P2, so it is 0 away. D3 is child
  283. 4 of P2, so it is 2 away. Therefore, the final order for choosing positions
  284. is D2, D1, D3, C1, C2.
  285. In a situation similar to the dummy nodes, if a non-dummy node has a only one
  286. parent, and that parent has other children with just one parent, then these
  287. one parent child nodes that have the same parent need additional sorting in
  288. the exact same manner that we just did the dummy nodes.
  289. The whole purpose behind this is so that the left most node doesn't get first
  290. choice. If it did, we would get graphs that look like:
  291. A A
  292. | |
  293. |_________ instead of _____^_____
  294. | | | | | |
  295. B C D B C D
  296. Anyway, once we have a sort order for the nodes of a row, we place the nodes
  297. in their preferred positions. Using the previous example, assume that P1
  298. is in grid position 2 and P2 is in grid position 5. D2 gets first choice,
  299. and its column barycenter (based now on parent grid positions, not column
  300. positions) is 5, so we place D2 in position 5. D1 is next, its barycenter
  301. is also 5. We can't give it 5 since that position is occupied, so we give
  302. it the closest possible position we can, which in this case is 4. D3 is next,
  303. and its barycenter is also 5. The closest position that we can give it is
  304. position 7, since we must allow room for C2. C1 is next, and its barycenter
  305. is (2 + 5)/2 = 3.5, which we round to 3. Position 3 is open, so we go ahead
  306. and give it position 3. C2 is last, and its barycenter is 5. However, the
  307. first position available to it based on its left neighbor is position 6, so
  308. we assign it position 6.
  309. -------------------------------------------------------------------------------
  310. GOING 'UP' OR 'DOWN' THE GRAPH
  311. "Going down the graph" means taking each realization matrix situation,
  312. starting with Realization Matrix 0, and performing some action on it, then
  313. going to the next realization matrix, and continuing until all of the
  314. realization matrices have been visited.
  315. "Going up the graph" is analogous, except you start at the bottom with the
  316. last realization matrix and work up to Realization Matrix 0.
  317. */
  318. /* config.h must have set HAVE_INTTYPES_H */
  319. #include "config.h"
  320. #ifndef HAVE_INTTYPES_H
  321. #warning "this file needs inttypes.h for PRIi64 printf macro's and c99 mode with -std=c99 option"
  322. #endif
  323. #include <stdio.h>
  324. #include <stdlib.h>
  325. #include <stdarg.h>
  326. #include <string.h>
  327. #include <stdint.h>
  328. #include <inttypes.h> /* this needs c99 mode with gcc -std=c99 */
  329. #include <stddef.h> /* for ptrdiff_t etc. */
  330. #include "splay-tree.h"
  331. #include "rhp.h"
  332. #include "dpmem.h"
  333. /* main place tp wrap malloc/free */
  334. static void *mymalloc(size_t n, char *f, int l)
  335. {
  336. if (f) {
  337. }
  338. if (l) {
  339. }
  340. return (dp_calloc(1, n));
  341. }
  342. static void myfree(void *ptr, char *f, int l)
  343. {
  344. void *ptr2 = NULL;
  345. if (f) {
  346. }
  347. if (l) {
  348. }
  349. if (ptr) {
  350. ptr2 = dp_free(ptr);
  351. if (ptr2) {
  352. }
  353. }
  354. return;
  355. }
  356. /* struct def forward decl */
  357. struct rhpedge;
  358. /* int64_t or ptrdiff_t or long long int (64-bits) */
  359. /* at 32bits compilation this create warning different size from pointer to int but not problem */
  360. typedef long long int rhp_spkey;
  361. typedef long long int rhp_spval;
  362. /* */
  363. struct rhp_spn {
  364. rhp_spkey key;
  365. rhp_spval value;
  366. struct rhp_spn *l;
  367. struct rhp_spn *r;
  368. };
  369. /* */
  370. struct rhp_sp {
  371. struct rhp_spn *root;
  372. int delval; /* if set delete the value with a free() call */
  373. };
  374. /* node info */
  375. struct rhpnode {
  376. int num; /* internal number */
  377. int innum; /* number as supplied */
  378. int level; /* relative level of node */
  379. void *data; /* extra data as supplied */
  380. int position; /* relative x position within level */
  381. int up_degree; /* number of edges connecting to this node */
  382. int down_degree; /* number of edges connecting to this node */
  383. struct rhpedge **up_edges; /* edges connecting to this node */
  384. struct rhpedge **down_edges; /* edges connecting to this node */
  385. int weight; /* weight of a node determines position */
  386. int64_t up_crossings; /* */
  387. int64_t down_crossings; /* */
  388. };
  389. /* edge info */
  390. struct rhpedge {
  391. int num; /* internal number */
  392. int innum; /* number as supplied */
  393. struct rhpnode *fn; /* from node number */
  394. struct rhpnode *tn; /* to node number */
  395. void *data; /* extra data as supplied */
  396. struct rhpnode *up_node; /* */
  397. struct rhpnode *down_node; /* */
  398. int64_t crossings; /* */
  399. };
  400. /* level info */
  401. struct rhplevel {
  402. int number_of_nodes; /* number of nodes in this level */
  403. int number_adjustments; /* number of nodes in level weight adjustments done */
  404. struct rhpnode **nodes; /* nodes in this level */
  405. };
  406. /* edge crossing info */
  407. struct rhp_inter_layer_struct {
  408. int number_of_edges; /* number of edges in level */
  409. struct rhpedge **eedges; /* edges in level */
  410. int64_t number_of_crossings; /* number of crossings in level 64bit */
  411. };
  412. /* level node order info */
  413. struct rhp_order_struct {
  414. int num_layers;
  415. int *num_nodes_on_layer;
  416. struct rhpnode ***node_ptr_on_layer;
  417. };
  418. /* more verbose log output if set */
  419. static int rhp_verbose = 0;
  420. /* set if init() is done */
  421. static int rhp_inited = 0;
  422. /* stream to write log info or stdout */
  423. static FILE *rhp_logstream = NULL;
  424. /* name of log file */
  425. static char *rhp_logname = NULL;
  426. /* set if logging is needed */
  427. static int rhp_dolog = 0;
  428. /* master node+edge list as from input */
  429. static struct rhp_sp *rhp_sp_master_node_list = (struct rhp_sp *)0;
  430. static struct rhp_sp *rhp_sp_master_edge_list = (struct rhp_sp *)0;
  431. /* master nodelist sorted on positions after layout */
  432. static struct rhp_sp *rhp_sp_master_node_list_sorted = (struct rhp_sp *)0;
  433. /* uniq node+edge numbers */
  434. static int rhp_uniq_nodenum = 0;
  435. static int rhp_uniq_edgenum = 0;
  436. static int rhp_number_of_nodes = 0;
  437. static int rhp_number_of_edges = 0;
  438. static int rhp_number_of_isolated_nodes = 0;
  439. /* max and number of levels */
  440. static int rhp_maxlevel = 0;
  441. static int rhp_nlevels = 0;
  442. /* number of (initial) crossings in graph (this should be a 64bits int)*/
  443. static int64_t rhp_crossings = (signed long int)(-1);
  444. static int64_t rhp_start_crossings = (signed long int)(-1);
  445. /* layers data */
  446. static struct rhp_sp *rhp_sp_layers = (struct rhp_sp *)0;
  447. /* crossing edges layers info */
  448. static struct rhp_sp *rhp_sp_between_layers = (struct rhp_sp *)0;
  449. /* order status info */
  450. static struct rhp_order_struct *rhp_best_crossings_order = (struct rhp_order_struct *)0;
  451. /* iteration counts */
  452. static int rhp_iter = 0;
  453. static int rhp_maxiter = 0;
  454. /* optional callback() routine during layouet */
  455. static int (*rhp_getlayoutdata)(int iter, int maxiter, uint64_t ncross,
  456. uint64_t previous_ncross, uint64_t reduction, int better, int improvements, int notimprovements);
  457. /* number of iterations improves or not-improved */
  458. static int rhp_improvements = 0;
  459. static int rhp_notimprovements = 0;
  460. /* node weight adjustment if set to 1 do avg mode, else left mode */
  461. static int rhp_adjustweight = 0;
  462. /* malloc free counter */
  463. static int64_t rhp_n_malloc = 0;
  464. static int64_t rhp_n_free = 0;
  465. /* forward decl. */
  466. static void *rhp_free(void *ptr, const char *func, int line);
  467. static void *rhp_malloc(size_t n, const char *func, int line);
  468. static struct rhp_sp *rhp_sp_new(int delval);
  469. static int rhp_sp_has_data(struct rhp_sp *sp);
  470. static struct rhp_spn *rhp_sp_min(struct rhp_sp *sp);
  471. static struct rhp_sp *rhp_sp_delete(struct rhp_sp *sp);
  472. static void rhp_tree_delete_helper(struct rhp_sp *sp, struct rhp_spn *node);
  473. static void rhp_sp_insert(struct rhp_sp *sp, rhp_spkey key, rhp_spval value);
  474. static struct rhp_spn *rhp_sp_next(struct rhp_sp *sp, rhp_spkey key);
  475. static struct rhp_spn *rhp_sp_lookup(struct rhp_sp *sp, rhp_spkey key);
  476. static inline void rhp_sp_sp_rr(struct rhp_spn **pp, struct rhp_spn *p, struct rhp_spn *n);
  477. static inline void rhp_sp_sp_rl(struct rhp_spn **pp, struct rhp_spn *p, struct rhp_spn *n);
  478. static void rhp_sp_sp(struct rhp_sp *sp, rhp_spkey key);
  479. static void rhp_log(char *format, ...);
  480. static void rhp_empty_best_crossings_order(void);
  481. static void rhp_empty_sp_master_node_list(void);
  482. static void rhp_empty_sp_master_edge_list(void);
  483. static void rhp_empty_sp_layers(void);
  484. static void rhp_empty_sp_between_layers(void);
  485. static void rhp_allocatelayers(void);
  486. static void rhp_allocateadjacencylists(void);
  487. static int rhp_countisolatednodes(void);
  488. static void rhp_initcrossings(void);
  489. static struct rhp_inter_layer_struct *rhp_makeinterlayer(int upper_layer);
  490. static int rhp_count_down_edges(int layer_number);
  491. static void rhp_updateallcrossings(void);
  492. static void rhp_updateallpositions(void);
  493. static void rhp_updatenodepositions(int layer);
  494. static void rhp_updatecrossingsforlayer(int layer);
  495. static void rhp_updatecrossingsbetweenlayers(int layer_number);
  496. static int rhp_compare_down_edges(const void *ptr_i, const void *ptr_j);
  497. static void rhp_sortbydownnodeposition(struct rhpedge **edge_array, int num_edges);
  498. static void rhp_add_edges_to_array(struct rhpedge **edge_array, struct rhpedge **edges_to_add, int num_edges, int start_pos);
  499. static int64_t rhp_count_inversions_down(struct rhpedge **edge_array, int num_of_edges, int diff);
  500. static int64_t rhp_insert_and_count_inversions_down(struct rhpedge
  501. **edge_array, int starting_index, int diff);
  502. static int64_t rhp_numberofcrossings(void);
  503. static void rhp_order_init(void);
  504. static void rhp_barycenter(void);
  505. static int rhp_terminate(void);
  506. static int rhp_end_of_iteration(void);
  507. static int rhp_barycenterupsweep(int lowlevel, int hilevel);
  508. static int rhp_barycenterdownsweep(int lowlevel, int hilevel);
  509. static int rhp_barycenterweights(int layer, int orientation);
  510. static void rhp_barycenterweights_adjust(int layer, int orientation);
  511. static void rhp_node_weight(struct rhpnode *node, int orientation);
  512. static int rhp_compare_weights(const void *ptr_i, const void *ptr_j);
  513. static void rhp_layersort(int layer);
  514. static void rhp_updatecrossingsforlayer(int layer);
  515. static void rhp_save_order(void);
  516. static void rhp_restore_order(void);
  517. /* create new sorted nodelist */
  518. static void rhp_sorted_nodelist(void);
  519. /* version info */
  520. char *rhp_version(void)
  521. {
  522. return ((char *)"1.7");
  523. }
  524. /* init with optional filename to log to or stdout if filename is "" */
  525. void rhp_init(char *logname, int loglevel)
  526. {
  527. /* set if init() is done */
  528. if (rhp_inited) {
  529. /* should not happen */
  530. rhp_log("%s(): rhp_deinit() not done and doing it now shouldnothappen!\n", __func__);
  531. rhp_deinit();
  532. /* now re-start with fresh init() */
  533. }
  534. /* stream to write log info or stdout if name is "" */
  535. rhp_logstream = NULL;
  536. /* set if logging is needed */
  537. rhp_dolog = 0;
  538. /* name of log file */
  539. rhp_logname = NULL;
  540. /* option */
  541. if (logname) {
  542. if (strlen(logname) == 0) {
  543. /* log stream to stdout */
  544. rhp_logstream = stdout;
  545. } else {
  546. /* log stream to named file or stdout at error */
  547. rhp_logstream = fopen(logname, "wb");
  548. if (rhp_logstream == NULL) {
  549. rhp_logstream = stdout;
  550. }
  551. }
  552. /* set if logging is needed */
  553. rhp_dolog = loglevel;
  554. rhp_log("%s(): starting logfile!\n", __func__);
  555. }
  556. /* master node+edge list as from input */
  557. rhp_sp_master_node_list = rhp_sp_new(1);
  558. rhp_sp_master_edge_list = rhp_sp_new(1);
  559. /* uniq node+edge numbers */
  560. rhp_uniq_nodenum = 0;
  561. rhp_uniq_edgenum = 0;
  562. rhp_number_of_nodes = 0;
  563. rhp_number_of_edges = 0;
  564. rhp_number_of_isolated_nodes = 0;
  565. /* crossing edges layers info */
  566. rhp_sp_between_layers = rhp_sp_new(1);
  567. /* max and number of levels */
  568. rhp_maxlevel = 0;
  569. rhp_nlevels = 0;
  570. /* number of (initial) crossings in graph */
  571. rhp_crossings = (int64_t) (-1);
  572. rhp_start_crossings = (int64_t) (-1);
  573. /* no order info */
  574. rhp_best_crossings_order = (struct rhp_order_struct *)0;
  575. /* no optional callback() routine during layouet */
  576. rhp_getlayoutdata = NULL;
  577. /* number of iterations improves or not-improved */
  578. rhp_improvements = 0;
  579. rhp_notimprovements = 0;
  580. /* print some machine info */
  581. rhp_log("%s(): sizeof (int) is %d bytes (expect 4)\n", __func__, (int)sizeof(int));
  582. rhp_log("%s(): sizeof (long long) is %d bytes (expect 8)\n", __func__, (int)sizeof(long long));
  583. rhp_log("%s(): sizeof (int64_t) is %d bytes (expect 8)\n", __func__, (int)sizeof(int64_t));
  584. rhp_log("%s(): sizeof (intptr_t) is %d bytes (expect 8)\n", __func__, (int)sizeof(intptr_t));
  585. rhp_log("%s(): sizeof (struct rhp_spn) is %d bytes\n", __func__, (int)sizeof(struct rhp_spn));
  586. rhp_log("%s(): sizeof (struct rhp_sp) is %d bytes\n", __func__, (int)sizeof(struct rhp_sp));
  587. rhp_log("%s(): sizeof (struct rhpnode) is %d bytes\n", __func__, (int)sizeof(struct rhpnode));
  588. rhp_log("%s(): sizeof (struct rhpedge) is %d bytes\n", __func__, (int)sizeof(struct rhpedge));
  589. rhp_log("%s(): sizeof (struct rhplevel) is %d bytes\n", __func__, (int)sizeof(struct rhplevel));
  590. rhp_log("%s(): sizeof (struct rhp_inter_layer_struct) is %d bytes\n", __func__, (int)sizeof(struct rhp_inter_layer_struct));
  591. rhp_log("%s(): sizeof (struct rhp_order_struct) is %d bytes\n", __func__, (int)sizeof(struct rhp_order_struct));
  592. /* set if init() is done */
  593. rhp_inited = 1;
  594. return;
  595. }
  596. /* */
  597. void rhp_deinit(void)
  598. {
  599. /* check on inited */
  600. if (rhp_inited == 0) {
  601. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  602. return;
  603. }
  604. rhp_log("%s():\n", __func__);
  605. /* empty crossing order info */
  606. rhp_empty_best_crossings_order();
  607. /* empty data inside layers */
  608. rhp_empty_sp_layers();
  609. /* empty data inside node+edge list */
  610. rhp_empty_sp_between_layers();
  611. rhp_empty_sp_master_node_list();
  612. rhp_empty_sp_master_edge_list();
  613. /* empty layers itself */
  614. rhp_sp_layers = rhp_sp_delete(rhp_sp_layers);
  615. /* clear order info. can be 0 at only init/deinit */
  616. if (rhp_best_crossings_order) {
  617. rhp_best_crossings_order = (struct rhp_order_struct *)rhp_free((void *)
  618. rhp_best_crossings_order, __func__, __LINE__);
  619. }
  620. /* master node+edge list as from input */
  621. rhp_sp_master_node_list = rhp_sp_delete(rhp_sp_master_node_list);
  622. rhp_sp_master_edge_list = rhp_sp_delete(rhp_sp_master_edge_list);
  623. rhp_sp_master_node_list_sorted = rhp_sp_delete(rhp_sp_master_node_list_sorted);
  624. /* crossing edges layers info */
  625. rhp_sp_between_layers = rhp_sp_delete(rhp_sp_between_layers);
  626. /* uniq node+edge numbers */
  627. rhp_uniq_nodenum = 0;
  628. rhp_uniq_edgenum = 0;
  629. rhp_number_of_nodes = 0;
  630. rhp_number_of_edges = 0;
  631. rhp_number_of_isolated_nodes = 0;
  632. /* max and number of levels */
  633. rhp_maxlevel = 0;
  634. rhp_nlevels = 0;
  635. /* number of (initial) crossings in graph */
  636. rhp_crossings = (int64_t) (-1);
  637. rhp_start_crossings = (int64_t) (-1);
  638. /* no optional callback() routine during layouet */
  639. rhp_getlayoutdata = NULL;
  640. /* number of iterations improves or not-improved */
  641. rhp_improvements = 0;
  642. rhp_notimprovements = 0;
  643. /* check malloc/free balance */
  644. if (rhp_n_malloc != rhp_n_free) {
  645. /* fixup needed here */
  646. if (rhp_dolog > 1) {
  647. rhp_log("%s(): done %" PRIu64 " malloc and %" PRIu64
  648. " free and delta is %" PRIi64 " shouldnothappen!\n",
  649. __func__, rhp_n_malloc, rhp_n_free, (rhp_n_malloc - rhp_n_free));
  650. }
  651. }
  652. rhp_n_malloc = 0;
  653. rhp_n_free = 0;
  654. /* check opt logfile */
  655. if (rhp_dolog) {
  656. rhp_log("%s(): closing logfile\n", __func__);
  657. /* name of log file */
  658. if (rhp_logname) {
  659. /* glibc fclose() does not set some ptr's to 0 creating valgrind error messages */
  660. (void)fclose(rhp_logstream);
  661. rhp_logname = NULL;
  662. }
  663. }
  664. /* set if logging is needed */
  665. rhp_dolog = 0;
  666. /* set if init() is done */
  667. rhp_inited = 0;
  668. return;
  669. }
  670. /* return 0 if oke, or 1 at error */
  671. int rhp_addnode(int num, int level, void *data)
  672. {
  673. struct rhp_spn *spn = (struct rhp_spn *)0;
  674. struct rhpnode *nn = (struct rhpnode *)0;
  675. /* check on inited */
  676. if (rhp_inited == 0) {
  677. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  678. return (1);
  679. }
  680. /* validate input */
  681. if (num < 0) {
  682. rhp_log("%s(): number %d is below zero and node skipped shouldnothappen!\n", __func__, num);
  683. return (1);
  684. }
  685. if (level < 0) {
  686. rhp_log("%s(): level %d is below zero and node skipped shouldnothappen!\n", __func__, level);
  687. return (1);
  688. }
  689. /* check if node already existed */
  690. spn = rhp_sp_lookup(rhp_sp_master_node_list, (splay_tree_key) num);
  691. if (spn) {
  692. rhp_log("%s(): node number %d already existed, skipped this add shouldnothappen!\n", __func__, num);
  693. return (1);
  694. }
  695. /* create fresh node data info */
  696. nn = (struct rhpnode *)rhp_malloc(sizeof(struct rhpnode), __func__, __LINE__);
  697. /* set the info n */
  698. nn->num = (rhp_uniq_nodenum);
  699. rhp_uniq_nodenum = (rhp_uniq_nodenum + 1);
  700. nn->innum = num;
  701. nn->level = level;
  702. nn->data = data;
  703. /* insert node in database indexed on initial supplied number */
  704. rhp_sp_insert(rhp_sp_master_node_list, (splay_tree_key) num, (splay_tree_value) nn);
  705. /* keep track of max. level seen */
  706. if (level >= rhp_maxlevel) {
  707. rhp_maxlevel = level;
  708. rhp_nlevels = (rhp_maxlevel + 1);
  709. }
  710. rhp_log("%s(): added node %d level %d data=%p now maxlevel=%d\n", __func__, num, level, (void *)data, rhp_maxlevel);
  711. return (0);
  712. }
  713. /* return 0 if oke or 1 at error */
  714. int rhp_addedge(int num, int fnode, int tnode, void *data)
  715. {
  716. struct rhp_spn *spn = (struct rhp_spn *)0;
  717. struct rhp_spn *spnf = (struct rhp_spn *)0;
  718. struct rhp_spn *spnt = (struct rhp_spn *)0;
  719. struct rhpnode *fn = (struct rhpnode *)0;
  720. struct rhpnode *tn = (struct rhpnode *)0;
  721. struct rhpedge *ne = (struct rhpedge *)0;
  722. int edgelen = 0;
  723. /* check on inited */
  724. if (rhp_inited == 0) {
  725. rhp_log("%s(): first rhp_init() mus be done shouldnothappen!\n", __func__);
  726. return (1);
  727. }
  728. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  729. /* no data nothing todo */
  730. rhp_log("%s(): there are no nodes in database skipping edge %d->%d shouldnothappen!\n", __func__, fnode, tnode);
  731. return (1);
  732. }
  733. /* validate input */
  734. if (num < 0) {
  735. rhp_log("%s(): number %d is below zero and edge skipped shouldnothappen!\n", __func__, num);
  736. return (1);
  737. }
  738. if (fnode < 0) {
  739. rhp_log("%s(): from node number %d is below zero and edge skipped shouldnothappen!\n", __func__, fnode);
  740. return (1);
  741. }
  742. if (tnode < 0) {
  743. rhp_log("%s(): to node number %d is below zero and edge skipped shouldnothappen!\n", __func__, tnode);
  744. return (1);
  745. }
  746. /* check if edge already existed */
  747. spn = rhp_sp_lookup(rhp_sp_master_edge_list, (splay_tree_key) num);
  748. if (spn) {
  749. rhp_log("%s(): edge number %d does already exists from %d->%d and skipped shouldnothappen!\n", __func__, num, fnode, tnode);
  750. return (1);
  751. }
  752. /* check if node exist */
  753. spnf = rhp_sp_lookup(rhp_sp_master_node_list, (splay_tree_key) fnode);
  754. if (spnf == (struct rhp_spn *)0) {
  755. rhp_log
  756. ("%s(): from node %d is not in database in edge from %d->%d and skipped shouldnothappen!\n",
  757. __func__, fnode, fnode, tnode);
  758. return (1);
  759. }
  760. /* get the node info */
  761. fn = (struct rhpnode *)spnf->value;
  762. spnt = rhp_sp_lookup(rhp_sp_master_node_list, (splay_tree_key) tnode);
  763. if (spnt == (struct rhp_spn *)0) {
  764. rhp_log
  765. ("%s(): to node %d is not in database in edge from %d->%d and skipped shouldnothappen!\n",
  766. __func__, tnode, fnode, tnode);
  767. return (1);
  768. }
  769. /* get the node info */
  770. tn = (struct rhpnode *)spnt->value;
  771. /* check for exact 1 level len of edge */
  772. edgelen = (tn->level - fn->level);
  773. if (edgelen != 1) {
  774. rhp_log
  775. ("%s(): edge len is %d at %d->%d from level %d->%d and should be 1 shouldnothappen!\n",
  776. __func__, edgelen, fnode, tnode, fn->level, tn->level);
  777. return (1);
  778. }
  779. /* edge must point downwards */
  780. if (tn->level < fn->level) {
  781. rhp_log
  782. ("%s(): edge len %d at %d->%d from level %d->%d should be downward shouldnothappen!\n",
  783. __func__, edgelen, fnode, tnode, fn->level, tn->level);
  784. return (1);
  785. }
  786. /* create fresh edge info */
  787. ne = (struct rhpedge *)rhp_malloc(sizeof(struct rhpedge), __func__, __LINE__);
  788. /* set the info */
  789. ne->num = rhp_uniq_edgenum; /* internal uniq edge number */
  790. rhp_uniq_edgenum = (rhp_uniq_edgenum + 1);
  791. ne->innum = num; /* incoming edge number */
  792. ne->fn = fn; /* from-node */
  793. ne->tn = tn; /* to-node */
  794. ne->data = data; /* opt. extra data */
  795. ne->up_node = tn; /* highest node level */
  796. ne->down_node = fn; /* lowest node level */
  797. ne->crossings = (int64_t) 0; /* crossings of edge */
  798. /* insert the edge in database initially indexed on supplied number */
  799. rhp_sp_insert(rhp_sp_master_edge_list, (rhp_spkey) num, (rhp_spval) ne);
  800. rhp_log("%s(): added edge %d from node %d to node %d data=%p\n", __func__, num, fnode, tnode, (void *)data);
  801. return (0);
  802. }
  803. /* run layouter */
  804. void rhp_layout(int options, int nodeweightadjust)
  805. {
  806. int64_t redu = 0;
  807. /* to implement */
  808. if (options) {
  809. }
  810. if (nodeweightadjust) {
  811. /* 0 is leftish node weight adjust, 1 is avg. node weight adjust */
  812. rhp_adjustweight = 1;
  813. }
  814. else {
  815. rhp_adjustweight = 0;
  816. }
  817. rhp_log("%s(): start with rhp_adjustweight %d\n", __func__, rhp_adjustweight);
  818. /* check on inited */
  819. if (rhp_inited == 0) {
  820. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  821. return;
  822. }
  823. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  824. /* no data nothing todo */
  825. rhp_log("%s(): there is no node data shouldnothappen!\n", __func__);
  826. return;
  827. }
  828. /* no edges is oke but there must be at least 1 node */
  829. /* number of iterations improves or not-improved */
  830. rhp_improvements = 0;
  831. rhp_notimprovements = 0;
  832. rhp_log("%s(): setting initial edge crossings is %" PRIi64 " %" PRIi64
  833. "\n", __func__, (int64_t) rhp_start_crossings, (int64_t) rhp_crossings);
  834. /* setup data structs and calculate initial crossings */
  835. rhp_crossings = rhp_initial_crossings();
  836. rhp_log("%s(): configured initial edge crossings is %" PRIi64 " %" PRIi64
  837. "\n", __func__, (int64_t) rhp_start_crossings, (int64_t) rhp_crossings);
  838. /* save status arrays initialize */
  839. rhp_order_init();
  840. /* reduce edge crossings */
  841. if (rhp_start_crossings) {
  842. rhp_barycenter();
  843. }
  844. /* calculate percentage of edge crossing reduction */
  845. if (rhp_start_crossings) {
  846. redu = ((100 * rhp_crossings) / rhp_start_crossings);
  847. redu = (100 - redu);
  848. } else {
  849. redu = 0;
  850. }
  851. /* create new sorted nodelist */
  852. rhp_sorted_nodelist();
  853. rhp_log("%s(): end and final edge crossings is %" PRIi64
  854. " after %d iterations and started with %" PRIi64
  855. " crossings reducing edge crossings with %" PRIu64 " percent\n",
  856. __func__, (int64_t) rhp_crossings, rhp_iter, rhp_start_crossings, redu);
  857. return;
  858. }
  859. /* get layouter data during iteration */
  860. void rhp_layout_callback(int (*getlayoutdata)
  861. (int iter, int maxiter, uint64_t ncross,
  862. uint64_t previous_ncross, uint64_t reduction, int better, int improvements, int notimprovements))
  863. {
  864. /* check for callback() */
  865. if (getlayoutdata == NULL) {
  866. rhp_log("%s(): no callback routine shouldnothappen!\n", __func__);
  867. return;
  868. }
  869. /* set the callback routine to use */
  870. rhp_getlayoutdata = getlayoutdata;
  871. return;
  872. }
  873. /* get node data */
  874. int rhp_node_foreach(int (*getnodedata)
  875. (int num, int level, int pos, void *data))
  876. {
  877. struct rhp_spn *spn = (struct rhp_spn *)0;
  878. struct rhpnode *nd = (struct rhpnode *)0;
  879. int status = 0;
  880. /* check on inited */
  881. if (rhp_inited == 0) {
  882. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  883. return (0);
  884. }
  885. if (getnodedata == NULL) {
  886. /* no callback() nothing todo */
  887. return (0);
  888. }
  889. if (rhp_sp_has_data(rhp_sp_master_node_list_sorted) == 0) {
  890. /* no data nothing todo */
  891. return (0);
  892. }
  893. status = 0;
  894. spn = rhp_sp_min(rhp_sp_master_node_list_sorted);
  895. while (spn) {
  896. nd = (struct rhpnode *)spn->value;
  897. rhp_log("%s(): node %d level %d pos %d\n", __func__, nd->innum, nd->level, nd->position);
  898. status = (*getnodedata) (nd->innum, nd->level, nd->position, nd->data);
  899. if (status) {
  900. break;
  901. }
  902. spn = rhp_sp_next(rhp_sp_master_node_list_sorted, spn->key);
  903. }
  904. return (status);
  905. }
  906. /* instead of foreach() get level of given node or -1 at error */
  907. int rhp_node_get_level(int num)
  908. {
  909. struct rhp_spn *spn = (struct rhp_spn *)0;
  910. struct rhpnode *nd = (struct rhpnode *)0;
  911. /* check on inited */
  912. if (rhp_inited == 0) {
  913. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  914. return (-1);
  915. }
  916. /* master node list is sorted on innum, input number
  917. * master_node_list_sorted is sorted on internal number
  918. */
  919. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  920. /* no data nothing todo */
  921. return (-1);
  922. }
  923. /* check if node exist */
  924. spn = rhp_sp_lookup(rhp_sp_master_node_list, (splay_tree_key) num);
  925. if (spn == (struct rhp_spn *)0) {
  926. /* not in database */
  927. rhp_log("%s(): could not find node %d in master_node_list!\n", __func__, num);
  928. return (-1);
  929. }
  930. nd = (struct rhpnode *)spn->value;
  931. rhp_log("%s(): node %d level %d pos %d\n", __func__, nd->innum, nd->level, nd->position);
  932. return (nd->level);
  933. }
  934. /* instead of foreach() get position of given node or -1 at error */
  935. int rhp_node_get_position(int num)
  936. {
  937. struct rhp_spn *spn = (struct rhp_spn *)0;
  938. struct rhpnode *nd = (struct rhpnode *)0;
  939. /* check on inited */
  940. if (rhp_inited == 0) {
  941. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  942. return (-1);
  943. }
  944. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  945. /* no data nothing todo */
  946. return (-1);
  947. }
  948. /* check if node exist */
  949. spn = rhp_sp_lookup(rhp_sp_master_node_list, (splay_tree_key) num);
  950. if (spn == (struct rhp_spn *)0) {
  951. /* not in database */
  952. rhp_log("%s(): could not find node %d in master_node_list!\n", __func__, num);
  953. return (-1);
  954. }
  955. nd = (struct rhpnode *)spn->value;
  956. rhp_log("%s(): node %d level %d pos %d\n", __func__, nd->innum, nd->level, nd->position);
  957. return (nd->position);
  958. }
  959. /* instead of foreach() get data of given node or -1 at error */
  960. void *rhp_node_get_data(int num)
  961. {
  962. struct rhp_spn *spn = (struct rhp_spn *)0;
  963. struct rhpnode *nd = (struct rhpnode *)0;
  964. /* check on inited */
  965. if (rhp_inited == 0) {
  966. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  967. return ((void *)-1);
  968. }
  969. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  970. /* no data nothing todo */
  971. return ((void *)-1);
  972. }
  973. /* check if node exist */
  974. spn = rhp_sp_lookup(rhp_sp_master_node_list, (splay_tree_key) num);
  975. if (spn == (struct rhp_spn *)0) {
  976. /* not in database */
  977. rhp_log("%s(): could not find node %d in master_node_list!\n", __func__, num);
  978. return ((void *)-1);
  979. }
  980. nd = (struct rhpnode *)spn->value;
  981. rhp_log("%s(): node %d level %d pos %d\n", __func__, nd->innum, nd->level, nd->position);
  982. return (nd->data);
  983. }
  984. /* get edge data via callback routine */
  985. int rhp_edge_foreach(int (*getedgedata)
  986. (int num, int fnum, int flevel, int fpos, int tnum, int tlevel, int tpos, int64_t ecross, void *data))
  987. {
  988. struct rhp_spn *spn = (struct rhp_spn *)0;
  989. struct rhpedge *ed = (struct rhpedge *)0;
  990. int status = 0;
  991. /* check on inited */
  992. if (rhp_inited == 0) {
  993. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  994. return (0);
  995. }
  996. if (getedgedata == NULL) {
  997. /* no callback() nothing todo */
  998. return (0);
  999. }
  1000. if (rhp_sp_has_data(rhp_sp_master_edge_list) == 0) {
  1001. /* no data nothing todo */
  1002. return (0);
  1003. }
  1004. /* status 0 means no errors */
  1005. status = 0;
  1006. spn = rhp_sp_min(rhp_sp_master_edge_list);
  1007. while (spn) {
  1008. ed = (struct rhpedge *)spn->value;
  1009. status =
  1010. (*getedgedata) (ed->innum, ed->fn->innum, ed->fn->level,
  1011. ed->fn->position, ed->tn->innum, ed->tn->level, ed->tn->position, ed->crossings, ed->data);
  1012. if (status) {
  1013. break;
  1014. }
  1015. spn = rhp_sp_next(rhp_sp_master_edge_list, spn->key);
  1016. }
  1017. return (status);
  1018. }
  1019. /* setup data structs and calculate initial crossings */
  1020. int64_t rhp_initial_crossings(void)
  1021. {
  1022. rhp_log("%s(): start crossings %" PRIi64 "\n", __func__, (int64_t) rhp_start_crossings);
  1023. /* check on inited */
  1024. if (rhp_inited == 0) {
  1025. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  1026. return ((int64_t) 0);
  1027. }
  1028. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  1029. /* no data nothing todo */
  1030. rhp_log("%s(): no nodes shouldnothappen!\n", __func__);
  1031. rhp_start_crossings = (int64_t) 0;
  1032. return ((int64_t) rhp_start_crossings);
  1033. }
  1034. /* at no edges there are no edge crossings */
  1035. if (rhp_sp_has_data(rhp_sp_master_edge_list) == 0) {
  1036. /* no edges is oke */
  1037. rhp_start_crossings = (int64_t) 0;
  1038. /* data structs below must be build */
  1039. }
  1040. /* if already inited then oke. */
  1041. if (rhp_start_crossings >= 0) {
  1042. rhp_log("%s(): already inited start crossings %" PRIi64 "\n", __func__, (int64_t) rhp_start_crossings);
  1043. /* data structs below must be build */
  1044. return (rhp_start_crossings);
  1045. }
  1046. rhp_log("%s(): allocate layers\n", __func__);
  1047. /* create layers and count nodes in layers */
  1048. rhp_allocatelayers();
  1049. /* handle edges */
  1050. rhp_allocateadjacencylists();
  1051. /* get number of single nodes */
  1052. rhp_number_of_isolated_nodes = rhp_countisolatednodes();
  1053. rhp_log("%s(): %" PRIi64 " single nodes\n", __func__, (int64_t) rhp_number_of_isolated_nodes);
  1054. /* init data for edge crossing counting */
  1055. rhp_initcrossings();
  1056. /* set for every level number of edge crossings */
  1057. rhp_updateallcrossings();
  1058. /* sum all levels edge crossings for the whole graph */
  1059. rhp_start_crossings = rhp_numberofcrossings();
  1060. /* node data without the extra storage for the connects */
  1061. rhp_log("%s(): using %" PRIu64 " bytes for the node data core part\n",
  1062. __func__, (rhp_number_of_nodes * sizeof(struct rhpnode)));
  1063. rhp_log("%s(): using %" PRIu64 " bytes for the edge data\n", __func__, (rhp_number_of_edges * sizeof(struct rhpedge)));
  1064. return ((int64_t) rhp_start_crossings);
  1065. }
  1066. /* calculate crossings */
  1067. int64_t rhp_current_crossings(void)
  1068. {
  1069. /* check on inited */
  1070. if (rhp_inited == 0) {
  1071. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  1072. return ((int64_t) 0);
  1073. }
  1074. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  1075. /* no data nothing todo */
  1076. rhp_log("%s(): no nodes shouldnothappen!\n", __func__);
  1077. rhp_crossings = (int64_t) 0;
  1078. return ((int64_t) rhp_crossings);
  1079. }
  1080. /* at no edges there are no edge crossings */
  1081. if (rhp_sp_has_data(rhp_sp_master_edge_list) == 0) {
  1082. /* no edges is oke */
  1083. rhp_crossings = (int64_t) 0;
  1084. return ((int64_t) rhp_crossings);
  1085. }
  1086. /* if not inited yet */
  1087. if (rhp_start_crossings < 0) {
  1088. rhp_crossings = rhp_initial_crossings();
  1089. } else {
  1090. /* now calculate crossings */
  1091. if (rhp_number_of_edges == 1) {
  1092. /* at one edge there cannot be crossing edges */
  1093. rhp_crossings = (int64_t) 0;
  1094. } else {
  1095. rhp_crossings = (int64_t) 0;
  1096. /* do actual re-cualculation of crossings */
  1097. /* set for every level number of edge crossings */
  1098. rhp_updateallcrossings();
  1099. /* sum all levels edge crossings for the whole graph */
  1100. rhp_crossings = rhp_numberofcrossings();
  1101. }
  1102. }
  1103. return ((int64_t) rhp_crossings);
  1104. }
  1105. /* calculate crossings at level */
  1106. int64_t rhp_current_crossings_at_level(int level)
  1107. {
  1108. struct rhp_spn *spn = (struct rhp_spn *)0;
  1109. struct rhp_inter_layer_struct *is = (struct rhp_inter_layer_struct *)0;
  1110. /* check on inited */
  1111. if (rhp_inited == 0) {
  1112. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  1113. return (0);
  1114. }
  1115. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  1116. /* no data nothing todo */
  1117. rhp_log("%s(): no nodes shouldnothappen!\n", __func__);
  1118. return (0);
  1119. }
  1120. if (level < 0) {
  1121. rhp_log("%s(): level %d is below 0 shouldnothappen!\n", __func__, level);
  1122. return (0);
  1123. }
  1124. if (level > rhp_maxlevel) {
  1125. rhp_log("%s(): level %d is above max level %dshouldnothappen!\n", __func__, level, rhp_maxlevel);
  1126. return (0);
  1127. }
  1128. if (rhp_sp_has_data(rhp_sp_between_layers) == 0) {
  1129. /* no data can happen when there were no edges and that is oke. */
  1130. return ((uint64_t) 0);
  1131. }
  1132. if (rhp_nlevels == 0) {
  1133. /* nothing todo */
  1134. rhp_log("%s(): no levels shouldnothappen!\n", __func__);
  1135. return ((uint64_t) 0);
  1136. }
  1137. spn = rhp_sp_lookup(rhp_sp_between_layers, (rhp_spkey) level);
  1138. if (spn) {
  1139. is = (struct rhp_inter_layer_struct *)spn->value;
  1140. return ((uint64_t) is->number_of_crossings);
  1141. } else {
  1142. rhp_log("%s(): could not get data for level %d shouldnothappen!\n", __func__, level);
  1143. }
  1144. return ((uint64_t) 0);
  1145. }
  1146. /* get number of nodes in level */
  1147. int rhp_nodes_in_level(int level)
  1148. {
  1149. struct rhp_spn *spn = (struct rhp_spn *)0;
  1150. struct rhplevel *rl = (struct rhplevel *)0;
  1151. /* check on inited */
  1152. if (rhp_inited == 0) {
  1153. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  1154. return (0);
  1155. }
  1156. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  1157. /* no data nothing todo */
  1158. rhp_log("%s(): no nodes shouldnothappen!\n", __func__);
  1159. return (0);
  1160. }
  1161. if (level < 0) {
  1162. rhp_log("%s(): level %d is below 0 shouldnothappen!\n", __func__, level);
  1163. return (0);
  1164. }
  1165. if (level > rhp_maxlevel) {
  1166. rhp_log("%s(): level %d is above max level %dshouldnothappen!\n", __func__, level, rhp_maxlevel);
  1167. return (0);
  1168. }
  1169. /* empty layer info */
  1170. if (rhp_sp_has_data(rhp_sp_layers) == 0) {
  1171. /* nothing todo. */
  1172. return (0);
  1173. }
  1174. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  1175. /* nodes in level */
  1176. if (spn) {
  1177. rl = (struct rhplevel *)spn->value;
  1178. return (rl->number_of_nodes);
  1179. } else {
  1180. rhp_log("%s(): could not find data for level %d shouldnothappen!\n", __func__, level);
  1181. }
  1182. return (0);
  1183. }
  1184. /* get number of nodes in layouter */
  1185. int rhp_nodes_in_layout(void)
  1186. {
  1187. int c = 0;
  1188. struct rhp_spn *spn = (struct rhp_spn *)0;
  1189. /* check on inited */
  1190. if (rhp_inited == 0) {
  1191. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  1192. return (0);
  1193. }
  1194. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  1195. /* no data nothing todo */
  1196. return (0);
  1197. }
  1198. /* count */
  1199. c = 0;
  1200. spn = rhp_sp_min(rhp_sp_master_node_list);
  1201. while (spn) {
  1202. c = (c + 1);
  1203. spn = rhp_sp_next(rhp_sp_master_node_list, spn->key);
  1204. }
  1205. return (c);
  1206. }
  1207. /* get number of edges in layouter */
  1208. int rhp_edges_in_layout(void)
  1209. {
  1210. int c = 0;
  1211. struct rhp_spn *spn = (struct rhp_spn *)0;
  1212. /* check on inited */
  1213. if (rhp_inited == 0) {
  1214. rhp_log("%s(): first rhp_init() must be done shouldnothappen!\n", __func__);
  1215. return (0);
  1216. }
  1217. if (rhp_sp_has_data(rhp_sp_master_edge_list) == 0) {
  1218. /* no data nothing todo */
  1219. return (0);
  1220. }
  1221. /* count */
  1222. c = 0;
  1223. spn = rhp_sp_min(rhp_sp_master_edge_list);
  1224. while (spn) {
  1225. c = (c + 1);
  1226. spn = rhp_sp_next(rhp_sp_master_edge_list, spn->key);
  1227. }
  1228. return (c);
  1229. }
  1230. /****zz1****/
  1231. /* or wrap to free() */
  1232. static void *rhp_free(void *ptr, const char *func, int line)
  1233. {
  1234. if (ptr == (void *)0) {
  1235. rhp_log("%s(): nil ptr from %s line %d shouldnothappen!\n", __func__, (char *)func, line);
  1236. return ((void *)0);
  1237. }
  1238. if (ptr) {
  1239. myfree(ptr, (char *)func, line);
  1240. rhp_n_free = (rhp_n_free + 1);
  1241. if (rhp_dolog > 1) {
  1242. rhp_log("%p\t%s\tline %d\trhp_free()\tmemtrace\n", (void *)ptr, func, line);
  1243. }
  1244. }
  1245. return ((void *)0);
  1246. }
  1247. /* or wrap to calloc() */
  1248. static void *rhp_malloc(size_t n, const char *func, int line)
  1249. {
  1250. void *ptr = (void *)0;
  1251. if (n == 0) {
  1252. rhp_log("%s(): 0 bytes to malloc from %s line %d shouldnothappen!\n", __func__, (char *)func, line);
  1253. return ((void *)0);
  1254. }
  1255. rhp_n_malloc = (rhp_n_malloc + 1);
  1256. ptr = mymalloc(n, (char *)func, line);
  1257. if (rhp_dolog > 1) {
  1258. rhp_log("%p\t%s\tline %d\trhp_malloc(%" PRIu64 ")\tmemtrace\n", (void *)ptr, func, line, (uint64_t) n);
  1259. }
  1260. return ((void *)ptr);
  1261. }
  1262. /* gcc splay gnu gpl version 3 fsf */
  1263. static struct rhp_sp *rhp_sp_new(int delval)
  1264. {
  1265. struct rhp_sp *newsp = (struct rhp_sp *)0;
  1266. /* new splay */
  1267. newsp = rhp_malloc(sizeof(struct rhp_sp), __func__, __LINE__);
  1268. /* empty data */
  1269. newsp->root = (struct rhp_spn *)0;
  1270. /* if set sp_delete() deletes the value */
  1271. newsp->delval = delval;
  1272. return ((struct rhp_sp *)newsp);
  1273. }
  1274. /* A splay-tree datatype.
  1275. Copyright (C) 1998-2021 Free Software Foundation, Inc.
  1276. Contributed by Mark Mitchell (mark@markmitchell.com).
  1277. This file is part of GNU CC.
  1278. GNU CC is free software; you can redistribute it and/or modify it
  1279. under the terms of the GNU General Public License as published by
  1280. the Free Software Foundation; either version 2, or (at your option)
  1281. any later version.
  1282. GNU CC is distributed in the hope that it will be useful, but
  1283. WITHOUT ANY WARRANTY; without even the implied warranty of
  1284. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1285. General Public License for more details.
  1286. You should have received a copy of the GNU General Public License
  1287. along with GNU CC; see the file COPYING. If not, write to
  1288. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  1289. Boston, MA 02110-1301, USA. */
  1290. /* For an easily readable description of splay-trees, see:
  1291. Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
  1292. Algorithms. Harper-Collins, Inc. 1991. */
  1293. /* return 1 if splay has data */
  1294. static int rhp_sp_has_data(struct rhp_sp *sp)
  1295. {
  1296. if (sp == (struct rhp_sp *)0) {
  1297. /* no splay at all is no data */
  1298. return (0);
  1299. }
  1300. if (sp->root == (struct rhp_spn *)0) {
  1301. /* nil root is no data */
  1302. return (0);
  1303. }
  1304. /* tree has data */
  1305. return (1);
  1306. }
  1307. /* */
  1308. static struct rhp_spn *rhp_sp_min(struct rhp_sp *sp)
  1309. {
  1310. struct rhp_spn *nspn = (struct rhp_spn *)0;
  1311. if (sp == (struct rhp_sp *)0) {
  1312. /* no tree */
  1313. return ((struct rhp_spn *)0);
  1314. }
  1315. if (sp->root == (struct rhp_spn *)0) {
  1316. /* no data */
  1317. return ((struct rhp_spn *)0);
  1318. }
  1319. nspn = sp->root;
  1320. if (nspn) {
  1321. while (nspn->l) {
  1322. nspn = nspn->l;
  1323. }
  1324. }
  1325. return ((struct rhp_spn *)nspn);
  1326. }
  1327. /* */
  1328. static void rhp_sp_insert(struct rhp_sp *sp, rhp_spkey key, rhp_spval value)
  1329. {
  1330. struct rhp_spn *newspn = (struct rhp_spn *)0;
  1331. if (sp == (struct rhp_sp *)0) {
  1332. return;
  1333. }
  1334. if (sp->root == (struct rhp_spn *)0) {
  1335. /* sp->root==0 this is first entry */
  1336. newspn = rhp_malloc(sizeof(struct rhp_spn), __func__, __LINE__);
  1337. sp->root = newspn;
  1338. sp->root->l = (struct rhp_spn *)0;
  1339. sp->root->r = (struct rhp_spn *)0;
  1340. sp->root->key = key;
  1341. sp->root->value = value;
  1342. if (rhp_verbose) {
  1343. rhp_log("%s(): sp (%p) sp->root (%p) is %" PRIi64
  1344. " at first entry\n", __func__, (void *)sp, (void *)sp->root, (uint64_t) sp->root->key);
  1345. }
  1346. return;
  1347. }
  1348. /* */
  1349. rhp_sp_sp(sp, key);
  1350. if (sp->root->key == key) {
  1351. if (sp->delval) {
  1352. if (sp->root->value) {
  1353. (void)rhp_free((void *)sp->root->value, __func__, __LINE__);
  1354. sp->root->value = (rhp_spval) 0;
  1355. }
  1356. }
  1357. sp->root->value = value;
  1358. return;
  1359. }
  1360. newspn = rhp_malloc(sizeof(struct rhp_spn), __func__, __LINE__);
  1361. /* set data for this node */
  1362. newspn->key = key;
  1363. newspn->value = value;
  1364. if (sp->root->key < key) {
  1365. newspn->l = sp->root;
  1366. newspn->r = newspn->l->r;
  1367. newspn->l->r = (struct rhp_spn *)0;
  1368. } else {
  1369. newspn->r = sp->root;
  1370. newspn->l = newspn->r->l;
  1371. newspn->r->l = (struct rhp_spn *)0;
  1372. }
  1373. sp->root = newspn;
  1374. if (rhp_verbose) {
  1375. rhp_log("%s(): sp (%p) sp->root (%p) is %" PRIi64 " at next entry\n",
  1376. __func__, (void *)sp, (void *)sp->root, (uint64_t) sp->root->key);
  1377. }
  1378. return;
  1379. }
  1380. /* Deallocate NODE (a member of SP), and all its sub-trees. */
  1381. static void rhp_tree_delete_helper(struct rhp_sp *sp, struct rhp_spn *node)
  1382. {
  1383. if (node == NULL) {
  1384. return;
  1385. }
  1386. /* recurse */
  1387. rhp_tree_delete_helper(sp, node->l);
  1388. rhp_tree_delete_helper(sp, node->r);
  1389. if (sp->delval) {
  1390. if (node->value) {
  1391. (void)rhp_free((void *)node->value, __func__, __LINE__);
  1392. node->value = (rhp_spval) 0;
  1393. }
  1394. }
  1395. (void)rhp_free((void *)node, __func__, __LINE__);
  1396. return;
  1397. }
  1398. /* */
  1399. static struct rhp_sp *rhp_sp_delete(struct rhp_sp *sp)
  1400. {
  1401. if (sp) {
  1402. rhp_tree_delete_helper(sp, sp->root);
  1403. (void)rhp_free((void *)sp, __func__, __LINE__);
  1404. }
  1405. return ((struct rhp_sp *)0);
  1406. }
  1407. /* */
  1408. static struct rhp_spn *rhp_sp_next(struct rhp_sp *sp, rhp_spkey key)
  1409. {
  1410. struct rhp_spn *n = (struct rhp_spn *)0;
  1411. if (sp == (struct rhp_sp *)0) {
  1412. return ((struct rhp_spn *)0);
  1413. }
  1414. if (sp->root == (struct rhp_spn *)0) {
  1415. return ((struct rhp_spn *)0);
  1416. }
  1417. rhp_sp_sp(sp, key);
  1418. if (sp->root->key > key) {
  1419. return ((struct rhp_spn *)sp->root);
  1420. }
  1421. n = sp->root->r;
  1422. if (n) {
  1423. while (n->l) {
  1424. n = n->l;
  1425. }
  1426. }
  1427. return ((struct rhp_spn *)n);
  1428. }
  1429. /* */
  1430. static struct rhp_spn *rhp_sp_lookup(struct rhp_sp *sp, rhp_spkey key)
  1431. {
  1432. char *s = "not-found";
  1433. if (sp == (struct rhp_sp *)0) {
  1434. return ((struct rhp_spn *)0);
  1435. }
  1436. if (sp->root == (struct rhp_spn *)0) {
  1437. return ((struct rhp_spn *)0);
  1438. }
  1439. if (sp->root->key == key) {
  1440. return ((struct rhp_spn *)sp->root);
  1441. }
  1442. rhp_sp_sp(sp, key);
  1443. if (sp->root == (struct rhp_spn *)0) {
  1444. return ((struct rhp_spn *)0);
  1445. }
  1446. if (sp->root->key == key) {
  1447. s = "found";
  1448. } else {
  1449. s = "not-found";
  1450. }
  1451. if (rhp_verbose) {
  1452. rhp_log("%s(): %s in sp (%p) at %" PRIi64 " when search for %" PRIi64
  1453. "\n", __func__, s, (void *)sp, (uint64_t) sp->root->key, (uint64_t) key);
  1454. }
  1455. if (sp->root->key == key) {
  1456. return ((struct rhp_spn *)sp->root);
  1457. }
  1458. return ((struct rhp_spn *)0);
  1459. }
  1460. /* */
  1461. static inline void rhp_sp_sp_rl(struct rhp_spn **pp, struct rhp_spn *p, struct rhp_spn *n)
  1462. {
  1463. struct rhp_spn *tmp = (struct rhp_spn *)0;
  1464. tmp = n->r;
  1465. n->r = p;
  1466. p->l = tmp;
  1467. *pp = n;
  1468. return;
  1469. }
  1470. /* */
  1471. static inline void rhp_sp_sp_rr(struct rhp_spn **pp, struct rhp_spn *p, struct rhp_spn *n)
  1472. {
  1473. struct rhp_spn *tmp = (struct rhp_spn *)0;
  1474. tmp = n->l;
  1475. n->l = p;
  1476. p->r = tmp;
  1477. *pp = n;
  1478. return;
  1479. }
  1480. /* Bottom up splay of key. */
  1481. static void rhp_sp_sp(struct rhp_sp *sp, rhp_spkey key)
  1482. {
  1483. struct rhp_spn *n = (struct rhp_spn *)0;
  1484. struct rhp_spn *c = (struct rhp_spn *)0;
  1485. if (sp == (struct rhp_sp *)0) {
  1486. return;
  1487. }
  1488. if (sp->root == (struct rhp_spn *)0) {
  1489. return;
  1490. }
  1491. label:
  1492. n = sp->root;
  1493. if (n == (struct rhp_spn *)0) {
  1494. return;
  1495. }
  1496. if (rhp_verbose) {
  1497. rhp_log("%s(): at %" PRIi64 " when search for %" PRIi64 "\n", __func__, (uint64_t) n->key, (uint64_t) key);
  1498. }
  1499. if (key == n->key) {
  1500. return;
  1501. }
  1502. if (key < n->key) {
  1503. c = n->l;
  1504. } else {
  1505. c = n->r;
  1506. }
  1507. if (c == (struct rhp_spn *)0) {
  1508. if (rhp_verbose) {
  1509. rhp_log("%s(): c=<nil> at %" PRIi64 " when search for %" PRIi64
  1510. "\n", __func__, (uint64_t) sp->root->key, (uint64_t) key);
  1511. }
  1512. return;
  1513. }
  1514. if ((key == c->key) || ((key < c->key) && (c->l == (struct rhp_spn *)0))
  1515. || ((key > c->key) && (c->r == (struct rhp_spn *)0))) {
  1516. if (key < n->key) {
  1517. rhp_sp_sp_rl(&sp->root, n, c);
  1518. } else {
  1519. rhp_sp_sp_rr(&sp->root, n, c);
  1520. }
  1521. return;
  1522. }
  1523. if ((key < n->key) && (key < c->key)) {
  1524. rhp_sp_sp_rl(&n->l, c, c->l);
  1525. rhp_sp_sp_rl(&sp->root, n, n->l);
  1526. } else if ((key > n->key) && (key > c->key)) {
  1527. rhp_sp_sp_rr(&n->r, c, c->r);
  1528. rhp_sp_sp_rr(&sp->root, n, n->r);
  1529. } else if ((key < n->key) && (key > c->key)) {
  1530. rhp_sp_sp_rr(&n->l, c, c->r);
  1531. rhp_sp_sp_rl(&sp->root, n, n->l);
  1532. } else if ((key > n->key) && (key < c->key)) {
  1533. rhp_sp_sp_rl(&n->r, c, c->l);
  1534. rhp_sp_sp_rr(&sp->root, n, n->r);
  1535. } else {
  1536. /* == handled at start of label */
  1537. }
  1538. goto label;
  1539. return;
  1540. }
  1541. /* print to log
  1542. * next line is needed because clang warning at line:
  1543. * vfprintf(stderr, format, ap);
  1544. * format is not a string literal
  1545. */
  1546. __attribute__((__format__(__printf__, 1, 0)))
  1547. static void rhp_log(char *format, ...)
  1548. {
  1549. va_list ap;
  1550. char *p = (char *)0;
  1551. char *q = (char *)0;
  1552. /* lines with '!' are printed on stderr, do a strchr() */
  1553. p = format;
  1554. q = (char *)0;
  1555. while ((*p)) {
  1556. if ((*p) == '!') {
  1557. q = p;
  1558. }
  1559. p = (p + 1);
  1560. }
  1561. /* if a '!' found print on stderr */
  1562. if (q) {
  1563. va_start(ap, format);
  1564. vfprintf(stderr, format, ap);
  1565. va_end(ap);
  1566. fflush(stderr);
  1567. }
  1568. if (rhp_dolog == 0) {
  1569. /* no logging active */
  1570. return;
  1571. }
  1572. if (rhp_logstream == NULL) {
  1573. /* no stream to log to */
  1574. return;
  1575. }
  1576. va_start(ap, format);
  1577. vfprintf(rhp_logstream, format, ap);
  1578. va_end(ap);
  1579. fflush(rhp_logstream);
  1580. return;
  1581. }
  1582. /* empty data inside order info */
  1583. static void rhp_empty_best_crossings_order(void)
  1584. {
  1585. int level = 0;
  1586. rhp_log("%s():\n", __func__);
  1587. /* if no data nothing todo */
  1588. if (rhp_best_crossings_order == (struct rhp_order_struct *)0) {
  1589. rhp_log("%s(): no data\n", __func__);
  1590. return;
  1591. }
  1592. /* number of nodes on level */
  1593. rhp_best_crossings_order->num_nodes_on_layer =
  1594. (int *)rhp_free((void *)rhp_best_crossings_order->num_nodes_on_layer, __func__, __LINE__);
  1595. /* scan the levels */
  1596. for (level = 0; level < rhp_nlevels; level++) {
  1597. if (rhp_best_crossings_order->node_ptr_on_layer[level]) {
  1598. rhp_best_crossings_order->node_ptr_on_layer[level] = (struct rhpnode * *)rhp_free((void *)
  1599. rhp_best_crossings_order->node_ptr_on_layer[level], __func__, __LINE__);
  1600. }
  1601. }
  1602. /* nodes on layer */
  1603. if (rhp_best_crossings_order->node_ptr_on_layer) {
  1604. rhp_best_crossings_order->node_ptr_on_layer =
  1605. (struct rhpnode * **)rhp_free((void *)rhp_best_crossings_order->node_ptr_on_layer, __func__, __LINE__);
  1606. }
  1607. return;
  1608. }
  1609. /* empty data inside node list */
  1610. static void rhp_empty_sp_master_node_list(void)
  1611. {
  1612. struct rhp_spn *spn = (struct rhp_spn *)0;
  1613. struct rhpnode *nd = (struct rhpnode *)0;
  1614. rhp_log("%s():\n", __func__);
  1615. spn = rhp_sp_min(rhp_sp_master_node_list);
  1616. while (spn) {
  1617. nd = (struct rhpnode *)spn->value;
  1618. /* edges connecting to this node */
  1619. if (nd->up_edges) {
  1620. nd->up_edges = rhp_free((void *)nd->up_edges, __func__, __LINE__);
  1621. nd->up_degree = 0;
  1622. }
  1623. if (nd->down_edges) {
  1624. nd->down_edges = rhp_free((void *)nd->down_edges, __func__, __LINE__);
  1625. nd->down_degree = 0;
  1626. }
  1627. spn = rhp_sp_next(rhp_sp_master_node_list, spn->key);
  1628. }
  1629. /* sorted list */
  1630. spn = rhp_sp_min(rhp_sp_master_node_list_sorted);
  1631. while (spn) {
  1632. (void)rhp_free((void *)spn->value, __func__, __LINE__);
  1633. spn->value = (rhp_spval) 0;
  1634. spn = rhp_sp_next(rhp_sp_master_node_list_sorted, spn->key);
  1635. }
  1636. return;
  1637. }
  1638. /* empty data inside edge list */
  1639. static void rhp_empty_sp_master_edge_list(void)
  1640. {
  1641. rhp_log("%s():\n", __func__);
  1642. return;
  1643. }
  1644. /* empty edge crossing info */
  1645. static void rhp_empty_sp_between_layers(void)
  1646. {
  1647. struct rhp_spn *spn = (struct rhp_spn *)0;
  1648. struct rhp_inter_layer_struct *is = (struct rhp_inter_layer_struct *)0;
  1649. rhp_log("%s(): data is %d\n", __func__, rhp_sp_has_data(rhp_sp_between_layers));
  1650. spn = rhp_sp_min(rhp_sp_between_layers);
  1651. while (spn) {
  1652. is = (struct rhp_inter_layer_struct *)spn->value;
  1653. rhp_log("%s(): free eedges %p\n", __func__, (void *)is->eedges);
  1654. /* */
  1655. if (is->eedges) {
  1656. is->eedges = rhp_free((void *)is->eedges, __func__, __LINE__);
  1657. }
  1658. spn = rhp_sp_next(rhp_sp_between_layers, spn->key);
  1659. }
  1660. return;
  1661. }
  1662. /* empty data inside layers */
  1663. static void rhp_empty_sp_layers(void)
  1664. {
  1665. struct rhp_spn *spn = (struct rhp_spn *)0;
  1666. struct rhplevel *rl = (struct rhplevel *)0;
  1667. struct rhpnode *nd = (struct rhpnode *)0;
  1668. int level = 0;
  1669. int i = 0;
  1670. rhp_log("%s(): %d levels to clear in sp %p\n", __func__, rhp_nlevels, (void *)rhp_sp_layers);
  1671. /* only if there is layer data */
  1672. if (rhp_sp_layers == NULL) {
  1673. return;
  1674. }
  1675. /* scan the levels */
  1676. for (level = 0; level < rhp_nlevels; level++) {
  1677. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  1678. if (spn == (struct rhp_spn *)0) {
  1679. /* if here is only 1 level with single nodes, no edges then oke to skip */
  1680. /* only log message if there are sp_layers */
  1681. if (rhp_nlevels > 1) {
  1682. rhp_log("%s(): could not get data for level %d shouldnothappen!\n", __func__, level);
  1683. }
  1684. continue;
  1685. }
  1686. /* clear nodes in level */
  1687. rl = (struct rhplevel *)spn->value;
  1688. rhp_log("%s(): clear level %d with %d nodes %p\n", __func__, level, rl->number_of_nodes, (void *)rl->nodes);
  1689. if (rl->nodes) {
  1690. /* empty inside nodes */
  1691. for (i = 0; i < rl->number_of_nodes; i++) {
  1692. nd = rl->nodes[i];
  1693. if (nd->up_degree) {
  1694. nd->up_edges = rhp_free((void *)nd->up_edges, __func__, __LINE__);
  1695. nd->up_degree = 0;
  1696. }
  1697. if (nd->down_degree) {
  1698. nd->down_edges = rhp_free((void *)nd->down_edges, __func__, __LINE__);
  1699. nd->down_degree = 0;
  1700. }
  1701. }
  1702. rl->nodes = rhp_free((void *)rl->nodes, __func__, __LINE__);
  1703. rl->number_of_nodes = 0;
  1704. }
  1705. }
  1706. return;
  1707. }
  1708. /* alloc layers info structs and set number of nodes in level */
  1709. static void rhp_allocatelayers(void)
  1710. {
  1711. int i = 0;
  1712. int ii = 0;
  1713. struct rhplevel *rl = (struct rhplevel *)0;
  1714. struct rhp_spn *spn = (struct rhp_spn *)0;
  1715. struct rhp_spn *spnn = (struct rhp_spn *)0;
  1716. struct rhpnode *nd = (struct rhpnode *)0;
  1717. int level = 0;
  1718. size_t bt = (size_t)0;
  1719. /* overall count of nodes */
  1720. rhp_number_of_nodes = 0;
  1721. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  1722. rhp_log("%s(): no nodes shouldnothappen!\n", __func__);
  1723. return;
  1724. }
  1725. if (rhp_nlevels == 0) {
  1726. rhp_log("%s(): no levels shouldnothappen!\n", __func__);
  1727. return;
  1728. }
  1729. if (rhp_sp_layers != (struct rhp_sp *)0) {
  1730. rhp_log("%s(): rhp_sp_layers %p shouldnothappen!\n", __func__, (void *)rhp_sp_layers);
  1731. }
  1732. /* layer data */
  1733. rhp_sp_layers = rhp_sp_new(1);
  1734. /* create empty layer info */
  1735. for (i = 0; i < rhp_nlevels; i++) {
  1736. rl = (struct rhplevel *)rhp_malloc(sizeof(struct rhplevel), __func__, __LINE__);
  1737. bt = (bt + sizeof(struct rhplevel));
  1738. rl->number_of_nodes = 0;
  1739. rl->nodes = (struct rhpnode **)0;
  1740. rhp_log("%s(): creating entry for level %d\n", __func__, i);
  1741. rhp_sp_insert(rhp_sp_layers, (rhp_spkey) i, (rhp_spval) rl);
  1742. }
  1743. /* scan the nodes and update count of nodes in each level */
  1744. spnn = rhp_sp_min(rhp_sp_master_node_list);
  1745. while (spnn) {
  1746. nd = (struct rhpnode *)spnn->value;
  1747. level = nd->level;
  1748. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  1749. /* update count of nodes in level and add node */
  1750. rl = (struct rhplevel *)spn->value;
  1751. /* update count of nodes in this level */
  1752. rl->number_of_nodes = (rl->number_of_nodes + 1);
  1753. /* update overall count of nodes */
  1754. rhp_number_of_nodes = (rhp_number_of_nodes + 1);
  1755. rhp_log("%s(): now counted %d nodes in level %d total %d\n", __func__, rl->number_of_nodes, i, rhp_number_of_nodes);
  1756. spnn = rhp_sp_next(rhp_sp_master_node_list, spnn->key);
  1757. }
  1758. /* */
  1759. for (i = 0; i < rhp_nlevels; i++) {
  1760. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) i);
  1761. /* update count of nodes in level and add node */
  1762. rl = (struct rhplevel *)spn->value;
  1763. if (rl->number_of_nodes) {
  1764. rhp_log("%s(): there are %d nodes in level %d\n", __func__, rl->number_of_nodes, i);
  1765. rl->nodes = (struct rhpnode **)
  1766. rhp_malloc((rl->number_of_nodes * sizeof(struct rhpnode *)), __func__, __LINE__);
  1767. bt = (bt + (rl->number_of_nodes * sizeof(struct rhpnode *)));
  1768. } else {
  1769. rhp_log("%s(): there are no nodes in level %d\n", __func__, i);
  1770. }
  1771. rl->number_of_nodes = 0;
  1772. }
  1773. /* put node data in levels */
  1774. spnn = rhp_sp_min(rhp_sp_master_node_list);
  1775. while (spnn) {
  1776. nd = (struct rhpnode *)spnn->value;
  1777. level = nd->level;
  1778. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  1779. if (spn == (struct rhp_spn *)0) {
  1780. rhp_log("%s(): no spn level %d shouldnothappen!\n", __func__, level);
  1781. return;
  1782. }
  1783. rl = (struct rhplevel *)spn->value;
  1784. /* set node data */
  1785. if (rl->nodes) {
  1786. rl->nodes[rl->number_of_nodes] = nd;
  1787. rl->number_of_nodes = (rl->number_of_nodes + 1);
  1788. } else {
  1789. rhp_log("%s(): should have been nodes[] for node %d level %d shouldnothappen!\n", __func__, nd->innum, nd->level);
  1790. }
  1791. /* */
  1792. spnn = rhp_sp_next(rhp_sp_master_node_list, spnn->key);
  1793. }
  1794. /* total count of nodes */
  1795. i = 0;
  1796. /* log end result */
  1797. for (level = 0; level < rhp_nlevels; level++) {
  1798. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  1799. rl = (struct rhplevel *)spn->value;
  1800. if (rl->number_of_nodes == 0) {
  1801. if (0) {
  1802. rhp_log("%s(): level %d has no nodes shouldnothappen!\n", __func__, level);
  1803. }
  1804. } else {
  1805. rhp_log("%s(): level %d has %d nodes", __func__, level, rl->number_of_nodes);
  1806. /* scan the nodes in this level */
  1807. if (rl->nodes) {
  1808. rhp_log("%s", " with numbers");
  1809. for (ii = 0; ii < rl->number_of_nodes; ii++) {
  1810. rhp_log(" %d", rl->nodes[ii]->innum);
  1811. }
  1812. }
  1813. rhp_log("%s", "\n");
  1814. }
  1815. /* update total count of nodes in all levels */
  1816. i = (i + rl->number_of_nodes);
  1817. }
  1818. rhp_log
  1819. ("%s(): in %d levels (0...%d) are %d nodes and %d number_of_nodes\n",
  1820. __func__, rhp_nlevels, rhp_maxlevel, i, rhp_number_of_nodes);
  1821. rhp_log("%s(): using %" PRIu64 " bytes for the level data\n", __func__, (uint64_t) bt);
  1822. return;
  1823. }
  1824. /* handle edges */
  1825. static void rhp_allocateadjacencylists(void)
  1826. {
  1827. struct rhp_spn *spn = (struct rhp_spn *)0;
  1828. struct rhpedge *re = (struct rhpedge *)0;
  1829. struct rhpnode *upper_node = (struct rhpnode *)0;
  1830. struct rhpnode *lower_node = (struct rhpnode *)0;
  1831. struct rhpnode *nd = (struct rhpnode *)0;
  1832. struct rhplevel *rl = (struct rhplevel *)0;
  1833. int level = 0;
  1834. int pos = 0;
  1835. int i = 0;
  1836. size_t bt = (size_t)0;
  1837. /* overall count of edges */
  1838. rhp_number_of_edges = 0;
  1839. if (rhp_sp_has_data(rhp_sp_master_edge_list) == 0) {
  1840. /* no edges is oke */
  1841. return;
  1842. }
  1843. /* scan edges */
  1844. spn = rhp_sp_min(rhp_sp_master_edge_list);
  1845. while (spn) {
  1846. re = (struct rhpedge *)spn->value;
  1847. /* overall count of edges */
  1848. rhp_number_of_edges = (rhp_number_of_edges + 1);
  1849. /* update node edge connect degree */
  1850. if (re->fn->level > re->tn->level) {
  1851. /* this should have been checked at edge creation */
  1852. rhp_log("%s(): uppernode is above lowernode shouldnothappen!\n", __func__);
  1853. upper_node = re->fn;
  1854. lower_node = re->tn;
  1855. } else {
  1856. upper_node = re->tn;
  1857. lower_node = re->fn;
  1858. }
  1859. /* update degrees */
  1860. upper_node->down_degree = (upper_node->down_degree + 1);
  1861. lower_node->up_degree = (lower_node->up_degree + 1);
  1862. spn = rhp_sp_next(rhp_sp_master_edge_list, spn->key);
  1863. }
  1864. /* */
  1865. bt = 0;
  1866. /* scan the levels */
  1867. for (level = 0; level < rhp_nlevels; level++) {
  1868. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  1869. if (spn == (struct rhp_spn *)0) {
  1870. rhp_log("%s(): no spn level %d shouldnothappen!\n", __func__, level);
  1871. return;
  1872. }
  1873. /* start pos in level is 0 */
  1874. pos = 0;
  1875. /* scan nodes in level */
  1876. rl = (struct rhplevel *)spn->value;
  1877. if (rl->nodes) {
  1878. for (i = 0; i < rl->number_of_nodes; i++) {
  1879. nd = rl->nodes[i];
  1880. /* set node initial position based on input order */
  1881. nd->position = pos;
  1882. rhp_log
  1883. ("%s(): node %d pos %d in level %d has up/down degree %d/%d\n",
  1884. __func__, nd->innum, pos, level, nd->up_degree, nd->down_degree);
  1885. if (nd->up_degree) {
  1886. nd->up_edges = (struct rhpedge * *)rhp_malloc((nd->up_degree * sizeof(struct rhpedge *)), __func__, __LINE__);
  1887. bt = (bt + (nd->up_degree * sizeof(struct rhpedge *)));
  1888. }
  1889. if (nd->down_degree) {
  1890. nd->down_edges =
  1891. (struct rhpedge * *)rhp_malloc((nd->down_degree * sizeof(struct rhpedge *)), __func__, __LINE__);
  1892. bt = (bt + (nd->down_degree * sizeof(struct rhpedge *)));
  1893. }
  1894. /* next pos in level */
  1895. pos = (pos + 1);
  1896. /* updated below */
  1897. nd->up_degree = 0;
  1898. nd->down_degree = 0;
  1899. }
  1900. }
  1901. }
  1902. rhp_log("%s(): using %" PRIu64
  1903. " additional more bytes for the node data and total now %" PRIu64
  1904. " bytes\n", __func__, (long long)bt, (long long)(bt + (rhp_number_of_nodes * sizeof(struct rhpnode))));
  1905. /* add edge data to nodes */
  1906. spn = rhp_sp_min(rhp_sp_master_edge_list);
  1907. while (spn) {
  1908. re = (struct rhpedge *)spn->value;
  1909. /* */
  1910. if (re->fn->level > re->tn->level) {
  1911. /* this should have been checked at edge creation */
  1912. rhp_log("%s(): uppernode is above lowernode shouldnothappen!\n", __func__);
  1913. upper_node = re->fn;
  1914. lower_node = re->tn;
  1915. } else {
  1916. upper_node = re->tn;
  1917. lower_node = re->fn;
  1918. }
  1919. /* */
  1920. upper_node->down_edges[upper_node->down_degree] = re;
  1921. upper_node->down_degree = (upper_node->down_degree + 1);
  1922. /* */
  1923. lower_node->up_edges[lower_node->up_degree] = re;
  1924. lower_node->up_degree = (lower_node->up_degree + 1);
  1925. /* */
  1926. spn = rhp_sp_next(rhp_sp_master_edge_list, spn->key);
  1927. }
  1928. /* */
  1929. rhp_log("%s(): number_of_edges is %d\n", __func__, rhp_number_of_edges);
  1930. /* scan nodes and print the connections */
  1931. spn = rhp_sp_min(rhp_sp_master_node_list);
  1932. while (spn) {
  1933. nd = (struct rhpnode *)spn->value;
  1934. rhp_log("%s(): node %d has up/down degree %d/%d", __func__, nd->innum, nd->up_degree, nd->down_degree);
  1935. if (nd->up_edges) {
  1936. rhp_log(" up connected with edge number");
  1937. for (i = 0; i < nd->up_degree; i++) {
  1938. re = (struct rhpedge *)nd->up_edges[i];
  1939. rhp_log(" %d", re->innum);
  1940. }
  1941. }
  1942. if (nd->down_edges) {
  1943. rhp_log(" down connected with edge number");
  1944. for (i = 0; i < nd->down_degree; i++) {
  1945. re = (struct rhpedge *)nd->down_edges[i];
  1946. rhp_log(" %d", re->innum);
  1947. }
  1948. }
  1949. rhp_log("%s", "\n");
  1950. /* */
  1951. spn = rhp_sp_next(rhp_sp_master_node_list, spn->key);
  1952. }
  1953. return;
  1954. }
  1955. /* count nodes without connections */
  1956. static int rhp_countisolatednodes(void)
  1957. {
  1958. struct rhpnode *nd = (struct rhpnode *)0;
  1959. struct rhp_spn *spn = (struct rhp_spn *)0;
  1960. int c = 0;
  1961. rhp_number_of_isolated_nodes = 0;
  1962. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  1963. /* no data */
  1964. rhp_log("%s(): no nodes shouldnothappen!\n", __func__);
  1965. return (0);
  1966. }
  1967. spn = rhp_sp_min(rhp_sp_master_node_list);
  1968. while (spn) {
  1969. nd = (struct rhpnode *)spn->value;
  1970. if ((nd->up_degree == 0) && (nd->down_degree == 0)) {
  1971. c = (c + 1);
  1972. }
  1973. spn = rhp_sp_next(rhp_sp_master_node_list, spn->key);
  1974. }
  1975. rhp_log("%s(): %d nodes without edge connections found\n", __func__, c);
  1976. return (c);
  1977. }
  1978. /* init data for edge crossing counting */
  1979. static void rhp_initcrossings(void)
  1980. {
  1981. int i = 0;
  1982. struct rhp_inter_layer_struct *is = (struct rhp_inter_layer_struct *)0;
  1983. rhp_log("%s(): rhp_nlevels is %d\n", __func__, rhp_nlevels);
  1984. if (rhp_nlevels == 0) {
  1985. rhp_log("%s(): no levels shouldnothappen!\n", __func__);
  1986. return;
  1987. }
  1988. for (i = 0; i < rhp_nlevels; i++) {
  1989. is = rhp_makeinterlayer(i);
  1990. rhp_sp_insert(rhp_sp_between_layers, (rhp_spkey) i, (rhp_spval) is);
  1991. }
  1992. return;
  1993. }
  1994. /* */
  1995. static struct rhp_inter_layer_struct *rhp_makeinterlayer(int upper_layer)
  1996. {
  1997. struct rhp_inter_layer_struct *is = (struct rhp_inter_layer_struct *)0;
  1998. size_t bt = (size_t)0;
  1999. /* fresh info for edge crossings */
  2000. is = (struct rhp_inter_layer_struct *)
  2001. rhp_malloc(sizeof(struct rhp_inter_layer_struct), __func__, __LINE__);
  2002. bt = (bt + (sizeof(struct rhp_inter_layer_struct)));
  2003. /* */
  2004. is->number_of_edges = rhp_count_down_edges(upper_layer);
  2005. /* indicate unknown number of crossings in level 64bit */
  2006. is->number_of_crossings = (int64_t) - 1;
  2007. /* optional storage of edge ptr's */
  2008. if (is->number_of_edges) {
  2009. is->eedges = (struct rhpedge **)
  2010. rhp_malloc((is->number_of_edges * sizeof(struct rhpedge *)), __func__, __LINE__);
  2011. bt = (bt + (is->number_of_edges * sizeof(struct rhpedge *)));
  2012. }
  2013. rhp_log("%s(): using %" PRIu64 " bytes for the interlevels\n", __func__, (long long)bt);
  2014. return ((struct rhp_inter_layer_struct *)is);
  2015. }
  2016. /* */
  2017. static int rhp_count_down_edges(int layer_number)
  2018. {
  2019. struct rhp_spn *spn = (struct rhp_spn *)0;
  2020. struct rhplevel *lv = (struct rhplevel *)0;
  2021. struct rhpnode *nd = (struct rhpnode *)0;
  2022. int count = 0;
  2023. int i = 0;
  2024. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) layer_number);
  2025. if (spn == (struct rhp_spn *)0) {
  2026. rhp_log("%s(): no data for level %d shouldnothappen!\n", __func__, layer_number);
  2027. return (0);
  2028. }
  2029. lv = (struct rhplevel *)spn->value;
  2030. if (lv == (struct rhplevel *)0) {
  2031. rhp_log("%s(): no nodes in level %d shouldnothappen!\n", __func__, layer_number);
  2032. return (0);
  2033. }
  2034. /* */
  2035. if (lv->nodes == (struct rhpnode **)0) {
  2036. if (0) {
  2037. rhp_log("%s(): no data for nodes in level %d shouldnothappen!\n", __func__, layer_number);
  2038. }
  2039. return (0);
  2040. }
  2041. /* */
  2042. count = 0;
  2043. /* scan nodes in this level */
  2044. if (lv->nodes) {
  2045. for (i = 0; i < lv->number_of_nodes; i++) {
  2046. nd = lv->nodes[i];
  2047. /* */
  2048. count = (count + nd->down_degree);
  2049. }
  2050. }
  2051. rhp_log("%s(): down_degree count is %d at level %d\n", __func__, count, layer_number);
  2052. return (count);
  2053. }
  2054. /* */
  2055. static void rhp_updateallcrossings(void)
  2056. {
  2057. int i = 0;
  2058. struct rhp_spn *spnb = (struct rhp_spn *)0;
  2059. struct rhp_inter_layer_struct *is = NULL;
  2060. /* update node positions */
  2061. rhp_updateallpositions();
  2062. /* make sure crossing count is preset to 0 */
  2063. for (i = 1; i < rhp_nlevels; i++) {
  2064. /* */
  2065. spnb = rhp_sp_lookup(rhp_sp_between_layers, (rhp_spkey) i);
  2066. if (spnb) {
  2067. is = (struct rhp_inter_layer_struct *)spnb->value;
  2068. is->number_of_crossings = 0;
  2069. } else {
  2070. rhp_log("%s(): could not find level %d shouldnothappen!\n", __func__, i);
  2071. }
  2072. }
  2073. for (i = 1; i < rhp_nlevels; i++) {
  2074. /* update at every level total number of edge crossings */
  2075. rhp_updatecrossingsbetweenlayers(i);
  2076. }
  2077. return;
  2078. }
  2079. /* */
  2080. static void rhp_updateallpositions(void)
  2081. {
  2082. int i = 0;
  2083. rhp_log("%s(): updating all node positions in %d levels\n", __func__, rhp_nlevels);
  2084. for (i = 0; i < rhp_nlevels; i++) {
  2085. rhp_updatenodepositions(i);
  2086. }
  2087. return;
  2088. }
  2089. /* */
  2090. static void rhp_updatenodepositions(int layer_number)
  2091. {
  2092. struct rhp_spn *spn = (struct rhp_spn *)0;
  2093. struct rhplevel *lv = (struct rhplevel *)0;
  2094. struct rhpnode *nd = (struct rhpnode *)0;
  2095. int pos = 0;
  2096. int i = 0;
  2097. rhp_log("%s(): updating node positions for level %d\n", __func__, layer_number);
  2098. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) layer_number);
  2099. if (spn == (struct rhp_spn *)0) {
  2100. if (0) {
  2101. rhp_log("%s(): no data for level %d shouldnothappen!\n", __func__, layer_number);
  2102. }
  2103. return;
  2104. }
  2105. /* */
  2106. lv = (struct rhplevel *)spn->value;
  2107. if (lv == (struct rhplevel *)0) {
  2108. rhp_log("%s(): no nodes in level %d shouldnothappen!\n", __func__, layer_number);
  2109. return;
  2110. }
  2111. /* */
  2112. if (lv->nodes == (struct rhpnode **)0) {
  2113. if (0) {
  2114. rhp_log("%s(): no data for nodes in level %d shouldnothappen!\n", __func__, layer_number);
  2115. }
  2116. return;
  2117. }
  2118. /* */
  2119. pos = 0;
  2120. /* scan nodes in this level */
  2121. for (i = 0; i < lv->number_of_nodes; i++) {
  2122. nd = lv->nodes[i];
  2123. /* position within layer */
  2124. nd->position = pos;
  2125. rhp_log("%s(): node %d level %d pos %d weight %d\n", __func__, nd->innum, nd->level, nd->position, nd->weight);
  2126. pos = (pos + 1);
  2127. }
  2128. return;
  2129. }
  2130. /* */
  2131. static void rhp_updatecrossingsforlayer(int layer)
  2132. {
  2133. rhp_updatenodepositions(layer);
  2134. /* this is correct */
  2135. if (layer > 0) {
  2136. rhp_updatecrossingsbetweenlayers(layer);
  2137. }
  2138. /* */
  2139. if (layer < (rhp_nlevels - 1)) {
  2140. rhp_updatecrossingsbetweenlayers(layer + 1);
  2141. }
  2142. return;
  2143. }
  2144. /* */
  2145. static void rhp_updatecrossingsbetweenlayers(int upper_layer)
  2146. {
  2147. struct rhp_spn *spn = (struct rhp_spn *)0;
  2148. struct rhp_spn *spnb = (struct rhp_spn *)0;
  2149. struct rhplevel *lv = (struct rhplevel *)0;
  2150. struct rhpnode *nd = (struct rhpnode *)0;
  2151. struct rhp_inter_layer_struct *is = (struct rhp_inter_layer_struct *)0;
  2152. int ix = 0;
  2153. int i = 0;
  2154. int ii = 0;
  2155. int64_t ncross = (int64_t) 0;
  2156. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) upper_layer);
  2157. if (spn == (struct rhp_spn *)0) {
  2158. if (0) {
  2159. rhp_log("%s(): no data for level %d shouldnothappen!\n", __func__, upper_layer);
  2160. }
  2161. return;
  2162. }
  2163. /* */
  2164. lv = (struct rhplevel *)spn->value;
  2165. if (lv == (struct rhplevel *)0) {
  2166. rhp_log("%s(): no nodes in level %d shouldnothappen!\n", __func__, upper_layer);
  2167. return;
  2168. }
  2169. /* */
  2170. if (lv->nodes == (struct rhpnode **)0) {
  2171. if (0) {
  2172. rhp_log("%s(): no data for nodes in level %d shouldnothappen!\n", __func__, upper_layer);
  2173. }
  2174. /* */
  2175. spnb = rhp_sp_lookup(rhp_sp_between_layers, (rhp_spkey) upper_layer);
  2176. if (spnb) {
  2177. is = (struct rhp_inter_layer_struct *)spnb->value;
  2178. ncross = 0;
  2179. is->number_of_crossings = ncross;
  2180. } else {
  2181. rhp_log("%s(): could not find level %d in between_layers shouldnothappen!\n", __func__, upper_layer);
  2182. }
  2183. return;
  2184. }
  2185. /* */
  2186. for (i = 0; i < lv->number_of_nodes; i++) {
  2187. nd = lv->nodes[i];
  2188. /* only sort if there is something to sort */
  2189. if (nd->down_degree > 1) {
  2190. rhp_sortbydownnodeposition(nd->down_edges, nd->down_degree);
  2191. }
  2192. /* */
  2193. if (nd->down_degree > 0) {
  2194. spnb = rhp_sp_lookup(rhp_sp_between_layers, (rhp_spkey) upper_layer);
  2195. if (spnb) {
  2196. is = (struct rhp_inter_layer_struct *)spnb->value;
  2197. /* copy from between->edges to node->down_edges node->down_degree items starting with index ix */
  2198. rhp_add_edges_to_array(is->eedges, nd->down_edges, nd->down_degree, ix);
  2199. }
  2200. }
  2201. /* */
  2202. ix = (ix + nd->down_degree);
  2203. }
  2204. /* init */
  2205. for (i = 0; i < lv->number_of_nodes; i++) {
  2206. nd = lv->nodes[i];
  2207. nd->down_crossings = (int64_t) 0;
  2208. if (nd->down_degree > 0) {
  2209. for (ii = 0; ii < nd->down_degree; ii++) {
  2210. nd->down_edges[ii]->crossings = (int64_t) 0;
  2211. }
  2212. }
  2213. }
  2214. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) (upper_layer - 1));
  2215. if (spn == (struct rhp_spn *)0) {
  2216. if (0) {
  2217. rhp_log("%s(): no data for level %d shouldnothappen!\n", __func__, (upper_layer - 1));
  2218. }
  2219. return;
  2220. }
  2221. /* */
  2222. lv = (struct rhplevel *)spn->value;
  2223. if (lv == (struct rhplevel *)0) {
  2224. rhp_log("%s(): no nodes in level %d shouldnothappen!\n", __func__, (upper_layer - 1));
  2225. return;
  2226. }
  2227. /* */
  2228. if (lv->nodes == (struct rhpnode **)0) {
  2229. if (0) {
  2230. rhp_log("%s(): no data for nodes in level %d shouldnothappen!\n", __func__, (upper_layer - 1));
  2231. }
  2232. return;
  2233. }
  2234. for (i = 0; i < lv->number_of_nodes; i++) {
  2235. nd = lv->nodes[i];
  2236. nd->up_crossings = (int64_t) 0;
  2237. }
  2238. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) (upper_layer));
  2239. if (spn == (struct rhp_spn *)0) {
  2240. if (0) {
  2241. rhp_log("%s(): no data for level %d shouldnothappen!\n", (upper_layer));
  2242. }
  2243. return;
  2244. }
  2245. /* */
  2246. lv = (struct rhplevel *)spn->value;
  2247. rhp_log("%s(): level %d has %d nodes:\n", __func__, upper_layer, lv->number_of_nodes);
  2248. for (i = 0; i < lv->number_of_nodes; i++) {
  2249. nd = lv->nodes[i];
  2250. rhp_log("%s(): node %d down-degree %d connected to edges:", __func__, nd->innum, nd->down_degree);
  2251. if (nd->down_degree > 0) {
  2252. for (ii = 0; ii < nd->down_degree; ii++) {
  2253. rhp_log(" %d", nd->down_edges[ii]->innum);
  2254. }
  2255. }
  2256. rhp_log("%s", "\n");
  2257. }
  2258. /* */
  2259. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) (upper_layer - 1));
  2260. if (spn == (struct rhp_spn *)0) {
  2261. if (0) {
  2262. rhp_log("%s(): no data for level %d shouldnothappen!\n", (upper_layer - 1));
  2263. }
  2264. return;
  2265. }
  2266. /* */
  2267. lv = (struct rhplevel *)spn->value;
  2268. if (lv == (struct rhplevel *)0) {
  2269. rhp_log("%s(): no nodes in level %d shouldnothappen!\n", (upper_layer - 1));
  2270. return;
  2271. }
  2272. /* */
  2273. if (lv->nodes == (struct rhpnode **)0) {
  2274. if (0) {
  2275. rhp_log("%s(): no data for nodes in level %d shouldnothappen!\n", (upper_layer - 1));
  2276. }
  2277. return;
  2278. }
  2279. rhp_log("%s(): level %d has %d nodes:", __func__, (upper_layer - 1), lv->number_of_nodes);
  2280. for (i = 0; i < lv->number_of_nodes; i++) {
  2281. nd = lv->nodes[i];
  2282. rhp_log(" %d", nd->innum);
  2283. }
  2284. rhp_log("%s", "\n");
  2285. /* */
  2286. spnb = rhp_sp_lookup(rhp_sp_between_layers, (rhp_spkey) upper_layer);
  2287. if (spnb) {
  2288. is = (struct rhp_inter_layer_struct *)spnb->value;
  2289. ncross = rhp_count_inversions_down(is->eedges, is->number_of_edges, 1);
  2290. is->number_of_crossings = ncross;
  2291. } else {
  2292. rhp_log("%s(): could not find level %d in between_layers shouldnothappen!\n", __func__, upper_layer);
  2293. }
  2294. rhp_log("%s(): %" PRIi64 " crossings at level %d\n", __func__, (int64_t) ncross, upper_layer);
  2295. return;
  2296. }
  2297. /* */
  2298. static int rhp_compare_down_edges(const void *ptr_i, const void *ptr_j)
  2299. {
  2300. struct rhpedge **entry_ptr_i = (struct rhpedge * *)0;
  2301. struct rhpedge **entry_ptr_j = (struct rhpedge * *)0;
  2302. struct rhpedge *edge_i = (struct rhpedge *)0;
  2303. struct rhpedge *edge_j = (struct rhpedge *)0;
  2304. entry_ptr_i = (struct rhpedge * *)ptr_i;
  2305. entry_ptr_j = (struct rhpedge * *)ptr_j;
  2306. edge_i = *entry_ptr_i;
  2307. edge_j = *entry_ptr_j;
  2308. /* possible here to abs() and compare and return 0 at almost at same pos */
  2309. /* position is an int */
  2310. if (edge_i->down_node->position > edge_j->down_node->position) {
  2311. return (1);
  2312. } else if (edge_i->down_node->position < edge_j->down_node->position) {
  2313. return (-1);
  2314. } else {
  2315. /* == pos */
  2316. return (0);
  2317. }
  2318. }
  2319. /* */
  2320. static void rhp_sortbydownnodeposition(struct rhpedge **edge_array, int num_edges)
  2321. {
  2322. /* there can be multiple entries with same pos as key */
  2323. qsort(edge_array, num_edges, sizeof(struct rhpedge *), rhp_compare_down_edges);
  2324. return;
  2325. }
  2326. /* copy to edge_array from edges_to_add num_edges items starting at offset start_pos in edge_array */
  2327. static void rhp_add_edges_to_array(struct rhpedge **edge_array, struct rhpedge **edges_to_add, int num_edges, int start_pos)
  2328. {
  2329. int edges_added = 0;
  2330. if (num_edges <= 0) {
  2331. rhp_log("%s(): %d num_edges is too low shouldnothappen!\n", __func__, num_edges);
  2332. return;
  2333. }
  2334. for (edges_added = 0; edges_added < num_edges; edges_added++) {
  2335. edge_array[(start_pos + edges_added)] = edges_to_add[edges_added];
  2336. }
  2337. return;
  2338. }
  2339. /* diff=1 means increment edge crossing count with 1 at every edge crossing */
  2340. static int64_t rhp_count_inversions_down(struct rhpedge **edge_array, int num_of_edges, int diff)
  2341. {
  2342. int64_t number_of_inversions = (int64_t) 0;
  2343. int i = 0;
  2344. /* for every edge */
  2345. for (i = 1; i < num_of_edges; i++) {
  2346. /* now start at eedges[i] position and increment edge crossing count with 1 */
  2347. number_of_inversions = (number_of_inversions + rhp_insert_and_count_inversions_down(edge_array, i, diff));
  2348. }
  2349. return ((int64_t) number_of_inversions);
  2350. }
  2351. /* at every edge */
  2352. static int64_t rhp_insert_and_count_inversions_down(struct rhpedge **edge_array, int starting_index, int diff)
  2353. {
  2354. int64_t number_of_crossings = (int64_t) 0;
  2355. int ix = 0;
  2356. struct rhpedge *edge_to_insert = (struct rhpedge *)0;
  2357. struct rhpedge *edge_one = (struct rhpedge *)0;
  2358. struct rhpedge *edge_two = (struct rhpedge *)0;
  2359. struct rhpnode *up_node_one = (struct rhpnode *)0;
  2360. struct rhpnode *up_node_two = (struct rhpnode *)0;
  2361. struct rhpnode *down_node_one = (struct rhpnode *)0;
  2362. struct rhpnode *down_node_two = (struct rhpnode *)0;
  2363. /* crossings at this edge */
  2364. number_of_crossings = (int64_t) 0;
  2365. /* check param */
  2366. if (starting_index <= 0) {
  2367. rhp_log("%s(): starting_index is %d shouldnothappen!\n", __func__, starting_index);
  2368. return ((int64_t) number_of_crossings);
  2369. }
  2370. /* */
  2371. ix = (starting_index - 1);
  2372. /* */
  2373. edge_to_insert = edge_array[starting_index];
  2374. /* check param */
  2375. if (edge_array[ix] == (struct rhpedge *)0) {
  2376. rhp_log("%s(): edge_array[%d] is nil and starting_index is %d shouldnothappen!\n", __func__, ix, starting_index);
  2377. return ((int64_t) number_of_crossings);
  2378. }
  2379. /* compare the position of the edges */
  2380. while ((ix >= 0)
  2381. && (edge_array[ix]->down_node->position > edge_to_insert->down_node->position)) {
  2382. /* one more edge crossing */
  2383. number_of_crossings = (number_of_crossings + 1);
  2384. /* */
  2385. edge_one = edge_array[ix];
  2386. edge_two = edge_to_insert;
  2387. /* */
  2388. edge_one->crossings += diff;
  2389. edge_two->crossings += diff;
  2390. /* */
  2391. up_node_one = edge_one->up_node;
  2392. up_node_two = edge_two->up_node;
  2393. /* */
  2394. down_node_one = edge_one->down_node;
  2395. down_node_two = edge_two->down_node;
  2396. /* */
  2397. up_node_one->down_crossings = (up_node_one->down_crossings + diff);
  2398. up_node_two->down_crossings = (up_node_two->down_crossings + diff);
  2399. /* */
  2400. down_node_one->up_crossings = (down_node_one->up_crossings + diff);
  2401. down_node_two->up_crossings = (down_node_two->up_crossings + diff);
  2402. /* */
  2403. edge_array[(ix + 1)] = edge_array[ix];
  2404. /* until 0 */
  2405. ix = (ix - 1);
  2406. }
  2407. edge_array[ix + 1] = edge_to_insert;
  2408. rhp_log("%s() at edge %d are %" PRIi64 " crossings\n", __func__, edge_to_insert->innum, number_of_crossings);
  2409. return ((int64_t) number_of_crossings);
  2410. }
  2411. /* sum in all levels total number of edge crossings determinated in rhp_updateallcrossings() */
  2412. static int64_t rhp_numberofcrossings(void)
  2413. {
  2414. struct rhp_spn *spnb = (struct rhp_spn *)0;
  2415. struct rhp_inter_layer_struct *is = (struct rhp_inter_layer_struct *)0;
  2416. int i = 1;
  2417. int64_t crossings = 0;
  2418. /* if there is only 1 level there cannot be crossing edges */
  2419. if (rhp_nlevels < 2) {
  2420. rhp_log("%s(): graph has no edge crossings because low number of levels which is %d\n", __func__, rhp_nlevels);
  2421. return (0);
  2422. }
  2423. /* if there is only 1 or 0 edges then there cannot be crossing edges */
  2424. if (rhp_number_of_edges < 2) {
  2425. rhp_log("%s(): graph has no edge crossings because low number of edge which is %d\n", __func__, rhp_number_of_edges);
  2426. return (0);
  2427. }
  2428. for (i = 1; i < rhp_nlevels; i++) {
  2429. /* */
  2430. spnb = rhp_sp_lookup(rhp_sp_between_layers, (rhp_spkey) i);
  2431. if (spnb) {
  2432. is = (struct rhp_inter_layer_struct *)spnb->value;
  2433. crossings = (crossings + is->number_of_crossings);
  2434. } else {
  2435. rhp_log("%s(): could not find level %d shouldnothappen!\n", __func__, i);
  2436. }
  2437. }
  2438. rhp_log("%s(): graph has %" PRIi64
  2439. " edge crossings in %d levels and %d edges\n", __func__, crossings, rhp_nlevels, rhp_number_of_edges);
  2440. return ((int64_t) crossings);
  2441. }
  2442. /****zz2****/
  2443. /* init save layout order */
  2444. static void rhp_order_init(void)
  2445. {
  2446. struct rhp_spn *spn = (struct rhp_spn *)0;
  2447. struct rhplevel *rl = (struct rhplevel *)0;
  2448. int level = 0;
  2449. size_t tb = (size_t)0;
  2450. rhp_log("%s(): rhp_nlevels is %d\n", __func__, rhp_nlevels);
  2451. /* main info */
  2452. rhp_best_crossings_order = (struct rhp_order_struct *)rhp_malloc(sizeof(struct rhp_order_struct), __func__, __LINE__);
  2453. tb = (tb + sizeof(struct rhp_order_struct));
  2454. /* how many levels */
  2455. rhp_best_crossings_order->num_layers = rhp_nlevels;
  2456. /* how many nodes in every level */
  2457. rhp_best_crossings_order->num_nodes_on_layer = (int *)rhp_malloc((rhp_nlevels * sizeof(int)), __func__, __LINE__);
  2458. tb = (tb + (rhp_nlevels * sizeof(int)));
  2459. /* nodes on level */
  2460. rhp_best_crossings_order->node_ptr_on_layer =
  2461. (struct rhpnode * **)rhp_malloc((rhp_nlevels * sizeof(struct rhpnode **)), __func__, __LINE__);
  2462. tb = (tb + (rhp_nlevels * sizeof(struct rhpnode **)));
  2463. if (rhp_sp_has_data(rhp_sp_layers) == 0) {
  2464. /* if there is only 1 level with single nodes, no edges then nlevels is 1 and this is oke. */
  2465. if (rhp_nlevels > 1) {
  2466. rhp_log("%s(): there is no level data for %d levels shouldnothappen!\n", __func__, rhp_nlevels);
  2467. }
  2468. return;
  2469. }
  2470. /* scan the levels */
  2471. for (level = 0; level < rhp_nlevels; level++) {
  2472. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  2473. /* nodes in level */
  2474. if (spn == (struct rhp_spn *)0) {
  2475. rhp_log
  2476. ("%s(): could not get data for level %d in sp_layers=%p shouldnothappen!\n",
  2477. __func__, level, (void *)rhp_sp_layers);
  2478. continue;
  2479. }
  2480. /* level data */
  2481. rl = (struct rhplevel *)spn->value;
  2482. rhp_best_crossings_order->num_nodes_on_layer[level] = rl->number_of_nodes;
  2483. /* only if there are nodes in level which normally is so. */
  2484. if (rl->number_of_nodes) {
  2485. /* fresh buffer */
  2486. rhp_best_crossings_order->node_ptr_on_layer[level] =
  2487. (struct rhpnode * *)rhp_malloc((rl->number_of_nodes * sizeof(struct rhpnode *)), __func__, __LINE__);
  2488. tb = (tb + (rl->number_of_nodes * sizeof(struct rhpnode *)));
  2489. }
  2490. }
  2491. /* create initial copy of the node status */
  2492. rhp_save_order();
  2493. rhp_log("%s(): using %" PRIu64 " bytes for the order data\n", __func__, (long long)tb);
  2494. return;
  2495. }
  2496. /* mirror node setting in backup */
  2497. static void rhp_save_order(void)
  2498. {
  2499. struct rhp_spn *spn = (struct rhp_spn *)0;
  2500. struct rhplevel *rl = (struct rhplevel *)0;
  2501. struct rhpnode *nd = (struct rhpnode *)0;
  2502. int level = 0;
  2503. int i = 0;
  2504. /* how many levels */
  2505. rhp_best_crossings_order->num_layers = rhp_nlevels;
  2506. /* scan the levels */
  2507. for (level = 0; level < rhp_nlevels; level++) {
  2508. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  2509. if (spn == (struct rhp_spn *)0) {
  2510. rhp_log("%s(): could not get data for level %d shouldnothappen!\n", __func__, level);
  2511. continue;
  2512. }
  2513. /* nodes in level */
  2514. rl = (struct rhplevel *)spn->value;
  2515. rhp_log("%s(): level %d has nodes:", __func__, level);
  2516. /* scan all nodes in this level */
  2517. for (i = 0; i < rl->number_of_nodes; i++) {
  2518. /* get node from layout level */
  2519. nd = rl->nodes[i];
  2520. /* copy node into backup */
  2521. rhp_best_crossings_order->node_ptr_on_layer[level][i] = nd;
  2522. rhp_log(" %d", nd->innum);
  2523. }
  2524. rhp_log("\n");
  2525. }
  2526. return;
  2527. }
  2528. /* mirror backup setting to nodes */
  2529. static void rhp_restore_order(void)
  2530. {
  2531. struct rhp_spn *spn = (struct rhp_spn *)0;
  2532. struct rhplevel *rl = (struct rhplevel *)0;
  2533. struct rhpnode *nd = (struct rhpnode *)0;
  2534. int level = 0;
  2535. int i = 0;
  2536. rhp_log("%s(): \n", __func__);
  2537. /* how many levels */
  2538. rhp_best_crossings_order->num_layers = rhp_nlevels;
  2539. /* scan the levels */
  2540. for (level = 0; level < rhp_nlevels; level++) {
  2541. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  2542. /* nodes in level */
  2543. if (spn == (struct rhp_spn *)0) {
  2544. rhp_log("%s(): could not get data for level %d shouldnothappen!\n", __func__, level);
  2545. continue;
  2546. }
  2547. /* level data */
  2548. rl = (struct rhplevel *)spn->value;
  2549. /* scan all nodes in this level */
  2550. for (i = 0; i < rl->number_of_nodes; i++) {
  2551. /* get node from layout level */
  2552. nd = rhp_best_crossings_order->node_ptr_on_layer[level][i];
  2553. /* copy node from backup */
  2554. rl->nodes[i] = nd;
  2555. /* set node position */
  2556. nd->position = i;
  2557. }
  2558. }
  2559. return;
  2560. }
  2561. /****zz3****/
  2562. /* */
  2563. static void rhp_barycenter(void)
  2564. {
  2565. int64_t redu = (int64_t) 0;
  2566. /* maxiter can be made better depending on levels, nodes+edge etc and/or configurable */
  2567. rhp_iter = 0;
  2568. rhp_maxiter = 255;
  2569. rhp_log("%s(): starting barycenter with %" PRIi64 " edge crossings at start\n", __func__, rhp_start_crossings);
  2570. /* at least once */
  2571. do {
  2572. /* return if max iterations have been reached */
  2573. if (rhp_barycenterupsweep(1, (rhp_nlevels - 2))) {
  2574. return;
  2575. }
  2576. if (rhp_barycenterdownsweep(1, (rhp_nlevels - 2))) {
  2577. return;
  2578. }
  2579. }
  2580. while (rhp_terminate() == 0 /* false */ );
  2581. /* */
  2582. rhp_log("%s(): ending barycenter with %" PRIi64
  2583. " edge crossings and started with %" PRIi64 " edge crossings at start\n", __func__, rhp_crossings, rhp_start_crossings);
  2584. /* calculate percentage of edge crossing reduction */
  2585. if (rhp_start_crossings) {
  2586. redu = ((100 * rhp_crossings) / rhp_start_crossings);
  2587. redu = (100 - redu);
  2588. } else {
  2589. redu = 0;
  2590. }
  2591. rhp_log("%s(): reduced edge crossings with %" PRIi64 " percent from %"
  2592. PRIi64 "->%" PRIi64 "\n", __func__, redu, rhp_start_crossings, rhp_crossings);
  2593. return;
  2594. }
  2595. /* return 1 to stop barycenter main sweeping */
  2596. static int rhp_terminate(void)
  2597. {
  2598. uint64_t redu = (uint64_t) 0;
  2599. int64_t rhp_cur_crossings = (int64_t) 0;
  2600. int status = 0;
  2601. int better = 0;
  2602. /* get current number of crossings */
  2603. rhp_cur_crossings = rhp_numberofcrossings();
  2604. /* update iteration count */
  2605. rhp_iter = (rhp_iter + 1);
  2606. rhp_log("%s(): changed from %" PRIi64 " to %" PRIi64 " crossings\n", __func__, rhp_crossings, rhp_cur_crossings);
  2607. if (rhp_cur_crossings < rhp_crossings) {
  2608. /* better */
  2609. better = 1;
  2610. rhp_improvements = (rhp_improvements + 1);
  2611. } else {
  2612. better = 0;
  2613. rhp_notimprovements = (rhp_notimprovements + 1);
  2614. }
  2615. /* run callback routine if set */
  2616. if (rhp_getlayoutdata) {
  2617. /* calculate percentage of edge crossing reduction */
  2618. if (rhp_crossings) {
  2619. redu = ((100 * rhp_cur_crossings) / rhp_crossings);
  2620. redu = (100 - redu);
  2621. } else {
  2622. redu = 0;
  2623. }
  2624. /* run the callback */
  2625. status =
  2626. rhp_getlayoutdata(rhp_iter, rhp_maxiter, rhp_cur_crossings,
  2627. rhp_crossings, redu, better, rhp_improvements, rhp_notimprovements);
  2628. /* if to stop */
  2629. if (status) {
  2630. rhp_log
  2631. ("%s(): stop barycenter because status %d from callback routine at %"
  2632. PRIu64 " crossings\n", __func__, status, rhp_cur_crossings);
  2633. /* save this status */
  2634. rhp_save_order();
  2635. /* set current crossings */
  2636. rhp_crossings = rhp_cur_crossings;
  2637. /* stop barycenter */
  2638. return (1);
  2639. }
  2640. }
  2641. /* stop if no crossings anymore */
  2642. if ((rhp_cur_crossings == 0) || (rhp_crossings == 0)) {
  2643. rhp_log("%s(): stop barycenter because of no edge crossings cur=%"
  2644. PRIu64 " old=%" PRIu64 "\n", __func__, rhp_cur_crossings, rhp_crossings);
  2645. /* save this status */
  2646. rhp_save_order();
  2647. /* set current crossings */
  2648. rhp_crossings = rhp_cur_crossings;
  2649. /* stop barycenter */
  2650. return (1);
  2651. }
  2652. /* this can be done better on amount of change,
  2653. * avg. number of crossings per level or
  2654. * low cross count compared to nodes+edge
  2655. * or configurable setting
  2656. * or sweep level range can be changed here for the next sweeps
  2657. * to limit to a specific difficult part
  2658. * or other barycenter params can be changed on-the-fly
  2659. * etc.
  2660. */
  2661. if (rhp_crossings > rhp_cur_crossings) {
  2662. /* now less crossings save this status */
  2663. rhp_save_order();
  2664. /* set current crossings */
  2665. rhp_crossings = rhp_cur_crossings;
  2666. /* check if no crossings anymore */
  2667. if (rhp_crossings == 0) {
  2668. /* stop barycenter */
  2669. return (1);
  2670. }
  2671. /* check if max iteration reached */
  2672. if (rhp_iter > rhp_maxiter) {
  2673. /* stop barycenter */
  2674. return (1);
  2675. } else {
  2676. /* continue barycenter */
  2677. return (0);
  2678. }
  2679. } else {
  2680. if (rhp_crossings != rhp_cur_crossings) {
  2681. /* now more crossings and continue with saved best status */
  2682. rhp_restore_order();
  2683. } else {
  2684. rhp_save_order();
  2685. }
  2686. /* check if max iteration reached */
  2687. if (rhp_iter > rhp_maxiter) {
  2688. /* stop barycenter */
  2689. return (1);
  2690. }
  2691. /* continue barycenter */
  2692. return (0);
  2693. }
  2694. }
  2695. /* return 1 to stop barycenter single sweep */
  2696. static int rhp_end_of_iteration(void)
  2697. {
  2698. /* no stop here to allow bot up+down to run */
  2699. return (0);
  2700. }
  2701. /* sweep from lo to hi level */
  2702. static int rhp_barycenterupsweep(int lowlevel, int hilevel)
  2703. {
  2704. int layer = 0;
  2705. int nadj = 0;
  2706. for (layer = lowlevel; layer < hilevel; layer++) {
  2707. /* set node weight */
  2708. nadj = rhp_barycenterweights(layer, 0 /* DOWNWARD */ );
  2709. if (nadj) {
  2710. /* there are nodes weight (-1) to adjust */
  2711. rhp_barycenterweights_adjust(layer, 0 /* DOWNWARD */ );
  2712. }
  2713. /* sort depending on node weight */
  2714. rhp_layersort(layer);
  2715. /* get number of edge crossings */
  2716. rhp_updatecrossingsforlayer(layer);
  2717. if (rhp_end_of_iteration()) {
  2718. /* stop main barycenter sweeps */
  2719. return (1); /* true */
  2720. }
  2721. }
  2722. /* return 0 to keep sweeping */
  2723. return (0);
  2724. }
  2725. /* sweep from hi to lo level */
  2726. static int rhp_barycenterdownsweep(int lowlevel, int hilevel)
  2727. {
  2728. int layer = 0;
  2729. int nadj = 0;
  2730. /* also to layer==0 */
  2731. for (layer = hilevel; layer >= (lowlevel - 1); layer--) {
  2732. /* set node weights */
  2733. nadj = rhp_barycenterweights(layer, 1 /* UPWARD */ );
  2734. if (nadj) {
  2735. /* there are nodes weight (-1) to adjust */
  2736. rhp_barycenterweights_adjust(layer, 1 /* UPWARD */ );
  2737. }
  2738. /* sort nodes depending on weight */
  2739. rhp_layersort(layer);
  2740. /* get edge crossing count */
  2741. rhp_updatecrossingsforlayer(layer);
  2742. if (rhp_end_of_iteration()) {
  2743. /* stop main barycenter sweeps */
  2744. return (1); /* true */
  2745. }
  2746. }
  2747. /* return 0 to keep sweeping */
  2748. return (0);
  2749. }
  2750. /*
  2751. * 0 is orientation DOWNWARD
  2752. * 1 is orientation UPWARD
  2753. */
  2754. static int rhp_barycenterweights(int level, int orientation)
  2755. {
  2756. struct rhp_spn *spn = (struct rhp_spn *)0;
  2757. struct rhplevel *rl = (struct rhplevel *)0;
  2758. int i = 0;
  2759. int n_node_adjust = 0;
  2760. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  2761. if (spn == (struct rhp_spn *)0) {
  2762. rhp_log("%s(): could not find data for level %d shouldnothappen!\n", __func__, level);
  2763. return (0);
  2764. }
  2765. rl = (struct rhplevel *)spn->value;
  2766. rhp_log("%s(): weights for %d nodes in level %d mode %d\n", __func__, rl->number_of_nodes, level, orientation);
  2767. /* number of node which need adjust */
  2768. n_node_adjust = 0;
  2769. for (i = 0; i < rl->number_of_nodes; i++) {
  2770. /* for every node in this level new weight */
  2771. rhp_node_weight(rl->nodes[i], orientation);
  2772. /* check node weight set */
  2773. if (rl->nodes[i]->weight == (int)(-1)) {
  2774. /* this node needs adjust */
  2775. n_node_adjust = (n_node_adjust + 1);
  2776. }
  2777. }
  2778. if (n_node_adjust) {
  2779. rhp_log
  2780. ("%s(): iteration %d %d nodes need adjust from %d nodes in level %d mode %d\n",
  2781. __func__, rhp_iter, n_node_adjust, rl->number_of_nodes, level, orientation);
  2782. }
  2783. /* set number of node adjustments todo */
  2784. rl->number_adjustments = n_node_adjust;
  2785. return (n_node_adjust);
  2786. }
  2787. /* adjust nodes with weight (-1) */
  2788. static void rhp_barycenterweights_adjust(int level, int orientation)
  2789. {
  2790. struct rhp_spn *spn = (struct rhp_spn *)0;
  2791. struct rhplevel *rl = (struct rhplevel *)0;
  2792. struct rhpnode *node = (struct rhpnode *)0;
  2793. int number_of_weights = 0;
  2794. int total_weight = 0;
  2795. int i = 0;
  2796. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  2797. if (spn == (struct rhp_spn *)0) {
  2798. rhp_log("%s(): could not find data for level %d shouldnothappen!\n", __func__, level);
  2799. return;
  2800. }
  2801. rl = (struct rhplevel *)spn->value;
  2802. rhp_log
  2803. ("%s(): to adjust %d weights for %d nodes in level %d orientation %d\n",
  2804. __func__, number_of_weights, rl->number_of_nodes, level, orientation);
  2805. for (i = 0; i < rl->number_of_nodes; i++) {
  2806. /* adjust if -1 weight based on avg */
  2807. if (rl->nodes[i]->weight == (int)(-1)) {
  2808. node = rl->nodes[i];
  2809. /* if set to 1 do avg mode, else left mode */
  2810. if (rhp_adjustweight) {
  2811. /* */
  2812. number_of_weights = 0;
  2813. total_weight = 0;
  2814. if (i <= 0) {
  2815. /* */
  2816. }
  2817. if (i > 0) {
  2818. number_of_weights = (number_of_weights + 1);
  2819. total_weight = (total_weight + rl->nodes[i - 1]->weight);
  2820. }
  2821. if ((i < (rl->number_of_nodes - 1))
  2822. && (rl->nodes[i + 1]->weight >= 0)) {
  2823. number_of_weights = (number_of_weights + 1);
  2824. total_weight = (total_weight + rl->nodes[i + 1]->weight);
  2825. }
  2826. if (number_of_weights > 0) {
  2827. node->weight = (100 * total_weight);
  2828. node->weight = (int)(node->weight / number_of_weights);
  2829. } else {
  2830. /* leftmost node has a right neighbor with weight of -1 */
  2831. node->weight = 0;
  2832. }
  2833. rhp_log("%s(): adjusted node[%d] weight from -1 to %d (avg mode)\n", __func__, i, node->weight);
  2834. } else {
  2835. /* left mode */
  2836. if (i <= 0) {
  2837. /* do not go more left then minimum 0 */
  2838. node->weight = 0;
  2839. } else {
  2840. /* take weight of node left from this one */
  2841. node->weight = rl->nodes[i - 1]->weight;
  2842. }
  2843. rhp_log("%s(): adjusted node[%d] weight from -1 to %d (leftish mode)\n", __func__, i, node->weight);
  2844. }
  2845. }
  2846. }
  2847. return;
  2848. }
  2849. /*
  2850. * 0 is orientation DOWNWARD
  2851. * 1 is orientation UPWARD
  2852. */
  2853. static void rhp_node_weight(struct rhpnode *node, int orientation)
  2854. {
  2855. int total_degree1 = 0;
  2856. int total_degree2 = 0;
  2857. int total_degree = 0;
  2858. int total_of_positions = 0;
  2859. int adj_index = 0;
  2860. /* this influences positioning and can be done better */
  2861. rhp_log("%s(): at node %d up degree %d down degree %d mode %d\n", __func__,
  2862. node->innum, node->up_degree, node->down_degree, orientation);
  2863. if ((node->up_degree + node->down_degree) == 0) {
  2864. /* isolated nodes to the far left */
  2865. node->weight = 0;
  2866. rhp_log("%s(): node %d has weight set (0) isolated node special mode %d\n", __func__, node->innum, orientation);
  2867. return;
  2868. }
  2869. if (orientation != 1 /* UPWARD */ ) {
  2870. /* is DOWNWARD, BOTH, NONE */
  2871. total_degree1 = node->down_degree;
  2872. for (adj_index = 0; adj_index < node->down_degree; adj_index++) {
  2873. total_of_positions = (total_of_positions + node->down_edges[adj_index]->down_node->position);
  2874. }
  2875. }
  2876. if (orientation != 0 /* DOWNWARD */ ) {
  2877. /* is UPWARD, BOTH, NONE */
  2878. total_degree2 = node->up_degree;
  2879. for (adj_index = 0; adj_index < node->up_degree; adj_index++) {
  2880. total_of_positions = (total_of_positions + node->up_edges[adj_index]->up_node->position);
  2881. }
  2882. }
  2883. /* */
  2884. total_degree = (total_degree1 + total_degree2);
  2885. /* */
  2886. if (total_degree > 0) {
  2887. node->weight = (100 * total_of_positions);
  2888. node->weight = (int)(node->weight / total_degree);
  2889. } else {
  2890. /* special: no edges in the given orientation and needs to be fixed later on based on avg etc. */
  2891. node->weight = (int)(-1);
  2892. rhp_log
  2893. ("%s(): node %d has weight set (-1) special degree=(%d+%d) mode %d\n",
  2894. __func__, node->innum, total_degree1, total_degree2, orientation);
  2895. }
  2896. rhp_log("%s(): node %d has now weight %d mode %d\n", __func__, node->innum, node->weight, orientation);
  2897. return;
  2898. }
  2899. /* sort on the (double) weight of a node, same weight may happen more then pnce */
  2900. static int rhp_compare_weights(const void *ptr_i, const void *ptr_j)
  2901. {
  2902. struct rhpnode **entry_ptr_i = (struct rhpnode **)0;
  2903. struct rhpnode **entry_ptr_j = (struct rhpnode * *)0;
  2904. struct rhpnode *node_i = (struct rhpnode *)0;
  2905. struct rhpnode *node_j = (struct rhpnode *)0;
  2906. int diff = 0;
  2907. entry_ptr_i = (struct rhpnode * *)ptr_i;
  2908. entry_ptr_j = (struct rhpnode **)ptr_j;
  2909. node_i = *entry_ptr_i;
  2910. node_j = *entry_ptr_j;
  2911. /* a abs() to compare if are almost the same */
  2912. diff = (node_i->weight > node_j->weight);
  2913. if (diff < 0) {
  2914. diff = (int)(0 - diff);
  2915. }
  2916. if (diff < 1) {
  2917. /* +/- 1 considered same */
  2918. return (0);
  2919. }
  2920. if (node_i->weight > node_j->weight) {
  2921. return (1);
  2922. } else if (node_i->weight < node_j->weight) {
  2923. return (-1);
  2924. } else {
  2925. return (0);
  2926. }
  2927. }
  2928. /* */
  2929. static void rhp_layersort(int level)
  2930. {
  2931. struct rhp_spn *spn = (struct rhp_spn *)0;
  2932. struct rhplevel *rl = (struct rhplevel *)0;
  2933. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) level);
  2934. if (spn == (struct rhp_spn *)0) {
  2935. rhp_log("%s(): could not find data for level %d shouldnothappen!\n", __func__, level);
  2936. return;
  2937. }
  2938. rl = (struct rhplevel *)spn->value;
  2939. /* sort depending on node weight */
  2940. qsort(rl->nodes, rl->number_of_nodes, sizeof(struct rhpnode *), rhp_compare_weights);
  2941. /* set the new node positions */
  2942. rhp_updatenodepositions(level);
  2943. rhp_log("%s(): sorted level %d and updated node positions\n", __func__, level);
  2944. return;
  2945. }
  2946. /* create new sorted nodelist */
  2947. static void rhp_sorted_nodelist(void)
  2948. {
  2949. struct rhp_spn *spn = (struct rhp_spn *)0;
  2950. struct rhplevel *lv = (struct rhplevel *)0;
  2951. struct rhpnode *nd = (struct rhpnode *)0;
  2952. struct rhpnode *nn = (struct rhpnode *)0;
  2953. int i = 0;
  2954. int ii = 0;
  2955. int num = 0;
  2956. rhp_log("%s(): %d levels\n", __func__, rhp_nlevels);
  2957. if (rhp_nlevels == 0) {
  2958. rhp_log("%s(): no levels shouldnothappen!\n", __func__);
  2959. return;
  2960. }
  2961. if (rhp_sp_has_data(rhp_sp_master_node_list) == 0) {
  2962. rhp_log("%s(): no node data shouldnothappen!\n", __func__);
  2963. return;
  2964. }
  2965. /* special case: only nodes in 1 level 0 */
  2966. if (rhp_nlevels == 1) {
  2967. num = 0;
  2968. rhp_sp_master_node_list_sorted = rhp_sp_new(1);
  2969. spn = rhp_sp_min(rhp_sp_master_node_list);
  2970. while (spn) {
  2971. nd = (struct rhpnode *)spn->value;
  2972. rhp_log("%s(): node %d level %d pos %d\n", __func__, nd->innum, nd->level, nd->position);
  2973. /* create fresh node data info */
  2974. nn = (struct rhpnode *)rhp_malloc(sizeof(struct rhpnode), __func__, __LINE__);
  2975. /* set the info n */
  2976. nn->num = num;
  2977. nn->innum = nd->innum;
  2978. nn->level = nd->level;
  2979. nn->data = nd->data;
  2980. nn->position = nd->position;
  2981. nn->up_degree = nd->up_degree;
  2982. nn->down_degree = nd->down_degree;
  2983. nn->up_edges = nd->up_edges;
  2984. nn->down_edges = nd->down_edges;
  2985. nn->weight = nd->weight;
  2986. nn->up_crossings = nd->up_crossings;
  2987. nn->down_crossings = nd->down_crossings;
  2988. rhp_log("%s(): node %d level %d pos %d weight %d at %d\n",
  2989. __func__, nd->innum, nd->level, nd->position, nd->weight, num);
  2990. /* insert node in database indexed on position number */
  2991. rhp_sp_insert(rhp_sp_master_node_list_sorted, (splay_tree_key) num, (splay_tree_value) nn);
  2992. num = (num + 1);
  2993. spn = rhp_sp_next(rhp_sp_master_node_list, spn->key);
  2994. }
  2995. return;
  2996. }
  2997. /* multi level drawing */
  2998. num = 0;
  2999. rhp_sp_master_node_list_sorted = rhp_sp_new(1);
  3000. for (i = 0; i < rhp_nlevels; i++) {
  3001. spn = rhp_sp_lookup(rhp_sp_layers, (rhp_spkey) i);
  3002. if (spn == (struct rhp_spn *)0) {
  3003. if (0) {
  3004. rhp_log("%s(): no data for level %d shouldnothappen!\n", __func__, i);
  3005. }
  3006. continue;
  3007. }
  3008. /* */
  3009. lv = (struct rhplevel *)spn->value;
  3010. if (lv == (struct rhplevel *)0) {
  3011. rhp_log("%s(): no nodes in level %d shouldnothappen!\n", __func__, i);
  3012. continue;
  3013. }
  3014. /* */
  3015. if (lv->nodes == (struct rhpnode **)0) {
  3016. if (0) {
  3017. rhp_log("%s(): no data for nodes in level %d shouldnothappen!\n", __func__, i);
  3018. }
  3019. continue;
  3020. }
  3021. if (lv->number_of_nodes == 0) {
  3022. rhp_log("%s(): no nodes in level %d shouldnothappen!\n", __func__, i);
  3023. continue;
  3024. }
  3025. /* scan nodes in this level */
  3026. for (ii = 0; ii < lv->number_of_nodes; ii++) {
  3027. nd = lv->nodes[ii];
  3028. /* create fresh node data info */
  3029. nn = (struct rhpnode *)rhp_malloc(sizeof(struct rhpnode), __func__, __LINE__);
  3030. /* set the info n */
  3031. nn->num = num;
  3032. nn->innum = nd->innum;
  3033. nn->level = nd->level;
  3034. nn->data = nd->data;
  3035. nn->position = nd->position;
  3036. nn->up_degree = nd->up_degree;
  3037. nn->down_degree = nd->down_degree;
  3038. nn->up_edges = nd->up_edges;
  3039. nn->down_edges = nd->down_edges;
  3040. nn->weight = nd->weight;
  3041. nn->up_crossings = nd->up_crossings;
  3042. nn->down_crossings = nd->down_crossings;
  3043. rhp_log("%s(): node %d level %d pos %d weight %d at %d\n",
  3044. __func__, nd->innum, nd->level, nd->position, nd->weight, num);
  3045. /* insert node in database indexed on position number */
  3046. rhp_sp_insert(rhp_sp_master_node_list_sorted, (splay_tree_key) num, (splay_tree_value) nn);
  3047. num = (num + 1);
  3048. }
  3049. }
  3050. return;
  3051. }
  3052. /* End. */