Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
StrongIndexPermutation.h
Go to the documentation of this file.
1 
7 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_STRONG_INDEX_PERMUTATION_H
8 #define INCLUDE_MOLASSEMBLER_TEMPLE_STRONG_INDEX_PERMUTATION_H
9 
11 
12 namespace Scine {
13 namespace Molassembler {
14 namespace Temple {
15 
16 template<typename Key, typename Value>
17 struct StrongIndexPermutation : public Crtp::LexicographicComparable<StrongIndexPermutation<Key, Value>> {
20  using key_type = Key;
21  using value_type = Value;
22 
24  using base_iterator = std::vector<unsigned>::const_iterator;
25  using iterator_category = base_iterator::iterator_category;
26  using value_type = std::pair<Key, Value>;
27 
28  Iterator(base_iterator a, base_iterator b) : begin(std::move(a)), iter(std::move(b)) {}
29 
30  value_type operator * () const {
31  return {
32  Key {static_cast<typename Key::value_type>(iter - begin)},
33  Value {*iter}
34  };
35  }
36 
37  Iterator& operator ++ () {
38  ++iter;
39  return *this;
40  }
41 
42  Iterator operator ++ (int) {
43  Iterator copy = *this;
44  ++(*this);
45  return copy;
46  }
47 
48  auto tie() const {
49  return std::tie(iter);
50  }
51 
52  base_iterator begin;
53  base_iterator iter;
54  };
55 
56  using iterator = Iterator;
57  using const_iterator = Iterator;
59 
62  StrongIndexPermutation() = default;
63 
64  explicit StrongIndexPermutation(Permutation p) : permutation(std::move(p)) {}
65 
66  explicit StrongIndexPermutation(Permutation::Sigma p) : permutation(std::move(p)) {}
67 
68  template<typename T, std::enable_if_t<std::is_convertible<T, unsigned>::value, int>* = nullptr>
69  explicit StrongIndexPermutation(std::initializer_list<T> vs) : permutation(std::move(vs)) {}
71 
74  template<typename Container>
75  static std::enable_if_t<!std::is_same<Container, Permutation::Sigma>::value, StrongIndexPermutation> from(const Container& p) {
76  static_assert(
77  std::is_convertible<Traits::getValueType<Container>, unsigned>::value,
78  "Value type of container must be convertible to unsigned!"
79  );
80  Permutation::Sigma sigma;
81  for(auto v : p) {
82  sigma.emplace_back(static_cast<unsigned>(v));
83  }
84  return StrongIndexPermutation {Permutation {std::move(sigma)}};
85  }
87 
90  Value at(const Key i) const {
91  return Value {permutation.at(i)};
92  }
93 
94  Value operator() (const Key i) const {
95  return Value {permutation.at(i)};
96  }
97 
98  Key indexOf(const Value v) const {
99  return Key {permutation.indexOf(v)};
100  }
101 
102  unsigned size() const {
103  return permutation.size();
104  }
105 
106  StrongIndexPermutation<Value, Key> inverse() const {
107  return StrongIndexPermutation<Value, Key> {permutation.inverse()};
108  }
109 
110  template<typename OtherValue>
112  compose(const StrongIndexPermutation<Value, OtherValue>& other) const {
113  return StrongIndexPermutation<Key, OtherValue> {permutation.compose(other.permutation)};
114  }
115 
117  return StrongIndexPermutation {permutation.compose(other.permutation)};
118  }
119 
120  auto tie() const {
121  return std::tie(permutation);
122  }
124 
127  void clear() {
128  permutation.clear();
129  }
131 
134  Iterator begin() const {
135  return Iterator {
136  std::begin(permutation.sigma),
137  std::begin(permutation.sigma)
138  };
139  }
140 
141  Iterator end() const {
142  return Iterator {
143  std::begin(permutation.sigma),
144  std::end(permutation.sigma)
145  };
146  }
148 
149  Permutation permutation;
150 };
151 
152 } // namespace Temple
153 } // namespace Molassembler
154 } // namespace Scine
155 
156 #endif
Container-abstracted permutation.
Definition: Permutations.h:287
Generates all operators using a method returning a tuple.
Definition: OperatorSuppliers.h:78
Provides functionality related to permutations.
Definition: StrongIndexPermutation.h:17