Skip to content

Commit

Permalink
[ADD] BranchContainers and Graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthou committed Jun 19, 2020
1 parent 97a6890 commit d9554fa
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ add_library(mementar_memGraphs_lib
src/core/memGraphs/Branchs/types/Action.cpp
src/core/memGraphs/DoublyLinkedList/DllCargoNode.cpp
src/core/memGraphs/DoublyLinkedList/DllLinkedElement.cpp
src/core/memGraphs/Graphs/ActionGraph.cpp
src/core/memGraphs/Graphs/EventGraph.cpp
)

add_library(mementar_core_lib
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef MEMENTAR_BRANCHCONTAINERBASE_H
#define MEMENTAR_BRANCHCONTAINERBASE_H

#include <string>
#include <vector>

#include "mementar/core/memGraphs/Branchs/ValuedNode.h"

namespace mementar {

template <typename B>
class BranchContainerBase
{
static_assert(std::is_base_of<ValuedNode,B>::value, "B must be derived from ValuedNode");
public:
BranchContainerBase() {}
virtual ~BranchContainerBase() {}

virtual B* find(const std::string& word) = 0;
virtual void load(std::vector<B*>& vect) = 0;
virtual void insert(B* branch) = 0;
virtual void erase(B* branch) = 0;
private:
};

} // namespace mementar

#endif //MEMENTAR_BRANCHCONTAINERBASE_H
147 changes: 147 additions & 0 deletions include/mementar/core/memGraphs/BranchContainer/BranchContainerDyn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#ifndef MEMENTAR_BRANCHCONTAINERDYN_H
#define MEMENTAR_BRANCHCONTAINERDYN_H

#include <map>
#include <iostream>
#include <math.h>

#include "mementar/core/memGraphs/BranchContainer/BranchContainerBase.h"

namespace mementar {

template <typename T>
struct BranchNode_t
{
BranchNode_t<T>* next;
std::string id;
T* branch;

BranchNode_t() {next = nullptr; id = ""; branch = nullptr; }
~BranchNode_t()
{
//T* branch is destructed by ontograph
if(next != nullptr)
{
delete next;
next = nullptr;
}
}
};

template <typename B>
class BranchContainerDyn : public BranchContainerBase<B>
{
public:
BranchContainerDyn()
{
nodes_ = new BranchNode_t<B>;
nodes_end_ = nodes_;
nb_elem_ = 0;
}
BranchContainerDyn(const BranchContainerDyn& base);

virtual ~BranchContainerDyn()
{
delete nodes_;
}

virtual B* find(const std::string& word);
virtual void load(std::vector<B*>& vect);
virtual void insert(B* branch);
virtual void erase(B* branch);
private:
BranchNode_t<B>* nodes_;
BranchNode_t<B>* nodes_end_;
size_t buffer_size_;
size_t nb_elem_;

void insertEnd(std::string id, B* branch);
void reconf(BranchNode_t<B>* node);
};

template <typename B>
BranchContainerDyn<B>::BranchContainerDyn(const BranchContainerDyn& base)
{
BranchNode_t<B>* current = base.nodes_;
while(current != nullptr)
{
B* tmp = new B();
*tmp = *(current->branch);
insertEnd(current->id, tmp);
}

buffer_size_ = base.buffer_size_;
nb_elem_ = base.nb_elem_;
}

template <typename B>
B* BranchContainerDyn<B>::find(const std::string& word)
{
B* tmp = nullptr;
size_t i = 0;
for(BranchNode_t<B>* node = nodes_; node->next != nullptr; node = node->next)
{
if(node->next->id == word)
{
tmp = node->next->branch;
if(i > 5)
reconf(node);
break;
}
i++;
}

return tmp;
}

template <typename B>
void BranchContainerDyn<B>::load(std::vector<B*>& vect)
{
for(size_t i = 0; i < vect.size(); i++)
insertEnd(vect[i]->getValue(), vect[i]);

nb_elem_ += vect.size();
buffer_size_ = log2(nb_elem_);
}

template <typename B>
void BranchContainerDyn<B>::insert(B* branch)
{
insertEnd(branch->getValue(), branch);
}

template <typename B>
void BranchContainerDyn<B>::erase(B* branch)
{
for(BranchNode_t<B>* node = nodes_; node->next != nullptr; node = node->next)
if(node->next->branch == branch)
{
BranchNode_t<B>* tmp = node->next;
node->next = tmp->next;
delete(tmp);
break;
}
}

template <typename B>
void BranchContainerDyn<B>::insertEnd(std::string id, B* branch)
{
BranchNode_t<B>* tmp = new BranchNode_t<B>;
tmp->id = id;
tmp->branch = branch;
nodes_end_->next = tmp;
nodes_end_ = tmp;
}

template <typename B>
void BranchContainerDyn<B>::reconf(BranchNode_t<B>* node)
{
BranchNode_t<B>* tmp = node->next;
node->next = tmp->next;
tmp->next = nodes_->next;
nodes_->next = tmp;
}

} // namespace mementar

#endif // MEMENTAR_BRANCHCONTAINERDYN_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef MEMENTAR_BRANCHCONTAINERMAP_H
#define MEMENTAR_BRANCHCONTAINERMAP_H

#include <map>
#include <unordered_map>

#include "mementar/core/memGraphs/BranchContainer/BranchContainerBase.h"

namespace mementar {

template <typename B>
class BranchContainerMap : public BranchContainerBase<B>
{
public:
BranchContainerMap() {}
BranchContainerMap(const BranchContainerMap& base);
virtual ~BranchContainerMap() {} //B* must by destructed by the owner

virtual B* find(const std::string& word);
virtual void load(std::vector<B*>& vect);
virtual void insert(B* branch);
virtual void erase(B* branch);
private:
std::unordered_map<std::string, B*> nodes_;
};

template <typename B>
BranchContainerMap<B>::BranchContainerMap(const BranchContainerMap& base)
{
for(auto& it : base.nodes_)
{
B* tmp = new B();
*tmp = *(it.second);
nodes_[it.first] = tmp;
}
}

template <typename B>
B* BranchContainerMap<B>::find(const std::string& word)
{
typename std::unordered_map<std::string, B*>::iterator it = nodes_.find(word);
if(it == nodes_.end())
return nullptr;
else
return it->second;
}

template <typename B>
void BranchContainerMap<B>::load(std::vector<B*>& vect)
{
for(size_t i = 0; i < vect.size(); i++)
nodes_[vect[i]->getValue()] = vect[i];
}

template <typename B>
void BranchContainerMap<B>::insert(B* branch)
{
nodes_[branch->getValue()] = branch;
}

template <typename B>
void BranchContainerMap<B>::erase(B* branch)
{
nodes_.erase(branch->getValue());
}

} // namespace mementar

#endif // MEMENTAR_BRANCHCONTAINERMAP_H
51 changes: 51 additions & 0 deletions include/mementar/core/memGraphs/Graphs/ActionGraph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef MEMENTAR_ACTIONGRAPH_H
#define MEMENTAR_ACTIONGRAPH_H

#include "mementar/core/memGraphs/Graphs/Graph.h"
#include "mementar/core/memGraphs/Graphs/EventGraph.h"
#include "mementar/core/memGraphs/Branchs/ContextualizedEvent.h"
#include "mementar/core/memGraphs/Branchs/types/Action.h"

#include <vector>
#include <map>
#include <string>

namespace mementar {

class ActionGraph : public Graph<Action>
{
public:
ActionGraph(EventGraph* event_graph);
~ActionGraph();

void add(Action* action);
bool setEnd(const std::string& action_name, const SoftPoint& end);

std::vector<Action*> get()
{
return all_actions_;
}

std::vector<Action*> getSafe()
{
std::shared_lock<std::shared_timed_mutex> lock(Graph<Action>::mutex_);
return all_actions_;
}

std::vector<Action*> getPending();
std::vector<Action*> getPendingSafe()
{
std::shared_lock<std::shared_timed_mutex> lock(Graph<Action>::mutex_);
return getPending();
}


private:
EventGraph* event_graph_;
std::vector<Action*> all_actions_;
std::map<std::string, Action*> pending_actions_;
};

} // namespace mementar

#endif // MEMENTAR_ACTIONGRAPH_H
40 changes: 40 additions & 0 deletions include/mementar/core/memGraphs/Graphs/EventGraph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef MEMENTAR_EVENTGRAPH_H
#define MEMENTAR_EVENTGRAPH_H

#include "mementar/core/memGraphs/Graphs/Graph.h"
#include "mementar/core/memGraphs/Branchs/ContextualizedEvent.h"

#include "mementar/core/memGraphs/Btree/Btree.h"

namespace mementar {

class EventGraph : public Graph<ContextualizedEvent>
{
public:

~EventGraph();

void add(ContextualizedEvent* event);

std::vector<ContextualizedEvent*> get()
{
return all_events_;
}

std::vector<ContextualizedEvent*> getSafe()
{
std::shared_lock<std::shared_timed_mutex> lock(Graph<ContextualizedEvent>::mutex_);

return all_events_;
}

Btree<SoftPoint::Ttime, DllLinkedElement*, DllCargoNode>* getTimeline() { return &timeline; }

private:
std::vector<ContextualizedEvent*> all_events_;
Btree<SoftPoint::Ttime, DllLinkedElement*, DllCargoNode> timeline;
};

} // namespace mementar

#endif // MEMENTAR_EVENTGRAPH_H
Loading

0 comments on commit d9554fa

Please sign in to comment.