Molassembler  1.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
StrongIndexMap.h
Go to the documentation of this file.
1 
7 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_STRONG_INDEX_MAP_H
8 #define INCLUDE_MOLASSEMBLER_TEMPLE_STRONG_INDEX_MAP_H
9 
10 #include <vector>
11 #include <algorithm>
12 #include <stdexcept>
13 
14 namespace Scine {
15 namespace Molassembler {
16 namespace Temple {
17 
18 template<typename T, typename U>
20 public:
21  using key_type = T;
22  using value_type = U;
23 
24  using iterator = typename std::vector<U>::iterator;
25  using const_iterator = typename std::vector<U>::const_iterator;
26 
27  StrongIndexFlatMap() = default;
28  explicit StrongIndexFlatMap(std::vector<U> strong) : map_(std::move(strong)) {}
29  explicit StrongIndexFlatMap(const std::vector<typename U::value_type>& weak)
30  : map_(std::begin(weak), std::end(weak)) {}
31 
32  T indexOf(const U u) const {
33  auto findIter = std::find(
34  std::begin(map_),
35  std::end(map_),
36  u
37  );
38 
39  if(findIter == std::end(map_)) {
40  throw std::out_of_range("Item not found");
41  }
42 
43  return T(findIter - std::begin(map_));
44  }
45 
46  void resize(unsigned newSize) {
47  map_.resize(newSize);
48  }
49 
50  unsigned size() const {
51  return map_.size();
52  }
53 
54  U& at(const T t) {
55  return map_.at(t);
56  }
57 
58  const U& at(const T t) const {
59  return map_.at(t);
60  }
61 
62  bool empty() const {
63  return map_.empty();
64  }
65 
66  void pushIsometric() {
67  map_.emplace_back(map_.size());
68  }
69 
70  void clear() {
71  map_.clear();
72  }
73 
74  StrongIndexFlatMap<U, T> invert() const {
75  const unsigned N = map_.size();
76  std::vector<T> inverted(N);
77  for(unsigned i = 0; i < N; ++i) {
78  inverted.at(map_.at(i)) = T(i);
79  }
80  return StrongIndexFlatMap<U, T>(inverted);
81  }
82 
83  iterator begin() { return std::begin(map_); }
84  iterator end() { return std::end(map_); }
85  const_iterator begin() const { return std::begin(map_); }
86  const_iterator end() const { return std::end(map_); }
87 
88  bool operator == (const StrongIndexFlatMap& other) const {
89  return map_ == other.map_;
90  }
91 
92  bool operator != (const StrongIndexFlatMap& other) const {
93  return map_ != other.map_;
94  }
95 
96 private:
97  std::vector<U> map_;
98 };
99 
100 } // namespace Temple
101 } // namespace Molassembler
102 } // namespace Scine
103 
104 #endif
auto find(const Container &container, const T &needle)
std::find shorthand
Definition: Functional.h:359