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.
This commit is contained in:
Simon Bruder 2023-04-22 15:22:18 +02:00
parent 9b7f4fac4a
commit b7694639b6
2 changed files with 9 additions and 9 deletions

View file

@ -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<const node *> 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<std::pair<const node *, size_t>> stack;
std::stack<std::pair<const node *, unsigned int>> stack;
stack.push(std::make_pair(this, 0));
std::set<const node *> 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 {

View file

@ -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<const node *> visited = {}) const;
std::string print_iterative() const;