graph.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (C) 2002 Roberto Micarelli
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /*
  19. * best view tabstop=4
  20. */
  21. #ifndef _DGL_dglGraph_s_H_
  22. #define _DGL_dglGraph_s_H_
  23. #ifdef DGL_STATS
  24. #include <time.h>
  25. #endif
  26. #include "heap.h"
  27. #include "tree.h"
  28. /*
  29. * Graph State bitmask - returned by dglGet_State() function
  30. */
  31. #define DGL_GS_FLAT 0x1 /* otherwise is TREE */
  32. /*
  33. * Graph Family
  34. */
  35. #define DGL_GF_COMPLETE 0x1
  36. #define DGL_GF_BIPARTITE 0x2
  37. #define DGL_GF_REGULAR 0x4
  38. #define DGL_GF_BOUQUET 0x8
  39. #define DGL_GF_DIPOLE 0x10
  40. #define DGL_GF_PATH 0x20
  41. #define DGL_GF_CYCLE 0x40
  42. /*
  43. * Graph Options
  44. */
  45. #define DGL_GO_EdgePrioritize_COST 0x10
  46. #define DGL_GO_EdgePrioritize_ATTR 0x20
  47. #define DGL_GO_NodePrioritize_ATTR 0x40
  48. /*
  49. * Node Status bitmask - returned by dglNodeGet_Status()
  50. */
  51. #define DGL_NS_HEAD 0x1 /* node exists as at least one edge's head (static) */
  52. #define DGL_NS_TAIL 0x2 /* node exists as at least one edge's tail (static) */
  53. #define DGL_NS_ALONE 0x4 /* node is a component */
  54. /*
  55. * Edge Status bitmask - returned by dglEdgeGet_Status()
  56. */
  57. #define DGL_ES_DIRECTED 0x1 /* force edge to be directed */
  58. /*
  59. * Endianess Values - returned by dglGet_Endianess() function
  60. */
  61. #define DGL_ENDIAN_BIG 1
  62. #define DGL_ENDIAN_LITTLE 2
  63. /*
  64. * miscellaneous
  65. */
  66. /* add-edge/add-node flags */
  67. #define DGL_STRONGCONNECT 0x1
  68. #define DGL_ALONE 0x2
  69. #define DGL_MERGE_EDGE 0x4
  70. /* */
  71. /*
  72. * Shortest Path clip definitions
  73. */
  74. typedef struct _dglSPClipInput
  75. {
  76. dglInt32_t *pnPrevEdge;
  77. dglInt32_t *pnNodeFrom;
  78. dglInt32_t *pnEdge;
  79. dglInt32_t *pnNodeTo;
  80. dglInt32_t nFromDistance;
  81. } dglSPClipInput_s;
  82. typedef struct _dglSPClipOutput
  83. {
  84. dglInt32_t nEdgeCost;
  85. } dglSPClipOutput_s;
  86. /*
  87. * Spanning clip definitions
  88. */
  89. typedef struct _dglSpanClipInput
  90. {
  91. dglInt32_t *pnNodeFrom;
  92. dglInt32_t *pnEdge;
  93. dglInt32_t *pnNodeTo;
  94. } dglSpanClipInput_s;
  95. typedef struct _dglSpanClipOutput
  96. {
  97. dglInt32_t *pnReserved;
  98. } dglSpanClipOutput_s;
  99. struct dglGraph;
  100. /*
  101. * Node Prioritizer
  102. */
  103. typedef struct
  104. {
  105. void *pvAVL;
  106. } dglNodePrioritizer_s;
  107. /*
  108. * Edge Prioritizer
  109. */
  110. typedef struct
  111. {
  112. int cEdge;
  113. int iEdge;
  114. dglTreeEdgePri32_s *pEdgePri32Item;
  115. void *pvAVL;
  116. } dglEdgePrioritizer_s;
  117. /*
  118. * The graph context
  119. */
  120. typedef struct _dglGraph
  121. {
  122. int iErrno;
  123. dglByte_t Version;
  124. dglByte_t Endian;
  125. dglInt32_t NodeAttrSize;
  126. dglInt32_t EdgeAttrSize;
  127. dglInt32_t aOpaqueSet[16];
  128. dglInt32_t cNode;
  129. dglInt32_t cHead;
  130. dglInt32_t cTail;
  131. dglInt32_t cAlone;
  132. dglInt32_t cEdge;
  133. dglInt64_t nnCost;
  134. dglInt32_t Flags;
  135. dglInt32_t nFamily;
  136. dglInt32_t nOptions;
  137. void *pNodeTree;
  138. void *pEdgeTree;
  139. dglByte_t *pNodeBuffer;
  140. dglInt32_t iNodeBuffer;
  141. dglByte_t *pEdgeBuffer;
  142. dglInt32_t iEdgeBuffer;
  143. dglEdgePrioritizer_s edgePrioritizer;
  144. dglNodePrioritizer_s nodePrioritizer;
  145. /* so far statistics are only computed by dglAddEdge() */
  146. #ifdef DGL_STATS
  147. clock_t clkAddEdge; /* cycles spent during the last addedge execution */
  148. int cAddEdge; /* # of calls to dglAddEdge() */
  149. clock_t clkNodeTree; /* cycles spent in accessing the node binary tree */
  150. int cNodeTree; /* # of probes in the node tree */
  151. #endif
  152. }
  153. dglGraph_s;
  154. /*
  155. * Shortest Path clip function type
  156. */
  157. typedef int (*dglSPClip_fn) (dglGraph_s *, dglSPClipInput_s *,
  158. dglSPClipOutput_s *, void *);
  159. /*
  160. * Spanning clip function type
  161. */
  162. typedef int (*dglSpanClip_fn) (dglGraph_s *, dglGraph_s *,
  163. dglSpanClipInput_s *, dglSpanClipOutput_s *,
  164. void *);
  165. /*
  166. * An ARC defined as : from-node, to-node, edge pointer, to-node-distance (from the path starting node)
  167. */
  168. typedef struct _dglSPArc
  169. {
  170. dglInt32_t nFrom;
  171. dglInt32_t nTo;
  172. dglInt32_t *pnEdge;
  173. dglInt32_t nDistance;
  174. }
  175. dglSPArc_s;
  176. /*
  177. * Shortest Path Report
  178. */
  179. typedef struct _dglSPReport
  180. {
  181. dglInt32_t nStartNode;
  182. dglInt32_t nDestinationNode;
  183. dglInt32_t nDistance;
  184. dglInt32_t cArc;
  185. dglSPArc_s *pArc;
  186. }
  187. dglSPReport_s;
  188. /*
  189. * Shortest Path Cache
  190. */
  191. typedef struct
  192. {
  193. dglInt32_t nStartNode;
  194. dglHeap_s NodeHeap;
  195. void *pvVisited;
  196. void *pvPredist;
  197. }
  198. dglSPCache_s;
  199. /*
  200. * Node Traverser
  201. */
  202. typedef struct
  203. {
  204. dglGraph_s *pGraph;
  205. void *pvAVLT;
  206. dglInt32_t *pnNode;
  207. } dglNodeTraverser_s;
  208. /*
  209. * Edgeset Traverser
  210. */
  211. typedef struct
  212. {
  213. dglGraph_s *pGraph;
  214. dglInt32_t *pnEdgeset;
  215. void *pvCurrentItem;
  216. int cEdge, iEdge;
  217. } dglEdgesetTraverser_s;
  218. /*
  219. * Edge Traverser
  220. */
  221. typedef struct
  222. {
  223. dglGraph_s *pGraph;
  224. void *pvAVLT;
  225. dglInt32_t *pnEdge;
  226. dglEdgePrioritizer_s *pEdgePrioritizer;
  227. } dglEdgeTraverser_s;
  228. /*
  229. * Error codes returned by dglError
  230. */
  231. #define DGL_ERR_BadVersion 1
  232. #define DGL_ERR_BadNodeType 2
  233. #define DGL_ERR_MemoryExhausted 3
  234. #define DGL_ERR_HeapError 4
  235. #define DGL_ERR_UndefinedMethod 5
  236. #define DGL_ERR_Write 6
  237. #define DGL_ERR_Read 7
  238. #define DGL_ERR_NotSupported 8
  239. #define DGL_ERR_UnknownByteOrder 9
  240. #define DGL_ERR_HeadNodeNotFound 10
  241. #define DGL_ERR_TailNodeNotFound 11
  242. #define DGL_ERR_BadEdge 12
  243. #define DGL_ERR_BadOnFlatGraph 13
  244. #define DGL_ERR_BadOnTreeGraph 14
  245. #define DGL_ERR_NodeNotFound 15
  246. #define DGL_ERR_TreeSearchError 16
  247. #define DGL_ERR_UnexpectedNullPointer 17
  248. #define DGL_ERR_VersionNotSupported 18
  249. #define DGL_ERR_EdgeNotFound 19
  250. #define DGL_ERR_NodeAlreadyExist 20
  251. #define DGL_ERR_NodeIsAComponent 21
  252. #define DGL_ERR_EdgeAlreadyExist 22
  253. #define DGL_ERR_BadArgument 23
  254. /*
  255. * graph context management
  256. */
  257. int dglInitialize(dglGraph_s * pGraph, dglByte_t Version,
  258. dglInt32_t NodeAttrSize, dglInt32_t EdgeAttrSize,
  259. dglInt32_t * pOpaqueSet);
  260. int dglRelease(dglGraph_s * pGraph);
  261. int dglUnflatten(dglGraph_s * pGraph);
  262. int dglFlatten(dglGraph_s * pGraph);
  263. void dglResetStats(dglGraph_s * pgraph);
  264. /*
  265. * node management
  266. */
  267. dglInt32_t *dglGetNode(dglGraph_s * pGraph, dglInt32_t nNodeId);
  268. int dglAddNode(dglGraph_s * pGraph,
  269. dglInt32_t nNodeId, void *pvNodeAttr, dglInt32_t nFlags);
  270. int dglDelNode(dglGraph_s * pGraph, dglInt32_t nNodeId);
  271. dglInt32_t dglNodeGet_Id(dglGraph_s * pGraph, dglInt32_t * pnNode);
  272. dglInt32_t *dglNodeGet_OutEdgeset(dglGraph_s * pGraph, dglInt32_t * pnNode);
  273. dglInt32_t *dglNodeGet_InEdgeset(dglGraph_s * pGraph, dglInt32_t * pnNode);
  274. dglInt32_t dglNodeGet_Status(dglGraph_s * pGraph, dglInt32_t * pnNode);
  275. dglInt32_t *dglNodeGet_Attr(dglGraph_s * pGraph, dglInt32_t * pnNode);
  276. void dglNodeSet_Attr(dglGraph_s * pGraph, dglInt32_t * pnNode,
  277. dglInt32_t * pnAttr);
  278. int dglNodeGet_InDegree(dglGraph_s * pGraph, dglInt32_t * pnNode);
  279. int dglNodeGet_OutDegree(dglGraph_s * pGraph, dglInt32_t * pnNode);
  280. int dglNodeGet_Valence(dglGraph_s * pGraph, dglInt32_t * pnNode);
  281. /*
  282. * edge management
  283. */
  284. dglInt32_t dglEdgesetGet_EdgeCount(dglGraph_s * pGraph,
  285. dglInt32_t * pnOutEdgeset);
  286. dglInt32_t dglEdgeGet_Id(dglGraph_s * pGraph, dglInt32_t * pnEdge);
  287. dglInt32_t dglEdgeGet_Cost(dglGraph_s * pGraph, dglInt32_t * pnEdge);
  288. dglInt32_t *dglEdgeGet_Head(dglGraph_s * pGraph, dglInt32_t * pnEdge);
  289. dglInt32_t *dglEdgeGet_Tail(dglGraph_s * pGraph, dglInt32_t * pnEdge);
  290. dglInt32_t *dglEdgeGet_Attr(dglGraph_s * pGraph, dglInt32_t * pnEdge);
  291. int dglEdgeSet_Attr(dglGraph_s * pGraph, dglInt32_t * pnAttr,
  292. dglInt32_t * pnEdge);
  293. dglInt32_t *dglGetEdge(dglGraph_s * pGraph, dglInt32_t nEdgeId);
  294. int dglDelEdge(dglGraph_s * pGraph, dglInt32_t nEdgeId);
  295. int dglAddEdge(dglGraph_s * pGraph,
  296. dglInt32_t nHead,
  297. dglInt32_t nTail, dglInt32_t nCost, dglInt32_t nEdge);
  298. int dglAddEdgeX(dglGraph_s * pGraph,
  299. dglInt32_t nHead,
  300. dglInt32_t nTail,
  301. dglInt32_t nCost,
  302. dglInt32_t nEdge,
  303. void *pvFnodeAttr,
  304. void *pvTnodeAttr, void *pvEdgeAttr, dglInt32_t nFlags);
  305. /*
  306. * graph I/O
  307. */
  308. int dglWrite(dglGraph_s * pGraph, int fd);
  309. int dglRead(dglGraph_s * pGraph, int fd);
  310. typedef struct
  311. {
  312. dglGraph_s *pG;
  313. int nState;
  314. int fSwap;
  315. int cb;
  316. int ib;
  317. unsigned char *pb;
  318. unsigned char ab[118]; /* 118 = graph header size */
  319. } dglIOContext_s;
  320. int dglIOContextInitialize(dglGraph_s *, dglIOContext_s *);
  321. void dglIOContextRelease(dglIOContext_s *);
  322. /*
  323. * Chunked Write callback function type
  324. */
  325. typedef int (*dglWriteChunk_fn) (dglGraph_s *, unsigned char *pbChunk,
  326. int cbChunk, void *pvArg);
  327. int dglWriteChunk(dglIOContext_s *, dglWriteChunk_fn, void *pvArg);
  328. int dglReadChunk(dglIOContext_s *, dglByte_t * pbChunk, int cbChunk);
  329. /*
  330. * Algorithms
  331. */
  332. int dglShortestPath(dglGraph_s * pGraph,
  333. dglSPReport_s ** ppReport,
  334. dglInt32_t nStartNode,
  335. dglInt32_t nDestinationNode,
  336. dglSPClip_fn fnClip,
  337. void *pvClipArg, dglSPCache_s * pCache);
  338. int dglShortestPathGraph(dglGraph_s * pGraph,
  339. dglGraph_s * pGraphOut,
  340. dglInt32_t nStartNode,
  341. dglInt32_t nDestinationNode,
  342. dglSPClip_fn fnClip,
  343. void *pvClipArg, dglSPCache_s * pCache);
  344. int dglShortestDistance(dglGraph_s * pGraph,
  345. dglInt32_t * pnDistance,
  346. dglInt32_t nStartNode,
  347. dglInt32_t nDestinationNode,
  348. dglSPClip_fn fnClip,
  349. void *pvClipArg, dglSPCache_s * pCache);
  350. int dglShortestDistanceGraph(dglGraph_s * pGraph,
  351. dglGraph_s * pGraphOut,
  352. dglInt32_t nStartNode,
  353. dglInt32_t nDestinationNode,
  354. dglSPClip_fn fnClip,
  355. void *pvClipArg, dglSPCache_s * pCache);
  356. int dglInitializeSPCache(dglGraph_s * pgraph, dglSPCache_s * pCache);
  357. void dglReleaseSPCache(dglGraph_s * pgraph, dglSPCache_s * pCache);
  358. void dglFreeSPReport(dglGraph_s * pGraph, dglSPReport_s * pSPReport);
  359. int dglDepthSpanning(dglGraph_s * pgraphInput,
  360. dglGraph_s * pgraphOutput,
  361. dglInt32_t nVertexNode,
  362. dglSpanClip_fn fnClip, void *pvClipArg);
  363. int dglDepthComponents(dglGraph_s * pgraphInput,
  364. dglGraph_s * pgraphComponents,
  365. int cgraphComponents,
  366. dglSpanClip_fn fnClip, void *pvClipArg);
  367. int dglMinimumSpanning(dglGraph_s * pgraphInput,
  368. dglGraph_s * pgraphOutput,
  369. dglInt32_t nVertexNode,
  370. dglSpanClip_fn fnClip, void *pvClipArg);
  371. /*
  372. * error management
  373. */
  374. int dglErrno(dglGraph_s * pgraph);
  375. char *dglStrerror(dglGraph_s * pgraph);
  376. /*
  377. * graph property hiders
  378. */
  379. int dglGet_Version(dglGraph_s * pGraph);
  380. void dglSet_Version(dglGraph_s * pGraph, int Version);
  381. int dglGet_Endianess(dglGraph_s * pGraph);
  382. int dglGet_NodeAttrSize(dglGraph_s * pGraph);
  383. int dglGet_EdgeAttrSize(dglGraph_s * pGraph);
  384. int dglGet_NodeCount(dglGraph_s * pGraph);
  385. int dglGet_HeadNodeCount(dglGraph_s * pGraph);
  386. int dglGet_TailNodeCount(dglGraph_s * pGraph);
  387. int dglGet_AloneNodeCount(dglGraph_s * pGraph);
  388. int dglGet_EdgeCount(dglGraph_s * pGraph);
  389. int dglGet_State(dglGraph_s * pGraph);
  390. dglInt32_t *dglGet_Opaque(dglGraph_s * pGraph);
  391. void dglSet_Opaque(dglGraph_s * pGraph, dglInt32_t * pOpaque);
  392. int dglGet_NodeSize(dglGraph_s * pGraph);
  393. int dglGet_EdgeSize(dglGraph_s * pGraph);
  394. dglInt64_t dglGet_Cost(dglGraph_s * pGraph);
  395. void dglSet_Cost(dglGraph_s * pGraph, dglInt64_t nnCost);
  396. dglInt32_t dglGet_Family(dglGraph_s * pGraph);
  397. void dglSet_Family(dglGraph_s * pGraph, dglInt32_t nFamily);
  398. dglInt32_t dglGet_Options(dglGraph_s * pGraph);
  399. void dglSet_Options(dglGraph_s * pGraph, dglInt32_t nOptions);
  400. dglEdgePrioritizer_s *dglGet_EdgePrioritizer(dglGraph_s * pGraph);
  401. dglNodePrioritizer_s *dglGet_NodePrioritizer(dglGraph_s * pGraph);
  402. /*
  403. * node traverser
  404. */
  405. int dglNode_T_Initialize(dglNodeTraverser_s * pTraverser,
  406. dglGraph_s * pGraph);
  407. void dglNode_T_Release(dglNodeTraverser_s * pTraverser);
  408. dglInt32_t *dglNode_T_First(dglNodeTraverser_s * pTraverser);
  409. dglInt32_t *dglNode_T_Last(dglNodeTraverser_s * pTraverser);
  410. dglInt32_t *dglNode_T_Next(dglNodeTraverser_s * pTraverser);
  411. dglInt32_t *dglNode_T_Prev(dglNodeTraverser_s * pTraverser);
  412. dglInt32_t *dglNode_T_Find(dglNodeTraverser_s * pTraverser,
  413. dglInt32_t nNodeId);
  414. /*
  415. * edgeset traverser
  416. */
  417. int dglEdgeset_T_Initialize(dglEdgesetTraverser_s * pTraverser,
  418. dglGraph_s * pGraph, dglInt32_t * pnEdgeset);
  419. void dglEdgeset_T_Release(dglEdgesetTraverser_s * pTraverser);
  420. dglInt32_t *dglEdgeset_T_First(dglEdgesetTraverser_s * pTraverser);
  421. dglInt32_t *dglEdgeset_T_Next(dglEdgesetTraverser_s * pTraverser);
  422. /*
  423. * edge traverser
  424. */
  425. int dglEdge_T_Initialize(dglEdgeTraverser_s * pTraverser,
  426. dglGraph_s * pGraph,
  427. dglEdgePrioritizer_s * pEdgePrioritizer);
  428. void dglEdge_T_Release(dglEdgeTraverser_s * pTraverser);
  429. dglInt32_t *dglEdge_T_First(dglEdgeTraverser_s * pTraverser);
  430. dglInt32_t *dglEdge_T_Next(dglEdgeTraverser_s * pTraverser);
  431. #endif