Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Cycles.h
Go to the documentation of this file.
1 
11 #ifndef INCLUDE_MOLASSEMBLER_CYCLES_H
12 #define INCLUDE_MOLASSEMBLER_CYCLES_H
13 
14 #include "boost/functional/hash.hpp"
15 #include "boost/optional/optional_fwd.hpp"
16 
18 #include "Molassembler/Types.h"
19 
20 #include <functional>
21 #include <unordered_map>
22 
23 struct RDL_data;
24 
25 namespace Scine {
26 namespace Molassembler {
27 
28 // Forward-declarations
29 class Graph;
30 class PrivateGraph;
31 
44 class MASM_EXPORT Cycles {
45 public:
52  struct RdlDataPtrs;
53 
54 
62  struct RdlCyclePtrs;
63 
66  public:
67  // Iterator traits
68  using iterator_category = std::forward_iterator_tag;
69  using difference_type = std::ptrdiff_t;
70  using value_type = const std::vector<BondIndex>&;
71  using pointer = const std::vector<BondIndex>*;
72  using reference = value_type;
73 
74  AllCyclesIterator() = delete;
76  const std::shared_ptr<RdlDataPtrs>& dataPtr,
77  unsigned rCycleIndex = 0
78  );
79 
80  /* Rule of five members */
82  AllCyclesIterator(AllCyclesIterator&& other) noexcept;
83  AllCyclesIterator& operator = (const AllCyclesIterator& other);
84  AllCyclesIterator& operator = (AllCyclesIterator&& other) noexcept;
86 
87  AllCyclesIterator& operator ++ ();
88  AllCyclesIterator operator ++ (int);
89  value_type operator * () const;
90  pointer operator -> () const;
91 
92 
94  bool operator == (const AllCyclesIterator& other) const;
95  bool operator != (const AllCyclesIterator& other) const;
96 
97  private:
99  std::shared_ptr<RdlDataPtrs> rdlPtr_;
101  std::unique_ptr<RdlCyclePtrs> cyclePtr_;
102  };
103 
106  public:
107  using difference_type = unsigned;
108  using value_type = const std::vector<BondIndex>&;
109  using pointer = const std::vector<BondIndex>*;
110  using reference = value_type;
111  using iterator_category = std::forward_iterator_tag;
112 
113  /* Rule of five members */
115  UrfIdsCycleIterator(UrfIdsCycleIterator&& other) noexcept;
116  UrfIdsCycleIterator& operator = (const UrfIdsCycleIterator& other);
117  UrfIdsCycleIterator& operator = (UrfIdsCycleIterator&& other) noexcept;
119 
120  UrfIdsCycleIterator() = delete;
121 
122  /* Constructors */
124  AtomIndex soughtIndex,
125  const std::shared_ptr<RdlDataPtrs>& dataPtr
126  );
127 
129  const BondIndex& soughtBond,
130  std::vector<unsigned> urfs,
131  const std::shared_ptr<RdlDataPtrs>& dataPtr
132  );
133 
135  const std::vector<BondIndex>& soughtBonds,
136  std::vector<unsigned> urfs,
137  const std::shared_ptr<RdlDataPtrs>& dataPtr
138  );
139 
140  UrfIdsCycleIterator& operator ++ ();
141  UrfIdsCycleIterator operator ++ (int);
142  value_type operator * () const;
143  pointer operator -> () const;
144 
145  void advanceToEnd();
146 
147  bool operator == (const UrfIdsCycleIterator& other) const;
148  bool operator != (const UrfIdsCycleIterator& other) const;
149 
150  private:
151  struct UrfHelper;
152 
153  std::shared_ptr<RdlDataPtrs> rdlPtr_;
154  std::unique_ptr<UrfHelper> urfsPtr_;
155  std::unique_ptr<RdlCyclePtrs> cyclePtr_;
156 
157  void advanceToNextPermissibleCycle_();
158  void initializeCyclesFromUrfId_();
159  void matchCycleState_(const UrfIdsCycleIterator& other);
160  };
161 
164 
168  Cycles(const Graph& sourceGraph, bool ignoreEtaBonds = true);
170  Cycles(const PrivateGraph& innerGraph, bool ignoreEtaBonds = true);
172 
175 
179  unsigned numCycleFamilies() const;
180 
186  unsigned numCycleFamilies(AtomIndex index) const;
187 
193  unsigned numCycleFamilies(const BondIndex& bond) const;
194 
199  unsigned numRelevantCycles() const;
200 
206  unsigned numRelevantCycles(AtomIndex index) const;
207 
213  unsigned numRelevantCycles(const BondIndex& bond) const;
214 
216  RDL_data* dataPtr() const;
218 
221  AllCyclesIterator begin() const;
222  AllCyclesIterator end() const;
224 
227 
232  IteratorRange<UrfIdsCycleIterator> containing(AtomIndex atom) const;
237  IteratorRange<UrfIdsCycleIterator> containing(const BondIndex& bond) const;
243  IteratorRange<UrfIdsCycleIterator> containing(const std::vector<BondIndex>& bonds) const;
245 
249  bool operator == (const Cycles& other) const;
250  bool operator != (const Cycles& other) const;
252 
253 private:
254  std::shared_ptr<RdlDataPtrs> rdlPtr_;
255  // Map from BondIndex to ordered list of its URF IDs
256  std::unordered_map<BondIndex, std::vector<unsigned>, boost::hash<BondIndex>> urfMap_;
257 };
258 
267 MASM_EXPORT boost::optional<unsigned> smallestCycleContaining(AtomIndex atom, const Cycles& cycles);
268 
278 MASM_EXPORT std::unordered_map<AtomIndex, unsigned> makeSmallestCycleMap(const Cycles& cycleData);
279 
287 MASM_EXPORT std::vector<AtomIndex> makeRingIndexSequence(
288  std::vector<BondIndex> edgeDescriptors
289 );
290 
295 MASM_EXPORT std::vector<AtomIndex> centralizeRingIndexSequence(
296  std::vector<AtomIndex> ringIndexSequence,
297  AtomIndex center
298 );
299 
307 MASM_EXPORT unsigned countPlanarityEnforcingBonds(
308  const std::vector<BondIndex>& edgeSet,
309  const Graph& graph
310 );
311 
312 } // namespace Molassembler
313 } // namespace Scine
314 
315 #endif
std::unordered_map< AtomIndex, unsigned > makeSmallestCycleMap(const Cycles &cycleData)
Creates a mapping from atom index to the size of the smallest cycle containing that index...
Defines basic types widely shared across the project.
Represents the connectivity of atoms of a molecule.
Definition: Graph.h:54
Iterator for cycles of specific universal ring families.
Definition: Cycles.h:105
Wrapper class to make working with RDL in C++ more pleasant.
Definition: Cycles.h:44
Homogeneous pair of iterators with begin and end member fns.
Definition: IteratorRange.h:26
Range type.
Iterator for all relevant cycles of the graph.
Definition: Cycles.h:65
std::size_t AtomIndex
Unsigned integer atom index type. Used to refer to particular atoms.
Definition: Types.h:51
Library internal graph class wrapping BGL types.
Definition: PrivateGraph.h:26
std::vector< AtomIndex > centralizeRingIndexSequence(std::vector< AtomIndex > ringIndexSequence, AtomIndex center)
Centralize a cycle vertex sequence at a particular vertex.
Type used to refer to particular bonds. Orders first &lt; second.
Definition: Types.h:54
std::shared_ptr< RdlDataPtrs > rdlPtr_
Hold an owning reference to the base data to avoid dangling pointers.
Definition: Cycles.h:99
unsigned countPlanarityEnforcingBonds(const std::vector< BondIndex > &edgeSet, const Graph &graph)
Count the number of planarity enforcing bonds.
boost::optional< unsigned > smallestCycleContaining(AtomIndex atom, const Cycles &cycles)
Yields the size of the smallest cycle containing an atom.
std::unique_ptr< RdlCyclePtrs > cyclePtr_
Manage cycle data as shared pointer to permit expected iterator functionality.
Definition: Cycles.h:101
std::vector< AtomIndex > makeRingIndexSequence(std::vector< BondIndex > edgeDescriptors)
Create cycle vertex sequence from unordered edges.