helpers.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <stdlib.h>
  22. #include <string.h>
  23. #include "type.h"
  24. #include "tree.h"
  25. #include "graph.h"
  26. #include "helpers.h"
  27. /*
  28. * helpers for parametric stack
  29. */
  30. unsigned char *dgl_mempush(unsigned char *pstack, long *istack, long size,
  31. void *pv)
  32. {
  33. if (*istack == 0)
  34. pstack = NULL;
  35. pstack = realloc(pstack, size * (1 + *istack));
  36. if (pstack == NULL)
  37. return NULL;
  38. memcpy(&pstack[(*istack) * size], pv, size);
  39. (*istack)++;
  40. return pstack;
  41. }
  42. unsigned char *dgl_mempop(unsigned char *pstack, long *istack, long size)
  43. {
  44. if (*istack == 0)
  45. return NULL;
  46. return &pstack[size * (--(*istack))];
  47. }
  48. void dgl_swapInt32Bytes(dglInt32_t * pn)
  49. {
  50. unsigned char *pb = (unsigned char *)pn;
  51. pb[0] ^= pb[3];
  52. pb[3] ^= pb[0];
  53. pb[0] ^= pb[3];
  54. pb[1] ^= pb[2];
  55. pb[2] ^= pb[1];
  56. pb[1] ^= pb[2];
  57. }
  58. void dgl_swapInt64Bytes(dglInt64_t * pn)
  59. {
  60. unsigned char *pb = (unsigned char *)pn;
  61. pb[0] ^= pb[7];
  62. pb[7] ^= pb[0];
  63. pb[0] ^= pb[7];
  64. pb[1] ^= pb[6];
  65. pb[6] ^= pb[1];
  66. pb[1] ^= pb[6];
  67. pb[2] ^= pb[5];
  68. pb[5] ^= pb[2];
  69. pb[2] ^= pb[5];
  70. pb[3] ^= pb[4];
  71. pb[4] ^= pb[3];
  72. pb[3] ^= pb[4];
  73. }
  74. /*
  75. * Keep the edge cost prioritizer in sync
  76. */
  77. int dgl_edge_prioritizer_del(dglGraph_s * pG, dglInt32_t nId,
  78. dglInt32_t nPriId)
  79. {
  80. dglTreeEdgePri32_s findPriItem, *pPriItem;
  81. register int iEdge1, iEdge2;
  82. dglInt32_t *pnNew;
  83. if (pG->edgePrioritizer.pvAVL) {
  84. findPriItem.nKey = nPriId;
  85. pPriItem = avl_find(pG->edgePrioritizer.pvAVL, &findPriItem);
  86. if (pPriItem && pPriItem->pnData) {
  87. pnNew = malloc(sizeof(dglInt32_t) * pPriItem->cnData);
  88. if (pnNew == NULL) {
  89. pG->iErrno = DGL_ERR_MemoryExhausted;
  90. return -pG->iErrno;
  91. }
  92. for (iEdge1 = 0, iEdge2 = 0; iEdge2 < pPriItem->cnData; iEdge2++) {
  93. if (pPriItem->pnData[iEdge2] != nId) {
  94. pnNew[iEdge1++] = pPriItem->pnData[iEdge2];
  95. }
  96. }
  97. free(pPriItem->pnData);
  98. if (iEdge1 == 0) {
  99. free(pnNew);
  100. pPriItem->pnData = NULL;
  101. pPriItem->cnData = 0;
  102. }
  103. else {
  104. pPriItem->pnData = pnNew;
  105. pPriItem->cnData = iEdge1;
  106. }
  107. }
  108. }
  109. return 0;
  110. }
  111. int dgl_edge_prioritizer_add(dglGraph_s * pG, dglInt32_t nId,
  112. dglInt32_t nPriId)
  113. {
  114. dglTreeEdgePri32_s *pPriItem;
  115. if (pG->edgePrioritizer.pvAVL == NULL) {
  116. pG->edgePrioritizer.pvAVL =
  117. avl_create(dglTreeEdgePri32Compare, NULL, dglTreeGetAllocator());
  118. if (pG->edgePrioritizer.pvAVL == NULL) {
  119. pG->iErrno = DGL_ERR_MemoryExhausted;
  120. return -pG->iErrno;
  121. }
  122. }
  123. pPriItem = dglTreeEdgePri32Add(pG->edgePrioritizer.pvAVL, nPriId);
  124. if (pPriItem == NULL) {
  125. pG->iErrno = DGL_ERR_MemoryExhausted;
  126. return -pG->iErrno;
  127. }
  128. if (pPriItem->cnData == 0) {
  129. pPriItem->pnData = (dglInt32_t *) malloc(sizeof(dglInt32_t));
  130. }
  131. else {
  132. pPriItem->pnData =
  133. (dglInt32_t *) realloc(pPriItem->pnData,
  134. sizeof(dglInt32_t) * (pPriItem->cnData +
  135. 1));
  136. }
  137. if (pPriItem->pnData == NULL) {
  138. pG->iErrno = DGL_ERR_MemoryExhausted;
  139. return -pG->iErrno;
  140. }
  141. pPriItem->pnData[pPriItem->cnData] = nId;
  142. pPriItem->cnData++;
  143. return 0;
  144. }