vertex.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*!
  2. Temelia - Graph Vertex implementation source file.
  3. Copyright (C) 2008, 2009 Ceata (http://ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #include "include/vertex.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. struct _vertex_t
  21. {
  22. /*! label */
  23. char *label;
  24. /*! cost */
  25. double cost;
  26. /*! identifier */
  27. unsigned int identifier;
  28. /*! key */
  29. void *key;
  30. /*! color */
  31. int color;
  32. /*! if 1 vertex visited and 0 if not explored */
  33. int visited;
  34. /*! parent identifier */
  35. unsigned int parent_identifier;
  36. /*! time when vertex was discovered by travel */
  37. unsigned int time_start;
  38. /*! time when travel finished visiting this vertex */
  39. unsigned int time_stop;
  40. };
  41. vertex_t vertex_new(char *label, unsigned int identifier, void *key)
  42. {
  43. vertex_t vertex = (struct _vertex_t *) _new(sizeof(struct _vertex_t));
  44. // Set current label
  45. vertex->label = label;
  46. // Set current key
  47. vertex->key = key;
  48. // Default color is WHITE
  49. vertex->color = WHITE;
  50. // Default cost is 0
  51. vertex->cost = 0;
  52. // Set current identifier
  53. vertex->identifier = identifier;
  54. // Default parent is -1
  55. vertex->parent_identifier = (unsigned int) -1;
  56. // Default time start and time stop are 0
  57. vertex->time_start = vertex->time_stop = 0;
  58. // Default the node is not visited
  59. vertex->visited = 0;
  60. return vertex;
  61. }
  62. void vertex_delete(vertex_t vertex)
  63. {
  64. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  65. vertex->visited = vertex->time_start = vertex->time_stop
  66. = vertex->parent_identifier = vertex->identifier = vertex->color = 0;
  67. vertex->cost = 0;
  68. vertex->key = vertex->label = NULL;
  69. _delete(vertex);
  70. }
  71. char *vertex_get_label(vertex_t vertex)
  72. {
  73. _ASSERT(vertex, ==, NULL, NULL_POINTER, NULL);
  74. return vertex->label;
  75. }
  76. double vertex_get_cost(vertex_t vertex)
  77. {
  78. _ASSERT(vertex, ==, NULL, NULL_POINTER, 0);
  79. return vertex->cost;
  80. }
  81. unsigned int vertex_get_identifier(vertex_t vertex)
  82. {
  83. _ASSERT(vertex, ==, NULL, NULL_POINTER, -1);
  84. return vertex->identifier;
  85. }
  86. void *vertex_get_key(vertex_t vertex)
  87. {
  88. _ASSERT(vertex, ==, NULL, NULL_POINTER, NULL);
  89. return vertex->key;
  90. }
  91. int vertex_get_color(vertex_t vertex)
  92. {
  93. _ASSERT(vertex, ==, NULL, NULL_POINTER, -1);
  94. return vertex->color;
  95. }
  96. int vertex_get_visited(vertex_t vertex)
  97. {
  98. _ASSERT(vertex, ==, NULL, NULL_POINTER, -1);
  99. return vertex->visited;
  100. }
  101. unsigned int vertex_get_parent_identifier(vertex_t vertex)
  102. {
  103. _ASSERT(vertex, ==, NULL, NULL_POINTER, -1);
  104. return vertex->parent_identifier;
  105. }
  106. unsigned int vertex_get_time_start(vertex_t vertex)
  107. {
  108. _ASSERT(vertex, ==, NULL, NULL_POINTER, -1);
  109. return vertex->time_start;
  110. }
  111. unsigned int vertex_get_time_stop(vertex_t vertex)
  112. {
  113. _ASSERT(vertex, ==, NULL, NULL_POINTER, -1);
  114. return vertex->time_stop;
  115. }
  116. void vertex_set_label(vertex_t vertex, char *label)
  117. {
  118. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  119. vertex->label = label;
  120. }
  121. void vertex_set_cost(vertex_t vertex, double cost)
  122. {
  123. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  124. vertex->cost = cost;
  125. }
  126. void vertex_set_identifier(vertex_t vertex, unsigned int identifier)
  127. {
  128. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  129. vertex->identifier = identifier;
  130. }
  131. void vertex_set_key(vertex_t vertex, void *key)
  132. {
  133. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  134. vertex->key = key;
  135. }
  136. void vertex_set_color(vertex_t vertex, int color)
  137. {
  138. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  139. vertex->color = color;
  140. }
  141. void vertex_set_visited(vertex_t vertex, int visited)
  142. {
  143. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  144. vertex->visited = visited;
  145. }
  146. void vertex_set_parent_identifier(vertex_t vertex,
  147. unsigned int parent_identifier)
  148. {
  149. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  150. vertex->parent_identifier = parent_identifier;
  151. }
  152. void vertex_set_time_start(vertex_t vertex, unsigned int time_start)
  153. {
  154. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  155. vertex->time_start = time_start;
  156. }
  157. void vertex_set_time_stop(vertex_t vertex, unsigned int time_stop)
  158. {
  159. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  160. vertex->time_stop = time_stop;
  161. }
  162. void vertex_debug(vertex_t vertex, void *stream)
  163. {
  164. _ASSERT(vertex, ==, NULL, NULL_POINTER,);
  165. _ASSERT(stream, ==, NULL, NULL_POINTER,);
  166. fprintf(
  167. stream,
  168. "id %u, label %s, cost %lf, key %p, color %d, visited %d, parent %u, time start %u, time stop %u\n",
  169. vertex->identifier, vertex->label, vertex->cost, vertex->key,
  170. (int) vertex->color, (int) vertex->visited,
  171. vertex->parent_identifier, vertex->time_start, vertex->time_stop);
  172. }