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