edgemgmt-template.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. /*
  22. * Add edge can be performed on TREE state graph. If the state is FLAT
  23. * return BadOnFlatGraph error.
  24. */
  25. int DGL_ADD_EDGE_FUNC(dglGraph_s * pgraph,
  26. dglInt32_t nHead,
  27. dglInt32_t nTail,
  28. dglInt32_t nCost,
  29. dglInt32_t nEdge,
  30. void *pvHeadAttr,
  31. void *pvTailAttr, void *pvEdgeAttr, dglInt32_t nFlags)
  32. {
  33. dglInt32_t *pHead;
  34. dglInt32_t *pTail;
  35. dglInt32_t *pEdgeset;
  36. dglInt32_t *pEdge;
  37. DGL_T_NODEITEM_TYPE *pHeadNodeItem;
  38. DGL_T_NODEITEM_TYPE *pTailNodeItem;
  39. #if defined(_DGL_V2)
  40. dglInt32_t *pinEdgeset;
  41. dglTreeEdge_s *pEdgeItem;
  42. dglTreeEdge_s findEdge;
  43. #endif
  44. if (pgraph->Flags & DGL_GS_FLAT) {
  45. pgraph->iErrno = DGL_ERR_BadOnFlatGraph;
  46. return -pgraph->iErrno;
  47. }
  48. #ifdef DGL_STATS
  49. {
  50. clock_t clk = clock();
  51. #endif
  52. if ((pHeadNodeItem =
  53. DGL_T_NODEITEM_Add(pgraph->pNodeTree, nHead)) == NULL ||
  54. (pTailNodeItem =
  55. DGL_T_NODEITEM_Add(pgraph->pNodeTree, nTail)) == NULL) {
  56. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  57. return -pgraph->iErrno;
  58. }
  59. #ifdef DGL_STATS
  60. pgraph->clkNodeTree += clock() - clk;
  61. pgraph->cNodeTree++;
  62. pgraph->cNodeTree++;
  63. }
  64. #endif
  65. if (DGL_T_NODEITEM_NodePTR(pHeadNodeItem) == NULL) {
  66. if ((pHead = DGL_NODE_ALLOC(pgraph->NodeAttrSize)) == NULL) {
  67. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  68. return -1;
  69. }
  70. DGL_NODE_STATUS(pHead) = 0;
  71. DGL_T_NODEITEM_Set_NodePTR(pHeadNodeItem, pHead);
  72. pgraph->cNode++;
  73. pgraph->cHead++;
  74. }
  75. else {
  76. pHead = DGL_T_NODEITEM_NodePTR(pHeadNodeItem);
  77. if (!(DGL_NODE_STATUS(pHead) & DGL_NS_HEAD))
  78. pgraph->cHead++;
  79. }
  80. if (DGL_T_NODEITEM_NodePTR(pTailNodeItem) == NULL) {
  81. if ((pTail = DGL_NODE_ALLOC(pgraph->NodeAttrSize)) == NULL) {
  82. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  83. return -pgraph->iErrno;
  84. }
  85. DGL_NODE_STATUS(pTail) = 0;
  86. DGL_T_NODEITEM_Set_NodePTR(pTailNodeItem, pTail);
  87. pgraph->cNode++;
  88. pgraph->cTail++;
  89. }
  90. else {
  91. pTail = DGL_T_NODEITEM_NodePTR(pTailNodeItem);
  92. if (!(DGL_NODE_STATUS(pTail) & DGL_NS_TAIL))
  93. pgraph->cTail++;
  94. }
  95. DGL_NODE_STATUS(pHead) |= DGL_NS_HEAD;
  96. DGL_NODE_STATUS(pTail) |= DGL_NS_TAIL;
  97. if (DGL_NODE_STATUS(pHead) & DGL_NS_ALONE) {
  98. DGL_NODE_STATUS(pHead) &= ~DGL_NS_ALONE;
  99. pgraph->cAlone--;
  100. }
  101. if (DGL_NODE_STATUS(pTail) & DGL_NS_ALONE) {
  102. DGL_NODE_STATUS(pTail) &= ~DGL_NS_ALONE;
  103. pgraph->cAlone--;
  104. }
  105. DGL_NODE_ID(pHead) = nHead;
  106. DGL_NODE_ID(pTail) = nTail;
  107. DGL_NODE_EDGESET_OFFSET(pHead) = -1;
  108. DGL_NODE_EDGESET_OFFSET(pTail) = -1;
  109. if (pvHeadAttr && pgraph->NodeAttrSize) {
  110. memcpy(DGL_NODE_ATTR_PTR(pHead), pvHeadAttr, pgraph->NodeAttrSize);
  111. }
  112. if (pvTailAttr && pgraph->NodeAttrSize) {
  113. memcpy(DGL_NODE_ATTR_PTR(pTail), pvTailAttr, pgraph->NodeAttrSize);
  114. }
  115. /*
  116. if ( DGL_T_NODEITEM_OutEdgesetPTR(pTailNodeItem) == NULL )
  117. {
  118. pEdgeset = DGL_EDGESET_ALLOC( 0 , pgraph->EdgeAttrSize );
  119. if ( pEdgeset == NULL ) {
  120. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  121. return -pgraph->iErrno;
  122. }
  123. DGL_EDGESET_EDGECOUNT(pEdgeset) = 0;
  124. DGL_T_NODEITEM_Set_OutEdgesetPTR(pTailNodeItem,pEdgeset);
  125. }
  126. */
  127. if ((pEdgeset = DGL_T_NODEITEM_OutEdgesetPTR(pHeadNodeItem)) == NULL) {
  128. pEdgeset = DGL_EDGESET_ALLOC(1, pgraph->EdgeAttrSize);
  129. if (pEdgeset == NULL) {
  130. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  131. return -pgraph->iErrno;
  132. }
  133. DGL_EDGESET_EDGECOUNT(pEdgeset) = 0;
  134. DGL_T_NODEITEM_Set_OutEdgesetPTR(pHeadNodeItem, pEdgeset);
  135. }
  136. else {
  137. pEdgeset = DGL_EDGESET_REALLOC(pEdgeset,
  138. DGL_EDGESET_EDGECOUNT(pEdgeset) + 1,
  139. pgraph->EdgeAttrSize);
  140. if (pEdgeset == NULL) {
  141. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  142. return -pgraph->iErrno;
  143. }
  144. DGL_T_NODEITEM_Set_OutEdgesetPTR(pHeadNodeItem, pEdgeset);
  145. }
  146. #if defined(_DGL_V2)
  147. /*
  148. if ( DGL_T_NODEITEM_InEdgesetPTR(pHeadNodeItem) == NULL )
  149. {
  150. pinEdgeset = DGL_EDGESET_ALLOC( 0 , pgraph->EdgeAttrSize );
  151. if ( pinEdgeset == NULL ) {
  152. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  153. return -pgraph->iErrno;
  154. }
  155. DGL_EDGESET_EDGECOUNT(pinEdgeset) = 0;
  156. DGL_T_NODEITEM_Set_InEdgesetPTR(pHeadNodeItem,pinEdgeset);
  157. }
  158. */
  159. if ((pinEdgeset = DGL_T_NODEITEM_InEdgesetPTR(pTailNodeItem)) == NULL) {
  160. pinEdgeset = DGL_EDGESET_ALLOC(1, pgraph->EdgeAttrSize);
  161. if (pinEdgeset == NULL) {
  162. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  163. return -pgraph->iErrno;
  164. }
  165. DGL_EDGESET_EDGECOUNT(pinEdgeset) = 0;
  166. DGL_T_NODEITEM_Set_InEdgesetPTR(pTailNodeItem, pinEdgeset);
  167. }
  168. else {
  169. pinEdgeset = DGL_EDGESET_REALLOC(pinEdgeset,
  170. DGL_EDGESET_EDGECOUNT(pinEdgeset) +
  171. 1, pgraph->EdgeAttrSize);
  172. if (pinEdgeset == NULL) {
  173. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  174. return -pgraph->iErrno;
  175. }
  176. DGL_T_NODEITEM_Set_InEdgesetPTR(pTailNodeItem, pinEdgeset);
  177. }
  178. /*
  179. * Set the edge-tree
  180. */
  181. findEdge.nKey = nEdge;
  182. if ((pEdgeItem = dglTreeEdgeAdd(pgraph->pEdgeTree, nEdge)) == NULL) {
  183. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  184. return -pgraph->iErrno;
  185. }
  186. if (pEdgeItem->pv) {
  187. pgraph->iErrno = DGL_ERR_EdgeAlreadyExist;
  188. return -pgraph->iErrno;
  189. }
  190. if ((pEdgeItem->pv = DGL_EDGE_ALLOC(pgraph->EdgeAttrSize)) == NULL) {
  191. pgraph->iErrno = DGL_ERR_MemoryExhausted;
  192. return -pgraph->iErrno;
  193. }
  194. /*
  195. * assign edge id
  196. */
  197. pEdgeset[DGL_EDGESET_EDGECOUNT(pEdgeset) + 1] = nEdge;
  198. pinEdgeset[DGL_EDGESET_EDGECOUNT(pinEdgeset) + 1] = nEdge;
  199. ++DGL_EDGESET_EDGECOUNT(pEdgeset);
  200. ++DGL_EDGESET_EDGECOUNT(pinEdgeset);
  201. /*
  202. printf( "add edge: node %ld(%ld,%ld) -> %ld(%ld,%ld)\n",
  203. DGL_NODE_ID(pHead), DGL_EDGESET_EDGECOUNT(pEdgeset),0,
  204. DGL_NODE_ID(pTail), 0,DGL_EDGESET_EDGECOUNT(pinEdgeset));
  205. */
  206. pEdge = pEdgeItem->pv;
  207. #endif
  208. #if defined(_DGL_V1)
  209. pEdge =
  210. DGL_EDGESET_EDGE_PTR(pEdgeset, DGL_EDGESET_EDGECOUNT(pEdgeset),
  211. pgraph->EdgeAttrSize);
  212. DGL_EDGESET_EDGECOUNT(pEdgeset)++;
  213. #endif
  214. DGL_EDGE_HEADNODE_OFFSET(pEdge) = nHead; /* will be an offset after flattening */
  215. DGL_EDGE_TAILNODE_OFFSET(pEdge) = nTail; /* will be an offset after flattening */
  216. DGL_EDGE_COST(pEdge) = nCost;
  217. DGL_EDGE_ID(pEdge) = nEdge;
  218. #if !defined(_DGL_V1)
  219. if (nFlags & DGL_ES_DIRECTED)
  220. DGL_EDGE_STATUS(pEdge) = DGL_ES_DIRECTED;
  221. else
  222. DGL_EDGE_STATUS(pEdge) = 0;
  223. #endif
  224. pgraph->cEdge++;
  225. pgraph->nnCost += (dglInt64_t) nCost;
  226. if (pvEdgeAttr && pgraph->EdgeAttrSize) {
  227. memcpy(DGL_EDGE_ATTR_PTR(pEdge), pvEdgeAttr, pgraph->EdgeAttrSize);
  228. }
  229. /*
  230. * If requested add a cost-weighted entry into the edge prioritizer
  231. */
  232. #if !defined(_DGL_V1)
  233. if (pgraph->nOptions & DGL_GO_EdgePrioritize_COST) {
  234. if (dgl_edge_prioritizer_add
  235. (pgraph, DGL_EDGE_ID(pEdge), DGL_EDGE_COST(pEdge)) < 0) {
  236. return -pgraph->iErrno;
  237. }
  238. }
  239. #endif
  240. if (nFlags & DGL_STRONGCONNECT) {
  241. return DGL_ADD_EDGE_FUNC(pgraph, nTail, nHead, nCost, nEdge,
  242. pvHeadAttr, pvTailAttr, pvEdgeAttr,
  243. nFlags & ~DGL_STRONGCONNECT);
  244. }
  245. return 0;
  246. }
  247. int DGL_DEL_EDGE_FUNC(dglGraph_s * pgraph, dglInt32_t nEdge)
  248. {
  249. #if defined(_DGL_V1)
  250. pgraph->iErrno = DGL_ERR_NotSupported;
  251. return -pgraph->iErrno;
  252. #else
  253. dglTreeEdge_s *pEdgeItem, findEdgeItem;
  254. dglInt32_t *pEdge;
  255. if (pgraph->Flags & DGL_GS_FLAT) {
  256. pgraph->iErrno = DGL_ERR_BadOnFlatGraph;
  257. return -pgraph->iErrno;
  258. }
  259. if (pgraph->pEdgeTree == NULL) {
  260. pgraph->iErrno = DGL_ERR_UnexpectedNullPointer;
  261. return -pgraph->iErrno;
  262. }
  263. findEdgeItem.nKey = nEdge;
  264. if ((pEdgeItem = avl_find(pgraph->pEdgeTree, &findEdgeItem)) == NULL) {
  265. pgraph->iErrno = DGL_ERR_EdgeNotFound;
  266. return -pgraph->iErrno;
  267. }
  268. pEdge = pEdgeItem->pv;
  269. if (DGL_DEL_NODE_INEDGE_FUNC
  270. (pgraph, DGL_EDGE_TAILNODE_OFFSET(pEdge), DGL_EDGE_ID(pEdge)) < 0) {
  271. return -pgraph->iErrno;
  272. }
  273. if (DGL_DEL_NODE_OUTEDGE_FUNC
  274. (pgraph, DGL_EDGE_HEADNODE_OFFSET(pEdge), DGL_EDGE_ID(pEdge)) < 0) {
  275. return -pgraph->iErrno;
  276. }
  277. /* prioritizer sync
  278. */
  279. if (pgraph->nOptions & DGL_GO_EdgePrioritize_COST) {
  280. if (dgl_edge_prioritizer_del
  281. (pgraph, DGL_EDGE_ID(pEdge), DGL_EDGE_COST(pEdge)) < 0) {
  282. return -pgraph->iErrno;
  283. }
  284. }
  285. /*
  286. */
  287. pgraph->cEdge--;
  288. pgraph->nnCost -= (dglInt64_t) DGL_EDGE_COST(pEdge);
  289. avl_delete(pgraph->pEdgeTree, pEdgeItem);
  290. dglTreeEdgeCancel(pEdgeItem, NULL);
  291. return 0;
  292. #endif
  293. }
  294. dglInt32_t *DGL_GET_EDGE_FUNC(dglGraph_s * pgraph, dglInt32_t nEdge)
  295. {
  296. #if defined(_DGL_V1)
  297. pgraph->iErrno = DGL_ERR_NotSupported;
  298. return NULL;
  299. #else
  300. register dglInt32_t top; /* top of table */
  301. register dglInt32_t pos; /* current position to compare */
  302. register dglInt32_t bot; /* bottom of table */
  303. register dglInt32_t *pref;
  304. register int cwords; /* size of a edge in words of 32 bit */
  305. register dglTreeEdge_s *ptreeEdge;
  306. dglTreeEdge_s findEdge;
  307. dglInt32_t id;
  308. pgraph->iErrno = 0;
  309. if (pgraph->Flags & DGL_GS_FLAT) {
  310. cwords = DGL_EDGE_WSIZE(pgraph->EdgeAttrSize);
  311. /*bot = pgraph->iEdgeBuffer / DGL_EDGE_SIZEOF(pgraph->EdgeAttrSize); */
  312. bot = pgraph->cEdge;
  313. top = 0;
  314. pos = 0;
  315. pref = (dglInt32_t *) pgraph->pEdgeBuffer;
  316. /* perform a binary search
  317. */
  318. while (top != bot) {
  319. pos = top + (bot - top) / 2;
  320. id = DGL_EDGE_ID(&pref[pos * cwords]);
  321. if (id == nEdge) {
  322. break;
  323. }
  324. else if (nEdge < id) {
  325. bot = pos;
  326. }
  327. else if (nEdge > id) {
  328. top = pos + 1;
  329. }
  330. }
  331. if (top == bot) {
  332. return NULL;
  333. }
  334. return &pref[pos * cwords];
  335. }
  336. else {
  337. findEdge.nKey = nEdge;
  338. ptreeEdge = avl_find(pgraph->pEdgeTree, &findEdge);
  339. if (ptreeEdge && ptreeEdge->pv) {
  340. return ptreeEdge->pv;
  341. }
  342. return NULL;
  343. }
  344. #endif
  345. }