graph_v2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* LIBDGL -- a Directed Graph Library implementation
  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 with tabstop=4
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include "type.h"
  29. #include "tree.h"
  30. #include "graph.h"
  31. #include "graph_v2.h"
  32. #include "helpers.h"
  33. /* Template expansion
  34. */
  35. #include "v2-defs.h"
  36. #include "sp-template.c"
  37. #include "nodemgmt-template.c"
  38. #include "edgemgmt-template.c"
  39. #include "misc-template.c"
  40. /* algorithms for TREE state
  41. */
  42. #define DGL_DEFINE_TREE_PROCS 1
  43. #include "v2-defs.h"
  44. #include "sp-template.c"
  45. #include "span-template.c"
  46. #undef DGL_DEFINE_TREE_PROCS
  47. /* algorithms for FLAT state
  48. */
  49. #define DGL_DEFINE_FLAT_PROCS 1
  50. #include "v2-defs.h"
  51. #include "sp-template.c"
  52. #include "span-template.c"
  53. #undef DGL_DEFINE_FLAT_PROCS
  54. int dgl_dijkstra_V2(dglGraph_s * pgraph,
  55. dglSPReport_s ** ppReport,
  56. dglInt32_t * pDistance,
  57. dglInt32_t nStart,
  58. dglInt32_t nDestination,
  59. dglSPClip_fn fnClip,
  60. void *pvClipArg, dglSPCache_s * pCache)
  61. {
  62. if (pgraph->Flags & DGL_GS_FLAT) {
  63. return dgl_dijkstra_V2_FLAT(pgraph, ppReport, pDistance, nStart,
  64. nDestination, fnClip, pvClipArg, pCache);
  65. }
  66. else {
  67. return dgl_dijkstra_V2_TREE(pgraph, ppReport, pDistance, nStart,
  68. nDestination, fnClip, pvClipArg, pCache);
  69. }
  70. }
  71. int dgl_depthfirst_spanning_V2(dglGraph_s * pgraphIn,
  72. dglGraph_s * pgraphOut,
  73. dglInt32_t nVertex,
  74. void *pvVisited,
  75. dglSpanClip_fn fnClip, void *pvClipArg)
  76. {
  77. if (pgraphIn->Flags & DGL_GS_FLAT) {
  78. return dgl_span_depthfirst_spanning_V2_FLAT(pgraphIn, pgraphOut,
  79. nVertex, pvVisited,
  80. fnClip, pvClipArg);
  81. }
  82. else {
  83. return dgl_span_depthfirst_spanning_V2_TREE(pgraphIn, pgraphOut,
  84. nVertex, pvVisited,
  85. fnClip, pvClipArg);
  86. }
  87. }
  88. int dgl_minimum_spanning_V2(dglGraph_s * pgraphIn,
  89. dglGraph_s * pgraphOut,
  90. dglInt32_t nVertex,
  91. dglSpanClip_fn fnClip, void *pvClipArg)
  92. {
  93. if (pgraphIn->Flags & DGL_GS_FLAT) {
  94. return dgl_span_minimum_spanning_V2_FLAT(pgraphIn, pgraphOut, nVertex,
  95. fnClip, pvClipArg);
  96. }
  97. else {
  98. return dgl_span_minimum_spanning_V2_TREE(pgraphIn, pgraphOut, nVertex,
  99. fnClip, pvClipArg);
  100. }
  101. }
  102. int dgl_initialize_V2(dglGraph_s * pgraph)
  103. {
  104. if (pgraph->pNodeTree == NULL)
  105. pgraph->pNodeTree =
  106. avl_create(dglTreeNode2Compare, NULL, dglTreeGetAllocator());
  107. if (pgraph->pNodeTree == NULL) {
  108. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  109. return -pgraph->iErrno;
  110. }
  111. if (pgraph->pEdgeTree == NULL)
  112. pgraph->pEdgeTree =
  113. avl_create(dglTreeEdgeCompare, NULL, dglTreeGetAllocator());
  114. if (pgraph->pEdgeTree == NULL) {
  115. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  116. return -pgraph->iErrno;
  117. }
  118. return 0;
  119. }
  120. int dgl_release_V2(dglGraph_s * pgraph)
  121. {
  122. pgraph->iErrno = 0;
  123. if (pgraph->pNodeTree)
  124. avl_destroy(pgraph->pNodeTree, dglTreeNodeCancel);
  125. if (pgraph->pEdgeTree)
  126. avl_destroy(pgraph->pEdgeTree, dglTreeEdgeCancel);
  127. if (pgraph->pNodeBuffer)
  128. free(pgraph->pNodeBuffer);
  129. if (pgraph->pEdgeBuffer)
  130. free(pgraph->pEdgeBuffer);
  131. if (pgraph->edgePrioritizer.pvAVL)
  132. avl_destroy(pgraph->edgePrioritizer.pvAVL, dglTreeEdgePri32Cancel);
  133. if (pgraph->nodePrioritizer.pvAVL)
  134. avl_destroy(pgraph->nodePrioritizer.pvAVL, dglTreeNodePri32Cancel);
  135. return 0;
  136. }
  137. int dgl_write_V2(dglGraph_s * pgraph, int fd)
  138. {
  139. long nret, cnt, tot;
  140. pgraph->iErrno = 0;
  141. if (write(fd, &pgraph->Version, 1) != 1) {
  142. pgraph->iErrno = DGL_ERR_Write;
  143. return -pgraph->iErrno;
  144. }
  145. if (write(fd, &pgraph->Endian, 1) != 1) {
  146. pgraph->iErrno = DGL_ERR_Write;
  147. return -pgraph->iErrno;
  148. }
  149. if (write(fd, &pgraph->NodeAttrSize, sizeof(dglInt32_t)) !=
  150. sizeof(dglInt32_t)) {
  151. pgraph->iErrno = DGL_ERR_Write;
  152. return -pgraph->iErrno;
  153. }
  154. if (write(fd, &pgraph->EdgeAttrSize, sizeof(dglInt32_t)) !=
  155. sizeof(dglInt32_t)) {
  156. pgraph->iErrno = DGL_ERR_Write;
  157. return -pgraph->iErrno;
  158. }
  159. for (cnt = 0; cnt < 16; cnt++) {
  160. if (write(fd, &pgraph->aOpaqueSet[cnt], sizeof(dglInt32_t)) !=
  161. sizeof(dglInt32_t)) {
  162. pgraph->iErrno = DGL_ERR_Write;
  163. return -pgraph->iErrno;
  164. }
  165. }
  166. if (write(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
  167. pgraph->iErrno = DGL_ERR_Write;
  168. return -pgraph->iErrno;
  169. }
  170. if (write(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  171. pgraph->iErrno = DGL_ERR_Write;
  172. return -pgraph->iErrno;
  173. }
  174. if (write(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  175. pgraph->iErrno = DGL_ERR_Write;
  176. return -pgraph->iErrno;
  177. }
  178. if (write(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  179. pgraph->iErrno = DGL_ERR_Write;
  180. return -pgraph->iErrno;
  181. }
  182. if (write(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  183. pgraph->iErrno = DGL_ERR_Write;
  184. return -pgraph->iErrno;
  185. }
  186. if (write(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  187. pgraph->iErrno = DGL_ERR_Write;
  188. return -pgraph->iErrno;
  189. }
  190. if (write(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
  191. sizeof(dglInt32_t)) {
  192. pgraph->iErrno = DGL_ERR_Write;
  193. return -pgraph->iErrno;
  194. }
  195. if (write(fd, &pgraph->iEdgeBuffer, sizeof(dglInt32_t)) !=
  196. sizeof(dglInt32_t)) {
  197. pgraph->iErrno = DGL_ERR_Write;
  198. return -pgraph->iErrno;
  199. }
  200. for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
  201. if ((nret = write(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
  202. pgraph->iErrno = DGL_ERR_Write;
  203. return -pgraph->iErrno;
  204. }
  205. }
  206. for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
  207. if ((nret = write(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
  208. pgraph->iErrno = DGL_ERR_Write;
  209. return -pgraph->iErrno;
  210. }
  211. }
  212. return 0;
  213. }
  214. int dgl_read_V2(dglGraph_s * pgraph, int fd, int version)
  215. {
  216. long nret, cnt, tot;
  217. dglByte_t Endian;
  218. dglInt32_t NodeAttrSize, EdgeAttrSize;
  219. int i, cn, fSwap;
  220. dglInt32_t *pn;
  221. if (read(fd, &Endian, 1) != 1) {
  222. pgraph->iErrno = DGL_ERR_Read;
  223. return -pgraph->iErrno;
  224. }
  225. fSwap = 0;
  226. #ifdef DGL_ENDIAN_BIG
  227. if (Endian == DGL_ENDIAN_LITTLE)
  228. fSwap = 1;
  229. #else
  230. if (Endian == DGL_ENDIAN_BIG)
  231. fSwap = 1;
  232. #endif
  233. if (read(fd, &NodeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  234. pgraph->iErrno = DGL_ERR_Read;
  235. return -pgraph->iErrno;
  236. }
  237. if (fSwap)
  238. dgl_swapInt32Bytes(&NodeAttrSize);
  239. if (read(fd, &EdgeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  240. pgraph->iErrno = DGL_ERR_Read;
  241. return -pgraph->iErrno;
  242. }
  243. if (fSwap)
  244. dgl_swapInt32Bytes(&EdgeAttrSize);
  245. if ((nret =
  246. dglInitialize(pgraph, version, NodeAttrSize, EdgeAttrSize,
  247. NULL)) < 0) {
  248. return nret;
  249. }
  250. for (cnt = 0; cnt < 16; cnt++) {
  251. if ((nret =
  252. read(fd, &pgraph->aOpaqueSet[cnt],
  253. sizeof(dglInt32_t))) != sizeof(dglInt32_t)) {
  254. pgraph->iErrno = DGL_ERR_Read;
  255. return -pgraph->iErrno;
  256. }
  257. if (fSwap)
  258. dgl_swapInt32Bytes(&pgraph->aOpaqueSet[cnt]);
  259. }
  260. if (read(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
  261. pgraph->iErrno = DGL_ERR_Read;
  262. return -pgraph->iErrno;
  263. }
  264. if (fSwap)
  265. dgl_swapInt64Bytes(&pgraph->nnCost);
  266. if (read(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  267. pgraph->iErrno = DGL_ERR_Read;
  268. return -pgraph->iErrno;
  269. }
  270. if (fSwap)
  271. dgl_swapInt32Bytes(&pgraph->cNode);
  272. if (read(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  273. pgraph->iErrno = DGL_ERR_Read;
  274. return -pgraph->iErrno;
  275. }
  276. if (fSwap)
  277. dgl_swapInt32Bytes(&pgraph->cHead);
  278. if (read(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  279. pgraph->iErrno = DGL_ERR_Read;
  280. return -pgraph->iErrno;
  281. }
  282. if (fSwap)
  283. dgl_swapInt32Bytes(&pgraph->cTail);
  284. if (read(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  285. pgraph->iErrno = DGL_ERR_Read;
  286. return -pgraph->iErrno;
  287. }
  288. if (fSwap)
  289. dgl_swapInt32Bytes(&pgraph->cAlone);
  290. if (read(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  291. pgraph->iErrno = DGL_ERR_Read;
  292. return -pgraph->iErrno;
  293. }
  294. if (fSwap)
  295. dgl_swapInt32Bytes(&pgraph->cEdge);
  296. if (read(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
  297. sizeof(dglInt32_t)) {
  298. pgraph->iErrno = DGL_ERR_Read;
  299. return -pgraph->iErrno;
  300. }
  301. if (fSwap)
  302. dgl_swapInt32Bytes(&pgraph->iNodeBuffer);
  303. if (read(fd, &pgraph->iEdgeBuffer, sizeof(dglInt32_t)) !=
  304. sizeof(dglInt32_t)) {
  305. pgraph->iErrno = DGL_ERR_Read;
  306. return -pgraph->iErrno;
  307. }
  308. if (fSwap)
  309. dgl_swapInt32Bytes(&pgraph->iEdgeBuffer);
  310. if ((pgraph->pNodeBuffer = malloc(pgraph->iNodeBuffer)) == NULL) {
  311. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  312. return -pgraph->iErrno;
  313. }
  314. if ((pgraph->pEdgeBuffer = malloc(pgraph->iEdgeBuffer)) == NULL) {
  315. free(pgraph->pNodeBuffer);
  316. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  317. return -pgraph->iErrno;
  318. }
  319. for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
  320. if ((nret = read(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
  321. free(pgraph->pNodeBuffer);
  322. free(pgraph->pEdgeBuffer);
  323. pgraph->iErrno = DGL_ERR_Read;
  324. return -pgraph->iErrno;
  325. }
  326. }
  327. if (fSwap) {
  328. pn = (dglInt32_t *) pgraph->pNodeBuffer;
  329. cn = pgraph->iNodeBuffer / sizeof(dglInt32_t);
  330. for (i = 0; i < cn; i++) {
  331. dgl_swapInt32Bytes(&pn[i]);
  332. }
  333. }
  334. for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
  335. if ((nret = read(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
  336. free(pgraph->pNodeBuffer);
  337. free(pgraph->pEdgeBuffer);
  338. pgraph->iErrno = DGL_ERR_Read;
  339. return -pgraph->iErrno;
  340. }
  341. }
  342. if (fSwap) {
  343. pn = (dglInt32_t *) pgraph->pEdgeBuffer;
  344. cn = pgraph->iEdgeBuffer / sizeof(dglInt32_t);
  345. for (i = 0; i < cn; i++) {
  346. dgl_swapInt32Bytes(&pn[i]);
  347. }
  348. }
  349. pgraph->Flags |= 0x1; /* flat-state */
  350. return 0;
  351. }