11#ifndef SKELETON_BLOCKER_INTERNAL_TRIE_H_
12#define SKELETON_BLOCKER_INTERNAL_TRIE_H_
21namespace skeleton_blocker {
23template<
typename SimplexHandle>
25 typedef SimplexHandle Simplex;
29 std::vector<std::shared_ptr<Trie> > childs;
35 Trie() : parent_(0) { }
39 Trie(
Vertex_handle v_, Trie* parent) : v(v_), parent_(parent) { }
41 bool operator==(
const Trie& other)
const {
42 return (v == other.v);
45 void add_child(Trie* child) {
47 std::shared_ptr<Trie> ptr_to_add(child);
48 childs.push_back(ptr_to_add);
49 child->parent_ =
this;
53 typedef typename Simplex::Simplex_vertex_const_iterator Simplex_vertex_const_iterator;
55 Trie* make_trie(Simplex_vertex_const_iterator s_it, Simplex_vertex_const_iterator s_end) {
59 Trie* res =
new Trie(*s_it);
60 Trie* child = make_trie(++s_it, s_end);
61 res->add_child(child);
69 void add_simplex_helper(Simplex_vertex_const_iterator s_it, Simplex_vertex_const_iterator s_end) {
72 if (s_it == s_end)
return;
74 for (
auto child : childs) {
75 if (child->v == *s_it)
76 return child->add_simplex_helper(s_it, s_end);
81 Trie * son_with_what_remains_of_s(make_trie(s_it, s_end));
82 add_child(son_with_what_remains_of_s);
86 void maximal_faces_helper(std::vector<Simplex>& res)
const {
87 if (is_leaf()) res.push_back(simplex());
89 for (
auto child : childs)
90 child->maximal_faces_helper(res);
97 void add_simplex(
const Simplex& s) {
98 if (s.empty())
return;
99 assert(v == s.first_vertex());
100 add_simplex_helper(s.begin(), s.end());
103 std::vector<Simplex> maximal_faces()
const {
104 std::vector<Simplex> res;
105 maximal_faces_helper(res);
112 void add_vertices_up_to_the_root(Simplex& res)
const {
115 parent_->add_vertices_up_to_the_root(res);
118 Simplex simplex()
const {
120 add_vertices_up_to_the_root(res);
124 bool is_leaf()
const {
125 return childs.empty();
128 bool is_root()
const {
132 const Trie* parent() {
139 parent_->childs.erase(
this);
145 bool contains(
const Simplex& s)
const {
146 Trie
const* current =
this;
147 if (s.empty())
return true;
148 if (current->v != s.first_vertex())
return false;
149 auto s_pos = s.begin();
151 while (s_pos != s.end() && current != 0) {
153 for (
const auto child : current->childs) {
154 if (child->v == *s_pos) {
156 current = child.get();
161 if (!found)
return false;
166 Trie* go_bottom_left() {
170 return (*childs.begin())->go_bottom_left();
173 friend std::ostream&
operator<<(std::ostream& stream,
const Trie& trie) {
174 stream <<
"T( " << trie.v <<
" ";
175 for (
auto t : trie.childs)
182template<
typename SimplexHandle>
185 typedef SimplexHandle Simplex;
187 typedef Trie<Simplex> STrie;
189 template<
typename SimpleHandleOutputIterator>
190 Tries(
unsigned num_vertices, SimpleHandleOutputIterator simplex_begin, SimpleHandleOutputIterator simplex_end) :
191 cofaces_(num_vertices, 0) {
192 for (
auto i = 0u; i < num_vertices; ++i)
194 for (
auto s_it = simplex_begin; s_it != simplex_end; ++s_it) {
195 if (s_it->dimension() >= 1)
196 cofaces_[s_it->first_vertex()]->add_simplex(*s_it);
201 for (STrie* t : cofaces_)
209 for (
auto child : cofaces_[v]->childs)
210 res.add_vertex(child->v);
214 bool contains(
const Simplex& s)
const {
215 auto first_v = s.first_vertex();
216 return cofaces_[first_v]->contains(s);
219 friend std::ostream&
operator<<(std::ostream& stream,
const Tries& tries) {
220 for (
auto trie : tries.cofaces_)
221 stream << *trie << std::endl;
227 std::vector<Simplex> next_dimension_simplices()
const {
228 std::vector<Simplex> res;
229 while (!(to_see_.empty()) && (to_see_.front()->simplex().dimension() == current_dimension_)) {
230 res.emplace_back(to_see_.front()->simplex());
231 for (
auto child : to_see_.front()->childs)
232 to_see_.push_back(child.get());
235 ++current_dimension_;
239 void init_next_dimension()
const {
240 for (
auto trie : cofaces_)
241 to_see_.push_back(trie);
245 mutable std::deque<STrie*> to_see_;
246 mutable int current_dimension_ = 0;
247 std::vector<STrie*> cofaces_;
252namespace skbl = skeleton_blocker;
std::ostream & operator<<(std::ostream &os, const Permutahedral_representation< Vertex, OrderedSetPartition > &simplex)
Print a permutahedral representation to a stream.
Definition: Permutahedral_representation.h:173
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15