Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
build/
solutions/
_build
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j'imagine que c'est la bibliotheque des testes qui veut rajouter '_build' ? Pourrait-on la configurer pour utiliser le 'build' qu'on a déjà (ça me soule un peu d'avoir 2 'build'...) ?

Copy link
Author

@marsault marsault Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je ne crois pas, je pense que je faisais des cmake .. à la main. J'ai supprimé le répertoire et le .gitignore de mon côté.

4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ file(GLOB tp_folders tp-*)
foreach(tp ${tp_folders})
add_subdirectory(${tp})
endforeach()


# To uncomment to start the TP about JSON
enable_testing()
1 change: 1 addition & 0 deletions tp-json/ArrayNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "ArrayNode.hpp"
132 changes: 132 additions & 0 deletions tp-json/ArrayNode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#pragma once

#include "Node.hpp"

#include <algorithm>
#include <numeric>
#include <vector>

class ArrayNode : public Node
{
private:
std::vector<Node_ptr> _data;

public:
std::vector<Node_ptr> const& data() const { return _data; }

std::string print() const override
{
std::string result = "[";
for (unsigned i = 0; i < _data.size(); ++i)
{
if (i > 0)
result += ",";
result += _data[i]->print();
}
result += ']';
return result;
}

void add(Node_ptr node) { _data.push_back(std::move(node)); }

ArrayNode()
: Node(NodeKind::ARRAY)
{}

ArrayNode(std::vector<Node_ptr> data)
: Node { NodeKind::ARRAY }
, _data { std::move(data) }
{}

static inline std::unique_ptr<ArrayNode> make_ptr(std::vector<Node_ptr> data = {})
{
return std::make_unique<ArrayNode>(std::move(data));
}

size_t child_count() const override { return _data.size(); }

ArrayNode* as_ArrayNode() override { return this; }
const ArrayNode* as_ArrayNode() const override { return this; }

inline bool operator==(const Node& other) const override
{
if (!(other.is_of_kind(kind())))
{
std::cerr << kind() << "!=" << other.kind() << std::endl;
return false;
}
ArrayNode const* other_s = other.as_ArrayNode();
size_t size = child_count();
if (other_s->child_count() != size)
{
std::cerr << size << " != " << other_s->child_count() << std::endl;
return false;
}
for (unsigned i = 0; i < size; i++)
{
if (*(other_s->_data[i]) != *(_data[i]))
{
std::cerr << *(other_s->_data[i]) << std::endl;
std::cerr << *_data[i] << std::endl;
return false;
}
}
return true;
}

size_t height() const override
{
return std::accumulate(_data.begin(), _data.end(), 0,
[](size_t i, Node_ptr const& child)
{ return std::max(i, 1u + child->height()); });
}

virtual size_t node_count() const override
{
return std::accumulate(_data.begin(), _data.end(), 1,
[](size_t i, Node_ptr const& child) { return i + child->node_count(); });
}

std::vector<Node_ptr>::iterator begin() { return _data.begin(); }
std::vector<Node_ptr>::iterator end() { return _data.end(); }

std::vector<Node_ptr>::const_iterator begin() const { return _data.begin(); }
std::vector<Node_ptr>::const_iterator end() const { return _data.end(); }

Node_ptr deep_copy() const override
{
auto result = make_ptr();
for (auto const& elt : this->data())
result->add(elt->deep_copy());
return result;
}

std::string dot_label() const override { return "[...]"; }

virtual void dot(std::ostream& o) const
{
Node::dot(o);
o << "subgraph cluster_" << dot_id() << " {style=invis;" << std::endl;
for (auto it = _data.rbegin(); it != _data.rend(); ++it)
{
(*it)->dot(o);
o << dot_id() << " -> " << (*it)->dot_id() << " ;" << std::endl;
}
o << "}" << std::endl;
}

Node* at(size_t index) override
{
if (index < child_count())
return _data.at(index).get();
else
return nullptr;
}
const Node* at(size_t index) const override
{
if (index < child_count())
return _data.at(index).get();
else
return nullptr;
}
};
1 change: 1 addition & 0 deletions tp-json/BooleanLeaf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "BooleanLeaf.hpp"
36 changes: 36 additions & 0 deletions tp-json/BooleanLeaf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "Node.hpp"

class BooleanLeaf : public Node
{
private:
bool _data;

public:
std::string print() const override { return _data ? "true" : "false"; }
BooleanLeaf(bool data)
: Node { NodeKind::BOOLEAN }
, _data { data }
{}

static std::unique_ptr<BooleanLeaf> make_ptr(bool data) { return std::make_unique<BooleanLeaf>(data); }

BooleanLeaf* as_BooleanLeaf() override { return this; }
BooleanLeaf const* as_BooleanLeaf() const override { return this; }

inline bool operator==(const Node& other) const override
{
if (!(other.is_of_kind(kind())))
{
return false;
}
return (_data == other.as_BooleanLeaf()->_data);
}

const bool& data() const { return _data; }

Node_ptr deep_copy() const override { return make_ptr(data()); }

std::string dot_label() const override { return _data ? "true" : "false"; }
};
3 changes: 3 additions & 0 deletions tp-json/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file(GLOB TP-JSON-SOURCES *.cpp)

add_subdirectory(tests)
Loading