sfg.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645
  1. /*
  2. * Copyright t lefering
  3. * parts are (C) Universitaet Passau 1986-1991
  4. * parts are Copyright (C) 1998-2021 Free Software Foundation, Inc.
  5. * parts are Copyright (C) Felix von Leitner from dietlibc
  6. *
  7. * https://notabug.org/mooigraph/sfgraph
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * These are the four essential freedoms with GNU GPL software:
  23. * 1: freedom to run the program, for any purpose
  24. * 2: freedom to study how the program works, and change it to make it do what you wish
  25. * 3: freedom to redistribute copies to help your Free Software friends
  26. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  27. * , ,
  28. * / \
  29. * ((__-^^-,-^^-__))
  30. * `-_---' `---_-'
  31. * `--|o` 'o|--'
  32. * \ ` /
  33. * ): :(
  34. * :o_o:
  35. * "-"
  36. *
  37. * SPDX-License-Identifier: GPL-3.0+
  38. * License-Filename: LICENSE
  39. */
  40. /* Single File Graph layout for directed graphs.
  41. * the api to use this file is in sfg.h
  42. * the demo program how to use is sfgdemo.c
  43. */
  44. #include <stdio.h>
  45. #include <stdlib.h> /* for calloc() free() */
  46. /* from <limits.h> for CHAR_BIT is 8 definition */
  47. #ifndef CHAR_BIT
  48. #define CHAR_BIT 8
  49. #endif
  50. /* min (x,y) spacing between nodes */
  51. #define NXSPACING 5
  52. #define NYSPACING 15
  53. #include "sfg.h"
  54. struct gml_graph;
  55. struct gml_node;
  56. struct gml_edge;
  57. struct gml_nlist;
  58. struct gml_elist;
  59. /* here calloc/free can be changed and malloc, realloc is not used */
  60. static inline void *sfg_calloc (size_t nmemb, size_t size)
  61. {
  62. void *ret = NULL;
  63. if ((nmemb * size) == 0) {
  64. /* should not happen */
  65. }
  66. ret = calloc (nmemb, size);
  67. if (ret == (void *)0) {
  68. /* should not happen */
  69. exit (1);
  70. }
  71. return (ret);
  72. }
  73. static inline void *sfg_free (void *ptr)
  74. {
  75. if (ptr) {
  76. free (ptr);
  77. }
  78. return ((void *)0);
  79. }
  80. /* how many bytes can a splay key to index on have max.
  81. * this data type must be large enough for a pointer.
  82. * The size of `void *', as computed by sizeof.
  83. * #define SIZEOF_VOID_P 8
  84. * in configure.ac is:
  85. * AC_CHECK_SIZEOF([void *])
  86. * The size of `uintptr_t', as computed by sizeof.
  87. * #define SIZEOF_UINTPTR_T 8
  88. *
  89. * #include <stdint.h> // for uintptr_t definition
  90. * typedef unsigned long long int splay_tree_key;
  91. * typedef uintptr_t splay_tree_key;
  92. * in this situation it can be:
  93. */
  94. typedef int splay_tree_key;
  95. /* how many bytes can a splay value have max
  96. * typedef unsigned long long int splay_tree_value;
  97. * typedef uintptr_t splay_tree_value;
  98. * int this situation it can be:
  99. */
  100. typedef struct gml_node *splay_tree_value;
  101. /* Forward declaration for a tree. */
  102. typedef struct splay_tree_t *splay_tree;
  103. /* The nodes in the splay tree. */
  104. struct splay_tree_node_n {
  105. /* The key. */
  106. splay_tree_key key;
  107. /* The value. */
  108. splay_tree_value value;
  109. /* The left and right children, respectively. */
  110. struct splay_tree_node_n *left;
  111. struct splay_tree_node_n *right;
  112. };
  113. /* Forward declaration for a node in the tree. */
  114. typedef struct splay_tree_node_n *splay_tree_node;
  115. /* The type of a function which compares two splay-tree keys. The
  116. function should return values as for qsort. */
  117. typedef int (*splay_tree_compare_fn)(splay_tree_key, splay_tree_key);
  118. /* The type of a function used to deallocate any resources associated
  119. with the key. */
  120. typedef void (*splay_tree_delete_key_fn)(splay_tree_key);
  121. /* The type of a function used to deallocate any resources associated
  122. with the value. */
  123. typedef void (*splay_tree_delete_value_fn)(splay_tree_value);
  124. /* The type of a function used to iterate over the tree. */
  125. typedef int (*splay_tree_foreach_fn)(splay_tree_node, void *);
  126. /* The splay tree itself. */
  127. struct splay_tree_t {
  128. /* The root of the tree. */
  129. struct splay_tree_node_n *root;
  130. /* The comparision function. */
  131. splay_tree_compare_fn comp;
  132. /* The deallocate-key function. NULL if no cleanup is necessary. */
  133. splay_tree_delete_key_fn delete_key;
  134. /* The deallocate-value function. NULL if no cleanup is necessary. */
  135. splay_tree_delete_value_fn delete_value;
  136. };
  137. struct gml_graph {
  138. int layouted; /* set if layout is done */
  139. int nodenum; /* highest node number in use */
  140. int nnodes; /* number of nodes in the graph */
  141. int edgenum; /* highest edge number in use */
  142. int nedges; /* number of edges in the graph */
  143. int maxlevel; /* maximum relative level */
  144. int nedgelabels; /* number of edgelabels */
  145. int do_edgelabels; /* if set add edgelabels in the graph */
  146. int nsinglenodes; /* number of single nodes */
  147. int nhedges; /* number of hor edges */
  148. int startnodeslevel; /* level where graph drawing starts */
  149. int nstartnodes; /* number of start node numbers */
  150. int *startnodes; /* array with start node numbers */
  151. int xspacing; /* min x spacing between nodes */
  152. int yspacing; /* min y spacing between nodes */
  153. struct gml_nlist *nodelist; /* list of nodes */
  154. struct gml_nlist *nodelistend;
  155. struct gml_nlist *singlenodelist; /* list of single nodes */
  156. struct gml_nlist *singlenodelistend;
  157. struct gml_elist *edgelist; /* list of edges */
  158. struct gml_elist *edgelistend;
  159. int *nnodes_of_level; /* number of nodes for each level */
  160. int widestnnodes; /* widest number of nodes on one level */
  161. int widestlevel; /* widest level */
  162. int sugi_icrossings; /* initial crossings */
  163. int sugi_fcrossings; /* final crossings */
  164. int sugi_changes; /* sugiyama changes made */
  165. int *numce; /* number of crossings at every level */
  166. int *nume; /* number of edges */
  167. int maxx; /* max x pos of drawing */
  168. int maxy; /* max y pos of drawing */
  169. int nodemin; /* min. node number in use */
  170. int nodemax; /* max. node number in use */
  171. int edgemin; /* min. edge number in use */
  172. int edgemax; /* max. edge number in use */
  173. };
  174. struct gml_node {
  175. int nr; /* uniq node number */
  176. int tx; /* text xsize */
  177. int ty; /* text ysize */
  178. int bbx; /* text xsize */
  179. int bby; /* text ysize */
  180. int dummy; /* set to 1 if dummy node */
  181. int elabel; /* set if node is a edge label */
  182. int enumber; /* orig. edge number of the edge label */
  183. int nselfedges; /* number of self edges at this node */
  184. int done; /* dfs black/white */
  185. int grey; /* dfs grey */
  186. int indegree; /* incoming edges to node */
  187. int outdegree; /* outgoing edges from node */
  188. int hashedge; /* set if node has hor. edge */
  189. void *data; /* user data */
  190. int relx; /* relative xpos */
  191. int rely; /* relative ypos */
  192. int absx; /* absolute xpos */
  193. int absy; /* absolute ypos */
  194. int lx0; /* absolute xpos */
  195. int ly0; /* absolute xpos */
  196. int lx1; /* absolute ypos */
  197. int ly1; /* absolute ypos */
  198. int finx; /* absolute xpos */
  199. int finy; /* absolute ypos */
  200. struct gml_elist *outgoing_e; /* source list, outgoing edges */
  201. struct gml_elist *outgoing_etail; /* source list, outgoing edges */
  202. struct gml_elist *incoming_e; /* target list, incoming edges */
  203. struct gml_elist *incoming_etail; /* target list, incoming edges */
  204. int startnode; /* node belongs to part of graph with this startnode */
  205. struct gml_node *el_fnode; /* in edge-label the from-node */
  206. struct gml_node *el_tnode; /* in edge-label the to-node */
  207. };
  208. struct gml_edge {
  209. int nr; /* uniq edge number */
  210. struct gml_node *from_node; /* from node */
  211. struct gml_node *to_node; /* to node */
  212. int tx; /* text xsize */
  213. int ty; /* text ysize */
  214. int elabel; /* set if there is a edge label */
  215. int reversed; /* set if edge is reversed */
  216. int hedge; /* set if hor. edge */
  217. };
  218. struct gml_nlist {
  219. struct gml_node *node;
  220. struct gml_nlist *next;
  221. };
  222. struct gml_elist {
  223. struct gml_edge *edge;
  224. struct gml_elist *next;
  225. };
  226. /* local vars */
  227. static struct gml_graph *maingraph = NULL;
  228. /* by uniq number of node */
  229. static splay_tree uniqnode_splaytree = NULL;
  230. /* forward declarations zz */
  231. static struct splay_tree_node_n *splay(splay_tree sp, splay_tree_key key);
  232. static splay_tree_node splay_tree_lookup(splay_tree sp, splay_tree_key key);
  233. static splay_tree splay_tree_delete(splay_tree sp);
  234. static splay_tree splay_tree_new(splay_tree_compare_fn compare_fn, splay_tree_delete_key_fn delete_key_fn,
  235. splay_tree_delete_value_fn delete_value_fn);
  236. static int splay_tree_compare_ints(splay_tree_key k1, splay_tree_key k2);
  237. static struct gml_node *uniqnode(struct gml_graph *g, int nr);
  238. static void uniqnode_add(struct gml_graph *g, struct gml_node *node);
  239. static void clear_nodelist(struct gml_graph *g);
  240. static void clear_edgelist(struct gml_graph *g);
  241. static void prep(struct gml_graph *g);
  242. static void reorg(struct gml_graph *g);
  243. static void uncycle(struct gml_graph *g);
  244. static void make_stlist(struct gml_graph *g);
  245. static void clear_stlist(struct gml_node *node);
  246. static void clear_stlist_all(struct gml_graph *g);
  247. static void ylevels(struct gml_graph *g);
  248. static void set_level2(struct gml_graph *g, struct gml_node *n, int i, int startnode);
  249. static void shorteredges(struct gml_graph *g);
  250. static void edgesdownwards(struct gml_graph *g);
  251. static void edgelen(struct gml_graph *g);
  252. static void doublespacey(struct gml_graph *g);
  253. static void edgelabels(struct gml_graph *g);
  254. static void splitedges(struct gml_graph *g);
  255. static void nodecounts(struct gml_graph *g);
  256. static void barycenter(struct gml_graph *g, int it1v, int it2v);
  257. static void improve_positions(struct gml_graph *g);
  258. static void finalxy(struct gml_graph *g);
  259. static struct gml_edge *findedge(int num);
  260. static void setminmax(struct gml_graph *g);
  261. /* returns a version number as version 1.0 returns int 10 */
  262. int sfg_version(void)
  263. {
  264. return (30);
  265. }
  266. /* init
  267. * returns 0 if oke
  268. * returns -1 if already inited
  269. * returns -2 if other error
  270. */
  271. int sfg_init(void)
  272. {
  273. if (maingraph) {
  274. return (-1);
  275. }
  276. maingraph = (struct gml_graph *) sfg_calloc(1, sizeof(struct gml_graph));
  277. if (maingraph == NULL) {
  278. return (-2);
  279. }
  280. uniqnode_splaytree = splay_tree_new(splay_tree_compare_ints, NULL, NULL);
  281. if (uniqnode_splaytree == NULL) {
  282. return (-2);
  283. }
  284. /* min (x,y) spacing between nodes */
  285. maingraph->xspacing = NXSPACING;
  286. maingraph->yspacing = NYSPACING;
  287. maingraph->do_edgelabels = 1;
  288. return (0);
  289. }
  290. /* de-init
  291. * returns 0 if oke
  292. * returns -1 if not inited
  293. */
  294. int sfg_deinit(void)
  295. {
  296. if (maingraph == NULL) {
  297. return (-1);
  298. }
  299. if (maingraph->numce) {
  300. maingraph->numce = (int *) sfg_free (maingraph->numce);
  301. }
  302. if (maingraph->nnodes_of_level) {
  303. maingraph->nnodes_of_level = (int *) sfg_free (maingraph->nnodes_of_level);
  304. }
  305. if (maingraph->startnodes) {
  306. maingraph->startnodes = (int *) sfg_free(maingraph->startnodes);
  307. }
  308. clear_stlist_all(maingraph);
  309. clear_edgelist(maingraph);
  310. clear_nodelist(maingraph);
  311. uniqnode_splaytree = splay_tree_delete(uniqnode_splaytree);
  312. maingraph = (struct gml_graph *) sfg_free (maingraph);
  313. return (0);
  314. }
  315. /* add a node with uniq number starting at 1
  316. * with (tx,ty) as rectangle size for label text or (0,0)
  317. * before adding edges all node numbers must be defined
  318. * returns 0 if oke
  319. * returns -1 if not inited
  320. * returns -2 if number is lower then 1
  321. * returns -3 if tx number is lower then 0
  322. * returns -4 if ty number is lower then 0
  323. * returns -5 if layout already done
  324. * returns -6 if node already defined
  325. * returns -7 if other error
  326. */
  327. int sfg_addnode(int number, int tx, int ty)
  328. {
  329. struct gml_node *nn = NULL;
  330. struct gml_nlist *nl = NULL;
  331. if (maingraph == NULL) {
  332. return (-1);
  333. }
  334. if (number < 1) {
  335. return (-2);
  336. }
  337. if (tx < 0) {
  338. return (-3);
  339. }
  340. if (ty < 0) {
  341. return (-4);
  342. }
  343. if (maingraph->layouted) {
  344. return (-5);
  345. }
  346. /* check if node does exist already */
  347. if (uniqnode(maingraph, number)) {
  348. return (-6);
  349. }
  350. /* create the new node */
  351. nn = (struct gml_node *) sfg_calloc(1, sizeof(struct gml_node));
  352. if (nn == NULL) {
  353. return (-7);
  354. }
  355. nl = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  356. if (nl == NULL) {
  357. nn = (struct gml_node *) sfg_free (nn);
  358. return (-7);
  359. }
  360. nn->nr = number;
  361. nn->tx = tx;
  362. nn->ty = ty;
  363. /* data field is inited NULL and is set via other routine */
  364. nl->node = nn;
  365. if (maingraph->nodelist == NULL) {
  366. maingraph->nodelist = nl;
  367. maingraph->nodelistend = nl;
  368. } else {
  369. maingraph->nodelistend->next = nl;
  370. maingraph->nodelistend = nl;
  371. }
  372. if (number > maingraph->nodenum) {
  373. /* highest node number in use */
  374. maingraph->nodenum = number;
  375. }
  376. uniqnode_add(maingraph, nn);
  377. /* number of nodes in the graph */
  378. maingraph->nnodes++;
  379. return (0);
  380. }
  381. /* add a edge with uniq number starting at 1
  382. * the from-node number is in from, the to-node number is in to
  383. * self-edges are allowed but not with a label
  384. * with (tx,ty) as rectangle size for label text or (0,0)
  385. * returns 0 if oke
  386. * returns -1 if not inited
  387. * returns -2 if number is lower then 1
  388. * returns -3 if tx number is lower then 0
  389. * returns -4 if ty number is lower then 0
  390. * returns -5 if from-node number is not defined
  391. * returns -6 if to-node number is not defined
  392. * returns -7 if self-edge with a label
  393. * returns -8 if layout already done
  394. * returns -9 if other error
  395. */
  396. int sfg_addedge(int number, int from, int to, int tx, int ty)
  397. {
  398. struct gml_node *fn = NULL;
  399. struct gml_node *tn = NULL;
  400. struct gml_edge *e = NULL;
  401. struct gml_elist *el = NULL;
  402. if (maingraph == NULL) {
  403. return (-1);
  404. }
  405. if (number < 1) {
  406. return (-2);
  407. }
  408. if (tx < 0) {
  409. return (-3);
  410. }
  411. if (ty < 0) {
  412. return (-4);
  413. }
  414. if (from < 1) {
  415. return (-5);
  416. }
  417. if (to < 1) {
  418. return (-6);
  419. }
  420. if (from == to) {
  421. if (tx || ty) { /* do not allow self-edge with a label */
  422. return (-7);
  423. }
  424. }
  425. if (maingraph->layouted) {
  426. return (-8);
  427. }
  428. fn = uniqnode(maingraph, from);
  429. if (fn == NULL) {
  430. return (-5);
  431. }
  432. tn = uniqnode(maingraph, to);
  433. if (tn == NULL) {
  434. return (-6);
  435. }
  436. if (number > maingraph->edgenum) {
  437. maingraph->edgenum = number;
  438. }
  439. maingraph->nedges++;
  440. if (fn == tn) {
  441. /* at self-edge increase counter at node */
  442. fn->nselfedges++;
  443. } else {
  444. /* fresh new edge */
  445. e = (struct gml_edge *) sfg_calloc(1, sizeof(struct gml_edge));
  446. if (e == NULL) {
  447. return (-9);
  448. }
  449. el = (struct gml_elist *) sfg_calloc(1, sizeof(struct gml_elist));
  450. if (el == NULL) {
  451. e = (struct gml_edge *) sfg_free(e);
  452. return (-9);
  453. }
  454. e->nr = number;
  455. e->from_node = fn;
  456. e->to_node = tn;
  457. e->tx = tx;
  458. e->ty = ty;
  459. if (tx || ty) {
  460. /* mark there is a edgelabel */
  461. e->elabel = 1;
  462. /* number of edge labels in the graph */
  463. maingraph->nedgelabels++;
  464. }
  465. el->edge = e;
  466. if (maingraph->edgelist == NULL) {
  467. maingraph->edgelist = el;
  468. maingraph->edgelistend = el;
  469. } else {
  470. maingraph->edgelistend->next = el;
  471. maingraph->edgelistend = el;
  472. }
  473. }
  474. return (0);
  475. }
  476. /* run sugiyama barycenter layout
  477. * returns 0 if oke
  478. * returns -1 if not inited
  479. * returns -2 if layout already done
  480. * returns -3 if no nodes in the graph
  481. */
  482. int sfg_layout(void)
  483. {
  484. if (maingraph == NULL) {
  485. return (-1);
  486. }
  487. if (maingraph->layouted) {
  488. return (-2);
  489. }
  490. if (maingraph->nodelist == NULL) {
  491. return (-3);
  492. }
  493. /* prepare */
  494. prep(maingraph);
  495. /* re-organize nodelist */
  496. reorg(maingraph);
  497. /* change cycles in the graph */
  498. uncycle(maingraph);
  499. /* re-organize nodelist */
  500. reorg(maingraph);
  501. /* set y level of all nodes */
  502. ylevels(maingraph);
  503. /* find shorter edges */
  504. shorteredges(maingraph);
  505. /* change edge directions downwards */
  506. edgesdownwards(maingraph);
  507. /* check length of edges */
  508. edgelen(maingraph);
  509. /* doublespace the vertical levels */
  510. doublespacey(maingraph);
  511. /* split edges with label into node->label->node */
  512. edgelabels(maingraph);
  513. /* split longer edges */
  514. splitedges(maingraph);
  515. /* create level node count data */
  516. nodecounts(maingraph);
  517. /* run barycenter using defaults (0,0) or a value */
  518. barycenter(maingraph, 100, 100);
  519. /* re-calc positions */
  520. improve_positions(maingraph);
  521. /* final (x,y) positioning of nodes/edges */
  522. finalxy(maingraph);
  523. /* update node min/max and edge min/max */
  524. setminmax(maingraph);
  525. /* set layout is calculated */
  526. maingraph->layouted = 1;
  527. return (0);
  528. }
  529. /* return edge crossings in the graph
  530. * returns crossings >= 0 if oke
  531. * returns -1 if not inited
  532. * returns -2 if layout not done
  533. */
  534. int sfg_crossings(void)
  535. {
  536. if (maingraph == NULL) {
  537. return (-1);
  538. }
  539. if (maingraph->layouted == 0) {
  540. return (-22);
  541. }
  542. return (maingraph->sugi_fcrossings);
  543. }
  544. /* return initial edge crossings in the graph
  545. * returns crossings >= 0 if oke
  546. * returns -1 if not inited
  547. * returns -2 if layout not done
  548. */
  549. int sfg_initialcrossings(void)
  550. {
  551. if (maingraph == NULL) {
  552. return (-1);
  553. }
  554. if (maingraph->layouted == 0) {
  555. return (-2);
  556. }
  557. return (maingraph->sugi_icrossings);
  558. }
  559. /* set 1 to add edgelabels, or 0 to remove edgelabels
  560. * returns 0 if oke
  561. * returns -1 if not inited
  562. * returns -2 if already layouted
  563. */
  564. int sfg_edgelabels(int status)
  565. {
  566. if (maingraph == NULL) {
  567. return (-1);
  568. }
  569. if (maingraph->layouted) {
  570. return (-2);
  571. }
  572. if (status) {
  573. maingraph->do_edgelabels = 1;
  574. } else {
  575. maingraph->do_edgelabels = 1;
  576. }
  577. return (0);
  578. }
  579. /* return x pos of node with uniq number
  580. * returns >= 0 if oke
  581. * returns -1 if not inited
  582. * returns -2 if layout not done
  583. * returns -3 if number < 1
  584. * returns -4 if node not found
  585. */
  586. int sfg_nodexpos(int num)
  587. {
  588. struct gml_node *n = NULL;
  589. if (maingraph == NULL) {
  590. return (-1);
  591. }
  592. if (maingraph->layouted == 0) {
  593. return (-2);
  594. }
  595. if (num < 1) {
  596. return (-3);
  597. }
  598. n = uniqnode(maingraph, num);
  599. if (n == NULL) {
  600. return (-4);
  601. }
  602. return (n->finx);
  603. }
  604. /* return y pos of node with uniq number
  605. * returns >= 0 if oke
  606. * returns -1 if not inited
  607. * returns -2 if layout not done
  608. * returns -3 if number < 1
  609. * returns -4 if node not found
  610. */
  611. int sfg_nodeypos(int num)
  612. {
  613. struct gml_node *n = NULL;
  614. if (maingraph == NULL) {
  615. return (-1);
  616. }
  617. if (maingraph->layouted == 0) {
  618. return (-2);
  619. }
  620. if (num < 1) {
  621. return (-3);
  622. }
  623. n = uniqnode(maingraph, num);
  624. if (n == NULL) {
  625. return (-4);
  626. }
  627. return (n->finy);
  628. }
  629. /* return relative x pos of node with uniq number
  630. * returns >= 0 if oke
  631. * returns -1 if not inited
  632. * returns -2 if layout not done
  633. * returns -3 if number < 1
  634. * returns -4 if node not found
  635. */
  636. int sfg_noderelxpos(int num)
  637. {
  638. struct gml_node *n = NULL;
  639. if (maingraph == NULL) {
  640. return (-1);
  641. }
  642. if (maingraph->layouted == 0) {
  643. return (-2);
  644. }
  645. if (num < 1) {
  646. return (-3);
  647. }
  648. n = uniqnode(maingraph, num);
  649. if (n == NULL) {
  650. return (-4);
  651. }
  652. return (n->relx);
  653. }
  654. /* return relative y pos of node with uniq number
  655. * returns >= 0 if oke
  656. * returns -1 if not inited
  657. * returns -2 if layout not done
  658. * returns -3 if number < 1
  659. * returns -4 if node not found
  660. */
  661. int sfg_noderelypos(int num)
  662. {
  663. struct gml_node *n = NULL;
  664. if (maingraph == NULL) {
  665. return (-1);
  666. }
  667. if (maingraph->layouted == 0) {
  668. return (-2);
  669. }
  670. if (num < 1) {
  671. return (-3);
  672. }
  673. n = uniqnode(maingraph, num);
  674. if (n == NULL) {
  675. return (-4);
  676. }
  677. return (n->rely);
  678. }
  679. /* return level y start pos of node with uniq number
  680. * returns >= 0 if oke
  681. * returns -1 if not inited
  682. * returns -2 if layout not done
  683. * returns -3 if number < 1
  684. * returns -4 if node not found
  685. */
  686. int sfg_nodely0(int num)
  687. {
  688. struct gml_node *n = NULL;
  689. if (maingraph == NULL) {
  690. return (-1);
  691. }
  692. if (maingraph->layouted == 0) {
  693. return (-2);
  694. }
  695. if (num < 1) {
  696. return (-3);
  697. }
  698. n = uniqnode(maingraph, num);
  699. if (n == NULL) {
  700. return (-4);
  701. }
  702. return (n->ly0);
  703. }
  704. /* return level y end pos of node with uniq number
  705. * returns >= 0 if oke
  706. * returns -1 if not inited
  707. * returns -2 if layout not done
  708. * returns -3 if number < 1
  709. * returns -4 if node not found
  710. */
  711. int sfg_nodely1(int num)
  712. {
  713. struct gml_node *n = NULL;
  714. if (maingraph == NULL) {
  715. return (-1);
  716. }
  717. if (maingraph->layouted == 0) {
  718. return (-2);
  719. }
  720. if (num < 1) {
  721. return (-3);
  722. }
  723. n = uniqnode(maingraph, num);
  724. if (n == NULL) {
  725. return (-4);
  726. }
  727. return (n->ly1);
  728. }
  729. /* return x size of node with uniq number
  730. * returns >= 0 if oke
  731. * returns -1 if not inited
  732. * returns -2 if layout not done
  733. * returns -3 if number < 1
  734. * returns -4 if node not found
  735. */
  736. int sfg_nodexsize(int num)
  737. {
  738. struct gml_node *n = NULL;
  739. if (maingraph == NULL) {
  740. return (-1);
  741. }
  742. if (maingraph->layouted == 0) {
  743. return (-2);
  744. }
  745. if (num < 1) {
  746. return (-3);
  747. }
  748. n = uniqnode(maingraph, num);
  749. if (n == NULL) {
  750. return (-4);
  751. }
  752. return (n->tx);
  753. }
  754. /* return y size of node with uniq number
  755. * returns >= 0 if oke
  756. * returns -1 if not inited
  757. * returns -2 if layout not done
  758. * returns -3 if number < 1
  759. * returns -4 if node not found
  760. */
  761. int sfg_nodeysize(int num)
  762. {
  763. struct gml_node *n = NULL;
  764. if (maingraph == NULL) {
  765. return (-1);
  766. }
  767. if (maingraph->layouted == 0) {
  768. return (-2);
  769. }
  770. if (num < 1) {
  771. return (-3);
  772. }
  773. n = uniqnode(maingraph, num);
  774. if (n == NULL) {
  775. return (-4);
  776. }
  777. return (n->ty);
  778. }
  779. /* set min. x spacing between nodes
  780. * returns 0 if oke
  781. * returns -1 if not inited
  782. * returns -2 if number is lower then 0
  783. * returns -3 if layout already done
  784. */
  785. int sfg_xspacing(int num)
  786. {
  787. if (maingraph == NULL) {
  788. return (-1);
  789. }
  790. if (num < 1) {
  791. return (-2);
  792. }
  793. if (maingraph->layouted) {
  794. return (-3);
  795. }
  796. maingraph->xspacing = num;
  797. return (0);
  798. }
  799. /* set min. y spacing between nodes
  800. * returns 0 if oke
  801. * returns -1 if not inited
  802. * returns -2 if number is lower then 0
  803. * returns -3 if layout already done
  804. */
  805. int sfg_yspacing(int num)
  806. {
  807. if (maingraph == NULL) {
  808. return (-1);
  809. }
  810. if (num < 1) {
  811. return (-2);
  812. }
  813. if (maingraph->layouted) {
  814. return (-3);
  815. }
  816. maingraph->yspacing = num;
  817. return (0);
  818. }
  819. /* get max x pos in drawing
  820. * returns value if oke
  821. * returns -1 if not inited
  822. * returns -2 if layout not done
  823. */
  824. int sfg_maxx(void)
  825. {
  826. if (maingraph == NULL) {
  827. return (-1);
  828. }
  829. if (maingraph->layouted == 0) {
  830. return (-2);
  831. }
  832. return (maingraph->maxx);
  833. }
  834. /* get max y pos in drawing
  835. * returns value if oke
  836. * returns -1 if not inited
  837. * returns -2 if layout not done
  838. */
  839. int sfg_maxy(void)
  840. {
  841. if (maingraph == NULL) {
  842. return (-1);
  843. }
  844. if (maingraph->layouted == 0) {
  845. return (-2);
  846. }
  847. return (maingraph->maxy);
  848. }
  849. /* get min node number in use after layout
  850. * returns value if oke
  851. * returns -1 if not inited
  852. * returns -2 if layout not done
  853. * returns -3 if there are no nodes
  854. */
  855. int sfg_nodemin(void)
  856. {
  857. if (maingraph == NULL) {
  858. return (-1);
  859. }
  860. if (maingraph->layouted == 0) {
  861. return (-2);
  862. }
  863. if (maingraph->nodelist == NULL) {
  864. return (-3);
  865. }
  866. return (maingraph->nodemin);
  867. }
  868. /* get maxc node number in use after layout
  869. * returns value if oke
  870. * returns -1 if not inited
  871. * returns -2 if layout not done
  872. * returns -3 if there are no nodes
  873. */
  874. int sfg_nodemax(void)
  875. {
  876. if (maingraph == NULL) {
  877. return (-1);
  878. }
  879. if (maingraph->layouted == 0) {
  880. return (-2);
  881. }
  882. if (maingraph->nodelist == NULL) {
  883. return (-3);
  884. }
  885. return (maingraph->nodemax);
  886. }
  887. /* get min edge number in use after layout
  888. * returns value if oke
  889. * returns -1 if not inited
  890. * returns -2 if layout not done
  891. * returns -3 if there are no edges
  892. */
  893. int sfg_edgemin(void)
  894. {
  895. if (maingraph == NULL) {
  896. return (-1);
  897. }
  898. if (maingraph->layouted == 0) {
  899. return (-2);
  900. }
  901. if (maingraph->edgelist == NULL) {
  902. return (-3);
  903. }
  904. return (maingraph->edgemin);
  905. }
  906. /* get max edge number in use after layout
  907. * returns value if oke
  908. * returns -1 if not inited
  909. * returns -2 if layout not done
  910. * returns -3 if there are no edges
  911. */
  912. int sfg_edgemax(void)
  913. {
  914. if (maingraph == NULL) {
  915. return (-1);
  916. }
  917. if (maingraph->layouted == 0) {
  918. return (-2);
  919. }
  920. if (maingraph->edgelist == NULL) {
  921. return (-3);
  922. }
  923. return (maingraph->edgemax);
  924. }
  925. /* get number of levels
  926. * returns value if oke
  927. * returns -1 if not inited
  928. * returns -2 if layout not done
  929. */
  930. int sfg_nlevels(void)
  931. {
  932. if (maingraph == NULL) {
  933. return (-1);
  934. }
  935. if (maingraph->layouted == 0) {
  936. return (-2);
  937. }
  938. return (maingraph->maxlevel + 1);
  939. }
  940. /* get number of nodes
  941. * returns value if oke
  942. * returns -1 if not inited
  943. * returns -2 if layout not done
  944. */
  945. int sfg_nnodes(void)
  946. {
  947. if (maingraph == NULL) {
  948. return (-1);
  949. }
  950. if (maingraph->layouted == 0) {
  951. return (-2);
  952. }
  953. return (maingraph->nnodes);
  954. }
  955. /* get number of edges
  956. * returns value if oke
  957. * returns -1 if not inited
  958. * returns -2 if layout not done
  959. */
  960. int sfg_nedges(void)
  961. {
  962. if (maingraph == NULL) {
  963. return (-1);
  964. }
  965. if (maingraph->layouted == 0) {
  966. return (-2);
  967. }
  968. return (maingraph->nedges);
  969. }
  970. /* return type of node with uniq number
  971. * returns type of node, 1=regular, 2=dummy, 3=edgelabel node
  972. * returns -1 not inited
  973. * returns -2 if layout not done
  974. * returns -3 if nodenumber is < 1
  975. * returns -4 if node not found
  976. */
  977. int sfg_nodetype(int num)
  978. {
  979. struct gml_node *n = NULL;
  980. int type = 0;
  981. if (maingraph == NULL) {
  982. return (-1);
  983. }
  984. if (maingraph->layouted == 0) {
  985. return (-2);
  986. }
  987. if (num < 1) {
  988. return (-3);
  989. }
  990. n = uniqnode(maingraph, num);
  991. if (n == NULL) {
  992. return (-4);
  993. }
  994. if (n->dummy) {
  995. type = 2;
  996. } else if (n->elabel) {
  997. type = 3;
  998. } else {
  999. type = 1;
  1000. }
  1001. return (type);
  1002. }
  1003. /* return number of selfedges at node
  1004. * returns number of selfedges if oke
  1005. * returns -1 not inited
  1006. * returns -2 if layout not done
  1007. * returns -3 if nodenumber is < 1
  1008. * returns -4 if node not found
  1009. */
  1010. int sfg_nodeselfedges(int num)
  1011. {
  1012. struct gml_node *n = NULL;
  1013. if (maingraph == NULL) {
  1014. return (-1);
  1015. }
  1016. if (maingraph->layouted == 0) {
  1017. return (-2);
  1018. }
  1019. if (num < 1) {
  1020. return (-3);
  1021. }
  1022. n = uniqnode(maingraph, num);
  1023. if (n == NULL) {
  1024. return (-4);
  1025. }
  1026. return (n->nselfedges);
  1027. }
  1028. /* return number of incoming edges to node
  1029. * returns indegree number if oke
  1030. * returns -1 not inited
  1031. * returns -2 if layout not done
  1032. * returns -3 if nodenumber is < 1
  1033. * returns -4 if node not found
  1034. */
  1035. int sfg_nodeindegree(int num)
  1036. {
  1037. struct gml_node *n = NULL;
  1038. if (maingraph == NULL) {
  1039. return (-1);
  1040. }
  1041. if (maingraph->layouted == 0) {
  1042. return (-2);
  1043. }
  1044. if (num < 1) {
  1045. return (-3);
  1046. }
  1047. n = uniqnode(maingraph, num);
  1048. if (n == NULL) {
  1049. return (-4);
  1050. }
  1051. return (n->indegree);
  1052. }
  1053. /* return number of outgoing edges from node
  1054. * returns outdegree number if oke
  1055. * returns -1 not inited
  1056. * returns -2 if layout not done
  1057. * returns -3 if nodenumber is < 1
  1058. * returns -4 if node not found
  1059. */
  1060. int sfg_nodeoutdegree(int num)
  1061. {
  1062. struct gml_node *n = NULL;
  1063. if (maingraph == NULL) {
  1064. return (-1);
  1065. }
  1066. if (maingraph->layouted == 0) {
  1067. return (-2);
  1068. }
  1069. if (num < 1) {
  1070. return (-3);
  1071. }
  1072. n = uniqnode(maingraph, num);
  1073. if (n == NULL) {
  1074. return (-4);
  1075. }
  1076. return (n->outdegree);
  1077. }
  1078. /* return edge number of node if edgelabel node
  1079. * returns number of original edge with edgelabel if oke
  1080. * returns -1 not inited
  1081. * returns -2 if layout not done
  1082. * returns -3 if nodenumber is < 1
  1083. * returns -4 if node not found
  1084. * returns -5 if node is not edgelabel
  1085. */
  1086. int sfg_nodeenum(int num)
  1087. {
  1088. struct gml_node *n = NULL;
  1089. if (maingraph == NULL) {
  1090. return (-1);
  1091. }
  1092. if (maingraph->layouted == 0) {
  1093. return (-2);
  1094. }
  1095. if (num < 1) {
  1096. return (-3);
  1097. }
  1098. n = uniqnode(maingraph, num);
  1099. if (n == NULL) {
  1100. return (-4);
  1101. }
  1102. if (n->elabel == 0) {
  1103. return (-5);
  1104. }
  1105. return (n->enumber);
  1106. }
  1107. /* get optional data pointer of node
  1108. * returns data pointer if oke
  1109. * returns NULL if not inited
  1110. * returns NULL if layout not done
  1111. * returns NULL if nodenumber is < 1
  1112. * returns NULL if node not found
  1113. */
  1114. void *sfg_nodedata(int num)
  1115. {
  1116. struct gml_node *n = NULL;
  1117. if (maingraph == NULL) {
  1118. return (NULL);
  1119. }
  1120. if (maingraph->layouted == 0) {
  1121. return (NULL);
  1122. }
  1123. if (num < 1) {
  1124. return (NULL);
  1125. }
  1126. n = uniqnode(maingraph, num);
  1127. if (n == NULL) {
  1128. return (NULL);
  1129. }
  1130. return (n->data);
  1131. }
  1132. /* set optional data pointer of node
  1133. * returns 0 if oke
  1134. * returns -1 if not inited
  1135. * returns -2 if layout not done
  1136. * returns -3 if nodenumber is < 1
  1137. * returns -4 if node not found
  1138. */
  1139. int sfg_setnodedata(int num, void *data)
  1140. {
  1141. struct gml_node *n = NULL;
  1142. if (maingraph == NULL) {
  1143. return (-1);
  1144. }
  1145. if (maingraph->layouted == 0) {
  1146. return (-2);
  1147. }
  1148. if (num < 1) {
  1149. return (-3);
  1150. }
  1151. n = uniqnode(maingraph, num);
  1152. if (n == NULL) {
  1153. return (-4);
  1154. }
  1155. n->data = data;
  1156. return (0);
  1157. }
  1158. /* get node data and the calculated positions
  1159. * the user must supply a pointer to the callback routine
  1160. * this runs a supplied callback routine for every node
  1161. * the callback routine gets the node number as supplied,
  1162. * the level as supplied and the calculated pos position.
  1163. * the data is the supplied data and can be used similar.
  1164. * when the callback function needs to stop the iteration
  1165. * over the node list then it must return a non-zero status
  1166. * and that status is returned by sfg_node_foreach()
  1167. * the parameters in the callback function are
  1168. * int num, uniq node number
  1169. * int level, relative vertical level
  1170. * int pos, relative horizontal level
  1171. * void *data, optional user data
  1172. * int xpos, x-coord of upperleft node label area or 0 at no label
  1173. * int ypos, y-coord of upperleft node label area or 0 at no label
  1174. * int tx, x size of text area
  1175. * int ty, y size of text area
  1176. * int nselfedges, number of self-edges at this node
  1177. * int type, type of node, 1=regular, 2=dummy, 3=edgelabel node
  1178. * int indegree, number of incoming edges to the node
  1179. * int outdegree, number of outgoing edges from the node
  1180. * int ly0, start y of level of node
  1181. * int ly1, end y of level of node
  1182. * returns 0 from this routine if everything is oke or no data.
  1183. * returns -1 if not inited
  1184. * returns -2 if no layout is done
  1185. * returns -3 if no callback routine
  1186. */
  1187. int sfg_node_foreach(int (*getnodedata)
  1188. (int num, int level, int pos, int xpos, int ypos, int tx, int ty, int nselfedges, int type,
  1189. int indegree, int outdegree, int ly0, int ly1))
  1190. {
  1191. struct gml_nlist *nl = NULL;
  1192. struct gml_node *n = NULL;
  1193. int status = 0;
  1194. int type = 0;
  1195. if (maingraph == NULL) {
  1196. return (-1);
  1197. }
  1198. if (maingraph->layouted == 0) {
  1199. return (-2);
  1200. }
  1201. if (getnodedata == NULL) {
  1202. return (-3);
  1203. }
  1204. nl = maingraph->nodelist;
  1205. while (nl) {
  1206. n = nl->node;
  1207. /* todo set type of node here */
  1208. type = 0;
  1209. status =
  1210. (*getnodedata) (n->nr, n->rely, n->relx, n->finx, n->finy, n->tx, n->ty, n->nselfedges, type,
  1211. n->indegree, n->outdegree, n->ly0, n->ly1);
  1212. if (status != 0) {
  1213. break;
  1214. }
  1215. nl = nl->next;
  1216. }
  1217. return (0);
  1218. }
  1219. /* get from-node of edge
  1220. * returns from-node number if oke
  1221. * returns -1 not inited
  1222. * returns -2 if layout not done
  1223. * returns -3 if edgenumber is < 1
  1224. * returns -4 if edge not found
  1225. */
  1226. int sfg_edgefrom(int num)
  1227. {
  1228. struct gml_edge *e = NULL;
  1229. if (maingraph == NULL) {
  1230. return (-1);
  1231. }
  1232. if (maingraph->layouted == 0) {
  1233. return (-2);
  1234. }
  1235. if (num < 1) {
  1236. return (-3);
  1237. }
  1238. e = findedge(num);
  1239. if (e == NULL) {
  1240. return (-4);
  1241. }
  1242. return (e->from_node->nr);
  1243. }
  1244. /* get to-node of edge
  1245. * returns to-node number if oke
  1246. * returns -1 not inited
  1247. * returns -2 if layout not done
  1248. * returns -3 if edgenumber is < 1
  1249. * returns -4 if edge not found
  1250. */
  1251. int sfg_edgeto(int num)
  1252. {
  1253. struct gml_edge *e = NULL;
  1254. if (maingraph == NULL) {
  1255. return (-1);
  1256. }
  1257. if (maingraph->layouted == 0) {
  1258. return (-2);
  1259. }
  1260. if (num < 1) {
  1261. return (-3);
  1262. }
  1263. e = findedge(num);
  1264. if (e == NULL) {
  1265. return (-4);
  1266. }
  1267. return (e->to_node->nr);
  1268. }
  1269. /* get edge type
  1270. * returns type if oke, 1=regular, 2=selfedge, 3=hor. edge
  1271. * returns -1 not inited
  1272. * returns -2 if layout not done
  1273. * returns -3 if edgenumber is < 1
  1274. * returns -4 if edge not found
  1275. */
  1276. int sfg_edgetype(int num)
  1277. {
  1278. struct gml_edge *e = NULL;
  1279. int type = 0;
  1280. if (maingraph == NULL) {
  1281. return (-1);
  1282. }
  1283. if (maingraph->layouted == 0) {
  1284. return (-2);
  1285. }
  1286. if (num < 1) {
  1287. return (-3);
  1288. }
  1289. e = findedge(num);
  1290. if (e == NULL) {
  1291. return (-4);
  1292. }
  1293. if (e->from_node->nr == e->to_node->nr) {
  1294. type = 2;
  1295. } else if (e->hedge) {
  1296. type = 3;
  1297. } else {
  1298. type = 1;
  1299. }
  1300. return (type);
  1301. }
  1302. /* get edge reversed status
  1303. * returns 1 if reversed edge or 0 if not
  1304. * returns -1 not inited
  1305. * returns -2 if layout not done
  1306. * returns -3 if edgenumber is < 1
  1307. * returns -4 if edge not found
  1308. */
  1309. int sfg_edgerev(int num)
  1310. {
  1311. struct gml_edge *e = NULL;
  1312. if (maingraph == NULL) {
  1313. return (-1);
  1314. }
  1315. if (maingraph->layouted == 0) {
  1316. return (-2);
  1317. }
  1318. if (num < 1) {
  1319. return (-3);
  1320. }
  1321. e = findedge(num);
  1322. if (e == NULL) {
  1323. return (-4);
  1324. }
  1325. return (e->reversed);
  1326. }
  1327. /* get edge data and the calculated position
  1328. * the user must supply a pointer to the callback routine
  1329. * this runs a supplied callback routine for every edge
  1330. * when the callback function needs to stop the iteration
  1331. * over the edge list then it must return a non-zero status
  1332. * the parameters in the callback function are
  1333. * int num, uniq edge number
  1334. * int from, uniq from-node number
  1335. * int to, uniq to-node number
  1336. * void *data, optional user data
  1337. * int type, 1=regular, 2=selfedge, 3 hor. edge
  1338. * int rev, set if edge is reversed
  1339. * returns 0 if oke
  1340. * returns -1 if not inited
  1341. * returns -2 if no layout is done
  1342. * returns -3 if no callback routine
  1343. */
  1344. int sfg_edge_foreach(int (*getedgedata)(int num, int from, int to, int type, int rev))
  1345. {
  1346. struct gml_elist *el = NULL;
  1347. struct gml_edge *e = NULL;
  1348. int status = 0;
  1349. int type = 0;
  1350. if (maingraph == NULL) {
  1351. return (-1);
  1352. }
  1353. if (maingraph->layouted == 0) {
  1354. return (-2);
  1355. }
  1356. if (getedgedata == NULL) {
  1357. return (-3);
  1358. }
  1359. el = maingraph->edgelist;
  1360. while (el) {
  1361. e = el->edge;
  1362. /* set type */
  1363. if (e->from_node->nr == e->to_node->nr) {
  1364. type = 2;
  1365. } else if (e->hedge) {
  1366. type = 3;
  1367. } else {
  1368. type = 1;
  1369. }
  1370. status = (*getedgedata) (e->nr, e->from_node->nr, e->to_node->nr, type, e->reversed);
  1371. if (status != 0) {
  1372. break;
  1373. }
  1374. el = el->next;
  1375. }
  1376. return (0);
  1377. }
  1378. /* A splay-tree datatype.
  1379. Copyright (C) 1998-2021 Free Software Foundation, Inc.
  1380. Contributed by Mark Mitchell (mark@markmitchell.com).
  1381. This file is part of GNU CC.
  1382. GNU CC is free software; you can redistribute it and/or modify it
  1383. under the terms of the GNU General Public License as published by
  1384. the Free Software Foundation; either version 2, or (at your option)
  1385. any later version.
  1386. GNU CC is distributed in the hope that it will be useful, but
  1387. WITHOUT ANY WARRANTY; without even the implied warranty of
  1388. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1389. General Public License for more details.
  1390. You should have received a copy of the GNU General Public License
  1391. along with GNU CC; see the file COPYING. If not, write to
  1392. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  1393. Boston, MA 02110-1301, USA. */
  1394. /* For an easily readable description of splay-trees, see:
  1395. Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
  1396. Algorithms. Harper-Collins, Inc. 1991. */
  1397. /* Deallocate NODE (a member of SP), and all its sub-trees. */
  1398. static void splay_tree_delete_helper(splay_tree sp, splay_tree_node node)
  1399. {
  1400. if (node == NULL) {
  1401. return;
  1402. }
  1403. /* recurse */
  1404. splay_tree_delete_helper(sp, node->left);
  1405. splay_tree_delete_helper(sp, node->right);
  1406. /* free() key if needed */
  1407. if (sp->delete_key) {
  1408. (*sp->delete_key) (node->key);
  1409. node->key = (splay_tree_key) 0;
  1410. }
  1411. /* free() value if needed */
  1412. if (sp->delete_value) {
  1413. (*sp->delete_value) (node->value);
  1414. node->value = (splay_tree_value) 0;
  1415. }
  1416. (void) sfg_free ((void *)node);
  1417. return;
  1418. }
  1419. /* delete whole sp tree */
  1420. static splay_tree splay_tree_delete(splay_tree sp)
  1421. {
  1422. if (sp) {
  1423. splay_tree_delete_helper(sp, sp->root);
  1424. (void) sfg_free ((void *)sp);
  1425. }
  1426. return ((splay_tree) 0);
  1427. }
  1428. /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
  1429. DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
  1430. values. */
  1431. static splay_tree
  1432. splay_tree_new(splay_tree_compare_fn compare_fn, splay_tree_delete_key_fn delete_key_fn, splay_tree_delete_value_fn delete_value_fn)
  1433. {
  1434. splay_tree sp = (splay_tree) 0;
  1435. /* there must be a compare function, the free() functions are optional */
  1436. if (compare_fn == (splay_tree_compare_fn) 0) {
  1437. return ((splay_tree) 0);
  1438. }
  1439. sp = (splay_tree) sfg_calloc(1, sizeof(struct splay_tree_t));
  1440. if (sp == (splay_tree) 0) {
  1441. return ((splay_tree) 0);
  1442. }
  1443. /* set root node to use and the functions */
  1444. sp->root = (splay_tree_node) 0;
  1445. sp->comp = compare_fn;
  1446. sp->delete_key = delete_key_fn;
  1447. sp->delete_value = delete_value_fn;
  1448. return ((splay_tree) sp);
  1449. }
  1450. /* Insert a new node (associating KEY with DATA) into SP. If a
  1451. previous node with the indicated KEY exists, its data is not replaced
  1452. with the new value. */
  1453. static void splay_tree_insert(splay_tree sp, splay_tree_key key, splay_tree_value value)
  1454. {
  1455. splay_tree_node spn = (splay_tree_node) 0;
  1456. int comparison = 0;
  1457. if (sp == (splay_tree) 0) {
  1458. /* no tree */
  1459. return;
  1460. }
  1461. spn = splay_tree_lookup(sp, key);
  1462. if (spn != (splay_tree_node) 0) {
  1463. /* did already exist */
  1464. return;
  1465. }
  1466. /* Create a new node, and insert it at the root. */
  1467. spn = (splay_tree_node) sfg_calloc(1, sizeof(struct splay_tree_node_n));
  1468. if (spn == (splay_tree_node) 0) {
  1469. /* shouldnothappen */
  1470. return;
  1471. }
  1472. /* set node data */
  1473. spn->key = key;
  1474. spn->value = value;
  1475. if (sp->root == (splay_tree_node) 0) {
  1476. /* first entry */
  1477. sp->root = spn;
  1478. return;
  1479. }
  1480. /* add in tree */
  1481. comparison = (*sp->comp) (sp->root->key, key);
  1482. if (comparison < 0) {
  1483. spn->left = sp->root;
  1484. spn->right = spn->left->right;
  1485. spn->left->right = (splay_tree_node) 0;
  1486. } else {
  1487. /* > or == */
  1488. spn->right = sp->root;
  1489. spn->left = spn->right->left;
  1490. spn->right->left = (splay_tree_node) 0;
  1491. }
  1492. sp->root = spn;
  1493. return;
  1494. }
  1495. /* Lookup KEY in SP, returning VALUE if present, and NULL
  1496. otherwise. */
  1497. static splay_tree_node splay_tree_lookup(splay_tree sp, splay_tree_key key)
  1498. {
  1499. splay_tree_node spn = (splay_tree_node) 0;
  1500. if (sp == (splay_tree) 0) {
  1501. /* no tree */
  1502. return ((splay_tree_node) 0);
  1503. }
  1504. if (sp->root == (splay_tree_node) 0) {
  1505. /* no data */
  1506. return ((splay_tree_node) 0);
  1507. }
  1508. if ((*sp->comp) (sp->root->key, key) == 0) {
  1509. /* found */
  1510. return ((splay_tree_node) sp->root);
  1511. }
  1512. spn = splay(sp, key);
  1513. if (spn) {
  1514. if ((*sp->comp) (sp->root->key, key) == 0) {
  1515. /* found */
  1516. return ((splay_tree_node) sp->root);
  1517. }
  1518. }
  1519. /* not found */
  1520. return ((splay_tree_node) 0);
  1521. }
  1522. /* Splay-tree comparison function, treating the keys as ints. */
  1523. static int splay_tree_compare_ints(splay_tree_key k1, splay_tree_key k2)
  1524. {
  1525. if ((int)k1 < (int)k2) {
  1526. return ((int)-1);
  1527. } else if ((int)k1 > (int)k2) {
  1528. return (1);
  1529. } else {
  1530. return (0);
  1531. }
  1532. }
  1533. /* */
  1534. static struct splay_tree_node_n *splay(splay_tree sp, splay_tree_key key)
  1535. {
  1536. struct splay_tree_node_n *t = (struct splay_tree_node_n *)0;
  1537. struct splay_tree_node_n *l = (struct splay_tree_node_n *)0;
  1538. struct splay_tree_node_n *r = (struct splay_tree_node_n *)0;
  1539. struct splay_tree_node_n *y = (struct splay_tree_node_n *)0;
  1540. int comparevalue = 0;
  1541. int comparevalue2 = 0;
  1542. struct splay_tree_node_n tmp = {
  1543. /* The key. */
  1544. (splay_tree_key) 0,
  1545. /* The value. */
  1546. (splay_tree_value) 0,
  1547. /* The left and right children, respectively. */
  1548. (struct splay_tree_node_n *)0, /* left */
  1549. (struct splay_tree_node_n *)0 /* right */
  1550. };
  1551. /* no tree */
  1552. if (sp == (splay_tree) 0) {
  1553. return ((struct splay_tree_node_n *)0);
  1554. }
  1555. /* no data in root. return 0 */
  1556. if (sp->root == (struct splay_tree_node_n *)0) {
  1557. return ((struct splay_tree_node_n *)0);
  1558. }
  1559. /* current root */
  1560. t = sp->root;
  1561. tmp.left = (struct splay_tree_node_n *)0;
  1562. tmp.right = (struct splay_tree_node_n *)0;
  1563. l = &tmp;
  1564. r = &tmp;
  1565. labelstart:
  1566. /* */
  1567. comparevalue = (*sp->comp) (key, t->key);
  1568. if (comparevalue < 0) {
  1569. if (t->left == (struct splay_tree_node_n *)0) {
  1570. goto labelend;
  1571. }
  1572. /* */
  1573. comparevalue2 = (*sp->comp) (key, t->left->key);
  1574. if (comparevalue2 < 0) {
  1575. y = t->left;
  1576. t->left = y->right;
  1577. y->right = t;
  1578. t = y;
  1579. if (t->left == (struct splay_tree_node_n *)0) {
  1580. goto labelend;
  1581. }
  1582. }
  1583. /* */
  1584. r->left = t;
  1585. r = t;
  1586. t = t->left;
  1587. } else if (comparevalue > 0) {
  1588. if (t->right == (struct splay_tree_node_n *)0) {
  1589. goto labelend;
  1590. }
  1591. /* */
  1592. comparevalue2 = (*sp->comp) (key, t->right->key);
  1593. if (comparevalue2 > 0) {
  1594. /* */
  1595. y = t->right;
  1596. t->right = y->left;
  1597. y->left = t;
  1598. t = y;
  1599. if (t->right == (struct splay_tree_node_n *)0) {
  1600. goto labelend;
  1601. }
  1602. }
  1603. /* */
  1604. l->right = t;
  1605. l = t;
  1606. t = t->right;
  1607. } else {
  1608. /* here if (comparevalue == 0) */
  1609. goto labelend;
  1610. }
  1611. goto labelstart;
  1612. labelend:
  1613. l->right = t->left;
  1614. r->left = t->right;
  1615. t->left = tmp.right;
  1616. t->right = tmp.left;
  1617. sp->root = t;
  1618. return ((struct splay_tree_node_n *)t);
  1619. }
  1620. /* get node with this number */
  1621. static struct gml_node *uniqnode(struct gml_graph *g, int nr)
  1622. {
  1623. splay_tree_node spn = NULL;
  1624. if (g) {
  1625. }
  1626. if (uniqnode_splaytree == NULL) {
  1627. return (NULL);
  1628. }
  1629. spn = splay_tree_lookup(uniqnode_splaytree, (splay_tree_key) nr);
  1630. if (spn) {
  1631. return ((struct gml_node *)spn->value);
  1632. } else {
  1633. return (NULL);
  1634. }
  1635. }
  1636. /* add node to db */
  1637. static void uniqnode_add(struct gml_graph *g, struct gml_node *node)
  1638. {
  1639. splay_tree_node spn = NULL;
  1640. if (g) {
  1641. }
  1642. if (node == NULL) {
  1643. /* shouldnothappen */
  1644. return;
  1645. }
  1646. if (uniqnode_splaytree == NULL) {
  1647. uniqnode_splaytree = splay_tree_new(splay_tree_compare_ints, NULL, NULL);
  1648. }
  1649. spn = splay_tree_lookup(uniqnode_splaytree, (splay_tree_key) node->nr);
  1650. if (spn) {
  1651. /* shouldnothappen */
  1652. return;
  1653. } else {
  1654. splay_tree_insert(uniqnode_splaytree, (splay_tree_key) node->nr, (splay_tree_value) node);
  1655. }
  1656. return;
  1657. }
  1658. /* clear nodelist with the nodes */
  1659. static void clear_nodelist(struct gml_graph *g)
  1660. {
  1661. struct gml_nlist *lnll = NULL;
  1662. struct gml_nlist *nlnext = NULL;
  1663. lnll = g->nodelist;
  1664. while (lnll) {
  1665. nlnext = lnll->next;
  1666. lnll->node = (struct gml_node *) sfg_free(lnll->node);
  1667. lnll = (struct gml_nlist *) sfg_free(lnll);
  1668. lnll = nlnext;
  1669. }
  1670. g->nodelist = NULL;
  1671. g->nodelistend = NULL;
  1672. g->nodenum = 0;
  1673. g->nnodes = 0;
  1674. return;
  1675. }
  1676. /* clear edgelist and edge itself */
  1677. static void clear_edgelist(struct gml_graph *g)
  1678. {
  1679. struct gml_elist *el = NULL;
  1680. struct gml_elist *elnext = NULL;
  1681. el = g->edgelist;
  1682. while (el) {
  1683. elnext = el->next;
  1684. el->edge = (struct gml_edge *) sfg_free(el->edge);
  1685. el = (struct gml_elist *) sfg_free(el);
  1686. el = elnext;
  1687. }
  1688. g->edgelist = NULL;
  1689. g->edgelistend = NULL;
  1690. g->nedges = 0;
  1691. g->edgenum = 0;
  1692. return;
  1693. }
  1694. /* optional prepare extra's here */
  1695. static void prep(struct gml_graph *g)
  1696. {
  1697. struct gml_elist *el = NULL;
  1698. el = g->edgelist;
  1699. while (el) {
  1700. /* update degree of nodes */
  1701. el->edge->from_node->outdegree++;
  1702. el->edge->to_node->indegree++;
  1703. el = el->next;
  1704. }
  1705. return;
  1706. }
  1707. /* re-organize nodelist */
  1708. static void reorg(struct gml_graph *g)
  1709. {
  1710. struct gml_nlist *nl = NULL;
  1711. struct gml_nlist *nlnext = NULL;
  1712. struct gml_nlist *nn1 = NULL;
  1713. struct gml_nlist *nn2 = NULL;
  1714. struct gml_nlist *nn3 = NULL;
  1715. struct gml_nlist *nn4 = NULL;
  1716. struct gml_nlist *nnl = NULL;
  1717. struct gml_nlist *nnlend = NULL;
  1718. nl = g->nodelist;
  1719. if (nl == NULL) {
  1720. return;
  1721. }
  1722. while (nl) {
  1723. /* first the single nodes */
  1724. if (nl->node->indegree == 0 && nl->node->outdegree == 0) {
  1725. nn1 = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  1726. if (nn1) {
  1727. nn1->node = nl->node;
  1728. if (nnl == NULL) {
  1729. nnl = nn1;
  1730. nnlend = nn1;
  1731. } else {
  1732. nnlend->next = nn1;
  1733. nnlend = nn1;
  1734. }
  1735. }
  1736. }
  1737. nl = nl->next;
  1738. }
  1739. nl = g->nodelist;
  1740. while (nl) {
  1741. /* second the starter nodes */
  1742. if (nl->node->indegree == 0 && nl->node->outdegree != 0) {
  1743. nn2 = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  1744. if (nn2) {
  1745. nn2->node = nl->node;
  1746. if (nnl == NULL) {
  1747. nnl = nn2;
  1748. nnlend = nn2;
  1749. } else {
  1750. nnlend->next = nn2;
  1751. nnlend = nn2;
  1752. }
  1753. }
  1754. }
  1755. nl = nl->next;
  1756. }
  1757. nl = g->nodelist;
  1758. while (nl) {
  1759. /* third the middle nodes */
  1760. if (nl->node->indegree != 0 && nl->node->outdegree != 0) {
  1761. nn3 = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  1762. if (nn3) {
  1763. nn3->node = nl->node;
  1764. if (nnl == NULL) {
  1765. nnl = nn3;
  1766. nnlend = nn3;
  1767. } else {
  1768. nnlend->next = nn3;
  1769. nnlend = nn3;
  1770. }
  1771. }
  1772. }
  1773. nl = nl->next;
  1774. }
  1775. nl = g->nodelist;
  1776. while (nl) {
  1777. /* fourth the tail nodes */
  1778. if (nl->node->indegree != 0 && nl->node->outdegree == 0) {
  1779. nn4 = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  1780. if (nn4) {
  1781. nn4->node = nl->node;
  1782. if (nnl == NULL) {
  1783. nnl = nn4;
  1784. nnlend = nn4;
  1785. } else {
  1786. nnlend->next = nn4;
  1787. nnlend = nn4;
  1788. }
  1789. }
  1790. }
  1791. nl = nl->next;
  1792. }
  1793. /* clear nodelist but keep the nodes */
  1794. nl = g->nodelist;
  1795. while (nl) {
  1796. nlnext = nl->next;
  1797. nl = (struct gml_nlist *) sfg_free(nl);
  1798. nl = nlnext;
  1799. }
  1800. /* set the refreshed nodelist */
  1801. g->nodelist = nnl;
  1802. g->nodelistend = nnlend;
  1803. return;
  1804. }
  1805. /* recursive dfs */
  1806. static int decycle3(struct gml_graph *g, struct gml_node *n, int level)
  1807. {
  1808. struct gml_node *tmpnode = NULL;
  1809. struct gml_node *source = NULL;
  1810. struct gml_node *target = NULL;
  1811. struct gml_elist *el = NULL;
  1812. struct gml_edge *edge = NULL;
  1813. int changed = 0;
  1814. if (n->done) {
  1815. if (level > n->rely) {
  1816. n->rely = level;
  1817. }
  1818. return (0);
  1819. }
  1820. n->rely = level;
  1821. n->done = 1;
  1822. /* mark this node is being processed */
  1823. n->grey = 1;
  1824. source = n;
  1825. /* follow outgoing edges */
  1826. el = source->outgoing_e;
  1827. while (el) {
  1828. edge = el->edge;
  1829. /* get the to-node */
  1830. target = edge->to_node;
  1831. if (target->grey) {
  1832. changed++;
  1833. tmpnode = edge->to_node;
  1834. edge->to_node = edge->from_node;
  1835. edge->from_node = tmpnode;
  1836. /* toggle the edge is reversed bit */
  1837. if (edge->reversed) {
  1838. edge->reversed = 0;
  1839. } else {
  1840. edge->reversed = 1;
  1841. }
  1842. } else {
  1843. if (target->done == 0) {
  1844. changed += decycle3(g, target, (level + 1));
  1845. }
  1846. }
  1847. el = el->next;
  1848. }
  1849. /* this node is ready being processed */
  1850. n->grey = 0;
  1851. return (changed);
  1852. }
  1853. /* remove cycles in the graph */
  1854. static void uncycle(struct gml_graph *g)
  1855. {
  1856. struct gml_nlist *lnll = NULL;
  1857. int changed = 0;
  1858. /* build the s/tlist of a node */
  1859. clear_stlist_all(g);
  1860. make_stlist(g);
  1861. /* revert cycles at the last edge in the chain */
  1862. g->maxlevel = 0;
  1863. lnll = g->nodelist;
  1864. while (lnll) {
  1865. lnll->node->rely = -1;
  1866. lnll->node->done = 0;
  1867. lnll->node->grey = 0;
  1868. lnll = lnll->next;
  1869. }
  1870. /* first the startnodes */
  1871. lnll = g->nodelist;
  1872. changed = 0;
  1873. while (lnll) {
  1874. /* select start nodes */
  1875. if (lnll->node->indegree == 0 && lnll->node->outdegree != 0) {
  1876. if (lnll->node->done == 0) {
  1877. /* use v3 */
  1878. changed += decycle3(g, lnll->node, 0);
  1879. }
  1880. }
  1881. lnll = lnll->next;
  1882. }
  1883. /* check nodes */
  1884. lnll = g->nodelist;
  1885. while (lnll) {
  1886. /* select other nodes */
  1887. if (lnll->node->rely == -1) {
  1888. /* use v3 */
  1889. changed += decycle3(g, lnll->node, 0);
  1890. }
  1891. lnll = lnll->next;
  1892. }
  1893. if (changed) {
  1894. /* build the s/tlist of a node */
  1895. clear_stlist_all(g);
  1896. make_stlist(g);
  1897. }
  1898. return;
  1899. }
  1900. /* rebuild nodes st lists */
  1901. static void make_stlist(struct gml_graph *g)
  1902. {
  1903. struct gml_elist *el = NULL;
  1904. struct gml_edge *edge = NULL;
  1905. struct gml_node *sn = NULL;
  1906. struct gml_node *tn = NULL;
  1907. struct gml_elist *ne = NULL;
  1908. struct gml_nlist *lnll = NULL;
  1909. /* refresh degree count of nodes */
  1910. lnll = g->nodelist;
  1911. while (lnll) {
  1912. /* make ure these are cleared */
  1913. lnll->node->outgoing_e = NULL; /* source list, outgoing edges */
  1914. lnll->node->outgoing_etail = NULL; /* source list, outgoing edges */
  1915. lnll->node->incoming_e = NULL; /* target list, incoming edges */
  1916. lnll->node->incoming_etail = NULL; /* target list, incoming edges */
  1917. lnll->node->indegree = 0;
  1918. lnll->node->outdegree = 0;
  1919. lnll = lnll->next;
  1920. }
  1921. el = g->edgelist;
  1922. while (el) {
  1923. edge = el->edge;
  1924. /* from/to nodes */
  1925. sn = edge->from_node;
  1926. tn = edge->to_node;
  1927. ne = (struct gml_elist *) sfg_calloc(1, sizeof(struct gml_elist));
  1928. if (ne == NULL) {
  1929. return;
  1930. }
  1931. ne->edge = edge;
  1932. /* list of outgoing edges */
  1933. if (sn->outgoing_e == NULL) {
  1934. sn->outgoing_e = ne;
  1935. sn->outgoing_etail = ne;
  1936. } else {
  1937. sn->outgoing_etail->next = ne;
  1938. sn->outgoing_etail = ne;
  1939. }
  1940. sn->outdegree++;
  1941. ne = (struct gml_elist *) sfg_calloc(1, sizeof(struct gml_elist));
  1942. if (ne == NULL) {
  1943. return;
  1944. }
  1945. ne->edge = edge;
  1946. /* list of incoming edges */
  1947. if (tn->incoming_e == NULL) {
  1948. tn->incoming_e = ne;
  1949. tn->incoming_etail = ne;
  1950. } else {
  1951. tn->incoming_etail->next = ne;
  1952. tn->incoming_etail = ne;
  1953. }
  1954. tn->indegree++;
  1955. el = el->next;
  1956. }
  1957. return;
  1958. }
  1959. /* clear the s/t list of a node */
  1960. static void clear_stlist(struct gml_node *node)
  1961. {
  1962. struct gml_elist *ell = NULL;
  1963. struct gml_elist *ellnext = NULL;
  1964. /* free outgoing edges */
  1965. ell = node->outgoing_e;
  1966. while (ell) {
  1967. ellnext = ell->next;
  1968. ell = (struct gml_elist *) sfg_free(ell);
  1969. ell = ellnext;
  1970. }
  1971. node->outgoing_e = NULL;
  1972. node->outgoing_etail = NULL;
  1973. node->outdegree = 0;
  1974. /* free incoming edges */
  1975. ell = node->incoming_e;
  1976. while (ell) {
  1977. ellnext = ell->next;
  1978. ell = (struct gml_elist *) sfg_free(ell);
  1979. ell = ellnext;
  1980. }
  1981. node->incoming_e = NULL;
  1982. node->incoming_etail = NULL;
  1983. node->indegree = 0;
  1984. return;
  1985. }
  1986. /* clear the s/t list of all nodes */
  1987. static void clear_stlist_all(struct gml_graph *g)
  1988. {
  1989. struct gml_nlist *lnll = NULL;
  1990. lnll = g->nodelist;
  1991. while (lnll) {
  1992. clear_stlist(lnll->node);
  1993. lnll = lnll->next;
  1994. }
  1995. return;
  1996. }
  1997. /* add node as single node */
  1998. static void add_singlenode(struct gml_graph *g, struct gml_node *node)
  1999. {
  2000. struct gml_nlist *lnll = NULL;
  2001. lnll = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  2002. if (lnll) {
  2003. lnll->node = node;
  2004. if (g->singlenodelist == NULL) {
  2005. g->singlenodelist = lnll;
  2006. g->singlenodelistend = lnll;
  2007. } else {
  2008. g->singlenodelistend->next = lnll;
  2009. g->singlenodelistend = lnll;
  2010. }
  2011. }
  2012. return;
  2013. }
  2014. /* set rel. y level of all nodes */
  2015. static void ylevels(struct gml_graph *g)
  2016. {
  2017. struct gml_nlist *lnll = NULL;
  2018. int i = 0;
  2019. int start2 = 0;
  2020. int special = 0;
  2021. int nnodes = 0;
  2022. if (g->nodelist == NULL) {
  2023. /* nothing to do */
  2024. return;
  2025. }
  2026. /* no single nodes in the graph */
  2027. g->nsinglenodes = 0;
  2028. /* set all y levels to undefined */
  2029. lnll = g->nodelist;
  2030. nnodes = 0;
  2031. while (lnll) {
  2032. nnodes++;
  2033. /* y = -1, means undefined */
  2034. lnll->node->rely = -1;
  2035. lnll->node->done = 0;
  2036. lnll->node->grey = 0;
  2037. lnll->node->startnode = -1;
  2038. /* check for single nodes and mark them */
  2039. if (lnll->node->outgoing_e == NULL && lnll->node->incoming_e == NULL) {
  2040. /* set single nodes at fixed level 0 */
  2041. lnll->node->rely = 0;
  2042. lnll->node->done = 1;
  2043. /* node belongs to part of graph with this startnode */
  2044. lnll->node->startnode = 0;
  2045. g->nsinglenodes = (g->nsinglenodes + 1);
  2046. add_singlenode(g, lnll->node);
  2047. }
  2048. lnll = lnll->next;
  2049. }
  2050. /* if there are single nodes on level 0, start the graph at level 1 */
  2051. if (g->nsinglenodes) {
  2052. start2 = 1;
  2053. } else {
  2054. start2 = 0;
  2055. }
  2056. /* number of start nodes in the graph */
  2057. g->nstartnodes = 0;
  2058. /* where the actual drawing starts at y-level */
  2059. g->startnodeslevel = start2;
  2060. special = 0;
  2061. /* dfs */
  2062. lnll = g->nodelist;
  2063. while (lnll) {
  2064. if (lnll->node->rely == -1) {
  2065. /* select start nodes */
  2066. if (lnll->node->indegree == 0 && lnll->node->outdegree != 0) {
  2067. g->nstartnodes++;
  2068. set_level2(g, lnll->node, start2, lnll->node->nr);
  2069. }
  2070. }
  2071. lnll = lnll->next;
  2072. }
  2073. /* check that all nodes have y position now */
  2074. lnll = g->nodelist;
  2075. while (lnll) {
  2076. if (lnll->node->rely == -1) {
  2077. set_level2(g, lnll->node, start2, lnll->node->nr);
  2078. }
  2079. lnll = lnll->next;
  2080. }
  2081. /* graph can have zero startnodes.
  2082. * set first node as startnode.
  2083. */
  2084. if (g->nstartnodes == 0) {
  2085. g->nstartnodes++;
  2086. if (g->nodelist) {
  2087. set_level2(g, g->nodelist->node, start2, g->nodelist->node->nr);
  2088. }
  2089. special = 1;
  2090. }
  2091. /* fill the table with startnodes */
  2092. g->startnodes = (int *) sfg_calloc(1, g->nstartnodes * sizeof(int));
  2093. if (g->startnodes == NULL) {
  2094. return;
  2095. }
  2096. /* special case if there were no startnodes */
  2097. if (special) {
  2098. /* set first node as startnode */
  2099. if (g->nodelist) {
  2100. g->startnodes[0] = g->nodelist->node->nr;
  2101. }
  2102. } else {
  2103. /* copy the startnodes numbers in the (int *)array */
  2104. i = 0;
  2105. lnll = g->nodelist;
  2106. while (lnll) {
  2107. /* no incoming edges and at least one outgoing edge */
  2108. if (lnll->node->indegree == 0 && lnll->node->outdegree != 0) {
  2109. /* set node number. this is not the id number in the input. */
  2110. g->startnodes[i] = lnll->node->nr;
  2111. i++;
  2112. }
  2113. lnll = lnll->next;
  2114. }
  2115. }
  2116. return;
  2117. }
  2118. /* set rel. y level of nodes */
  2119. static void set_level2(struct gml_graph *g, struct gml_node *n, int i, int startnode)
  2120. {
  2121. struct gml_node *target = NULL;
  2122. struct gml_edge *edge = NULL;
  2123. struct gml_elist *el = NULL;
  2124. if (n->done) {
  2125. if (i > n->rely && n->grey == 0) {
  2126. n->rely = i;
  2127. if (i > g->maxlevel) {
  2128. g->maxlevel = i;
  2129. }
  2130. }
  2131. if (n->grey) {
  2132. return;
  2133. }
  2134. if (n->done > 1) {
  2135. return;
  2136. }
  2137. }
  2138. n->grey++;
  2139. n->done++;
  2140. n->rely = i;
  2141. n->startnode = startnode;
  2142. if (i > g->maxlevel) {
  2143. g->maxlevel = i;
  2144. }
  2145. /* follow outgoing edges */
  2146. el = n->outgoing_e;
  2147. while (el) {
  2148. edge = el->edge;
  2149. target = edge->to_node;
  2150. set_level2(g, target, (i + 1), startnode);
  2151. el = el->next;
  2152. }
  2153. n->grey = 0;
  2154. return;
  2155. }
  2156. /* undo reversed edges and refresh node edgelists */
  2157. static void unrev(struct gml_graph *g)
  2158. {
  2159. struct gml_elist *el = NULL;
  2160. struct gml_node *tmpnode = NULL;
  2161. struct gml_node *sn = NULL;
  2162. struct gml_node *tn = NULL;
  2163. struct gml_edge *edge = NULL;
  2164. int changed = 0;
  2165. el = g->edgelist;
  2166. while (el) {
  2167. edge = el->edge;
  2168. if (el->edge->reversed) {
  2169. changed++;
  2170. sn = edge->from_node;
  2171. tn = edge->to_node;
  2172. /* swap */
  2173. tmpnode = tn;
  2174. el->edge->to_node = sn;
  2175. el->edge->from_node = tmpnode;
  2176. el->edge->reversed = 0;
  2177. }
  2178. el = el->next;
  2179. }
  2180. if (changed) {
  2181. /* rebuild the in/out edges lists at the nodes in the modified graph */
  2182. g->maxlevel = 0;
  2183. /* refresh st lists */
  2184. clear_stlist_all(g);
  2185. make_stlist(g);
  2186. }
  2187. return;
  2188. }
  2189. static int do_abs(int i)
  2190. {
  2191. if (i < 0) {
  2192. return (-i);
  2193. } else {
  2194. return (i);
  2195. }
  2196. }
  2197. /* try to find shorter edges */
  2198. static void shorteredges(struct gml_graph *g)
  2199. {
  2200. struct gml_nlist *lnll = NULL;
  2201. struct gml_elist *ine = NULL;
  2202. struct gml_elist *oute = NULL;
  2203. struct gml_edge *etop = NULL;
  2204. struct gml_edge *ebot = NULL;
  2205. struct gml_node *ntop = NULL;
  2206. struct gml_node *nbot = NULL;
  2207. /* undo reversed edges and refresh node edgelists */
  2208. unrev(g);
  2209. /* move in between nodes at halfway */
  2210. lnll = g->nodelist;
  2211. while (lnll) {
  2212. if ((lnll->node->indegree == 1) && (lnll->node->outdegree == 1)) {
  2213. oute = lnll->node->outgoing_e;
  2214. ine = lnll->node->incoming_e;
  2215. etop = ine->edge;
  2216. ebot = oute->edge;
  2217. ntop = etop->from_node;
  2218. nbot = ebot->to_node;
  2219. if ((do_abs(ntop->rely - lnll->node->rely) > 1)
  2220. || (do_abs(nbot->rely - lnll->node->rely) > 1)) {
  2221. /* put node at the middle, does not depend on edge direction up/down */
  2222. lnll->node->rely = ((ntop->rely + nbot->rely) / 2);
  2223. }
  2224. }
  2225. lnll = lnll->next;
  2226. }
  2227. return;
  2228. }
  2229. /* all edges downwards */
  2230. static void edgesdownwards(struct gml_graph *g)
  2231. {
  2232. struct gml_elist *el = NULL;
  2233. struct gml_node *tmpnode = NULL;
  2234. struct gml_node *sn = NULL;
  2235. struct gml_node *tn = NULL;
  2236. struct gml_edge *edge = NULL;
  2237. int changed1 = 0;
  2238. el = g->edgelist;
  2239. while (el) {
  2240. edge = el->edge;
  2241. sn = edge->from_node;
  2242. tn = edge->to_node;
  2243. if ((tn->rely - sn->rely) < 0) {
  2244. /* swap */
  2245. tmpnode = tn;
  2246. el->edge->to_node = el->edge->from_node;
  2247. el->edge->from_node = tmpnode;
  2248. /* toggle the edge is reversed bit */
  2249. if (el->edge->reversed) {
  2250. el->edge->reversed = 0;
  2251. } else {
  2252. el->edge->reversed = 1;
  2253. }
  2254. changed1++;
  2255. }
  2256. el = el->next;
  2257. }
  2258. if (changed1) {
  2259. /* rebuild the in/out edges lists at the nodes in the modified graph */
  2260. g->maxlevel = 0;
  2261. /* refresh st lists */
  2262. clear_stlist_all(g);
  2263. make_stlist(g);
  2264. }
  2265. return;
  2266. }
  2267. /* dfs check again and revers if needed */
  2268. static void edgelen(struct gml_graph *g)
  2269. {
  2270. struct gml_elist *el = NULL;
  2271. struct gml_edge *edge = NULL;
  2272. struct gml_node *sn = NULL;
  2273. struct gml_node *tn = NULL;
  2274. struct gml_node *tmpnode = NULL;
  2275. int changed = 0;
  2276. el = g->edgelist;
  2277. while (el) {
  2278. edge = el->edge;
  2279. sn = edge->from_node;
  2280. tn = edge->to_node;
  2281. if ((tn->rely - sn->rely) < 0) {
  2282. changed++;
  2283. tmpnode = tn;
  2284. el->edge->to_node = el->edge->from_node;
  2285. el->edge->from_node = tmpnode;
  2286. }
  2287. el = el->next;
  2288. }
  2289. if (changed) {
  2290. /* refresh st lists */
  2291. clear_stlist_all(g);
  2292. make_stlist(g);
  2293. }
  2294. return;
  2295. }
  2296. /* doublespace the vertical levels */
  2297. static void doublespacey(struct gml_graph *g)
  2298. {
  2299. struct gml_nlist *lnll = NULL;
  2300. /* same edges now will have different dummy nodes resulting in 2 lines */
  2301. g->maxlevel = 0;
  2302. /* at the odd levels the edge labels will be placed. */
  2303. lnll = g->nodelist;
  2304. while (lnll) {
  2305. lnll->node->rely = (2 * lnll->node->rely);
  2306. if (lnll->node->rely > g->maxlevel) {
  2307. g->maxlevel = lnll->node->rely;
  2308. }
  2309. lnll = lnll->next;
  2310. }
  2311. return;
  2312. }
  2313. /* dummy nodes are only in nodelist, not raw nodelist */
  2314. static void add_new_dummynode(struct gml_graph *g, int foundid)
  2315. {
  2316. struct gml_node *node = NULL;
  2317. struct gml_nlist *lnll = NULL;
  2318. if (uniqnode(maingraph, foundid)) {
  2319. return;
  2320. }
  2321. node = (struct gml_node *) sfg_calloc(1, sizeof(struct gml_node));
  2322. if (node == NULL) {
  2323. return;
  2324. }
  2325. node->nr = foundid;
  2326. uniqnode_add(maingraph, node);
  2327. lnll = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  2328. if (lnll == NULL) {
  2329. node = (struct gml_node *) sfg_free(node);
  2330. return;
  2331. }
  2332. lnll->node = node;
  2333. if (g->nodelist == NULL) {
  2334. g->nodelist = lnll;
  2335. g->nodelistend = lnll;
  2336. } else {
  2337. g->nodelistend->next = lnll;
  2338. g->nodelistend = lnll;
  2339. }
  2340. return;
  2341. }
  2342. /* edge to dummy node */
  2343. static void add_new_dummyedge(struct gml_graph *g, int foundsource, int foundtarget, int reversed)
  2344. {
  2345. struct gml_node *snode = NULL;
  2346. struct gml_node *tnode = NULL;
  2347. struct gml_edge *edge = NULL;
  2348. struct gml_elist *el = NULL;
  2349. snode = uniqnode(maingraph, foundsource);
  2350. if (snode == NULL) {
  2351. return;
  2352. }
  2353. tnode = uniqnode(maingraph, foundtarget);
  2354. if (tnode == NULL) {
  2355. return;
  2356. }
  2357. edge = (struct gml_edge *) sfg_calloc(1, sizeof(struct gml_edge));
  2358. if (edge == NULL) {
  2359. return;
  2360. }
  2361. g->edgenum++;
  2362. edge->nr = g->edgenum;
  2363. edge->from_node = snode; /* from-node */
  2364. edge->to_node = tnode; /* to-node */
  2365. edge->reversed = reversed; /* edge attr. edge-is-reversed */
  2366. el = (struct gml_elist *) sfg_calloc(1, sizeof(struct gml_elist));
  2367. if (el == NULL) {
  2368. edge = (struct gml_edge *) sfg_free(edge);
  2369. return;
  2370. }
  2371. el->edge = edge;
  2372. if (g->edgelist == NULL) {
  2373. g->edgelist = el;
  2374. g->edgelistend = el;
  2375. } else {
  2376. g->edgelistend->next = el;
  2377. g->edgelistend = el;
  2378. }
  2379. return;
  2380. }
  2381. /* delete edge when replaced with a chain of edges */
  2382. static void del_edge(struct gml_graph *g, struct gml_elist *edgeel)
  2383. {
  2384. struct gml_elist *elprev = NULL;
  2385. struct gml_elist *el = NULL;
  2386. struct gml_elist *elto = NULL;
  2387. if (g->edgelist == NULL) {
  2388. return;
  2389. }
  2390. if (edgeel == g->edgelist) {
  2391. g->edgelist = g->edgelist->next;
  2392. if (g->edgelistend == edgeel) {
  2393. g->edgelistend = NULL;
  2394. } else {
  2395. el = g->edgelist;
  2396. elprev = el;
  2397. while (el) {
  2398. elprev = el;
  2399. if (el->next == edgeel) {
  2400. break;
  2401. }
  2402. el = el->next;
  2403. }
  2404. g->edgelistend = elprev;
  2405. }
  2406. edgeel->edge = (struct gml_edge *) sfg_free(edgeel->edge);
  2407. edgeel = (struct gml_elist *) sfg_free(edgeel);
  2408. } else {
  2409. elto = edgeel->next;
  2410. el = g->edgelist;
  2411. elprev = el;
  2412. while (el) {
  2413. elprev = el;
  2414. if (el->next == edgeel) {
  2415. break;
  2416. }
  2417. el = el->next;
  2418. }
  2419. elprev->next = elto;
  2420. if (g->edgelistend == edgeel) {
  2421. g->edgelistend = elprev;
  2422. }
  2423. edgeel->edge = (struct gml_edge *) sfg_free(edgeel->edge);
  2424. edgeel = (struct gml_elist *) sfg_free(edgeel);
  2425. }
  2426. return;
  2427. }
  2428. /* splits edgelabel edges into node->label->node */
  2429. static void edgelabels(struct gml_graph *g)
  2430. {
  2431. struct gml_elist *el = NULL;
  2432. struct gml_elist *elnext = NULL;
  2433. struct gml_node *ln = NULL;
  2434. char rev = 0;
  2435. int ydiff = 0;
  2436. int enumber = 0;
  2437. if (g->nedgelabels == 0) {
  2438. /* no edge labels, nothing todo here */
  2439. return;
  2440. }
  2441. if (g->do_edgelabels == 0) {
  2442. /* skip edge label processing */
  2443. return;
  2444. }
  2445. /* scan edges all downwards */
  2446. el = g->edgelist;
  2447. while (el) {
  2448. /* make sure, from-node is lower y then to-node */
  2449. if (el->edge->from_node->rely > el->edge->to_node->rely) {
  2450. ln = el->edge->from_node;
  2451. el->edge->from_node = el->edge->to_node;
  2452. el->edge->to_node = ln;
  2453. /* toggle rev flag */
  2454. if (el->edge->reversed) {
  2455. el->edge->reversed = 0;
  2456. } else {
  2457. el->edge->reversed = 1;
  2458. }
  2459. }
  2460. el = el->next;
  2461. }
  2462. /* scan edges for edge labels */
  2463. el = g->edgelist;
  2464. while (el) {
  2465. elnext = el->next;
  2466. /* do if there is a edge label */
  2467. if (el->edge->elabel) {
  2468. /* number of edge with edgelabel */
  2469. enumber = el->edge->nr;
  2470. /* edge attr. is-reversed */
  2471. rev = el->edge->reversed;
  2472. maingraph->nodenum++;
  2473. /* create label node */
  2474. add_new_dummynode(g, maingraph->nodenum);
  2475. /* mark this is a label node and set label text */
  2476. ln = uniqnode(maingraph, maingraph->nodenum);
  2477. /* edge-label-node, original from/to node */
  2478. ln->el_fnode = el->edge->from_node;
  2479. ln->el_tnode = el->edge->to_node;
  2480. /* y level difference between original from/to-nodes */
  2481. ydiff = (ln->el_tnode->rely - ln->el_fnode->rely);
  2482. /* put edge label halfway */
  2483. ln->rely = ln->el_fnode->rely + (ydiff / 2);
  2484. ln->elabel = 1; /* mark this is a edgelabel */
  2485. ln->dummy = 0;
  2486. /* set in the edgelabel node the number of the orig. edge */
  2487. ln->enumber = enumber;
  2488. /* set the size of the edge label text in the new node */
  2489. ln->tx = el->edge->tx;
  2490. ln->ty = el->edge->ty;
  2491. /* node belongs to graph with this startnode */
  2492. ln->startnode = el->edge->from_node->startnode;
  2493. /* create new edges with label node in between */
  2494. add_new_dummyedge(g, el->edge->from_node->nr, maingraph->nodenum, rev);
  2495. add_new_dummyedge(g, maingraph->nodenum, el->edge->to_node->nr, rev);
  2496. /* free old edge */
  2497. del_edge(g, el);
  2498. }
  2499. el = elnext;
  2500. }
  2501. /* refresh st lists */
  2502. clear_stlist_all(g);
  2503. make_stlist(g);
  2504. return;
  2505. }
  2506. /* split longer edges */
  2507. static void splitedges(struct gml_graph *g)
  2508. {
  2509. struct gml_elist *el = NULL;
  2510. struct gml_elist *elnext = NULL;
  2511. struct gml_edge *edge = NULL;
  2512. struct gml_node *sn = NULL;
  2513. struct gml_node *tn = NULL;
  2514. struct gml_node *nlnode = NULL;
  2515. int edgelen = 0;
  2516. int prevnodeid = 0;
  2517. int newid = 0;
  2518. int i = 0;
  2519. int sny = 0;
  2520. char rev = 0;
  2521. el = g->edgelist;
  2522. while (el) {
  2523. elnext = el->next;
  2524. edge = el->edge;
  2525. sn = edge->from_node; /* from-node */
  2526. tn = edge->to_node; /* to-node */
  2527. rev = edge->reversed; /* edge attr. to copy when splitting edge */
  2528. edgelen = (tn->rely - sn->rely);
  2529. /* horizontal edge */
  2530. if (edgelen == 0) {
  2531. /* horizontal edge has original endpoints, used in drawing edges */
  2532. edge->hedge = 1;
  2533. g->nhedges++; /* number of horizontal edges */
  2534. /* mark that nodes have a hor. edge */
  2535. sn->hashedge = 1;
  2536. tn->hashedge = 1;
  2537. } else if (edgelen > 1) {
  2538. prevnodeid = sn->nr;
  2539. sny = sn->rely;
  2540. for (i = 1; i < edgelen; i++) {
  2541. /* dummy node numbers start at first free node nr number */
  2542. maingraph->nodenum++;
  2543. newid = maingraph->nodenum;
  2544. add_new_dummynode(maingraph, newid);
  2545. nlnode = uniqnode(maingraph, newid);
  2546. nlnode->dummy = 1; /* this is a dummy node */
  2547. nlnode->elabel = 0; /* not a edgelabel */
  2548. nlnode->rely = (sny + i);
  2549. nlnode->startnode = sn->startnode;
  2550. add_new_dummyedge(g, prevnodeid, newid, rev);
  2551. prevnodeid = newid;
  2552. }
  2553. add_new_dummyedge(g, prevnodeid, tn->nr, rev);
  2554. del_edge(g, el);
  2555. } else if (edgelen == 1) {
  2556. /* edge len is 1 oke. */
  2557. } else {
  2558. /* shouldnothappen */
  2559. }
  2560. el = elnext;
  2561. }
  2562. return;
  2563. }
  2564. /* create level node count data */
  2565. static void nodecounts(struct gml_graph *g)
  2566. {
  2567. struct gml_nlist *lnll = NULL;
  2568. /* refresh st lists */
  2569. clear_stlist_all(g);
  2570. make_stlist(g);
  2571. /* table with number of nodes per level */
  2572. g->nnodes_of_level = (int *) sfg_calloc((g->maxlevel + 1), sizeof(int));
  2573. if (g->nnodes_of_level == NULL) {
  2574. return;
  2575. }
  2576. /* determine widest level and how many nodes it has */
  2577. g->widestlevel = 0;
  2578. g->widestnnodes = 0;
  2579. lnll = g->nodelist;
  2580. while (lnll) {
  2581. /* rely used for sugi */
  2582. g->nnodes_of_level[lnll->node->rely] = g->nnodes_of_level[lnll->node->rely] + 1;
  2583. /* x used for sugi, offset 1...n */
  2584. lnll->node->relx = g->nnodes_of_level[lnll->node->rely];
  2585. if (g->nnodes_of_level[lnll->node->rely] >= g->widestnnodes) {
  2586. g->widestnnodes = g->nnodes_of_level[lnll->node->rely];
  2587. g->widestlevel = lnll->node->rely;
  2588. }
  2589. lnll = lnll->next;
  2590. }
  2591. return;
  2592. }
  2593. struct mmatrix {
  2594. int level; /* upper level */
  2595. int nrows; /* nr. of rows (in upper level) */
  2596. int ncols; /* nr. of cols (in level+1) */
  2597. int nbytes; /* bytes used for matrix */
  2598. int *mi0; /* row node id's level i */
  2599. int nmi0; /* how many byte's in mi0 */
  2600. int *m0i; /* col node id's level (i+1) */
  2601. int nm0i; /* how many bytes in m0i */
  2602. int bbytes; /* bytes for double barycenter values */
  2603. double *b; /* buffer barycenter values */
  2604. unsigned char *bits; /* matrix bits */
  2605. };
  2606. static inline void setbit(unsigned char a[], int k)
  2607. {
  2608. if (k == 0) {
  2609. } else {
  2610. a[k / CHAR_BIT] |= (1 << (k % CHAR_BIT));
  2611. }
  2612. return;
  2613. }
  2614. static inline void clearbit(unsigned char a[], int k)
  2615. {
  2616. if (k == 0) {
  2617. } else {
  2618. a[k / CHAR_BIT] &= ~(1 << (k % CHAR_BIT));
  2619. }
  2620. return;
  2621. }
  2622. static inline int testbit(struct mmatrix *m, unsigned char a[], int k)
  2623. {
  2624. int ret = 0;
  2625. unsigned int mask = 0;
  2626. unsigned int mask2 = 0;
  2627. unsigned int i = 0;
  2628. if (k == 0) {
  2629. }
  2630. /* todo here tofix */
  2631. if (k > ((m->ncols + 1) * (m->nrows + 1))) {
  2632. }
  2633. /* compiler issue: the use of << is undefined here */
  2634. /* issue CHAR_BIT is often 8 but does not have to be 8 */
  2635. mask = (k % CHAR_BIT);
  2636. /*old: mask2 = (1 << mask); */
  2637. mask2 = 1;
  2638. for (i = 0; i < mask; i++) {
  2639. mask2 = (mask2 * 2);
  2640. }
  2641. ret = ((a[k / CHAR_BIT]) & mask2);
  2642. /*old return ((a[k / CHAR_BIT] & (1 << (k % CHAR_BIT))) != 0); */
  2643. return (ret);
  2644. }
  2645. /* i cols, j rows */
  2646. static inline int mget(struct mmatrix *m, int i, int j)
  2647. {
  2648. return (testbit(m, m->bits, ((i * (m->ncols + 0)) + j)));
  2649. }
  2650. /* i is the from-node, j is the to-node, value is 1 for edge, otherwise 0 */
  2651. static inline void mget_set(struct mmatrix *m, int i, int j, int value)
  2652. {
  2653. if (value) {
  2654. setbit(m->bits, ((i * (m->ncols + 0)) + j));
  2655. } else {
  2656. clearbit(m->bits, ((i * (m->ncols + 0)) + j));
  2657. }
  2658. return;
  2659. }
  2660. static int number_of_crossings2(struct mmatrix *m, int r, int c)
  2661. {
  2662. int j = 0;
  2663. int k = 0;
  2664. int alpha = 0;
  2665. int beta = 1;
  2666. int result = 0;
  2667. for (j = 1; j <= r - 1; j++) {
  2668. for (k = j + 1; k <= r; k++) {
  2669. for (alpha = 1; alpha <= c - 1; alpha++) {
  2670. for (beta = alpha + 1; beta <= c; beta++) {
  2671. /* here 1*0=0, 0*1=0, 0*0=0 or 1*1=1 */
  2672. result = result + ((mget(m, j, beta) * mget(m, k, alpha)));
  2673. }
  2674. }
  2675. }
  2676. }
  2677. return (result);
  2678. }
  2679. static int number_of_crossings3(struct mmatrix *m, int r, int c)
  2680. {
  2681. int j = 0;
  2682. int k = 0;
  2683. int alpha = 0;
  2684. int beta = 1;
  2685. int result2 = 0;
  2686. if (0) {
  2687. result2 = number_of_crossings2(m, r, c);
  2688. }
  2689. for (j = 1; j <= (r - 1); j++) {
  2690. for (k = (j + 1); k <= r; k++) {
  2691. for (alpha = 1; alpha <= (c - 1); alpha++) {
  2692. /* */
  2693. if (mget(m, k, alpha)) {
  2694. for (beta = alpha + 1; beta <= c; beta++) {
  2695. /* */
  2696. if (mget(m, j, beta)) {
  2697. result2++;
  2698. }
  2699. }
  2700. }
  2701. /* */
  2702. }
  2703. }
  2704. }
  2705. return (result2);
  2706. }
  2707. /* number of crossings in whole graph */
  2708. static int number_of_crossings_a(struct gml_graph *g, struct mmatrix **mm)
  2709. {
  2710. int ktot = 0;
  2711. int k = 0;
  2712. int i = 0;
  2713. for (i = 0; i < g->maxlevel; i++) {
  2714. if (mm[i]) {
  2715. k = number_of_crossings3(mm[i], mm[i]->nrows, mm[i]->ncols);
  2716. /* save number of edge crossings at level */
  2717. g->numce[i] = k;
  2718. ktot = ktot + k;
  2719. }
  2720. }
  2721. return (ktot);
  2722. }
  2723. /* configure matrix data for level l in the graph */
  2724. static void make_matrix(struct gml_graph *g, int l, struct mmatrix *m)
  2725. {
  2726. struct gml_nlist *nl = NULL;
  2727. struct gml_elist *el = NULL;
  2728. int i = 0;
  2729. int j = 0;
  2730. int c = 0;
  2731. int r = 0;
  2732. /* add node numbers in the 0 position */
  2733. nl = g->nodelist;
  2734. while (nl) {
  2735. /* if (level(n) == l) */
  2736. if (nl->node->rely == l) {
  2737. /* rows */
  2738. i = nl->node->relx;
  2739. m->mi0[i] = nl->node->nr; /* uniq node number, not id */
  2740. } else if (nl->node->rely == (l + 1)) {
  2741. /* columns */
  2742. j = nl->node->relx;
  2743. m->m0i[j] = nl->node->nr; /* uniq node number, not id */
  2744. }
  2745. nl = nl->next;
  2746. }
  2747. /* matrix bits config, first init then set bits. */
  2748. r = m->nrows;
  2749. c = m->ncols;
  2750. for (i = 1; i <= r; i++) {
  2751. for (j = 1; j <= c; j++) {
  2752. mget_set(m, i, j, 0);
  2753. }
  2754. }
  2755. nl = g->nodelist;
  2756. while (nl) {
  2757. /* if (level(n) == l) */
  2758. if (nl->node->rely == l) {
  2759. /* outgoing edges : for_sourcelist (n, e) */
  2760. el = nl->node->outgoing_e;
  2761. while (el) {
  2762. /* skip the horizontal edges */
  2763. if (el->edge->hedge == 0) {
  2764. /* from-node rel. x pos */
  2765. i = nl->node->relx;
  2766. /* to-node rel. x pos */
  2767. j = el->edge->to_node->relx;
  2768. /* set this is edge */
  2769. mget_set(m, i, j, 1);
  2770. }
  2771. el = el->next;
  2772. }
  2773. }
  2774. nl = nl->next;
  2775. }
  2776. return;
  2777. }
  2778. /* find node with given id number */
  2779. static struct gml_node *su_find_node_with_number(struct gml_graph *g, int nr)
  2780. {
  2781. return (uniqnode(g, nr));
  2782. }
  2783. static void store_new_positions(struct gml_graph *g, struct mmatrix *m, int level)
  2784. {
  2785. struct gml_node *n = NULL;
  2786. int i = 0;
  2787. if (level) {
  2788. }
  2789. if (m == NULL) {
  2790. return;
  2791. }
  2792. for (i = 1; i <= m->nrows; i++) {
  2793. /* rows */
  2794. n = su_find_node_with_number(g, m->mi0[i]);
  2795. if (n) {
  2796. /* offset is 1, make it into 0..n */
  2797. n->relx = (i - 1);
  2798. } else {
  2799. }
  2800. }
  2801. for (i = 1; i <= m->ncols; i++) {
  2802. /* columns */
  2803. n = su_find_node_with_number(g, m->m0i[i]);
  2804. if (n) {
  2805. /* offset is 1, make it into 0..n */
  2806. n->relx = (i - 1);
  2807. } else {
  2808. }
  2809. }
  2810. return;
  2811. }
  2812. /* parts are Copyright (C) Felix von Leitner from dietlibc */
  2813. static void *do_memmove(void *dst, void *src, size_t count)
  2814. {
  2815. char *a = (char *)dst;
  2816. char *b = (char *)src;
  2817. if (src != dst) {
  2818. if (src > dst) {
  2819. while (count--)
  2820. *a++ = *b++;
  2821. } else {
  2822. a += count - 1;
  2823. b += count - 1;
  2824. while (count--)
  2825. *a-- = *b--;
  2826. }
  2827. }
  2828. return dst;
  2829. }
  2830. /* copy matrix m1 to m2 */
  2831. static void copy_m(struct mmatrix *m1, struct mmatrix *m2)
  2832. {
  2833. if (m1 && m2) {
  2834. m2->level = m1->level; /* upper level */
  2835. m2->nrows = m1->nrows; /* nr. of rows (in upper level) */
  2836. m2->ncols = m1->ncols; /* nr. of cols (in level+1) */
  2837. m2->nbytes = m1->nbytes; /* bytes used for matrix */
  2838. (void)do_memmove(m2->bits, m1->bits, m1->nbytes); /* matrix bits */
  2839. (void)do_memmove(m2->mi0, m1->mi0, m1->nmi0); /* row node id's level i */
  2840. m2->nmi0 = m1->nmi0; /* how many byte's in mi0 */
  2841. (void)do_memmove(m2->m0i, m1->m0i, m1->nm0i); /* col node id's level (i+1) */
  2842. m2->nm0i = m1->nm0i; /* how many bytes in m0i */
  2843. m2->bbytes = m1->bbytes; /* bytes for double barycenter values */
  2844. (void)do_memmove(m2->b, m1->b, m1->bbytes); /* barycenter values */
  2845. }
  2846. return;
  2847. }
  2848. /* are m1,m2 equal? */
  2849. static int equal_m(struct mmatrix *m1, struct mmatrix *m2, int r, int c)
  2850. {
  2851. int i = 0;
  2852. int j = 0;
  2853. for (i = 1; i <= r; i++) {
  2854. for (j = 1; j <= c; j++) {
  2855. if (mget(m1, i, j) != mget(m2, i, j)) {
  2856. return (0); /* FALSE */
  2857. }
  2858. }
  2859. }
  2860. return (1); /* TRUE */
  2861. }
  2862. /* is whole graph equal */
  2863. static int equal_a(struct gml_graph *g, struct mmatrix **mm1, struct mmatrix **mm2)
  2864. {
  2865. int l = 0;
  2866. if (mm1 == NULL || mm2 == NULL) {
  2867. return (0);
  2868. }
  2869. for (l = 0; l < g->maxlevel; l++) {
  2870. if (equal_m(mm1[l], mm2[l], mm1[l]->nrows /* old g->nnodes_of_level[l] */ ,
  2871. mm1[l]->ncols /* old g->nnodes_of_level[l + 1] */ ) ==
  2872. 0 /* FALSE */ ) {
  2873. return (0); /* FALSE */
  2874. }
  2875. }
  2876. return (1); /* TRUE */
  2877. }
  2878. /* copy whole graph */
  2879. static inline void copy_a(struct gml_graph *g, struct mmatrix **mm1, struct mmatrix **mm2)
  2880. {
  2881. int i = 0;
  2882. for (i = 0; i < g->maxlevel; i++) {
  2883. copy_m(mm1[i], mm2[i]);
  2884. }
  2885. return;
  2886. }
  2887. static void exch_rows(struct mmatrix *m, int r1, int r2)
  2888. {
  2889. int j = 0;
  2890. int id1 = 0;
  2891. int id2 = 0;
  2892. int bit1 = 0;
  2893. int bit2 = 0;
  2894. /*
  2895. * h = m[r1][j];
  2896. * m[r1][j] = m[r2][j];
  2897. * m[r2][j] = h;
  2898. */
  2899. /* swap node id numbers */
  2900. id1 = m->mi0[r1];
  2901. id2 = m->mi0[r2];
  2902. m->mi0[r1] = id2;
  2903. m->mi0[r2] = id1;
  2904. /* swap matrix bits */
  2905. for (j = 1; j <= m->ncols; j++) {
  2906. bit1 = mget(m, r1, j);
  2907. bit2 = mget(m, r2, j);
  2908. mget_set(m, r1, j, bit2);
  2909. mget_set(m, r2, j, bit1);
  2910. }
  2911. return;
  2912. }
  2913. static void exch_columns(struct mmatrix *m, int c1, int c2)
  2914. {
  2915. int i = 0;
  2916. int id1 = 0;
  2917. int id2 = 0;
  2918. int bit1 = 0;
  2919. int bit2 = 0;
  2920. /*
  2921. * h = m[i][c1];
  2922. * m[i][c1] = m[i][c2];
  2923. * m[i][c2] = h;
  2924. */
  2925. /* swap node id numbers */
  2926. id1 = m->m0i[c1];
  2927. id2 = m->m0i[c2];
  2928. m->m0i[c1] = id2;
  2929. m->m0i[c2] = id1;
  2930. /* swap matrix bits */
  2931. for (i = 1; i <= m->nrows; i++) {
  2932. bit1 = mget(m, i, c1);
  2933. bit2 = mget(m, i, c2);
  2934. mget_set(m, i, c1, bit2);
  2935. mget_set(m, i, c2, bit1);
  2936. }
  2937. return;
  2938. }
  2939. static int reverse_r(struct mmatrix *m, int r1, int r2)
  2940. {
  2941. int i = 0;
  2942. int j = 0;
  2943. int ch = 0;
  2944. for (i = r1, j = r2; i < j; i++, j--) {
  2945. ch++;
  2946. exch_rows(m, i, j);
  2947. }
  2948. return (ch);
  2949. }
  2950. static int reverse_c(struct mmatrix *m, int c1, int c2)
  2951. {
  2952. int i = 0;
  2953. int j = 0;
  2954. int ch = 0;
  2955. for (i = c1, j = c2; i < j; i++, j--) {
  2956. ch++;
  2957. exch_columns(m, i, j);
  2958. }
  2959. return (ch);
  2960. }
  2961. static double row_barycenter(struct mmatrix *m, int i, int maxval)
  2962. {
  2963. int j = 0;
  2964. int r1 = 0; /* sum */
  2965. int r2 = 0; /* div */
  2966. for (j = 1; j <= maxval; j++) {
  2967. if (mget(m, i, j)) {
  2968. r1 = r1 + j;
  2969. r2++;
  2970. }
  2971. }
  2972. if (r2 == 0) {
  2973. return (0.0);
  2974. } else {
  2975. return ((double)r1 / (double)r2);
  2976. }
  2977. }
  2978. static double column_barycenter(struct mmatrix *m, int j, int maxval)
  2979. {
  2980. int i = 0;
  2981. int r1 = 0; /* sum */
  2982. int r2 = 0; /* div */
  2983. for (i = 1; i <= maxval; i++) {
  2984. if (mget(m, i, j)) {
  2985. r1 = r1 + i;
  2986. r2++;
  2987. }
  2988. }
  2989. if (r2 == 0) {
  2990. return (0.0);
  2991. } else {
  2992. return ((double)r1 / (double)r2);
  2993. }
  2994. }
  2995. /* reverse rows */
  2996. static int r_r(struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  2997. {
  2998. int i = 0;
  2999. int j = 0;
  3000. int ch = 0;
  3001. for (i = 1; i <= max_r; i++) {
  3002. m1->b[i] = row_barycenter(m1, i, max_c);
  3003. }
  3004. for (i = 1; i < max_r; i++) {
  3005. j = i;
  3006. while ((j < max_r) && (m1->b[j + 1] == m1->b[j])) {
  3007. j++;
  3008. }
  3009. if (j > i) {
  3010. ch += reverse_r(m1, i, j);
  3011. if (m2 != NULL) {
  3012. ch += reverse_c(m2, i, j);
  3013. }
  3014. i = j;
  3015. }
  3016. }
  3017. return (ch);
  3018. }
  3019. /* reverse columns */
  3020. static int r_c(struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  3021. {
  3022. int i = 0;
  3023. int j = 0;
  3024. int ch = 0;
  3025. for (i = 1; i <= max_c; i++) {
  3026. m1->b[i] = column_barycenter(m1, i, max_r);
  3027. }
  3028. for (i = 1; i < max_c; i++) {
  3029. j = i;
  3030. while ((j < max_c) && (m1->b[j + 1] == m1->b[j])) {
  3031. j++;
  3032. }
  3033. if (j > i) {
  3034. ch += reverse_c(m1, i, j);
  3035. if (m2 != NULL) {
  3036. ch += reverse_r(m2, i, j);
  3037. }
  3038. i = j;
  3039. }
  3040. }
  3041. return (ch);
  3042. }
  3043. /* barycenter rows */
  3044. static int b_r(struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  3045. {
  3046. double tmpb = 0.0;
  3047. int i = 0;
  3048. int j = 0;
  3049. int k = 0;
  3050. int ch = 0;
  3051. for (i = 1; i <= max_r; i++) {
  3052. m1->b[i] = row_barycenter(m1, i, max_c);
  3053. }
  3054. for (j = max_r; j > 1; j--) {
  3055. if (m1->b[j] != 0.0) {
  3056. for (i = 1; i < j; i++) {
  3057. if (m1->b[i] != 0.0) {
  3058. k = i + 1;
  3059. while (m1->b[k] == 0.0) {
  3060. k++;
  3061. }
  3062. if (m1->b[i] > m1->b[k]) {
  3063. ch++;
  3064. /* exch_double */
  3065. tmpb = m1->b[k];
  3066. m1->b[k] = m1->b[i];
  3067. m1->b[i] = tmpb;
  3068. exch_rows(m1, i, k);
  3069. if (m2 != NULL) {
  3070. ch++;
  3071. /* exchange cols from i to k */
  3072. exch_columns(m2, i, k);
  3073. }
  3074. }
  3075. }
  3076. }
  3077. }
  3078. }
  3079. return (ch);
  3080. }
  3081. /* barycenter cols */
  3082. static int b_c(struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  3083. {
  3084. double tmpb = 0.0;
  3085. int i = 0;
  3086. int j = 0;
  3087. int k = 0;
  3088. int ch = 0;
  3089. for (i = 1; i <= max_c; i++) {
  3090. m1->b[i] = column_barycenter(m1, i, max_r);
  3091. }
  3092. for (j = max_c; j > 1; j--) {
  3093. if (m1->b[j] != 0.0) {
  3094. for (i = 1; i < j; i++) {
  3095. if (m1->b[i] != 0.0) {
  3096. k = i + 1;
  3097. while (m1->b[k] == 0.0) {
  3098. k++;
  3099. }
  3100. if (m1->b[i] > m1->b[k]) {
  3101. ch++;
  3102. /* exch_double */
  3103. tmpb = m1->b[k];
  3104. m1->b[k] = m1->b[i];
  3105. m1->b[i] = tmpb;
  3106. /* exchange cols from i to k */
  3107. exch_columns(m1, i, k);
  3108. if (m2 != NULL) {
  3109. ch++;
  3110. exch_rows(m2, i, k);
  3111. }
  3112. }
  3113. }
  3114. }
  3115. }
  3116. }
  3117. return (ch);
  3118. }
  3119. /* test if array is sorted, 1 if sorted from hight-to-low */
  3120. static int sorted(double *vector, int maxval)
  3121. {
  3122. int i = 0;
  3123. for (i = 1; i < maxval; i++) {
  3124. /* but ignore 0.0 values */
  3125. if ((vector[i] > vector[i + 1]) && (vector[i + 1] != 0.0)) {
  3126. return (0); /* FALSE */
  3127. }
  3128. }
  3129. return (1); /* TRUE */
  3130. }
  3131. static inline int phase1_down(struct gml_graph *g, struct mmatrix **mm)
  3132. {
  3133. int i = 0;
  3134. int ch = 0;
  3135. /* from level0 down to level max */
  3136. for (i = 0; i < g->maxlevel - 1; i++) {
  3137. ch += b_c(mm[i], mm[i + 1], mm[i]->nrows, mm[i]->ncols);
  3138. }
  3139. ch += b_c(mm[g->maxlevel - 1], NULL, mm[g->maxlevel - 1]->nrows, mm[g->maxlevel - 1]->ncols);
  3140. return (ch);
  3141. }
  3142. static inline int phase1_up(struct gml_graph *g, struct mmatrix **mm)
  3143. {
  3144. int i = 0;
  3145. int ch = 0;
  3146. if (mm == NULL) {
  3147. return (0);
  3148. }
  3149. /* from level max up to level0 */
  3150. for (i = (g->maxlevel - 1); i > 0; i--) {
  3151. ch += b_r(mm[i], mm[i - 1], mm[i]->nrows /* old g->nnodes_of_level[i] */ ,
  3152. mm[i]->ncols /* old g->nnodes_of_level[i + 1] */ );
  3153. }
  3154. ch += b_r(mm[0], NULL, mm[0]->nrows /* g->nnodes_of_level[0] */ ,
  3155. mm[0]->ncols /* g->nnodes_of_level[1] */ );
  3156. return (ch);
  3157. }
  3158. /* */
  3159. static inline int phase2_down(struct gml_graph *g, struct mmatrix **mm)
  3160. {
  3161. int l = 0; /* Level */
  3162. int i = 0;
  3163. int ch = 0;
  3164. for (l = 0; l < (g->maxlevel - 1); l++) {
  3165. for (i = 1; i <= mm[l]->ncols /* g->nnodes_of_level[l + 1] */ ; i++) {
  3166. mm[l]->b[i] = column_barycenter(mm[l], i, mm[l]->nrows /* g->nnodes_of_level[l] */ );
  3167. }
  3168. if (sorted(mm[l]->b, mm[l]->ncols /* g->nnodes_of_level[l + 1] */ ) ==
  3169. 1 /* TRUE */ ) {
  3170. /* reverse columns */
  3171. ch += r_c(mm[l], mm[l + 1], mm[l]->nrows /* g->nnodes_of_level[l] */ ,
  3172. mm[l]->ncols /* g->nnodes_of_level[l + 1] */ );
  3173. } else {
  3174. return (ch);
  3175. }
  3176. }
  3177. for (i = 1; i <= g->nnodes_of_level[g->maxlevel]; i++) {
  3178. mm[g->maxlevel - 1]->b[i] = column_barycenter(mm[g->maxlevel - 1], i, mm[g->maxlevel - 1]->nrows
  3179. /* g->nnodes_of_level[g->maxlevel - 1] */ );
  3180. }
  3181. if (sorted(mm[g->maxlevel - 1]->b, mm[g->maxlevel - 1]->ncols /* g->nnodes_of_level[g->maxlevel] */ ) ==
  3182. 1 /* TRUE */ ) {
  3183. /* reverse columns */
  3184. ch += r_c(mm[g->maxlevel - 1], NULL, mm[g->maxlevel - 1]->nrows /* g->nnodes_of_level[g->maxlevel - 1] */ ,
  3185. mm[g->maxlevel - 1]->ncols /* g->nnodes_of_level[g->maxlevel] */ );
  3186. }
  3187. return (ch);
  3188. }
  3189. /* */
  3190. static inline int phase2_up(struct gml_graph *g, struct mmatrix **mm)
  3191. {
  3192. int l = 0; /* Level */
  3193. int i = 0;
  3194. int ch = 0;
  3195. if (g) {
  3196. }
  3197. for (l = (g->maxlevel - 1); l > 0; l--) {
  3198. for (i = 1; i <= /* g->nnodes_of_level[l] */ mm[l]->nrows; i++) {
  3199. mm[l]->b[i] = row_barycenter(mm[l], i, /* g->nnodes_of_level[l + 1] */
  3200. mm[l]->ncols);
  3201. }
  3202. if (sorted(mm[l]->b, /* g->nnodes_of_level[l] */ mm[l]->nrows) ==
  3203. 1 /* TRUE */ ) {
  3204. /* reverse rows */
  3205. ch += r_r(mm[l], mm[l - 1], mm[l]->nrows /* g->nnodes_of_level[l] */ ,
  3206. mm[l]->ncols /* g->nnodes_of_level[l + 1] */ );
  3207. } else {
  3208. return (ch);
  3209. }
  3210. }
  3211. for (i = 1; i <= mm[0]->nrows /* g->nnodes_of_level[0] */ ; i++) {
  3212. mm[0]->b[i] = row_barycenter(mm[0], i, mm[0]->ncols /* g->nodes_of_level[1] */ );
  3213. }
  3214. /* if level0 is sorted, r_r */
  3215. if (sorted(mm[0]->b, mm[0]->nrows /* g->nnodes_of_level[0] */ ) ==
  3216. 1 /* TRUE */ ) {
  3217. /* reverse rows */
  3218. ch += r_r(mm[0], NULL, mm[0]->nrows /* g->nnodes_of_level[0] */ ,
  3219. mm[0]->ncols /* g->nnodes_of_level[1] */ );
  3220. }
  3221. return (ch);
  3222. }
  3223. /* here if maxlevel >1 */
  3224. static void bc_n(struct gml_graph *g, int it1value, int it2value)
  3225. {
  3226. struct mmatrix **a = NULL;
  3227. struct mmatrix **a1 = NULL;
  3228. struct mmatrix **a2 = NULL;
  3229. struct mmatrix **as = NULL;
  3230. int i = 0;
  3231. int ks = 0;
  3232. int k = 0;
  3233. int n1 = 0;
  3234. int n2 = 0;
  3235. int cht = 0;
  3236. int ch1 = 0;
  3237. int ch2 = 0;
  3238. int r1 = 0;
  3239. int r2 = 0;
  3240. int r3 = 0;
  3241. int rr1 = 0;
  3242. int rr2 = 0;
  3243. int rr3 = 0;
  3244. int it1 = 20; /* iterations phase1 */
  3245. int it2 = 40; /* iterations pahse2 */
  3246. if (it1value == 0) {
  3247. it1 = 20;
  3248. } else {
  3249. it1 = it1value;
  3250. }
  3251. if (it2value == 0) {
  3252. it2 = 40;
  3253. } else {
  3254. it2 = it2value;
  3255. }
  3256. /* the whole graph structures */
  3257. a = (struct mmatrix **) sfg_calloc(1, g->maxlevel * sizeof(struct mmatrix *));
  3258. a1 = (struct mmatrix **) sfg_calloc(1, g->maxlevel * sizeof(struct mmatrix *));
  3259. a2 = (struct mmatrix **) sfg_calloc(1, g->maxlevel * sizeof(struct mmatrix *));
  3260. as = (struct mmatrix **) sfg_calloc(1, g->maxlevel * sizeof(struct mmatrix *));
  3261. /* get matrix struct */
  3262. for (i = 0; i < g->maxlevel; i++) {
  3263. a[i] = (struct mmatrix *) sfg_calloc(1, sizeof(struct mmatrix));
  3264. a1[i] = (struct mmatrix *) sfg_calloc(1, sizeof(struct mmatrix));
  3265. a2[i] = (struct mmatrix *) sfg_calloc(1, sizeof(struct mmatrix));
  3266. as[i] = (struct mmatrix *) sfg_calloc(1, sizeof(struct mmatrix));
  3267. }
  3268. /* get data inside struct */
  3269. for (i = 0; i < g->maxlevel; i++) {
  3270. /* upper level */
  3271. a[i]->level = i;
  3272. a1[i]->level = i;
  3273. a2[i]->level = i;
  3274. as[i]->level = i;
  3275. /* number of rows */
  3276. a[i]->nrows = g->nnodes_of_level[i];
  3277. a1[i]->nrows = g->nnodes_of_level[i];
  3278. a2[i]->nrows = g->nnodes_of_level[i];
  3279. as[i]->nrows = g->nnodes_of_level[i];
  3280. /* number of columns */
  3281. a[i]->ncols = g->nnodes_of_level[i + 1];
  3282. a1[i]->ncols = g->nnodes_of_level[i + 1];
  3283. a2[i]->ncols = g->nnodes_of_level[i + 1];
  3284. as[i]->ncols = g->nnodes_of_level[i + 1];
  3285. /* buffer for barycenter values */
  3286. if (a[i]->nrows > a[i]->ncols) {
  3287. a[i]->bbytes = ((a[i]->nrows + 1) * sizeof(double));
  3288. a1[i]->bbytes = ((a1[i]->nrows + 1) * sizeof(double));
  3289. a2[i]->bbytes = ((a2[i]->nrows + 1) * sizeof(double));
  3290. as[i]->bbytes = ((as[i]->nrows + 1) * sizeof(double));
  3291. } else {
  3292. a[i]->bbytes = ((a[i]->ncols + 1) * sizeof(double));
  3293. a1[i]->bbytes = ((a1[i]->ncols + 1) * sizeof(double));
  3294. a2[i]->bbytes = ((a2[i]->ncols + 1) * sizeof(double));
  3295. as[i]->bbytes = ((as[i]->ncols + 1) * sizeof(double));
  3296. }
  3297. a[i]->b = (double *) sfg_calloc(1, a[i]->bbytes);
  3298. a1[i]->b = (double *) sfg_calloc(1, a1[i]->bbytes);
  3299. a2[i]->b = (double *) sfg_calloc(1, a2[i]->bbytes);
  3300. as[i]->b = (double *) sfg_calloc(1, as[i]->bbytes);
  3301. /* number of bytes used */
  3302. a[i]->nmi0 = ((a[i]->nrows + 1) * sizeof(int));
  3303. a1[i]->nmi0 = ((a[i]->nrows + 1) * sizeof(int));
  3304. a2[i]->nmi0 = ((a[i]->nrows + 1) * sizeof(int));
  3305. as[i]->nmi0 = ((a[i]->nrows + 1) * sizeof(int));
  3306. /* row node id's (int's) */
  3307. a[i]->mi0 = (int *) sfg_calloc(1, a[i]->nmi0);
  3308. a1[i]->mi0 = (int *) sfg_calloc(1, a1[i]->nmi0);
  3309. a2[i]->mi0 = (int *) sfg_calloc(1, a2[i]->nmi0);
  3310. as[i]->mi0 = (int *) sfg_calloc(1, as[i]->nmi0);
  3311. /* number of bytes used */
  3312. a[i]->nm0i = ((a[i]->ncols + 1) * sizeof(int));
  3313. a1[i]->nm0i = ((a[i]->ncols + 1) * sizeof(int));
  3314. a2[i]->nm0i = ((a[i]->ncols + 1) * sizeof(int));
  3315. as[i]->nm0i = ((a[i]->ncols + 1) * sizeof(int));
  3316. /* col node id's (int's) */
  3317. a[i]->m0i = (int *) sfg_calloc(1, a[i]->nm0i);
  3318. a1[i]->m0i = (int *) sfg_calloc(1, a1[i]->nm0i);
  3319. a2[i]->m0i = (int *) sfg_calloc(1, a2[i]->nm0i);
  3320. as[i]->m0i = (int *) sfg_calloc(1, as[i]->nm0i);
  3321. /* bits array for the matrix */
  3322. a[i]->nbytes = 1 + ((((a[i]->nrows + 1) * (a[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  3323. a1[i]->nbytes = 1 + ((((a1[i]->nrows + 1) * (a1[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  3324. a2[i]->nbytes = 1 + ((((a2[i]->nrows + 1) * (a2[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  3325. as[i]->nbytes = 1 + ((((as[i]->nrows + 1) * (as[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  3326. a[i]->bits = (unsigned char *) sfg_calloc(1, a[i]->nbytes);
  3327. a1[i]->bits = (unsigned char *) sfg_calloc(1, a1[i]->nbytes);
  3328. a2[i]->bits = (unsigned char *) sfg_calloc(1, a2[i]->nbytes);
  3329. as[i]->bits = (unsigned char *) sfg_calloc(1, as[i]->nbytes);
  3330. }
  3331. /* fill the matrix with data for all levels */
  3332. for (i = 0; i < g->maxlevel; i++) {
  3333. make_matrix(g, i, a[i]);
  3334. }
  3335. copy_a(g, a, as);
  3336. ks = number_of_crossings_a(g, as);
  3337. g->sugi_icrossings = ks; /* sugiyama initial crossings */
  3338. if (ks > 0) {
  3339. /* Phase1 */
  3340. ch1 = 0;
  3341. /* first does alwasy improve */
  3342. ch1 += phase1_down(g, a);
  3343. copy_a(g, a, as);
  3344. ch1 += phase1_up(g, a);
  3345. copy_a(g, a, as);
  3346. /* loop phase1 */
  3347. n1 = 0;
  3348. do {
  3349. copy_a(g, a, a1);
  3350. ch1 += phase1_down(g, a);
  3351. k = number_of_crossings_a(g, a);
  3352. if (k < ks) {
  3353. /* reduced crossings, save the new state */
  3354. ks = k;
  3355. copy_a(g, a, as);
  3356. }
  3357. ch1 += phase1_up(g, a);
  3358. k = number_of_crossings_a(g, a);
  3359. if (k < ks) {
  3360. ks = k;
  3361. copy_a(g, a, as);
  3362. }
  3363. cht += ch1;
  3364. if (ks == 0) {
  3365. break;
  3366. }
  3367. /* check if number of crossings changed */
  3368. r1 = r2;
  3369. r2 = r3;
  3370. r3 = ks;
  3371. if (r1 == r2) {
  3372. if (r2 == r3) {
  3373. break;
  3374. }
  3375. }
  3376. }
  3377. while (++n1 < it1 && (equal_a(g, a, a1) == 0 /* FALSE */ ));
  3378. /* if matrices differ, save state */
  3379. if (equal_a(g, a, as) == 0 /* FALSE */ ) {
  3380. copy_a(g, as, a);
  3381. }
  3382. if (ks > 0) {
  3383. /* Phase2 */
  3384. n2 = 0;
  3385. cht += ch1;
  3386. do {
  3387. ch2 = 0;
  3388. copy_a(g, a, a2);
  3389. ch2 += phase2_down(g, a);
  3390. n1 = 0;
  3391. do {
  3392. ch1 = 0;
  3393. copy_a(g, a, a1);
  3394. ch1 += phase1_down(g, a);
  3395. k = number_of_crossings_a(g, a);
  3396. if (k < ks) {
  3397. ks = k;
  3398. copy_a(g, a, as);
  3399. }
  3400. ch1 += phase1_up(g, a);
  3401. k = number_of_crossings_a(g, a);
  3402. if (k < ks) {
  3403. ks = k;
  3404. copy_a(g, a, as);
  3405. }
  3406. if (ks == 0) {
  3407. break;
  3408. }
  3409. /* check if number of crossings changed */
  3410. rr1 = rr2;
  3411. rr2 = rr3;
  3412. rr3 = ks;
  3413. if (rr1 == rr2) {
  3414. if (rr2 == rr3) {
  3415. break;
  3416. }
  3417. }
  3418. }
  3419. while (++n1 < it1 && equal_a(g, a, a1) == 0 /* FALSE */ );
  3420. ch2 += phase2_up(g, a);
  3421. n1 = 0;
  3422. do {
  3423. copy_a(g, a, a1);
  3424. ch1 += phase1_up(g, a);
  3425. k = number_of_crossings_a(g, a);
  3426. if (k < ks) {
  3427. ks = k;
  3428. copy_a(g, a, as);
  3429. }
  3430. ch1 += phase1_down(g, a);
  3431. k = number_of_crossings_a(g, a);
  3432. if (k < ks) {
  3433. ks = k;
  3434. copy_a(g, a, as);
  3435. }
  3436. cht += ch1;
  3437. if (ks == 0) {
  3438. break;
  3439. }
  3440. /* check if number of crossings changed */
  3441. rr1 = rr2;
  3442. rr2 = rr3;
  3443. rr3 = ks;
  3444. if (rr1 == rr2) {
  3445. if (rr2 == rr3) {
  3446. break;
  3447. }
  3448. }
  3449. }
  3450. while (++n1 < it1 && equal_a(g, a, a1) == 0 /* FALSE */ );
  3451. cht += ch1;
  3452. cht += ch2;
  3453. if (ks == 0) {
  3454. break;
  3455. }
  3456. /* check if number of crossings changed */
  3457. r1 = r2;
  3458. r2 = r3;
  3459. r3 = ks;
  3460. if (r1 == r2) {
  3461. if (r2 == r3) {
  3462. break;
  3463. }
  3464. }
  3465. }
  3466. while (++n2 < it2 && equal_a(g, a, a2) == 0 /* FALSE */ );
  3467. }
  3468. }
  3469. /* sugiyama final crossings */
  3470. g->sugi_fcrossings = ks;
  3471. /* sugiyama changes made */
  3472. g->sugi_changes = cht;
  3473. for (i = 0; i < g->maxlevel; i += 2) {
  3474. /* set new node positions for 2 levels */
  3475. store_new_positions(g, as[i], i);
  3476. }
  3477. if (i == g->maxlevel) {
  3478. store_new_positions(g, as[g->maxlevel - 1], (g->maxlevel - 1));
  3479. }
  3480. for (i = 0; i < g->maxlevel; i++) {
  3481. if (a[i]) {
  3482. a[i]->b = (double *) sfg_free(a[i]->b);
  3483. a[i]->mi0 = (int *) sfg_free(a[i]->mi0);
  3484. a[i]->m0i = (int *) sfg_free(a[i]->m0i);
  3485. free(a[i]->bits);
  3486. }
  3487. if (a1[i]) {
  3488. a1[i]->b = (double *) sfg_free(a1[i]->b);
  3489. a1[i]->mi0 = (int *) sfg_free(a1[i]->mi0);
  3490. a1[i]->m0i = (int *) sfg_free(a1[i]->m0i);
  3491. free(a1[i]->bits);
  3492. }
  3493. if (a2[i]) {
  3494. a2[i]->b = (double *) sfg_free(a2[i]->b);
  3495. a2[i]->mi0 = (int *) sfg_free(a2[i]->mi0);
  3496. a2[i]->m0i = (int *) sfg_free(a2[i]->m0i);
  3497. free(a2[i]->bits);
  3498. }
  3499. if (as[i]) {
  3500. as[i]->b = (double *) sfg_free(as[i]->b);
  3501. as[i]->mi0 = (int *) sfg_free(as[i]->mi0);
  3502. as[i]->m0i = (int *) sfg_free(as[i]->m0i);
  3503. free(as[i]->bits);
  3504. }
  3505. }
  3506. for (i = 0; i < g->maxlevel; i++) {
  3507. a[i] = (struct mmatrix *) sfg_free(a[i]);
  3508. a1[i] = (struct mmatrix *) sfg_free(a1[i]);
  3509. a2[i] = (struct mmatrix *) sfg_free(a2[i]);
  3510. as[i] = (struct mmatrix *) sfg_free(as[i]);
  3511. }
  3512. a = (struct mmatrix **) sfg_free(a);
  3513. a1 = (struct mmatrix **) sfg_free(a1);
  3514. a2 = (struct mmatrix **) sfg_free(a2);
  3515. as = (struct mmatrix **) sfg_free(as);
  3516. return;
  3517. }
  3518. /*
  3519. This algorithm is for routing hierarchies of elements. A "good route" is
  3520. one that has a minimum number of link crossings. An algorithm that was
  3521. truly optimal (for minimizing link crossings) would be combinatorial and
  3522. therefore cost prohibitive; therefore, this algorithm uses a heuristic
  3523. approach that finds a route with close to the minimum number of crossings
  3524. in a reasonable amount of time.
  3525. This algorithm assumes that all the elements form a directed acyclic graph
  3526. (DAG), which means (1) that links flow one way between elements and (2) for
  3527. any given node there is no way to get back to the node if, starting at the
  3528. node, you traverse the links going from node to node. This algorithm also
  3529. assumes that AT MOST only ONE link may exist between a pair of nodes.
  3530. -------------------------------------------------------------------------------
  3531. OVERVIEW OF ALGORITHM
  3532. All elements that do not have any parents are placed in the first row (row 0).
  3533. Elements are assigned to rows, where the row number for each child is equal to
  3534. the [maximum(row number of all its parents) + 1]. Crossovers are determined
  3535. by examining links between elements on adjacent rows, so if a parent is in a
  3536. row that is not adjacent to its child's row, "dummy" nodes are created on the
  3537. rows in between the parent and child, and the parent and child are connected
  3538. via these dummy nodes.
  3539. Once the elements (now called nodes) are assigned to individual rows, the
  3540. rows are sorted (repeatedly) in order to minimize link crossings. The
  3541. sort criteria involves attempting to both center children under parents and
  3542. to center parents over children. The sort orders are then tweaked by swapping
  3543. nodes that have the same sort value.
  3544. After the column orders are finalized, the nodes are spread out so they are
  3545. more or less centered above their children and below their parents. When
  3546. centering children below parents, a row of children is sorted by which node
  3547. has the greatest number of parents. These get first choice of where to be
  3548. placed under the parents (actually, dummy nodes get first preference, then
  3549. all of the others). Centering parents above children is analogous.
  3550. When done with node placement, there may be some empty columns, and the
  3551. numbering scheme may not start at 0. Therefore, the empty columns must
  3552. be eliminated and every node needs its column renumbered, starting at 0.
  3553. Then you are done.
  3554. -------------------------------------------------------------------------------
  3555. REALIZATION MATRIX
  3556. When it comes to both sorting nodes and horizontally spacing the nodes, two
  3557. adjacent rows are always involved. For example, if we are sorting row[i]
  3558. based on the children of row[i]'s nodes, then row[i+1] is also involved
  3559. at this step. These two rows are called the "i-th realization", and form
  3560. the "i-th realization matrix". A realization matrix shows the parent-child
  3561. relationships between adjacent rows, with the parents on the rows and the
  3562. children on the columns. If there is a parent-child relationship, a 1 is
  3563. stored in the matrix at the position, else a 0 is stored.
  3564. An example:
  3565. A B C D
  3566. \ \ / \ / / |
  3567. \ /\ / \ / |
  3568. /\ / \ / \ |
  3569. / /\ /\ \ |
  3570. // \ / \ \|
  3571. E F G H
  3572. E F G H
  3573. A 0 1 1 0 In this example, parent 'A' has children 'F' and 'G',
  3574. B 1 0 0 1 parent 'B' has children 'E' and 'H',
  3575. C 1 1 0 0 parent 'C' has children 'E' and 'F',
  3576. D 0 0 0 1 and parent 'D' has child 'H'.
  3577. -------------------------------------------------------------------------------
  3578. ROW AND COLUMN BARYCENTERS
  3579. Two other important concepts are the "row barycenter" and the "column
  3580. barycenter" for a node. The "row barycenter" is the basically the average
  3581. of the positions of a node's children. The "column barycenter" is the average
  3582. of the positions of a node's parents. These numbers tell us where a node
  3583. would like to be positioned in its row, depending whether we are positioning
  3584. relative to children or parents.
  3585. For example, using the above realization matrix, we can calculate the row
  3586. barycenters for A, B, C, and D, and the column barycenters for E, F, G, and H.
  3587. Since the row barycenter of a node is equal to the sum of the positions of
  3588. the node's children divided by the number of children of the node, the row
  3589. barycenter for A is (1 + 2)/2 = 1.5. This assumes that we start numbering
  3590. rows and columns at 0. Similarly, the column barycenter of a node is equal
  3591. to the sum of the positions of the node's parents divided by the number of
  3592. parents of the node. So, the column barycenter of F is (0 + 2)/2 = 1.0.
  3593. The complete example is as follows:
  3594. Row
  3595. | E F G H | Barys
  3596. ------+--------------------+-----
  3597. A | 0 1 1 0 | 1.5
  3598. B | 1 0 0 1 | 1.5
  3599. C | 1 1 0 0 | 0.5
  3600. D | 0 0 0 1 | 3.0
  3601. ------+--------------------+-----
  3602. Col | 1.5 1.0 0.0 2.0 |
  3603. Barys | |
  3604. If we were to sort the child nodes here by their column barycenters, the new
  3605. order would be G, F, E, H. If we were to sort the parent nodes here by their
  3606. row barycenters, then the order would be C, A, B, D (if two or more nodes have
  3607. the same value, be sure to keep the same precedence that already exists
  3608. between them, e.g., make sure that order after sorting is not C, B, A, D).
  3609. If a node has no parents then it can't have a column barycenter. This case
  3610. should never happen, as all nodes that have no parents should be in the first
  3611. level of the hierarchy, and these nodes would only be represented in
  3612. realization matrix 0, and they would only have row barycenters.
  3613. If a node has no children then it can't have a row barycenter. In this case,
  3614. while sorting based on row barycenters, sort AROUND these nodes, i.e., do
  3615. not change their positions at all. For example, if we had the following:
  3616. Row
  3617. | W X Y Z | Barys
  3618. ------+--------------------+-----
  3619. Q | 0 1 1 1 | 2.0
  3620. R | 0 0 0 0 | ???
  3621. S | 1 0 0 0 | 0.0
  3622. T | 0 1 0 1 | 2.0
  3623. ------+--------------------+-----
  3624. Col | 2.0 1.5 0.0 1.5 |
  3625. Barys | |
  3626. and we were to sort by row barycenters, the resulting order should be S, R,
  3627. Q, T. Notice how R stayed in its position, and even though Q and T had the
  3628. same barycentric value, Q stayed before T.
  3629. The whole reason for sorting rows and columns by their barycenters is to
  3630. decrease the number of crossovers.
  3631. -------------------------------------------------------------------------------
  3632. CROSSOVERS
  3633. The realization matrix is also used to count the number of crossovers between
  3634. two adjacent rows of nodes. For each row, starting with the second row, if
  3635. a row element has a 1, then sum up all of the matrix elements that are above
  3636. AND to the right of this element. Looking again at the first example:
  3637. A B C D
  3638. \ \ / \ / / |
  3639. \ /\ / \ / |
  3640. /\ / \ / \ |
  3641. / /\ /\ \ |
  3642. // \ / \ \|
  3643. E F G H
  3644. Row
  3645. | E F G H | Barys
  3646. ------+--------------------+-----
  3647. A | 0 1 1 0 | 1.5
  3648. B | 1 0 0 1 | 1.5
  3649. C | 1 1 0 0 | 0.5
  3650. D | 0 0 0 1 | 3.0
  3651. ------+--------------------+-----
  3652. Col | 1.5 1.0 0.0 2.0 |
  3653. Barys | |
  3654. Starting with the second row (parent B's row), position B-E has a 1. Looking
  3655. at positions above and to the right, we see positions A-F and A-G both have
  3656. a 1, so the number of crossovers is currently = 2. Position B-H has a 1, but
  3657. there are no elements above and to the right, so crossovers is still = 2.
  3658. For parent row of C, position C-E crosses over with B-H, A-F, and A-G, so
  3659. crossovers = 5. C-F crosses over with B-H and A-G, so crossovers = 7. For
  3660. parent row D, position D-H doesn't cross over with any other link. So for
  3661. this realization matrix representing these two rows, the number of crossovers
  3662. is 7.
  3663. The total number of crossovers for the whole graph would be the sum of the
  3664. crossovers from each matrix realization.
  3665. -------------------------------------------------------------------------------
  3666. NODE CENTERING
  3667. After the nodes for each row have their final sort order, the nodes need to
  3668. be assigned to grid positions. Their initial grid position will be their
  3669. column position, by which we mean their array position in the row. From now
  3670. on, when we take a row or column barycenter, we will be using grid positions
  3671. instead of column positions.
  3672. Note: The following examples will be based on centering children with respect
  3673. to their parents' positions. Centering parents based on their children's
  3674. positions is analogous.
  3675. When positioning the nodes on a row based on their parents' positions, the
  3676. nodes must be initially sorted to see which nodes get first choice. The dummy
  3677. nodes go first, and the rest of nodes are sorted in descending order based on
  3678. the number of parents the node has. If a dummy node has a parent that has
  3679. multiple dummy nodes, all of these dummy nodes are again sorted by how close
  3680. to the center of the parent's children they are. This is a confusing
  3681. statement, best illustrated by example:
  3682. P1 P2
  3683. \ |
  3684. \ __________^__________
  3685. \| | | | |
  3686. C1 D1 D2 C2 D3
  3687. Here, parent P1 has one child, C1. Parent P2 has five children, and three of
  3688. the child nodes are dummy nodes: D1, D2, and D3. C1 is child 0 of P2, D1 is
  3689. child 1 of P2, D2 is child 2 of P2, C2 is child 3 of P2, and D3 is child 4 of
  3690. P2. The child midpoint underneath the parent is equal to
  3691. (the number of children - 1) / 2, so (5 - 1) / 2 = 2. Since the dummy nodes
  3692. go first, the initial order is D1, D2, D3, C1 (because it has 2 parents), and
  3693. finally C2. All of the dummy nodes have the same parent, so we will sort them
  3694. based on how far away they are from the parent's child midpoint. D1 is child
  3695. 1 of P2, so it is 1 away. D2 is child 2 of P2, so it is 0 away. D3 is child
  3696. 4 of P2, so it is 2 away. Therefore, the final order for choosing positions
  3697. is D2, D1, D3, C1, C2.
  3698. In a situation similar to the dummy nodes, if a non-dummy node has a only one
  3699. parent, and that parent has other children with just one parent, then these
  3700. one parent child nodes that have the same parent need additional sorting in
  3701. the exact same manner that we just did the dummy nodes.
  3702. The whole purpose behind this is so that the left most node doesn't get first
  3703. choice. If it did, we would get graphs that look like:
  3704. A A
  3705. | |
  3706. |_________ instead of _____^_____
  3707. | | | | | |
  3708. B C D B C D
  3709. Anyway, once we have a sort order for the nodes of a row, we place the nodes
  3710. in their preferred positions. Using the previous example, assume that P1
  3711. is in grid position 2 and P2 is in grid position 5. D2 gets first choice,
  3712. and its column barycenter (based now on parent grid positions, not column
  3713. positions) is 5, so we place D2 in position 5. D1 is next, its barycenter
  3714. is also 5. We can't give it 5 since that position is occupied, so we give
  3715. it the closest possible position we can, which in this case is 4. D3 is next,
  3716. and its barycenter is also 5. The closest position that we can give it is
  3717. position 7, since we must allow room for C2. C1 is next, and its barycenter
  3718. is (2 + 5)/2 = 3.5, which we round to 3. Position 3 is open, so we go ahead
  3719. and give it position 3. C2 is last, and its barycenter is 5. However, the
  3720. first position available to it based on its left neighbor is position 6, so
  3721. we assign it position 6.
  3722. -------------------------------------------------------------------------------
  3723. GOING 'UP' OR 'DOWN' THE GRAPH
  3724. "Going down the graph" means taking each realization matrix situation,
  3725. starting with Realization Matrix 0, and performing some action on it, then
  3726. going to the next realization matrix, and continuing until all of the
  3727. realization matrices have been visited.
  3728. "Going up the graph" is analogous, except you start at the bottom with the
  3729. last realization matrix and work up to Realization Matrix 0.
  3730. */
  3731. static void barycenter(struct gml_graph *g, int it1v, int it2v)
  3732. {
  3733. /* number of crossing edges at level */
  3734. if (g->numce == NULL) {
  3735. g->numce = (int *) sfg_calloc(1, (g->maxlevel + 1) * sizeof(int));
  3736. }
  3737. if (g->maxlevel == 0) {
  3738. /* if graph has only 1 or more nodes */
  3739. return;
  3740. }
  3741. if (g->nnodes < 2) {
  3742. return;
  3743. }
  3744. if (g->nedges < 2) {
  3745. return;
  3746. }
  3747. bc_n(g, it1v, it2v);
  3748. return;
  3749. }
  3750. /* min. distance between 2 nodes */
  3751. static int mindist = 1;
  3752. /* current startnode nr field */
  3753. static int csn = 0;
  3754. /* node list of part of graph */
  3755. static struct gml_nlist *cnodelist = NULL;
  3756. static struct gml_nlist *cnodelisttail = NULL;
  3757. /* number of nodes at level */
  3758. static int *cnnodes_of_level = NULL;
  3759. /* max. x,y in part of graph */
  3760. static int cmaxx = 0;
  3761. static int cmaxy = 0;
  3762. /* widest x level */
  3763. static int cwidestnnodes = 0;
  3764. /* x width at position */
  3765. static int *cwpos = NULL;
  3766. /* lists per pos. */
  3767. static struct gml_nlist **cposnodes = NULL;
  3768. /* hor pos */
  3769. static int *chpos = NULL;
  3770. /* hor pos */
  3771. static struct gml_nlist **clevelnodes = NULL;
  3772. /* (x,y) spacing */
  3773. static int xspacing = 0;
  3774. static int yspacing = 0;
  3775. /* */
  3776. struct node_data {
  3777. struct gml_node *node;
  3778. int priority;
  3779. int done;
  3780. };
  3781. /* nodes in one level */
  3782. static struct node_data *nl = NULL;
  3783. static int is_dummy(struct gml_node *node)
  3784. {
  3785. if (node->dummy) {
  3786. return (1);
  3787. } else {
  3788. return (0);
  3789. }
  3790. }
  3791. /* how many connection edges from previous level */
  3792. static int upper_connectivity(struct gml_node *node)
  3793. {
  3794. struct gml_elist *el = NULL;
  3795. int result = 0;
  3796. result = 0;
  3797. if (node == NULL) {
  3798. /* shouldnothappen */
  3799. return (0);
  3800. }
  3801. /* incoming edges for_targetlist(node,edge) */
  3802. el = node->incoming_e;
  3803. while (el) {
  3804. /* skip hor. edges */
  3805. if (el->edge->hedge == 0) {
  3806. /* only in this part of graph */
  3807. if (el->edge->from_node->startnode == csn) {
  3808. result++;
  3809. }
  3810. }
  3811. el = el->next;
  3812. }
  3813. return (result);
  3814. }
  3815. /* how many connection edges to next level */
  3816. static int lower_connectivity(struct gml_node *node)
  3817. {
  3818. struct gml_elist *el = NULL;
  3819. int result = 0;
  3820. result = 0;
  3821. if (node == NULL) {
  3822. /* shouldnothappen */
  3823. return (0);
  3824. }
  3825. /* outgoing edges for_sourcelist(node,edge) */
  3826. el = node->outgoing_e;
  3827. while (el) {
  3828. /* skip hor. edges */
  3829. if (el->edge->hedge == 0) {
  3830. /* only in this part of graph */
  3831. if (el->edge->to_node->startnode == csn) {
  3832. result++;
  3833. }
  3834. }
  3835. el = el->next;
  3836. }
  3837. return (result);
  3838. }
  3839. /* simple floor() function */
  3840. static double do_floor(double num)
  3841. {
  3842. double ret = 0.0;
  3843. if (num < 0) {
  3844. ret = (int)(num - 1);
  3845. } else {
  3846. ret = (int)num;
  3847. }
  3848. return (ret);
  3849. }
  3850. /* avg x pos of incoming edges */
  3851. static int upper_barycenter(struct gml_node *node)
  3852. {
  3853. struct gml_elist *el = NULL;
  3854. int result = 0;
  3855. double r = 0.0;
  3856. if (node == NULL) {
  3857. /* shouldnothappen */
  3858. return (0);
  3859. }
  3860. /* incoming edges x sum for_targetlist(node,edge) */
  3861. el = node->incoming_e;
  3862. while (el) {
  3863. /* skip hor. edges */
  3864. if (el->edge->hedge == 0) {
  3865. /* only in this part of graph */
  3866. if (el->edge->from_node->startnode == csn) {
  3867. result += (el->edge->from_node->absx);
  3868. }
  3869. }
  3870. el = el->next;
  3871. }
  3872. if (result == 0) {
  3873. r = (0.0);
  3874. } else {
  3875. if (upper_connectivity(node) == 0) {
  3876. r = 0.0;
  3877. } else {
  3878. r = (result / upper_connectivity(node));
  3879. }
  3880. }
  3881. r = do_floor(r + 0.5);
  3882. return ((int)r);
  3883. }
  3884. /* avg x pos of outgoing edges */
  3885. static int lower_barycenter(struct gml_node *node)
  3886. {
  3887. struct gml_elist *el = NULL;
  3888. int result = 0;
  3889. double r = 0.0;
  3890. if (node == NULL) {
  3891. /* shouldnothappen */
  3892. return (0);
  3893. }
  3894. /* get avg. x pos of outgoing edges for_sourcelist(node,edge) */
  3895. el = node->outgoing_e;
  3896. while (el) {
  3897. /* skip hor. edges */
  3898. if (el->edge->hedge == 0) {
  3899. /* only in this part of graph */
  3900. if (el->edge->to_node->startnode == csn) {
  3901. result += (el->edge->to_node->absx);
  3902. }
  3903. }
  3904. el = el->next;
  3905. }
  3906. if (result == 0) {
  3907. r = (0.0);
  3908. } else {
  3909. if (lower_connectivity(node) == 0) {
  3910. r = 0.0;
  3911. } else {
  3912. r = (result / lower_connectivity(node));
  3913. }
  3914. }
  3915. r = do_floor(r + 0.5);
  3916. return ((int)r);
  3917. }
  3918. static void sort(int n)
  3919. {
  3920. int i = 0;
  3921. int j = 0;
  3922. struct node_data h;
  3923. for (j = n - 1; j > 0; j--) {
  3924. for (i = 0; i < j; i++) {
  3925. /* issue here */
  3926. if (nl[i].node && nl[i + 1].node) {
  3927. if (nl[i].node->relx > nl[i + 1].node->relx) {
  3928. /* swap */
  3929. h = nl[i];
  3930. nl[i] = nl[i + 1];
  3931. nl[i + 1] = h;
  3932. }
  3933. }
  3934. }
  3935. }
  3936. return;
  3937. }
  3938. /* */
  3939. static void make_node_list_up(int l)
  3940. {
  3941. struct gml_nlist *gnl = NULL;
  3942. struct gml_node *n = NULL;
  3943. int i = 0;
  3944. /* for_all_nodes(g,n) */
  3945. gnl = cnodelist;
  3946. i = 0;
  3947. while (gnl) {
  3948. n = gnl->node;
  3949. if (n->absy == l) {
  3950. nl[i].node = n;
  3951. nl[i].done = 0; /* FALSE */
  3952. if (is_dummy(n) == 1) { /* */
  3953. /* higer value then the highest node in this level */
  3954. /*old nl[i].priority = (cnnodes_of_level[l + 1] + 1000 */
  3955. nl[i].priority = (100000 - n->relx);
  3956. } else {
  3957. nl[i].priority = lower_connectivity(n);
  3958. }
  3959. i++;
  3960. }
  3961. gnl = gnl->next;
  3962. }
  3963. sort(cnnodes_of_level[l]);
  3964. return;
  3965. }
  3966. /* */
  3967. static void make_node_list_down(int l)
  3968. {
  3969. struct gml_nlist *gnl = NULL;
  3970. struct gml_node *n = NULL;
  3971. int i = 0;
  3972. /* for_all_nodes(g,n) */
  3973. gnl = cnodelist;
  3974. while (gnl) {
  3975. n = gnl->node;
  3976. if (n->absy == l) {
  3977. nl[i].node = n;
  3978. nl[i].done = 0; /* FALSE */
  3979. if (is_dummy(n) == 1) { /* */
  3980. /* give dummy node uniq high number */
  3981. /*old nl[i].priority = (cnnodes_of_level[l - 1] + 1000 */
  3982. nl[i].priority = (100000 - n->relx);
  3983. } else {
  3984. nl[i].priority = upper_connectivity(n);
  3985. }
  3986. i++;
  3987. }
  3988. gnl = gnl->next;
  3989. }
  3990. sort(cnnodes_of_level[l]);
  3991. return;
  3992. }
  3993. /* get number of node with highest prio which is not done yet */
  3994. static int find_next(int n)
  3995. {
  3996. int index = 0;
  3997. int i = 0;
  3998. int highest_priority = 0;
  3999. for (i = 0; i < n; i++) {
  4000. if ((nl[i].priority >= highest_priority)
  4001. && (nl[i].done == 0 /* FALSE */ )) {
  4002. index = i;
  4003. highest_priority = nl[i].priority;
  4004. }
  4005. }
  4006. return (index);
  4007. }
  4008. static void do_down(int l)
  4009. {
  4010. int i = 0;
  4011. int index = 0;
  4012. int j = 0;
  4013. int optimal_position = 0;
  4014. int distance = 0;
  4015. int possible_distance = 0;
  4016. for (i = 0; i < cnnodes_of_level[l]; i++) {
  4017. index = find_next(cnnodes_of_level[l]);
  4018. if (nl[index].node) {
  4019. optimal_position = upper_barycenter(nl[index].node);
  4020. if (optimal_position == 0) {
  4021. optimal_position = nl[index].node->absx;
  4022. }
  4023. if (optimal_position < nl[index].node->absx) {
  4024. distance = nl[index].node->absx - optimal_position;
  4025. possible_distance = 0;
  4026. j = index;
  4027. do {
  4028. if (j > 0) {
  4029. possible_distance += nl[j].node->absx - nl[j - 1].node->absx - mindist;
  4030. } else {
  4031. /* j==0, no nodes at left */
  4032. possible_distance += nl[j].node->absx - mindist;
  4033. }
  4034. j--;
  4035. }
  4036. while ((j >= 0) && !(nl[j].done));
  4037. if (possible_distance < distance) {
  4038. distance = possible_distance;
  4039. }
  4040. j = index;
  4041. while (distance > 0) {
  4042. int d = 0;
  4043. int k = 0;
  4044. if (j == 0) {
  4045. d = distance;
  4046. } else {
  4047. if (nl[j].node->absx - nl[j - 1].node->absx - mindist < distance) {
  4048. d = nl[j].node->absx - nl[j - 1].node->absx - mindist;
  4049. } else {
  4050. d = distance;
  4051. }
  4052. }
  4053. for (k = j; k <= index; k++) {
  4054. nl[k].node->absx -= d;
  4055. }
  4056. j--;
  4057. distance -= d;
  4058. }
  4059. } else {
  4060. distance = optimal_position - nl[index].node->absx;
  4061. possible_distance = 0;
  4062. j = index;
  4063. do {
  4064. if (j < cnnodes_of_level[l] - 1) {
  4065. possible_distance += nl[j + 1].node->absx - nl[j].node->absx - mindist;
  4066. } else {
  4067. /* j == cnnodes_of_level[l]-1, no nodes rechts */
  4068. possible_distance += distance;
  4069. }
  4070. j++;
  4071. }
  4072. while ((j < cnnodes_of_level[l]) && !(nl[j].done));
  4073. if (possible_distance < distance) {
  4074. distance = possible_distance;
  4075. }
  4076. j = index;
  4077. while (distance > 0) {
  4078. int d = 0;
  4079. int k = 0;
  4080. if (j == cnnodes_of_level[l] - 1) {
  4081. d = distance;
  4082. } else {
  4083. if (nl[j + 1].node->absx - nl[j].node->absx - mindist < distance) {
  4084. d = nl[j + 1].node->absx - nl[j].node->absx - mindist;
  4085. } else {
  4086. d = distance;
  4087. }
  4088. }
  4089. for (k = index; k <= j; k++) {
  4090. nl[k].node->absx += d;
  4091. }
  4092. j++;
  4093. distance -= d;
  4094. }
  4095. }
  4096. nl[index].done = 1; /* TRUE */
  4097. }
  4098. }
  4099. return;
  4100. }
  4101. static void do_up(int l)
  4102. {
  4103. int i = 0;
  4104. int index = 0;
  4105. int j = 0;
  4106. int optimal_position = 0;
  4107. int distance = 0;
  4108. int possible_distance = 0;
  4109. for (i = 0; i < cnnodes_of_level[l]; i++) {
  4110. index = find_next(cnnodes_of_level[l]);
  4111. if (nl[index].node) {
  4112. optimal_position = lower_barycenter(nl[index].node);
  4113. if (optimal_position == 0) {
  4114. optimal_position = nl[index].node->absx;
  4115. }
  4116. if (optimal_position < nl[index].node->absx) {
  4117. distance = nl[index].node->absx - optimal_position;
  4118. possible_distance = 0;
  4119. j = index;
  4120. do {
  4121. if (j > 0) {
  4122. possible_distance += nl[j].node->absx - nl[j - 1].node->absx - mindist;
  4123. } else {
  4124. /* j == 0, no nodes links */
  4125. possible_distance += nl[0].node->absx - mindist;
  4126. }
  4127. j--;
  4128. }
  4129. while ((j >= 0) && !(nl[j].done));
  4130. if (possible_distance < distance) {
  4131. distance = possible_distance;
  4132. }
  4133. j = index;
  4134. while (distance > 0) {
  4135. int d = 0;
  4136. int k = 0;
  4137. if (j == 0) {
  4138. d = distance;
  4139. } else {
  4140. if (nl[j].node->absx - nl[j - 1].node->absx - mindist < distance) {
  4141. d = nl[j].node->absx - nl[j - 1].node->absx - mindist;
  4142. } else {
  4143. d = distance;
  4144. }
  4145. }
  4146. for (k = j; k <= index; k++) {
  4147. nl[k].node->absx -= d;
  4148. }
  4149. j--;
  4150. distance -= d;
  4151. }
  4152. } else {
  4153. /* optimal_position >= nl[index].node->absx */
  4154. distance = optimal_position - nl[index].node->absx;
  4155. possible_distance = 0;
  4156. j = index;
  4157. do {
  4158. if (j < cnnodes_of_level[l] - 1) {
  4159. possible_distance += nl[j + 1].node->absx - nl[j].node->absx - mindist;
  4160. } else {
  4161. /* j == cnnodes_of_level[l]-1, no nodes rechts */
  4162. possible_distance += distance;
  4163. }
  4164. j++;
  4165. }
  4166. while ((j < cnnodes_of_level[l]) && !(nl[j].done));
  4167. if (possible_distance < distance) {
  4168. distance = possible_distance;
  4169. }
  4170. j = index;
  4171. while (distance > 0) {
  4172. int d = 0;
  4173. int k = 0;
  4174. if (j == cnnodes_of_level[l] - 1) {
  4175. d = distance;
  4176. } else {
  4177. if (nl[j + 1].node->absx - nl[j].node->absx - mindist < distance) {
  4178. d = nl[j + 1].node->absx - nl[j].node->absx - mindist;
  4179. } else {
  4180. d = distance;
  4181. }
  4182. }
  4183. for (k = index; k <= j; k++) {
  4184. nl[k].node->absx += d;
  4185. }
  4186. j++;
  4187. distance -= d;
  4188. }
  4189. }
  4190. nl[index].done = 1; /* TRUE */
  4191. }
  4192. }
  4193. return;
  4194. }
  4195. /* determine relative node pos. from the barycenter rel. node pos. */
  4196. static void improve_positions2local(struct gml_graph *g)
  4197. {
  4198. int i = 0;
  4199. int count = 0;
  4200. int ii = 0;
  4201. int sl = 0;
  4202. /* start level is 0 */
  4203. sl = 0;
  4204. /* min. node dist */
  4205. mindist = 1;
  4206. /* number of up/down sweeps */
  4207. count = 1;
  4208. for (ii = 0; ii < count; ii++) {
  4209. /* DOWN */
  4210. for (i = sl; i < g->maxlevel; i++) {
  4211. if (cnnodes_of_level[i]) {
  4212. nl = (struct node_data *) sfg_calloc(1, cnnodes_of_level[i] * sizeof(struct node_data));
  4213. make_node_list_down(i);
  4214. do_down(i);
  4215. nl = (struct node_data *) sfg_free(nl);
  4216. }
  4217. }
  4218. /* UP */
  4219. for (i = (g->maxlevel - 1); i >= sl; i--) {
  4220. if (cnnodes_of_level[i]) {
  4221. nl = (struct node_data *) sfg_calloc(1, cnnodes_of_level[i] * sizeof(struct node_data));
  4222. make_node_list_up(i);
  4223. do_up(i);
  4224. nl = (struct node_data *) sfg_free(nl);
  4225. }
  4226. }
  4227. }
  4228. /* top+bottom update */
  4229. if ((sl + 2) < g->maxlevel) {
  4230. for (i = sl + 2; i >= sl; i--) {
  4231. if (cnnodes_of_level[i]) {
  4232. nl = (struct node_data *) sfg_calloc(1, cnnodes_of_level[i] * sizeof(struct node_data));
  4233. make_node_list_up(i);
  4234. do_up(i);
  4235. nl = (struct node_data *) sfg_free(nl);
  4236. }
  4237. }
  4238. }
  4239. for (i = (g->maxlevel - 2); i <= g->maxlevel; i++) {
  4240. if (i >= 0) {
  4241. if (cnnodes_of_level[i]) {
  4242. nl = (struct node_data *) sfg_calloc(1, cnnodes_of_level[i] * sizeof(struct node_data));
  4243. make_node_list_down(i);
  4244. do_down(i);
  4245. nl = (struct node_data *) sfg_free(nl);
  4246. }
  4247. }
  4248. }
  4249. return;
  4250. }
  4251. /* create nodes-at-level-count */
  4252. static void make_cnnodes_at_level(struct gml_graph *g)
  4253. {
  4254. struct gml_nlist *gnl = NULL;
  4255. cnnodes_of_level = (int *) sfg_calloc(1, ((g->maxlevel + 1) * sizeof(int)));
  4256. gnl = cnodelist;
  4257. while (gnl) {
  4258. cnnodes_of_level[gnl->node->rely] = cnnodes_of_level[gnl->node->rely] + 1;
  4259. gnl = gnl->next;
  4260. }
  4261. return;
  4262. }
  4263. /* clear nodes-at-level-count */
  4264. static void clear_cnnodes_at_level(void)
  4265. {
  4266. /* number of nodes at level */
  4267. if (cnnodes_of_level) {
  4268. cnnodes_of_level = (int *) sfg_free(cnnodes_of_level);
  4269. }
  4270. /* number of nodes at level */
  4271. cnnodes_of_level = NULL;
  4272. return;
  4273. }
  4274. /* copy part of graph */
  4275. static void make_cnodelist(struct gml_graph *g)
  4276. {
  4277. struct gml_nlist *gnl = NULL;
  4278. struct gml_nlist *newnl = NULL;
  4279. gnl = g->nodelist;
  4280. while (gnl) {
  4281. /* check if node belongs to part of graph */
  4282. if (gnl->node->startnode == csn) {
  4283. /* copy node in new list */
  4284. newnl = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  4285. newnl->node = gnl->node;
  4286. if (cnodelist == NULL) {
  4287. cnodelist = newnl;
  4288. cnodelisttail = newnl;
  4289. } else {
  4290. cnodelisttail->next = newnl;
  4291. cnodelisttail = newnl;
  4292. }
  4293. }
  4294. gnl = gnl->next;
  4295. }
  4296. return;
  4297. }
  4298. /* done with this part of graph */
  4299. static void clear_cnodelist(void)
  4300. {
  4301. struct gml_nlist *gnl = NULL;
  4302. struct gml_nlist *gnlnext = NULL;
  4303. gnl = cnodelist;
  4304. while (gnl) {
  4305. gnlnext = gnl->next;
  4306. gnl = (struct gml_nlist *) sfg_free(gnl);
  4307. gnl = gnlnext;
  4308. }
  4309. /* node list of part of graph */
  4310. cnodelist = NULL;
  4311. cnodelisttail = NULL;
  4312. return;
  4313. }
  4314. /* move image of this part of graph */
  4315. static void move0(void)
  4316. {
  4317. struct gml_nlist *gnl = NULL;
  4318. int mx = 0;
  4319. /* find min. x pos in-use */
  4320. mx = 1000 * 1000; /* just some high value */
  4321. gnl = cnodelist;
  4322. while (gnl) {
  4323. if (gnl->node->absx < mx) {
  4324. mx = gnl->node->absx;
  4325. }
  4326. gnl = gnl->next;
  4327. }
  4328. /* move whole drawing to the left */
  4329. gnl = cnodelist;
  4330. while (gnl) {
  4331. gnl->node->absx = (gnl->node->absx - mx);
  4332. gnl = gnl->next;
  4333. }
  4334. return;
  4335. }
  4336. /* */
  4337. static void make_cposnodes(void)
  4338. {
  4339. struct gml_nlist *lnl = NULL;
  4340. struct gml_nlist *newl = NULL;
  4341. int i = 0;
  4342. int lmaxw = 0;
  4343. int maxrx = 0;
  4344. /* widest x level */
  4345. cwidestnnodes = 0;
  4346. /* x width at position */
  4347. cwpos = NULL;
  4348. /* lists per pos. */
  4349. cposnodes = NULL;
  4350. /* extra check max rel. x pos. */
  4351. lnl = cnodelist;
  4352. while (lnl) {
  4353. if (lnl->node->absx > maxrx) {
  4354. maxrx = lnl->node->absx;
  4355. }
  4356. lnl = lnl->next;
  4357. }
  4358. /* pos2.c has moved node in x dir. */
  4359. cwidestnnodes = maxrx;
  4360. /* x width at position */
  4361. cwpos = (int *) sfg_calloc(1, (cwidestnnodes + 1) * sizeof(int));
  4362. if (cwpos == NULL) {
  4363. return;
  4364. }
  4365. /* lists with nodes up to down at position */
  4366. cposnodes = (struct gml_nlist **) sfg_calloc(1, (cwidestnnodes + 1) * sizeof(struct gml_nlist *));
  4367. if (cposnodes == NULL) {
  4368. return;
  4369. }
  4370. /* create for every postion the list of nodes at that position */
  4371. lnl = cnodelist;
  4372. while (lnl) {
  4373. i = lnl->node->absx;
  4374. newl = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  4375. if (newl == NULL) {
  4376. return;
  4377. }
  4378. newl->node = lnl->node;
  4379. if (cposnodes[i] == NULL) {
  4380. cposnodes[i] = newl;
  4381. newl->next = NULL;
  4382. } else {
  4383. newl->next = cposnodes[i];
  4384. cposnodes[i] = newl;
  4385. }
  4386. lnl = lnl->next;
  4387. }
  4388. /* determine the max width of a element at vertical pos. */
  4389. for (i = 0; i < (cwidestnnodes + 1); i++) {
  4390. lmaxw = 0;
  4391. /* lists per pos. */
  4392. lnl = cposnodes[i];
  4393. while (lnl) {
  4394. if (lnl->node->bbx > lmaxw) {
  4395. lmaxw = lnl->node->bbx;
  4396. }
  4397. lnl = lnl->next;
  4398. }
  4399. cwpos[i] = lmaxw;
  4400. }
  4401. return;
  4402. }
  4403. /* */
  4404. static void clear_cposnodes(void)
  4405. {
  4406. int i = 0;
  4407. struct gml_nlist *lnl = NULL;
  4408. struct gml_nlist *nlnext = NULL;
  4409. /* width of positions */
  4410. if (cwpos) {
  4411. cwpos = (int *) sfg_free(cwpos);
  4412. }
  4413. for (i = 0; i < (cwidestnnodes + 1); i++) {
  4414. /* lists per pos. */
  4415. lnl = cposnodes[i];
  4416. while (lnl) {
  4417. nlnext = lnl->next;
  4418. lnl = (struct gml_nlist *) sfg_free(lnl);
  4419. lnl = nlnext;
  4420. }
  4421. cposnodes[i] = NULL;
  4422. }
  4423. cposnodes = (struct gml_nlist **) sfg_free(cposnodes);
  4424. return;
  4425. }
  4426. /* y positioning */
  4427. static void make_clevelnodes(struct gml_graph *g)
  4428. {
  4429. struct gml_nlist *lnl = NULL;
  4430. struct gml_nlist *newl = NULL;
  4431. int i = 0;
  4432. int lmaxh = 0;
  4433. chpos = (int *) sfg_calloc(1, (g->maxlevel + 1) * sizeof(int));
  4434. if (chpos == NULL) {
  4435. return;
  4436. }
  4437. clevelnodes = (struct gml_nlist **) sfg_calloc(1, (g->maxlevel + 1) * sizeof(struct gml_nlist *));
  4438. if (clevelnodes == NULL) {
  4439. return;
  4440. }
  4441. lnl = cnodelist;
  4442. while (lnl) {
  4443. i = lnl->node->absy;
  4444. newl = (struct gml_nlist *) sfg_calloc(1, sizeof(struct gml_nlist));
  4445. if (newl == NULL) {
  4446. return;
  4447. }
  4448. newl->node = lnl->node;
  4449. if (clevelnodes[i] == NULL) {
  4450. clevelnodes[i] = newl;
  4451. newl->next = NULL;
  4452. } else {
  4453. newl->next = clevelnodes[i];
  4454. clevelnodes[i] = newl;
  4455. }
  4456. lnl = lnl->next;
  4457. }
  4458. /* determine the max width of a element at vertical pos. */
  4459. for (i = 0; i < (g->maxlevel + 1); i++) {
  4460. lmaxh = 0;
  4461. /* lists per pos. */
  4462. lnl = clevelnodes[i];
  4463. while (lnl) {
  4464. if (lnl->node->bby > lmaxh) {
  4465. lmaxh = lnl->node->bby;
  4466. }
  4467. lnl = lnl->next;
  4468. }
  4469. chpos[i] = lmaxh;
  4470. }
  4471. return;
  4472. }
  4473. static void clear_clevelnodes(struct gml_graph *g)
  4474. {
  4475. int i = 0;
  4476. struct gml_nlist *lnl = NULL;
  4477. struct gml_nlist *nlnext = NULL;
  4478. /* width of positions */
  4479. if (chpos) {
  4480. chpos = (int *) sfg_free(chpos);
  4481. }
  4482. for (i = 0; i < (g->maxlevel + 1); i++) {
  4483. /* lists per pos. */
  4484. lnl = clevelnodes[i];
  4485. while (lnl) {
  4486. nlnext = lnl->next;
  4487. lnl = (struct gml_nlist *) sfg_free(lnl);
  4488. lnl = nlnext;
  4489. }
  4490. clevelnodes[i] = NULL;
  4491. }
  4492. clevelnodes = (struct gml_nlist **) sfg_free(clevelnodes);
  4493. return;
  4494. }
  4495. /* determine final (x,y) pos */
  4496. static void cfinalxy(struct gml_graph *g)
  4497. {
  4498. struct gml_nlist *lnl = NULL;
  4499. int hw = 0;
  4500. int xoff = 0;
  4501. int yoff = 0;
  4502. int i = 0;
  4503. int ecount = 0;
  4504. /* x positioning */
  4505. make_cposnodes();
  4506. cmaxx = 0;
  4507. xoff = 0;
  4508. /* scan hor. to adjust the x positions. */
  4509. for (i = 0; i < (cwidestnnodes + 1); i++) {
  4510. /* x spacing between the hor. levels */
  4511. if (0) {
  4512. xoff = xoff + xspacing;
  4513. }
  4514. /* determine half-way of the xpos. */
  4515. if (cwpos[i] == 0) {
  4516. /* if only dummy nodes */
  4517. hw = xspacing / 2;
  4518. } else {
  4519. hw = (cwpos[i] / 2);
  4520. }
  4521. /* update with current x */
  4522. hw = hw + xoff;
  4523. lnl = cposnodes[i];
  4524. /* scan the nodes at this x pos. */
  4525. while (lnl) {
  4526. /* center the node around the half-way */
  4527. lnl->node->finx = (hw - (lnl->node->bbx / 2));
  4528. if ((lnl->node->finx + lnl->node->bbx) > cmaxx) {
  4529. cmaxx = (lnl->node->finx + lnl->node->bbx);
  4530. }
  4531. lnl = lnl->next;
  4532. }
  4533. /* set x0,x1 pos in the nodes */
  4534. lnl = cposnodes[i];
  4535. /* scan the nodes at this x pos. */
  4536. while (lnl) {
  4537. /* */
  4538. lnl->node->lx0 = xoff;
  4539. lnl->node->lx1 = xoff + cwpos[i];
  4540. lnl = lnl->next;
  4541. }
  4542. /* x spacing between the hor. levels */
  4543. xoff = xoff + xspacing;
  4544. /* x to next pos. */
  4545. xoff = xoff + cwpos[i];
  4546. }
  4547. /* */
  4548. clear_cposnodes();
  4549. /* y positioning */
  4550. make_clevelnodes(g);
  4551. cmaxy = 0;
  4552. yoff = 0;
  4553. /* number of edges between level n and n+1 */
  4554. g->nume = (int *) sfg_calloc(1, (g->maxlevel + 1) * sizeof(int));
  4555. /* scan vert. to adjust the y positions. */
  4556. for (i = 0; i < (g->maxlevel + 1); i++) {
  4557. /* y spacing between the vert. levels */
  4558. if (0) {
  4559. yoff = (yoff + yspacing);
  4560. }
  4561. /* determine half-way of the ypos. */
  4562. if (chpos[i] == 0) {
  4563. /* if only dummy nodes */
  4564. hw = (yspacing / 2);
  4565. } else {
  4566. hw = (chpos[i] / 2);
  4567. }
  4568. /* update with current y */
  4569. hw = hw + yoff;
  4570. lnl = clevelnodes[i];
  4571. ecount = 0;
  4572. /* scan the nodes at this y pos. */
  4573. while (lnl) {
  4574. /* set start, end of y level */
  4575. lnl->node->ly0 = yoff;
  4576. lnl->node->ly1 = (yoff + chpos[i]);
  4577. /* center the node around the half-way */
  4578. lnl->node->finy = (hw - (lnl->node->bby / 2));
  4579. /* update drawing max y pos used */
  4580. if ((lnl->node->finy + lnl->node->bby) > cmaxy) {
  4581. cmaxy = (lnl->node->finy + lnl->node->bby);
  4582. }
  4583. /* give dummy nodes a vertical size of the level */
  4584. if (lnl->node->dummy) {
  4585. lnl->node->bby = chpos[i];
  4586. /* if only dummy nodes at level, use spacing */
  4587. if (chpos[i] == 0) {
  4588. lnl->node->bby = yspacing;
  4589. }
  4590. }
  4591. /* number of edges between level n and n+1 */
  4592. ecount = ecount + lnl->node->outdegree;
  4593. lnl = lnl->next;
  4594. }
  4595. g->nume[i] = ecount;
  4596. /* y spacing between the vert. levels */
  4597. yoff = yoff + yspacing;
  4598. /* yspacing depends on number of edges at this level
  4599. * turned off, does increase y too much
  4600. * yoff = yoff + (ecount * 2);
  4601. */
  4602. /* yspacing depends on number of crossing edges at this level
  4603. * temp test
  4604. */
  4605. yoff = yoff + (1 * (g->numce[i] / 16));
  4606. /* y to next pos. */
  4607. yoff = yoff + chpos[i];
  4608. }
  4609. clear_clevelnodes(g);
  4610. /* clear number of edges between level n and n+1 */
  4611. if (g->nume) {
  4612. g->nume = (int *) sfg_free(g->nume);
  4613. }
  4614. return;
  4615. }
  4616. static void movefinal(int xoffset)
  4617. {
  4618. struct gml_nlist *gnl = NULL;
  4619. gnl = cnodelist;
  4620. while (gnl) {
  4621. gnl->node->finx = gnl->node->finx + xoffset;
  4622. gnl->node->lx0 = gnl->node->lx0 + xoffset;
  4623. gnl->node->lx1 = gnl->node->lx1 + xoffset;
  4624. gnl = gnl->next;
  4625. }
  4626. return;
  4627. }
  4628. /* dummy nodes can be centered, or left/right most placed */
  4629. static void tunedummy(struct gml_graph *g)
  4630. {
  4631. struct gml_nlist *gnl = NULL;
  4632. int x1 = 0;
  4633. int x2 = 0;
  4634. int x3 = 0;
  4635. gnl = g->nodelist;
  4636. while (gnl) {
  4637. if (gnl->node->dummy) {
  4638. x1 = gnl->node->finx;
  4639. x2 = gnl->node->incoming_e->edge->from_node->finx + gnl->node->incoming_e->edge->from_node->bbx / 2;
  4640. x3 = gnl->node->outgoing_e->edge->to_node->finx + gnl->node->outgoing_e->edge->to_node->bbx / 2;
  4641. if ((x1 == x2) && (x1 == x3)) {
  4642. /* no move */
  4643. } else {
  4644. if ((x2 < x1) && (x3 < x1)) {
  4645. /* to left */
  4646. gnl->node->finx = gnl->node->lx0;
  4647. }
  4648. if ((x2 > x1) && (x3 > x1)) {
  4649. /* to right */
  4650. gnl->node->finx = gnl->node->lx1;
  4651. }
  4652. }
  4653. }
  4654. gnl = gnl->next;
  4655. }
  4656. return;
  4657. }
  4658. /* move some nodes up/down */
  4659. static void tunenodes(struct gml_graph *g)
  4660. {
  4661. struct gml_nlist *gnl = NULL;
  4662. gnl = g->nodelist;
  4663. while (gnl) {
  4664. /* only at real nodes */
  4665. if (gnl->node->dummy == 0) {
  4666. if (gnl->node->hashedge) {
  4667. /* do not move node with hor. edge */
  4668. } else {
  4669. if (gnl->node->indegree > 0 && gnl->node->outdegree == 0) {
  4670. /* move up */
  4671. gnl->node->finy = gnl->node->ly0;
  4672. }
  4673. if (gnl->node->indegree == 0 && gnl->node->outdegree > 0) {
  4674. /* move down */
  4675. gnl->node->finy = (gnl->node->ly1 - gnl->node->bby);
  4676. }
  4677. if (gnl->node->indegree > 0 && gnl->node->outdegree > 0) {
  4678. if (gnl->node->indegree == gnl->node->outdegree) {
  4679. /* no movement
  4680. *
  4681. */
  4682. } else {
  4683. if (gnl->node->indegree > gnl->node->outdegree) {
  4684. /* move up */
  4685. gnl->node->finy = gnl->node->ly0;
  4686. }
  4687. if (gnl->node->outdegree > gnl->node->indegree) {
  4688. /* move down */
  4689. gnl->node->finy = (gnl->node->ly1 - gnl->node->bby);
  4690. }
  4691. }
  4692. }
  4693. }
  4694. }
  4695. gnl = gnl->next;
  4696. }
  4697. return;
  4698. }
  4699. /* position in parts of graph at each step */
  4700. static void improve_positions(struct gml_graph *g)
  4701. {
  4702. struct gml_nlist *gnl = NULL;
  4703. int i = 0;
  4704. int xoffset = 0;
  4705. xspacing = g->xspacing;
  4706. yspacing = g->yspacing;
  4707. /* copy the rel(x,y) pos into abs(x,y) and modify the absx pos here */
  4708. gnl = g->nodelist;
  4709. while (gnl) {
  4710. gnl->node->bbx = gnl->node->tx;
  4711. gnl->node->bby = gnl->node->ty;
  4712. gnl->node->absx = gnl->node->relx;
  4713. gnl->node->absy = gnl->node->rely;
  4714. gnl->node->finx = 0;
  4715. gnl->node->finy = 0;
  4716. gnl = gnl->next;
  4717. }
  4718. /* offset in drawing of part of graph */
  4719. xoffset = 0;
  4720. for (i = 0; i < g->nstartnodes; i++) {
  4721. /* set current startnode */
  4722. csn = g->startnodes[i];
  4723. /* print progress info */
  4724. if ((i == 0) || (i == g->nstartnodes / 2) || (i == g->nstartnodes - 1)) {
  4725. }
  4726. /* max. x in part of graph */
  4727. cmaxx = 0;
  4728. /* copy part of graph */
  4729. make_cnodelist(g);
  4730. /* create nodes-at-level-count */
  4731. make_cnnodes_at_level(g);
  4732. /* run up/down placement */
  4733. improve_positions2local(g);
  4734. /* move image of this part of graph */
  4735. move0();
  4736. /* set final x,y */
  4737. cfinalxy(g);
  4738. /* tune dummy nodes */
  4739. tunedummy(g);
  4740. /* tune nodes */
  4741. tunenodes(g);
  4742. /* move */
  4743. movefinal(xoffset);
  4744. /* update for next */
  4745. xoffset = xoffset + cmaxx + xspacing;
  4746. /* clear nodes-at-level-count */
  4747. clear_cnnodes_at_level();
  4748. /* done with this part of graph */
  4749. clear_cnodelist();
  4750. }
  4751. /* position level 0, single nodes if any */
  4752. if (g->nsinglenodes) {
  4753. /* done in finalxy() in main.c */
  4754. }
  4755. return;
  4756. }
  4757. /* for pos2.c which does set finx,finy */
  4758. static void finalxy(struct gml_graph *g)
  4759. {
  4760. struct gml_nlist *lnl = NULL;
  4761. int maxx = 0;
  4762. int maxy = 0;
  4763. int curx = 0;
  4764. int my = 0;
  4765. /* position the single nodes */
  4766. if (g->nsinglenodes) {
  4767. lnl = maingraph->singlenodelist;
  4768. while (lnl) {
  4769. lnl->node->finx = curx;
  4770. curx = curx + g->xspacing + lnl->node->bbx;
  4771. if (lnl->node->bby > my) {
  4772. my = lnl->node->bby;
  4773. }
  4774. lnl = lnl->next;
  4775. }
  4776. my = my + g->yspacing;
  4777. /* update level data for singlenodes */
  4778. lnl = maingraph->singlenodelist;
  4779. while (lnl) {
  4780. lnl->node->ly0 = 0;
  4781. lnl->node->ly1 = my;
  4782. lnl = lnl->next;
  4783. }
  4784. }
  4785. /* determine max. x pos in use */
  4786. lnl = maingraph->nodelist;
  4787. while (lnl) {
  4788. if ((lnl->node->finx + lnl->node->bbx) > maxx) {
  4789. maxx = lnl->node->finx + lnl->node->bbx;
  4790. }
  4791. /* correct for height of single nodes if any */
  4792. if (lnl->node->indegree || lnl->node->outdegree) {
  4793. lnl->node->finy = lnl->node->finy + my;
  4794. }
  4795. /* update drawing max y pos used */
  4796. if ((lnl->node->finy + lnl->node->bby) > maxy) {
  4797. maxy = (lnl->node->finy + lnl->node->bby);
  4798. }
  4799. lnl = lnl->next;
  4800. }
  4801. g->maxx = maxx;
  4802. g->maxy = maxy;
  4803. return;
  4804. }
  4805. static struct gml_edge *findedge(int num)
  4806. {
  4807. struct gml_elist *el = NULL;
  4808. struct gml_edge *e = NULL;
  4809. if (maingraph == NULL) {
  4810. return (NULL);
  4811. }
  4812. el = maingraph->edgelist;
  4813. while (el) {
  4814. e = el->edge;
  4815. if (e->nr == num) {
  4816. break;
  4817. }
  4818. el = el->next;
  4819. }
  4820. return (e);
  4821. }
  4822. /* update node min/max and edge min/max */
  4823. static void setminmax(struct gml_graph *g)
  4824. {
  4825. struct gml_nlist *nl = NULL;
  4826. struct gml_elist *el = NULL;
  4827. int count = 0;
  4828. g->nodemin = 0;
  4829. g->nodemax = 0;
  4830. g->edgemin = 0;
  4831. g->edgemax = 0;
  4832. nl = g->nodelist;
  4833. count = 0;
  4834. while (nl) {
  4835. if (count == 0) {
  4836. g->nodemin = nl->node->nr;
  4837. g->nodemax = nl->node->nr;
  4838. } else {
  4839. if (nl->node->nr < g->nodemin) {
  4840. g->nodemin = nl->node->nr;
  4841. }
  4842. if (nl->node->nr > g->nodemax) {
  4843. g->nodemax = nl->node->nr;
  4844. }
  4845. }
  4846. count++;
  4847. nl = nl->next;
  4848. }
  4849. el = g->edgelist;
  4850. count = 0;
  4851. while (el) {
  4852. if (count == 0) {
  4853. g->edgemin = el->edge->nr;
  4854. g->edgemax = el->edge->nr;
  4855. } else {
  4856. if (el->edge->nr < g->edgemin) {
  4857. g->edgemin = el->edge->nr;
  4858. }
  4859. if (el->edge->nr > g->edgemax) {
  4860. g->edgemax = el->edge->nr;
  4861. }
  4862. }
  4863. count++;
  4864. el = el->next;
  4865. }
  4866. return;
  4867. }
  4868. /* end zzzz */