Advertisement
Guest User

Modifiable graph

a guest
May 16th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define GRAPH_DEFAULT_CAPACITY 10
  7.  
  8. typedef struct {
  9.     const char* city_name;
  10.     GraphNode* edges;
  11. } GraphNode;
  12.  
  13. typedef struct {
  14.     GraphNode* other_node;
  15.     GraphNode* sibling_edge;
  16. } GraphEdge;
  17.  
  18. typedef struct {
  19.     enum {GRAPH_NULL=0, GRAPH_NODE, GRAPH_EDGE} type;
  20.     union {
  21.         GraphNode* node;
  22.         GraphEdge* edge;
  23.         void* data_ptr;
  24.     };
  25. } GraphObject;
  26.  
  27. typedef struct {
  28.     GraphObject* gc_objects;
  29.     size_t gc_obj_capacity;
  30.     size_t gc_obj_count;
  31. } Graph;
  32.  
  33. void graph_gc_reserve_more_objects(Graph* graph) {
  34.  
  35.     GraphObject* new_storage = malloc(sizeof(GraphObject) * graph->gc_obj_capacity * 2);
  36.     memset(new_storage, 0, sizeof(graph->gc_obj_capacity * 2));
  37.  
  38.     memcpy(new_storage, graph->gc_objects, graph->gc_obj_capacity);
  39.     free(graph->gc_objects);
  40.  
  41.     graph->gc_objects = new_storage;
  42.     graph->gc_obj_capacity *= 2;
  43. }
  44.  
  45. void graph_gc_register_object(Graph* graph, GraphObject* object) {
  46.     if(graph->gc_obj_capacity == graph->gc_obj_count) {
  47.         graph_gc_reserve_more_objects(graph);
  48.     }
  49.  
  50.     memcpy(&graph->gc_objects[graph->gc_obj_count], object, sizeof(*object));
  51.     graph->gc_obj_count++;
  52. }
  53.  
  54. void graph_edge_new(Graph* graph, GraphNode* source, GraphNode* destination) {
  55.     assert(source != destination);
  56.  
  57.     GraphObject new_obj;
  58.     memset(&new_obj, 0, sizeof(new_obj));
  59.  
  60.     new_obj.type = GRAPH_EDGE;
  61.     new_obj.edge = malloc(sizeof(GraphEdge));
  62.  
  63.     new_obj.edge->other_node = destination;
  64.     new_obj.edge->sibling_edge = source->edges;
  65.  
  66.     graph_gc_register_object(graph, &new_obj);
  67.  
  68.     source->edges = new_obj.edge;
  69. }
  70.  
  71. GraphNode* graph_node_new(Graph* graph, const char* city_name) {
  72.     GraphObject new_obj;
  73.     memset(&new_obj, 0, sizeof(new_obj));
  74.  
  75.     GraphNode* node = malloc(sizeof(GraphNode));
  76.     memset(node, 0, sizeof(*node));
  77.  
  78.     new_obj.type = GRAPH_NODE;
  79.     new_obj.node = node;
  80.  
  81.     graph_gc_register_object(graph, &new_obj);
  82.  
  83.     node->city_name = city_name;
  84.     return node;
  85. }
  86.  
  87. void graph_destroy(Graph* graph) {
  88.     for(size_t i = 0; i < graph->gc_obj_count; i++) {
  89.         free(graph->gc_objects[i].data_ptr);
  90.     }
  91.     free(graph->gc_objects);
  92.     free(graph);
  93. }
  94.  
  95. Graph* graph_new() {
  96.     Graph *graph = malloc(sizeof(Graph));
  97.     memset(graph, 0, sizeof(*graph));
  98.     graph->gc_objects = malloc(sizeof(GraphObject)*GRAPH_DEFAULT_CAPACITY);
  99.     return graph;
  100. }
  101.  
  102. int main(int argc, char** argv) {
  103.  
  104.     Graph* graph = graph_new();
  105.  
  106.     GraphNode* node_a = graph_node_new(graph, "London");
  107.     GraphNode* node_b = graph_node_new(graph, "Birmingham");
  108.  
  109.     GraphEdge* edge0 = graph_edge_new(graph, node_a, node_b);
  110.  
  111.     graph_destroy(graph);
  112. };
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement