sfg.c 126 KB

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