network: Implement function to count all nodes
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Jan 2018 16:25:46 +0000 (16:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Jan 2018 16:25:46 +0000 (16:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libloc.sym
src/loc/network.h
src/network.c
src/test-network.c

index 4026939..bdccb70 100644 (file)
@@ -32,6 +32,8 @@ global:
 
        # Network Tree
        loc_network_tree_add_network;
+       loc_network_tree_count_networks;
+       loc_network_tree_count_nodes;
        loc_network_tree_dump;
        loc_network_tree_new;
        loc_network_tree_unref;
index 32e3091..71e31c1 100644 (file)
@@ -48,5 +48,6 @@ int loc_network_tree_walk(struct loc_network_tree* tree,
 int loc_network_tree_dump(struct loc_network_tree* tree);
 int loc_network_tree_add_network(struct loc_network_tree* tree, struct loc_network* network);
 size_t loc_network_tree_count_networks(struct loc_network_tree* tree);
+size_t loc_network_tree_count_nodes(struct loc_network_tree* tree);
 
 #endif
index d34f881..7c7a819 100644 (file)
@@ -527,3 +527,19 @@ LOC_EXPORT size_t loc_network_tree_count_networks(struct loc_network_tree* tree)
 
        return counter;
 }
+
+static size_t __loc_network_tree_count_nodes(struct loc_network_tree_node* node) {
+       size_t counter = 1;
+
+       if (node->zero)
+               counter += __loc_network_tree_count_nodes(node->zero);
+
+       if (node->one)
+               counter += __loc_network_tree_count_nodes(node->one);
+
+       return counter;
+}
+
+LOC_EXPORT size_t loc_network_tree_count_nodes(struct loc_network_tree* tree) {
+       return __loc_network_tree_count_nodes(tree->root);
+}
index b309d45..665b338 100644 (file)
@@ -67,6 +67,9 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
+       size_t nodes = loc_network_tree_count_nodes(tree);
+       printf("The tree has %zu nodes\n", nodes);
+
        // Create a database
        struct loc_writer* writer;
        err = loc_writer_new(ctx, &writer);