Gudhi  1.1.0
 All Classes Functions Typedefs Friends Groups Pages
Persistent_cohomology_column.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): Clément Maria
6  *
7  * Copyright (C) 2014 INRIA Sophia Antipolis-Méditerranée (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 SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_PERSISTENT_COHOMOLOGY_COLUMN_H_
24 #define SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_PERSISTENT_COHOMOLOGY_COLUMN_H_
25 
26 #include <list>
27 
28 #include "boost/tuple/tuple.hpp"
29 #include "boost/intrusive/set.hpp"
30 #include "boost/intrusive/list.hpp"
31 
32 namespace Gudhi {
33 
34 namespace persistent_cohomology {
35 
36 template<typename SimplexKey, typename ArithmeticElement>
37 class Persistent_cohomology_column;
38 
39 struct cam_h_tag;
40 // for horizontal traversal in the CAM
41 struct cam_v_tag;
42 // for vertical traversal in the CAM
43 
44 typedef boost::intrusive::list_base_hook<boost::intrusive::tag<cam_h_tag>,
45  boost::intrusive::link_mode<boost::intrusive::auto_unlink> // allows .unlink()
46 > base_hook_cam_h;
47 
48 typedef boost::intrusive::list_base_hook<boost::intrusive::tag<cam_v_tag>,
49  boost::intrusive::link_mode<boost::intrusive::normal_link> // faster hook, less safe
50 > base_hook_cam_v;
51 
56 template<typename SimplexKey, typename ArithmeticElement>
57 class Persistent_cohomology_cell : public base_hook_cam_h,
58  public base_hook_cam_v {
59  public:
60  template<class T1, class T2> friend class Persistent_cohomology;
61  friend class Persistent_cohomology_column<SimplexKey, ArithmeticElement>;
62 
63  typedef Persistent_cohomology_column<SimplexKey, ArithmeticElement> Column;
64 
65  Persistent_cohomology_cell(SimplexKey key, ArithmeticElement x,
66  Column * self_col)
67  : key_(key),
68  coefficient_(x),
69  self_col_(self_col) {
70  }
71 
72  SimplexKey key_;
73  ArithmeticElement coefficient_;
74  Column * self_col_;
75 };
76 
77 /*
78  * \brief Sparse column for the Compressed Annotation Matrix.
79  *
80  * The non-zero coefficients of the column are stored in a
81  * boost::intrusive::list. Contains a hook to be stored in a
82  * boost::intrusive::set.
83  */
84 template<typename SimplexKey, typename ArithmeticElement>
85 class Persistent_cohomology_column : public boost::intrusive::set_base_hook<
86  boost::intrusive::link_mode<boost::intrusive::normal_link> > {
87  private:
88  template<class T1, class T2> friend class Persistent_cohomology;
89 
90  typedef Persistent_cohomology_cell<SimplexKey, ArithmeticElement> Cell;
91  typedef boost::intrusive::list<Cell,
92  boost::intrusive::constant_time_size<false>,
93  boost::intrusive::base_hook<base_hook_cam_v> > Col_type;
94 
96  explicit Persistent_cohomology_column(SimplexKey key) {
97  class_key_ = key;
98  col_ = Col_type();
99  }
100  public:
102  Persistent_cohomology_column(Persistent_cohomology_column const &other)
103  : col_(),
104  class_key_(other.class_key_) {
105  if (!other.col_.empty())
106  std::cerr << "Copying a non-empty column.\n";
107  }
108 
110  bool is_null() {
111  return col_.empty();
112  }
116  SimplexKey class_key() {
117  return class_key_;
118  }
119 
121  friend bool operator<(const Persistent_cohomology_column& c1,
122  const Persistent_cohomology_column& c2) {
123  typename Col_type::const_iterator it1 = c1.col_.begin();
124  typename Col_type::const_iterator it2 = c2.col_.begin();
125  while (it1 != c1.col_.end() && it2 != c2.col_.end()) {
126  if (it1->key_ == it2->key_) {
127  if (it1->coefficient_ == it2->coefficient_) {
128  ++it1;
129  ++it2;
130  } else {
131  return it1->coefficient_ < it2->coefficient_;
132  }
133  } else {
134  return it1->key_ < it2->key_;
135  }
136  }
137  return (it2 != c2.col_.end());
138  }
139 
140  Col_type col_;
141  SimplexKey class_key_;
142 };
143 
144 } // namespace persistent_cohomology
145 
146 } // namespace Gudhi
147 
148 #endif // SRC_PERSISTENT_COHOMOLOGY_INCLUDE_GUDHI_PERSISTENT_COHOMOLOGY_PERSISTENT_COHOMOLOGY_COLUMN_H_
Key type used as simplex identifier.
Definition: SimplexKey.h:27