25 #ifndef SKELETON_BLOCKER_INTERNAL_TRIE_H_
26 #define SKELETON_BLOCKER_INTERNAL_TRIE_H_
37 template<
typename SimplexHandle>
40 typedef typename SimplexHandle::Vertex_handle Vertex_handle;
43 std::vector<std::shared_ptr<Trie> > childs;
49 Trie() : parent_(0) { }
51 Trie(Vertex_handle v_) : v(v_), parent_(0) { }
53 Trie(Vertex_handle v_, Trie* parent) : v(v_), parent_(parent) { }
55 bool operator==(
const Trie& other)
const {
56 return (v == other.v);
59 void add_child(Trie* child) {
61 std::shared_ptr<Trie> ptr_to_add(child);
62 childs.push_back(ptr_to_add);
63 child->parent_ =
this;
67 typedef typename Simplex_handle::Simplex_vertex_const_iterator Simplex_vertex_const_iterator;
69 Trie* make_trie(Simplex_vertex_const_iterator s_it, Simplex_vertex_const_iterator s_end) {
73 Trie* res =
new Trie(*s_it);
74 Trie* child = make_trie(++s_it, s_end);
75 res->add_child(child);
83 void add_simplex_helper(Simplex_vertex_const_iterator s_it, Simplex_vertex_const_iterator s_end) {
86 if (s_it == s_end)
return;
88 for (
auto child : childs) {
89 if (child->v == *s_it)
90 return child->add_simplex_helper(s_it, s_end);
95 Trie * son_with_what_remains_of_s(make_trie(s_it, s_end));
96 add_child(son_with_what_remains_of_s);
100 void maximal_faces_helper(std::vector<Simplex_handle>& res)
const {
101 if (is_leaf()) res.push_back(simplex());
103 for (
auto child : childs)
104 child->maximal_faces_helper(res);
112 if (s.empty())
return;
114 add_simplex_helper(s.begin(), s.end());
117 std::vector<Simplex_handle> maximal_faces()
const {
118 std::vector<Simplex_handle> res;
119 maximal_faces_helper(res);
129 parent_->add_vertices_up_to_the_root(res);
134 add_vertices_up_to_the_root(res);
138 bool is_leaf()
const {
139 return childs.empty();
142 bool is_root()
const {
146 const Trie* parent() {
153 parent_->childs.erase(
this);
160 Trie
const* current =
this;
161 if (s.empty())
return true;
163 auto s_pos = s.begin();
165 while (s_pos != s.end() && current != 0) {
167 for (
const auto child : current->childs) {
168 if (child->v == *s_pos) {
170 current = child.get();
175 if (!found)
return false;
180 Trie* go_bottom_left() {
184 return (*childs.begin())->go_bottom_left();
187 friend std::ostream& operator<<(std::ostream& stream,
const Trie& trie) {
188 stream <<
"T( " << trie.v <<
" ";
189 for (
auto t : trie.childs)
196 template<
typename SimplexHandle>
198 typedef typename SimplexHandle::Vertex_handle Vertex_handle;
201 typedef Trie<Simplex_handle> STrie;
203 template<
typename SimpleHandleOutputIterator>
204 Tries(
unsigned num_vertices, SimpleHandleOutputIterator simplex_begin, SimpleHandleOutputIterator simplex_end) :
205 cofaces_(num_vertices, 0) {
206 for (
auto i = 0u; i < num_vertices; ++i)
207 cofaces_[i] =
new STrie(Vertex_handle(i));
208 for (
auto s_it = simplex_begin; s_it != simplex_end; ++s_it) {
209 if (s_it->dimension() >= 1)
210 cofaces_[s_it->first_vertex()]->add_simplex(*s_it);
215 for (STrie* t : cofaces_)
223 for (
auto child : cofaces_[v]->childs)
230 return cofaces_[first_v]->contains(s);
233 friend std::ostream& operator<<(std::ostream& stream,
const Tries& tries) {
234 for (
auto trie : tries.cofaces_)
235 stream << *trie << std::endl;
241 std::vector<Simplex_handle> next_dimension_simplices()
const {
242 std::vector<Simplex_handle> res;
243 while (!to_see_.empty() && to_see_.front()->simplex().dimension() == current_dimension_) {
244 res.emplace_back(to_see_.front()->simplex());
245 for (
auto child : to_see_.front()->childs)
246 to_see_.push_back(child.get());
249 ++current_dimension_;
253 void init_next_dimension()
const {
254 for (
auto trie : cofaces_)
255 to_see_.push_back(trie);
259 mutable std::deque<STrie*> to_see_;
260 mutable unsigned current_dimension_ = 0;
261 std::vector<STrie*> cofaces_;
268 #endif // SKELETON_BLOCKER_INTERNAL_TRIE_H_
void add_vertex(T v)
Definition: Skeleton_blocker_simplex.h:124
T first_vertex() const
Definition: Skeleton_blocker_simplex.h:225
Abstract simplex used in Skeleton blockers data-structure.
Definition: Skeleton_blocker_simplex.h:50