Gudhi  1.1.0
 All Classes Functions Typedefs Friends Groups Pages
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 Sophia Antipolis-Mediterranee (France)
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 GUDHI_SKELETON_BLOCKER_SIMPLEX_H
24 #define GUDHI_SKELETON_BLOCKER_SIMPLEX_H
25 
26 #include<cassert>
27 #include<iostream>
28 #include<set>
29 #include<vector>
30 #include <initializer_list>
31 
32 namespace Gudhi{
33 
34 namespace skbl {
35 
46 template <typename T>
47 
49 
50 private :
51  std::set<T> simplex_set;
52 
53 public:
54  typedef typename T::boost_vertex_handle boost_vertex_handle;
55 
56  typedef T Vertex_handle;
57 
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 
67 // Skeleton_blocker_simplex():simplex_set() {}
68 
72  void clear() {
73  simplex_set.clear();
74  }
75 
76 
77  Skeleton_blocker_simplex(std::initializer_list<T>& list) {
78  for_each(list.begin(),list.end(),add_vertex);
79  }
80 
81 
82  template<typename... Args>
83  Skeleton_blocker_simplex(Args... args){
84  add_vertices(args...);
85  }
86 
87 
88  template<typename... Args>
89  void add_vertices(T v,Args... args){
90  add_vertex(v);
91  add_vertices(args...);
92  }
93 
94  void add_vertices(T v){
95  add_vertex(v);
96  }
97 
98  void add_vertices(){
99  }
100 
104  Skeleton_blocker_simplex(std::string token){
105  clear();
106  if ((token[0] == '{') && (token[token.size()-1] == '}' ) )
107  {
108  token.erase(0,1);
109  token.erase(token.size()-1,1);
110  while(token.size()!=0 ){
111  int coma_position=token.find_first_of(',');
112  //cout << "coma_position:"<<coma_position<<endl;
113  std::string n = token.substr(0,coma_position);
114  //cout << "token:"<<token<<endl;
115  token.erase(0,n.size()+1);
116  add_vertex((T)(atoi(n.c_str())));
117  }
118  }
119  }
120 
122 
126 
133  void add_vertex(T v)
134  {
135  simplex_set.insert(v);
136  }
137 
142  void remove_vertex(T v)
143  {
144  simplex_set.erase(v);
145  }
146 
152  std::vector<T> v;
153  v.reserve(std::min(simplex_set.size(), a.simplex_set.size()));
154 
155  set_intersection(simplex_set.begin(),simplex_set.end(),
156  a.simplex_set.begin(),a.simplex_set.end(),
157  std::back_inserter(v));
158  clear();
159  for (auto i:v)
160  simplex_set.insert(i);
161  }
162 
168  std::vector<T> v;
169  v.reserve(simplex_set.size());
170 
171  set_difference(simplex_set.begin(),simplex_set.end(),
172  a.simplex_set.begin(),a.simplex_set.end(),
173  std::back_inserter(v));
174 
175  clear();
176  for (auto i:v)
177  simplex_set.insert(i);
178  }
179 
185  std::vector<T> v;
186  v.reserve(simplex_set.size() + a.simplex_set.size());
187 
188  set_union(simplex_set.begin(),simplex_set.end(),
189  a.simplex_set.begin(),a.simplex_set.end(),
190  std::back_inserter(v));
191  clear();
192  simplex_set.insert(v.begin(),v.end());
193  }
194 
195  typename std::set<T>::const_iterator begin() const{
196  return simplex_set.cbegin();
197  }
198 
199  typename std::set<T>::const_iterator end() const{
200  return simplex_set.cend();
201  }
202 
203  typename std::set<T>::iterator begin(){
204  return simplex_set.begin();
205  }
206 
207  typename std::set<T>::iterator end(){
208  return simplex_set.end();
209  }
210 
211 
212 
214 
218 
222  int dimension() const{
223  return (simplex_set.size() - 1);
224  }
225 
226  bool empty() const{
227  return simplex_set.empty();
228  }
229 
235  T first_vertex() const{
236  assert(!empty());
237  return *(begin());
238  }
239 
245  T last_vertex() const
246  {
247  assert(!empty());
248  return *(simplex_set.rbegin());
249  }
253  bool contains(const Skeleton_blocker_simplex & a) const{
254  return includes(simplex_set.cbegin(),simplex_set.cend(),a.simplex_set.cbegin(),a.simplex_set.cend());
255  }
256 
261  auto first1 = begin();
262  auto last1 = end();
263 
264  auto first2 = a.begin();
265  auto last2 = a.end();
266 
267  while (first2!=last2) {
268  // we ignore vertices of b
269  if(b.contains(*first2)){
270  ++first2;
271  }
272  else{
273  if ( (first1==last1) || (*first2<*first1) ) return false;
274  if (!(*first1<*first2)) ++first2;
275  ++first1;
276  }
277  }
278  return true;
279  }
280 
285  auto first1 = begin();
286  auto last1 = end();
287 
288  auto first2 = a.begin();
289  auto last2 = a.end();
290 
291  while (first2!=last2) {
292  // we ignore vertices of b
293  if(x == *first2){
294  ++first2;
295  }
296  else{
297  if ( (first1==last1) || (*first2<*first1) ) return false;
298  if (!(*first1<*first2)) ++first2;
299  ++first1;
300  }
301  }
302  return true;
303  }
304 
308  bool contains_difference(const Skeleton_blocker_simplex& a, T x, T y) const{
309  auto first1 = begin();
310  auto last1 = end();
311 
312  auto first2 = a.begin();
313  auto last2 = a.end();
314 
315  while (first2!=last2) {
316  // we ignore vertices of b
317  if(x == *first2 || y == *first2){
318  ++first2;
319  }
320  else{
321  if ( (first1==last1) || (*first2<*first1) ) return false;
322  if (!(*first1<*first2)) ++first2;
323  ++first1;
324  }
325  }
326  return true;
327  }
328 
329 
333  bool contains(T v) const{
334  return (simplex_set.find(v) != simplex_set.end());
335  }
336 
340  bool disjoint(const Skeleton_blocker_simplex& a) const{
341  std::vector<T> v;
342  v.reserve(std::min(simplex_set.size(), a.simplex_set.size()));
343 
344  set_intersection(simplex_set.cbegin(),simplex_set.cend(),
345  a.simplex_set.cbegin(),a.simplex_set.cend(),
346  std::back_inserter(v));
347 
348  return (v.size()==0);
349  }
350 
351 
352  bool operator==(const Skeleton_blocker_simplex& other) const{
353  return (this->simplex_set == other.simplex_set);
354  }
355 
356  bool operator!=(const Skeleton_blocker_simplex& other) const{
357  return (this->simplex_set != other.simplex_set);
358  }
359 
360  bool operator<(const Skeleton_blocker_simplex& other) const{
361  return (std::lexicographical_compare(this->simplex_set.begin(),this->simplex_set.end(),
362  other.begin(),other.end()));
363  }
364 
366 
367 
368 
369 
370 
374  friend std::ostream& operator << (std::ostream& o, const Skeleton_blocker_simplex & sigma)
375  {
376  bool first = true;
377  o << "{";
378  for(auto i : sigma)
379  {
380  if(first) first = false ;
381  else o << ",";
382  o << i;
383  }
384  o << "}";
385  return o;
386  }
387 
388 
389 };
390 
391 }
392 
393 } // namespace GUDHI
394 
395 #endif
396 
397 
friend std::ostream & operator<<(std::ostream &o, const Skeleton_blocker_simplex &sigma)
Definition: Skeleton_blocker_simplex.h:374
void add_vertex(T v)
Definition: Skeleton_blocker_simplex.h:133
bool disjoint(const Skeleton_blocker_simplex &a) const
Definition: Skeleton_blocker_simplex.h:340
void union_vertices(const Skeleton_blocker_simplex &a)
Definition: Skeleton_blocker_simplex.h:184
void difference(const Skeleton_blocker_simplex &a)
Definition: Skeleton_blocker_simplex.h:167
void remove_vertex(T v)
Definition: Skeleton_blocker_simplex.h:142
T last_vertex() const
Definition: Skeleton_blocker_simplex.h:245
T first_vertex() const
Definition: Skeleton_blocker_simplex.h:235
Skeleton_blocker_simplex(std::string token)
Definition: Skeleton_blocker_simplex.h:104
bool contains(const Skeleton_blocker_simplex &a) const
Definition: Skeleton_blocker_simplex.h:253
bool contains_difference(const Skeleton_blocker_simplex &a, T x, T y) const
Definition: Skeleton_blocker_simplex.h:308
bool contains(T v) const
Definition: Skeleton_blocker_simplex.h:333
Abstract simplex used in Skeleton blockers data-structure.
Definition: Skeleton_blocker_simplex.h:48
void intersection(const Skeleton_blocker_simplex &a)
Definition: Skeleton_blocker_simplex.h:151
void clear()
Definition: Skeleton_blocker_simplex.h:72
int dimension() const
Definition: Skeleton_blocker_simplex.h:222
bool contains_difference(const Skeleton_blocker_simplex &a, T x) const
Definition: Skeleton_blocker_simplex.h:284
bool contains_difference(const Skeleton_blocker_simplex &a, const Skeleton_blocker_simplex &b) const
Definition: Skeleton_blocker_simplex.h:260