From db4d489ae80e116e39778dc6648c6e203b4c2901 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sat, 22 Apr 2023 15:22:18 +0200 Subject: [PATCH] u01: Consistently use size_t and unsigned int size_t should only be used for indexing. For all other purposes, unsigned int should be used. --- u01/node.cpp | 12 ++++++------ u01/node.h | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/u01/node.cpp b/u01/node.cpp index 7ee121d..dd3690b 100644 --- a/u01/node.cpp +++ b/u01/node.cpp @@ -31,20 +31,20 @@ std::string node::get_name() const { return name; } void node::set_name(const std::string new_name) { this->name = new_name; } -unsigned int node::get_nr_children() const { return children.size(); } +std::size_t node::get_nr_children() const { return children.size(); } node *node::get_child(std::size_t i) const { return children[i]; } void node::add_child(node *child) { children.push_back(child); } -void node::print(std::ostream &str, std::size_t depth) const { +void node::print(std::ostream &str, unsigned int depth) const { str << std::string(depth, '\t') << get_name() << std::endl; for (node *child : children) { child->print(str, depth + 1); } } -std::string node::print_recursive(std::size_t depth, +std::string node::print_recursive(unsigned int depth, std::set visited) const { std::stringstream output; output << std::string(depth, '\t') << get_name(); @@ -64,13 +64,13 @@ std::string node::print_recursive(std::size_t depth, std::string node::print_iterative() const { std::stringstream output; - std::stack> stack; + std::stack> stack; stack.push(std::make_pair(this, 0)); std::set visited = {}; while (!stack.empty()) { const node *n = nullptr; - size_t depth; + unsigned int depth; std::tie(n, depth) = stack.top(); stack.pop(); if (visited.find(n) == visited.end()) { @@ -80,7 +80,7 @@ std::string node::print_iterative() const { // Complex iteration (not default vector iteration is necessary, // to achieve the same output as the recursive approach. // Otherwise, the order of the children is reversed (due to LIFO). - for (size_t i = n->get_nr_children(); i > 0; i--) { + for (unsigned int i = n->get_nr_children(); i > 0; i--) { stack.push(std::make_pair(n->get_child(i - 1), depth + 1)); } } else { diff --git a/u01/node.h b/u01/node.h index 0fa6b85..def10b3 100644 --- a/u01/node.h +++ b/u01/node.h @@ -12,11 +12,11 @@ public: virtual ~node(); std::string get_name() const; void set_name(const std::string new_name); - unsigned int get_nr_children() const; + std::size_t get_nr_children() const; node *get_child(std::size_t i) const; void add_child(node *child); - void print(std::ostream &str, std::size_t depth = 0) const; - std::string print_recursive(std::size_t depth = 0, + void print(std::ostream &str, unsigned int depth = 0) const; + std::string print_recursive(unsigned int depth = 0, std::set visited = {}) const; std::string print_iterative() const;