Skeleton_blocker_simplex.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): David Salinas
6  *
7  * Copyright (C) 2014 Inria
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef SKELETON_BLOCKER_SKELETON_BLOCKER_SIMPLEX_H_
24 #define SKELETON_BLOCKER_SKELETON_BLOCKER_SIMPLEX_H_
25 
26 #include <cassert>
27 #include <iostream>
28 #include <set>
29 #include <vector>
30 #include <initializer_list>
31 #include <string>
32 #include <algorithm>
33 
34 namespace Gudhi {
35 
36 namespace skeleton_blocker {
37 
48 template<typename T>
49 
51  private:
52  std::set<T> simplex_set;
53 
54  public:
55  typedef typename T::boost_vertex_handle boost_vertex_handle;
56 
57  typedef T Vertex_handle;
58 
59  typedef typename std::set<T>::const_iterator Simplex_vertex_const_iterator;
60  typedef typename std::set<T>::iterator Simplex_vertex_iterator;
61 
65 
66  void clear() {
67  simplex_set.clear();
68  }
69 
70  Skeleton_blocker_simplex(std::initializer_list<T>& list) {
71  std::for_each(list.begin(), list.end(), [&] (T const& elt) {
72  add_vertex(elt);
73  });
74  }
75 
76  template<typename ... Args>
77  explicit Skeleton_blocker_simplex(Args ... args) {
78  add_vertices(args...);
79  }
80 
81  template<typename ... Args>
82  void add_vertices(T v, Args ... args) {
83  add_vertex(v);
84  add_vertices(args...);
85  }
86 
87  void add_vertices(T v) {
88  add_vertex(v);
89  }
90 
91  void add_vertices() { }
92 
96  explicit Skeleton_blocker_simplex(std::string token) {
97  clear();
98  if ((token[0] == '{') && (token[token.size() - 1] == '}')) {
99  token.erase(0, 1);
100  token.erase(token.size() - 1, 1);
101  while (token.size() != 0) {
102  int coma_position = token.find_first_of(',');
103  // cout << "coma_position:"<<coma_position<<endl;
104  std::string n = token.substr(0, coma_position);
105  // cout << "token:"<<token<<endl;
106  token.erase(0, n.size() + 1);
107  add_vertex((T) (atoi(n.c_str())));
108  }
109  }
110  }
111 
113 
122  void add_vertex(T v) {
123  simplex_set.insert(v);
124  }
125 
129  void remove_vertex(T v) {
130  simplex_set.erase(v);
131  }
132 
137  std::vector<T> v;
138  v.reserve((std::min)(simplex_set.size(), a.simplex_set.size()));
139 
140  set_intersection(simplex_set.begin(), simplex_set.end(),
141  a.simplex_set.begin(), a.simplex_set.end(),
142  std::back_inserter(v));
143  clear();
144  for (auto i : v)
145  simplex_set.insert(i);
146  }
147 
152  std::vector<T> v;
153  v.reserve(simplex_set.size());
154 
155  set_difference(simplex_set.begin(), simplex_set.end(),
156  a.simplex_set.begin(), a.simplex_set.end(),
157  std::back_inserter(v));
158 
159  clear();
160  for (auto i : v)
161  simplex_set.insert(i);
162  }
163 
168  std::vector<T> v;
169  v.reserve(simplex_set.size() + a.simplex_set.size());
170 
171  set_union(simplex_set.begin(), simplex_set.end(), a.simplex_set.begin(),
172  a.simplex_set.end(), std::back_inserter(v));
173  clear();
174  simplex_set.insert(v.begin(), v.end());
175  }
176 
177  typename std::set<T>::const_iterator begin() const {
178  return simplex_set.cbegin();
179  }
180 
181  typename std::set<T>::const_iterator end() const {
182  return simplex_set.cend();
183  }
184 
185  typename std::set<T>::const_reverse_iterator rbegin() const {
186  return simplex_set.crbegin();
187  }
188 
189  typename std::set<T>::const_reverse_iterator rend() const {
190  return simplex_set.crend();
191  }
192 
193  typename std::set<T>::iterator begin() {
194  return simplex_set.begin();
195  }
196 
197  typename std::set<T>::iterator end() {
198  return simplex_set.end();
199  }
200 
202 
209  int dimension() const {
210  return (simplex_set.size() - 1);
211  }
212 
213  bool empty() const {
214  return simplex_set.empty();
215  }
216 
222  T first_vertex() const {
223  assert(!empty());
224  return *(begin());
225  }
226 
232  T last_vertex() const {
233  assert(!empty());
234  return *(simplex_set.rbegin());
235  }
236 
240  bool contains(const Skeleton_blocker_simplex & a) const {
241  return includes(simplex_set.cbegin(), simplex_set.cend(),
242  a.simplex_set.cbegin(), a.simplex_set.cend());
243  }
244 
249  const Skeleton_blocker_simplex& b) const {
250  auto first1 = begin();
251  auto last1 = end();
252 
253  auto first2 = a.begin();
254  auto last2 = a.end();
255 
256  while (first2 != last2) {
257  // we ignore vertices of b
258  if (b.contains(*first2)) {
259  ++first2;
260  } else {
261  if ((first1 == last1) || (*first2 < *first1))
262  return false;
263  if (!(*first1 < *first2))
264  ++first2;
265  ++first1;
266  }
267  }
268  return true;
269  }
270 
274  bool contains_difference(const Skeleton_blocker_simplex& a, T x) const {
275  auto first1 = begin();
276  auto last1 = end();
277 
278  auto first2 = a.begin();
279  auto last2 = a.end();
280 
281  while (first2 != last2) {
282  // we ignore vertices x
283  if (x == *first2) {
284  ++first2;
285  } else {
286  if ((first1 == last1) || (*first2 < *first1))
287  return false;
288  if (!(*first1 < *first2))
289  ++first2;
290  ++first1;
291  }
292  }
293  return true;
294  }
295 
299  bool contains_difference(const Skeleton_blocker_simplex& a, T x, T y) const {
300  auto first1 = begin();
301  auto last1 = end();
302 
303  auto first2 = a.begin();
304  auto last2 = a.end();
305 
306  while (first2 != last2) {
307  // we ignore vertices of x,y
308  if (x == *first2 || y == *first2) {
309  ++first2;
310  } else {
311  if ((first1 == last1) || (*first2 < *first1))
312  return false;
313  if (!(*first1 < *first2))
314  ++first2;
315  ++first1;
316  }
317  }
318  return true;
319  }
320 
321  bool contains(T v) const {
322  return (simplex_set.find(v) != simplex_set.end());
323  }
324 
325  bool disjoint(const Skeleton_blocker_simplex& a) const {
326  std::vector<T> v;
327  v.reserve(std::min(simplex_set.size(), a.simplex_set.size()));
328 
329  set_intersection(simplex_set.cbegin(), simplex_set.cend(),
330  a.simplex_set.cbegin(), a.simplex_set.cend(),
331  std::back_inserter(v));
332 
333  return (v.size() == 0);
334  }
335 
336  bool operator==(const Skeleton_blocker_simplex& other) const {
337  return (this->simplex_set == other.simplex_set);
338  }
339 
340  bool operator!=(const Skeleton_blocker_simplex& other) const {
341  return (this->simplex_set != other.simplex_set);
342  }
343 
344  bool operator<(const Skeleton_blocker_simplex& other) const {
345  return (std::lexicographical_compare(this->simplex_set.begin(),
346  this->simplex_set.end(), other.begin(),
347  other.end()));
348  }
349 
351 
352  friend std::ostream& operator<<(std::ostream& o,
353  const Skeleton_blocker_simplex & sigma) {
354  bool first = true;
355  o << "{";
356  for (auto i : sigma) {
357  if (first)
358  first = false;
359  else
360  o << ",";
361  o << i;
362  }
363  o << "}";
364  return o;
365  }
366 };
367 
368 } // namespace skeleton_blocker
369 
370 namespace skbl = skeleton_blocker;
371 
372 } // namespace Gudhi
373 
374 #endif // SKELETON_BLOCKER_SKELETON_BLOCKER_SIMPLEX_H_
T last_vertex() const
Definition: Skeleton_blocker_simplex.h:232
Abstract simplex used in Skeleton blockers data-structure.
Definition: Skeleton_blocker_simplex.h:50
void add_vertex(T v)
Definition: Skeleton_blocker_simplex.h:122
bool contains_difference(const Skeleton_blocker_simplex &a, const Skeleton_blocker_simplex &b) const
Definition: Skeleton_blocker_simplex.h:248
int dimension() const
Definition: Skeleton_blocker_simplex.h:209
void difference(const Skeleton_blocker_simplex &a)
Definition: Skeleton_blocker_simplex.h:151
void remove_vertex(T v)
Definition: Skeleton_blocker_simplex.h:129
bool contains_difference(const Skeleton_blocker_simplex &a, T x, T y) const
Definition: Skeleton_blocker_simplex.h:299
bool contains_difference(const Skeleton_blocker_simplex &a, T x) const
Definition: Skeleton_blocker_simplex.h:274
Definition: SimplicialComplexForAlpha.h:26
bool contains(const Skeleton_blocker_simplex &a) const
Definition: Skeleton_blocker_simplex.h:240
Skeleton_blocker_simplex(std::string token)
Definition: Skeleton_blocker_simplex.h:96
void intersection(const Skeleton_blocker_simplex &a)
Definition: Skeleton_blocker_simplex.h:136
void union_vertices(const Skeleton_blocker_simplex &a)
Definition: Skeleton_blocker_simplex.h:167
T first_vertex() const
Definition: Skeleton_blocker_simplex.h:222
GUDHI  Version 2.2.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu Jun 14 2018 15:00:55 for GUDHI by Doxygen 1.8.13