diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8890db1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +exercise1/build/ \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..290445d --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "clang-x64", + "compileCommands": "${workspaceFolder}/exercise1/build/compile_commands.json" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/ipch/28d138b018a3ad64/main.ipch b/.vscode/ipch/28d138b018a3ad64/main.ipch new file mode 100644 index 0000000..a37289e Binary files /dev/null and b/.vscode/ipch/28d138b018a3ad64/main.ipch differ diff --git a/.vscode/ipch/28d138b018a3ad64/mmap_address.bin b/.vscode/ipch/28d138b018a3ad64/mmap_address.bin new file mode 100644 index 0000000..6398f9a Binary files /dev/null and b/.vscode/ipch/28d138b018a3ad64/mmap_address.bin differ diff --git a/.vscode/ipch/7e7bfb0cb21110c1/mmap_address.bin b/.vscode/ipch/7e7bfb0cb21110c1/mmap_address.bin new file mode 100644 index 0000000..1eac54b Binary files /dev/null and b/.vscode/ipch/7e7bfb0cb21110c1/mmap_address.bin differ diff --git a/.vscode/ipch/7e7bfb0cb21110c1/node.ipch b/.vscode/ipch/7e7bfb0cb21110c1/node.ipch new file mode 100644 index 0000000..d0c2307 Binary files /dev/null and b/.vscode/ipch/7e7bfb0cb21110c1/node.ipch differ diff --git a/.vscode/ipch/85b5a11b2d9f2c78/mmap_address.bin b/.vscode/ipch/85b5a11b2d9f2c78/mmap_address.bin new file mode 100644 index 0000000..c62a51e Binary files /dev/null and b/.vscode/ipch/85b5a11b2d9f2c78/mmap_address.bin differ diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2d27a79 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,40 @@ +{ + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cstdarg": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "*.tcc": "cpp", + "cmath": "cpp", + "cstdint": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "fstream": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "memory": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "type_traits": "cpp", + "tuple": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "__config": "cpp", + "__nullptr": "cpp" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..237f2bb --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,40 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Build Exercise 1", + "type": "shell", + "command": "cd exercise1 && meson . build/ && cd build && ninja", + "problemMatcher": [], + "presentation": { + "echo": true, + "reveal": "always", + "focus": false, + "panel": "shared", + "showReuseMessage": true, + "clear": true + } + }, + { + "label": "Run Exercise 1", + "type": "shell", + "command": "exercise1/build/./ecg_tree", + "dependsOn": "Build Exercise 1", + "problemMatcher": [] + }, + { + "type": "shell", + "label": "g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "/usr/bin" + } + } + ] +} \ No newline at end of file diff --git a/exercise1/main.cpp b/exercise1/main.cpp new file mode 100644 index 0000000..f8ae7ad --- /dev/null +++ b/exercise1/main.cpp @@ -0,0 +1,24 @@ +#include + +#include "node.h" + +int main() { + /* + Node* root = new Node("root"); + Node* left_child = new Node("left child"); + Node* right_child = new Node("right child"); + + root->add_child(left_child); + root->add_child(right_child); + + delete root; + */ + + Node* auto_root = Node::create_complete_tree(2, 4); + + std::cout << auto_root << std::endl; + + delete auto_root; + + return 0; +} diff --git a/exercise1/meson.build b/exercise1/meson.build new file mode 100644 index 0000000..3d5276b --- /dev/null +++ b/exercise1/meson.build @@ -0,0 +1,8 @@ +project('ecg_tree', 'cpp') + +files = [ + 'main.cpp', + 'node.cpp' +] + +executable('ecg_tree', files) diff --git a/exercise1/node.cpp b/exercise1/node.cpp new file mode 100644 index 0000000..5524510 --- /dev/null +++ b/exercise1/node.cpp @@ -0,0 +1,119 @@ +#include "node.h" + +uint32_t Node::node_id = 0; + +Node::Node(const std::string& name) +{ + node_id++; + + if (name.empty()) + { + std::stringstream str_sm; + str_sm << node_id; + std::string node_id_str = str_sm.str(); + + this->name = "node" + node_id_str; + } + else + { + this->name = name; + } +} + +Node::~Node() +{ + std::cout << "enter ~node() of \"" << name << "\"" << std::endl; + + for (Node* child : children) + { + delete child; + } + + std::cout << "leave ~node() of \"" << name << "\"" << std::endl; +} + +std::string Node::get_name() const +{ + return name; +} + +void Node::set_name(const std::string& new_name) +{ + name = new_name; +} + +int Node::get_nr_children() const +{ + return children.size(); +} + +Node* Node::get_child(int i) const +{ + if (i < 0 || i >= static_cast(children.size())) + { + return nullptr; + } + + return children[i]; +} + +void Node::add_child(Node* child) +{ + children.emplace_back(child); +} + +void Node::print(std::ostream& str, uint32_t depth) const +{ + auto predicate = [&str](const Node* node, uint32_t depth) + { + for (uint32_t i = 0; i < depth; i++) + { + str << "\t"; + } + + str << node->get_name() + "\n"; + }; + + Node::traverse_tree(this, predicate); +} + +Node* Node::create_complete_tree(uint32_t nr_child_nodes, uint32_t tree_depth) +{ + if (tree_depth == 0) + { + return nullptr; + } + + Node* node = new Node; + + for (uint32_t j = 0; j < nr_child_nodes; j++) + { + Node* child = Node::create_complete_tree(nr_child_nodes, tree_depth - 1); + + if (child == nullptr) + { + break; + } + + node->children.emplace_back(child); + } + + return node; +} + +void Node::traverse_tree(const Node* node, std::function predicate, uint32_t depth) +{ + predicate(node, depth); + + for (Node* child : node->children) + { + Node::traverse_tree(child, predicate, depth + 1); + } +} + +std::ostream& operator<<(std::ostream &os, const Node *node) +{ + node->print(os); + + return os; +} \ No newline at end of file diff --git a/exercise1/node.h b/exercise1/node.h new file mode 100644 index 0000000..d23be1d --- /dev/null +++ b/exercise1/node.h @@ -0,0 +1,36 @@ +#ifndef NODE_H +#define NODE_H + +#include +#include +#include +#include +#include + +class Node { + +public: + Node(const std::string& name = ""); + virtual ~Node(); + + std::string get_name() const; + void set_name(const std::string& new_name); + + int get_nr_children() const; + Node* get_child(int i) const; + void add_child(Node* child); + + void print(std::ostream& str = std::cout, uint32_t depth = 0) const; + static Node* create_complete_tree(uint32_t nr_child_nodes, uint32_t tree_depth); + static void traverse_tree(const Node* node, std::function predicate, uint32_t depth = 0); + +private: + std::string name; + std::vector children; + + static uint32_t node_id; +}; + +extern std::ostream& operator<<(std::ostream &os, const Node *node); + +#endif // NODE_H \ No newline at end of file