graph_v1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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_v1.h"
  32. #include "helpers.h"
  33. /* Template expansion
  34. */
  35. #include "v1-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 "v1-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 "v1-defs.h"
  51. #include "sp-template.c"
  52. #include "span-template.c"
  53. #undef DGL_DEFINE_FLAT_PROCS
  54. int dgl_dijkstra_V1(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_V1_FLAT(pgraph, ppReport, pDistance, nStart,
  64. nDestination, fnClip, pvClipArg, pCache);
  65. }
  66. else {
  67. return dgl_dijkstra_V1_TREE(pgraph, ppReport, pDistance, nStart,
  68. nDestination, fnClip, pvClipArg, pCache);
  69. }
  70. }
  71. int dgl_depthfirst_spanning_V1(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_V1_FLAT(pgraphIn, pgraphOut,
  79. nVertex, pvVisited,
  80. fnClip, pvClipArg);
  81. }
  82. else {
  83. return dgl_span_depthfirst_spanning_V1_TREE(pgraphIn, pgraphOut,
  84. nVertex, pvVisited,
  85. fnClip, pvClipArg);
  86. }
  87. }
  88. int dgl_minimum_spanning_V1(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_V1_FLAT(pgraphIn, pgraphOut, nVertex,
  95. fnClip, pvClipArg);
  96. }
  97. else {
  98. return dgl_span_minimum_spanning_V1_TREE(pgraphIn, pgraphOut, nVertex,
  99. fnClip, pvClipArg);
  100. }
  101. }
  102. int dgl_initialize_V1(dglGraph_s * pgraph)
  103. {
  104. if (pgraph->pNodeTree == NULL)
  105. pgraph->pNodeTree =
  106. avl_create(dglTreeNodeCompare, NULL, dglTreeGetAllocator());
  107. if (pgraph->pNodeTree == NULL) {
  108. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  109. return -pgraph->iErrno;
  110. }
  111. pgraph->pEdgeTree = NULL;
  112. return 0;
  113. }
  114. int dgl_release_V1(dglGraph_s * pgraph)
  115. {
  116. pgraph->iErrno = 0;
  117. if (pgraph->pNodeTree)
  118. avl_destroy(pgraph->pNodeTree, dglTreeNodeCancel);
  119. if (pgraph->pEdgeTree)
  120. avl_destroy(pgraph->pEdgeTree, dglTreeEdgeCancel);
  121. if (pgraph->pNodeBuffer)
  122. free(pgraph->pNodeBuffer);
  123. if (pgraph->pEdgeBuffer)
  124. free(pgraph->pEdgeBuffer);
  125. if (pgraph->edgePrioritizer.pvAVL)
  126. avl_destroy(pgraph->edgePrioritizer.pvAVL, dglTreeEdgePri32Cancel);
  127. if (pgraph->nodePrioritizer.pvAVL)
  128. avl_destroy(pgraph->nodePrioritizer.pvAVL, dglTreeNodePri32Cancel);
  129. return 0;
  130. }
  131. int dgl_write_V1(dglGraph_s * pgraph, int fd)
  132. {
  133. long nret, cnt, tot;
  134. pgraph->iErrno = 0;
  135. if (write(fd, &pgraph->Version, 1) != 1) {
  136. pgraph->iErrno = DGL_ERR_Write;
  137. return -pgraph->iErrno;
  138. }
  139. if (write(fd, &pgraph->Endian, 1) != 1) {
  140. pgraph->iErrno = DGL_ERR_Write;
  141. return -pgraph->iErrno;
  142. }
  143. if (write(fd, &pgraph->NodeAttrSize, sizeof(dglInt32_t)) !=
  144. sizeof(dglInt32_t)) {
  145. pgraph->iErrno = DGL_ERR_Write;
  146. return -pgraph->iErrno;
  147. }
  148. if (write(fd, &pgraph->EdgeAttrSize, sizeof(dglInt32_t)) !=
  149. sizeof(dglInt32_t)) {
  150. pgraph->iErrno = DGL_ERR_Write;
  151. return -pgraph->iErrno;
  152. }
  153. for (cnt = 0; cnt < 16; cnt++) {
  154. if (write(fd, &pgraph->aOpaqueSet[cnt], sizeof(dglInt32_t)) !=
  155. sizeof(dglInt32_t)) {
  156. pgraph->iErrno = DGL_ERR_Write;
  157. return -pgraph->iErrno;
  158. }
  159. }
  160. if (write(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
  161. pgraph->iErrno = DGL_ERR_Write;
  162. return -pgraph->iErrno;
  163. }
  164. if (write(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  165. pgraph->iErrno = DGL_ERR_Write;
  166. return -pgraph->iErrno;
  167. }
  168. if (write(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  169. pgraph->iErrno = DGL_ERR_Write;
  170. return -pgraph->iErrno;
  171. }
  172. if (write(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  173. pgraph->iErrno = DGL_ERR_Write;
  174. return -pgraph->iErrno;
  175. }
  176. if (write(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  177. pgraph->iErrno = DGL_ERR_Write;
  178. return -pgraph->iErrno;
  179. }
  180. if (write(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  181. pgraph->iErrno = DGL_ERR_Write;
  182. return -pgraph->iErrno;
  183. }
  184. if (write(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
  185. sizeof(dglInt32_t)) {
  186. pgraph->iErrno = DGL_ERR_Write;
  187. return -pgraph->iErrno;
  188. }
  189. if (write(fd, &pgraph->iEdgeBuffer, sizeof(dglInt32_t)) !=
  190. sizeof(dglInt32_t)) {
  191. pgraph->iErrno = DGL_ERR_Write;
  192. return -pgraph->iErrno;
  193. }
  194. for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
  195. if ((nret = write(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
  196. pgraph->iErrno = DGL_ERR_Write;
  197. return -pgraph->iErrno;
  198. }
  199. }
  200. for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
  201. if ((nret = write(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
  202. pgraph->iErrno = DGL_ERR_Write;
  203. return -pgraph->iErrno;
  204. }
  205. }
  206. return 0;
  207. }
  208. int dgl_read_V1(dglGraph_s * pgraph, int fd)
  209. {
  210. long nret, cnt, tot;
  211. dglByte_t Endian;
  212. dglInt32_t NodeAttrSize, EdgeAttrSize;
  213. int i, cn, fSwap;
  214. dglInt32_t *pn;
  215. if (read(fd, &Endian, 1) != 1) {
  216. pgraph->iErrno = DGL_ERR_Read;
  217. return -pgraph->iErrno;
  218. }
  219. fSwap = 0;
  220. #ifdef DGL_ENDIAN_BIG
  221. if (Endian == DGL_ENDIAN_LITTLE)
  222. fSwap = 1;
  223. #else
  224. if (Endian == DGL_ENDIAN_BIG)
  225. fSwap = 1;
  226. #endif
  227. if (read(fd, &NodeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  228. pgraph->iErrno = DGL_ERR_Read;
  229. return -pgraph->iErrno;
  230. }
  231. if (fSwap)
  232. dgl_swapInt32Bytes(&NodeAttrSize);
  233. if (read(fd, &EdgeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  234. pgraph->iErrno = DGL_ERR_Read;
  235. return -pgraph->iErrno;
  236. }
  237. if (fSwap)
  238. dgl_swapInt32Bytes(&EdgeAttrSize);
  239. if ((nret =
  240. dglInitialize(pgraph, 1, NodeAttrSize, EdgeAttrSize, NULL)) < 0) {
  241. return nret;
  242. }
  243. for (cnt = 0; cnt < 16; cnt++) {
  244. if ((nret =
  245. read(fd, &pgraph->aOpaqueSet[cnt],
  246. sizeof(dglInt32_t))) != sizeof(dglInt32_t)) {
  247. pgraph->iErrno = DGL_ERR_Read;
  248. return -pgraph->iErrno;
  249. }
  250. if (fSwap)
  251. dgl_swapInt32Bytes(&pgraph->aOpaqueSet[cnt]);
  252. }
  253. if (read(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
  254. pgraph->iErrno = DGL_ERR_Read;
  255. return -pgraph->iErrno;
  256. }
  257. if (fSwap)
  258. dgl_swapInt64Bytes(&pgraph->nnCost);
  259. if (read(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  260. pgraph->iErrno = DGL_ERR_Read;
  261. return -pgraph->iErrno;
  262. }
  263. if (fSwap)
  264. dgl_swapInt32Bytes(&pgraph->cNode);
  265. if (read(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  266. pgraph->iErrno = DGL_ERR_Read;
  267. return -pgraph->iErrno;
  268. }
  269. if (fSwap)
  270. dgl_swapInt32Bytes(&pgraph->cHead);
  271. if (read(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  272. pgraph->iErrno = DGL_ERR_Read;
  273. return -pgraph->iErrno;
  274. }
  275. if (fSwap)
  276. dgl_swapInt32Bytes(&pgraph->cTail);
  277. if (read(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  278. pgraph->iErrno = DGL_ERR_Read;
  279. return -pgraph->iErrno;
  280. }
  281. if (fSwap)
  282. dgl_swapInt32Bytes(&pgraph->cAlone);
  283. if (read(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
  284. pgraph->iErrno = DGL_ERR_Read;
  285. return -pgraph->iErrno;
  286. }
  287. if (fSwap)
  288. dgl_swapInt32Bytes(&pgraph->cEdge);
  289. if (read(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
  290. sizeof(dglInt32_t)) {
  291. pgraph->iErrno = DGL_ERR_Read;
  292. return -pgraph->iErrno;
  293. }
  294. if (fSwap)
  295. dgl_swapInt32Bytes(&pgraph->iNodeBuffer);
  296. if (read(fd, &pgraph->iEdgeBuffer, 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->iEdgeBuffer);
  303. if ((pgraph->pNodeBuffer = malloc(pgraph->iNodeBuffer)) == NULL) {
  304. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  305. return -pgraph->iErrno;
  306. }
  307. if ((pgraph->pEdgeBuffer = malloc(pgraph->iEdgeBuffer)) == NULL) {
  308. free(pgraph->pNodeBuffer);
  309. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  310. return -pgraph->iErrno;
  311. }
  312. for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
  313. if ((nret = read(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
  314. free(pgraph->pNodeBuffer);
  315. free(pgraph->pEdgeBuffer);
  316. pgraph->iErrno = DGL_ERR_Read;
  317. return -pgraph->iErrno;
  318. }
  319. }
  320. if (fSwap) {
  321. pn = (dglInt32_t *) pgraph->pNodeBuffer;
  322. cn = pgraph->iNodeBuffer / sizeof(dglInt32_t);
  323. for (i = 0; i < cn; i++) {
  324. dgl_swapInt32Bytes(&pn[i]);
  325. }
  326. }
  327. for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
  328. if ((nret = read(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
  329. free(pgraph->pNodeBuffer);
  330. free(pgraph->pEdgeBuffer);
  331. pgraph->iErrno = DGL_ERR_Read;
  332. return -pgraph->iErrno;
  333. }
  334. }
  335. if (fSwap) {
  336. pn = (dglInt32_t *) pgraph->pEdgeBuffer;
  337. cn = pgraph->iEdgeBuffer / sizeof(dglInt32_t);
  338. for (i = 0; i < cn; i++) {
  339. dgl_swapInt32Bytes(&pn[i]);
  340. }
  341. }
  342. pgraph->Flags |= 0x1; /* flat-state */
  343. return 0;
  344. }