Implement first task
This commit is contained in:
parent
104e46da64
commit
0ec58b65f5
13 changed files with 285 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
exercise1/build/
|
17
.vscode/c_cpp_properties.json
vendored
Normal file
17
.vscode/c_cpp_properties.json
vendored
Normal file
|
@ -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
|
||||
}
|
BIN
.vscode/ipch/28d138b018a3ad64/main.ipch
vendored
Normal file
BIN
.vscode/ipch/28d138b018a3ad64/main.ipch
vendored
Normal file
Binary file not shown.
BIN
.vscode/ipch/28d138b018a3ad64/mmap_address.bin
vendored
Normal file
BIN
.vscode/ipch/28d138b018a3ad64/mmap_address.bin
vendored
Normal file
Binary file not shown.
BIN
.vscode/ipch/7e7bfb0cb21110c1/mmap_address.bin
vendored
Normal file
BIN
.vscode/ipch/7e7bfb0cb21110c1/mmap_address.bin
vendored
Normal file
Binary file not shown.
BIN
.vscode/ipch/7e7bfb0cb21110c1/node.ipch
vendored
Normal file
BIN
.vscode/ipch/7e7bfb0cb21110c1/node.ipch
vendored
Normal file
Binary file not shown.
BIN
.vscode/ipch/85b5a11b2d9f2c78/mmap_address.bin
vendored
Normal file
BIN
.vscode/ipch/85b5a11b2d9f2c78/mmap_address.bin
vendored
Normal file
Binary file not shown.
40
.vscode/settings.json
vendored
Normal file
40
.vscode/settings.json
vendored
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
40
.vscode/tasks.json
vendored
Normal file
40
.vscode/tasks.json
vendored
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
24
exercise1/main.cpp
Normal file
24
exercise1/main.cpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include <iostream>
|
||||
|
||||
#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;
|
||||
}
|
8
exercise1/meson.build
Normal file
8
exercise1/meson.build
Normal file
|
@ -0,0 +1,8 @@
|
|||
project('ecg_tree', 'cpp')
|
||||
|
||||
files = [
|
||||
'main.cpp',
|
||||
'node.cpp'
|
||||
]
|
||||
|
||||
executable('ecg_tree', files)
|
119
exercise1/node.cpp
Normal file
119
exercise1/node.cpp
Normal file
|
@ -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<int>(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<void(const Node*, uint32_t)> 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;
|
||||
}
|
36
exercise1/node.h
Normal file
36
exercise1/node.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#ifndef NODE_H
|
||||
#define NODE_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
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<void(const Node*, uint32_t)> predicate, uint32_t depth = 0);
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
std::vector<Node*> children;
|
||||
|
||||
static uint32_t node_id;
|
||||
};
|
||||
|
||||
extern std::ostream& operator<<(std::ostream &os, const Node *node);
|
||||
|
||||
#endif // NODE_H
|
Loading…
Reference in a new issue