ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfe_flat.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file dfe_flat.hpp
1 // SPDX-License-Identifier: MIT
2 // Copyright 2019 Moritz Kiehn
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 // SOFTWARE.
21 
26 
27 #pragma once
28 
29 #include <algorithm>
30 #include <functional>
31 #include <stdexcept>
32 #include <vector>
33 
34 namespace dfe {
35 
52 template<
53  typename T, typename Compare = std::less<T>,
54  typename Container = std::vector<T>>
55 class FlatSet {
56 public:
57  using value_type = T;
58  using size_type = typename Container::size_type;
59  using const_iterator = typename Container::const_iterator;
60 
62  template<typename U>
63  const value_type& at(U&& u) const;
64 
65  const_iterator begin() const { return m_items.begin(); }
66  const_iterator end() const { return m_items.end(); }
67 
69  bool empty() const { return m_items.empty(); }
71  size_type size() const { return m_items.size(); }
72 
74  void clear() { m_items.clear(); }
81  void insert_or_assign(const T& t);
82 
84  template<typename U>
85  const_iterator find(U&& u) const;
87  template<typename U>
88  bool contains(U&& u) const;
89 
90 private:
91  Container m_items;
92 };
93 
104 template<typename Key, typename T, typename Compare = std::less<Key>>
105 class FlatMap {
106 public:
107  using key_type = Key;
108  using value_type = T;
109  using size_type = std::size_t;
110 
112  value_type& at(const Key& key) { return m_items[m_keys.at(key).index]; }
114  const value_type& at(const Key& key) const {
115  return m_items[m_keys.at(key).index];
116  }
117 
119  bool empty() const { return m_keys.empty(); }
121  size_type size() const { return m_keys.size(); }
122 
124  void clear() { m_keys.clear(), m_items.clear(); }
129  template<typename... Params>
130  void emplace(const Key& key, Params&&... params);
131 
133  bool contains(const Key& key) const { return m_keys.contains(key); }
134 
135 private:
136  struct KeyIndex {
137  Key key;
139  };
140  struct KeyCompare {
141  constexpr bool operator()(const KeyIndex& lhs, const KeyIndex& rhs) const {
142  return Compare()(lhs.key, rhs.key);
143  }
144  constexpr bool operator()(const KeyIndex& lhs, const Key& rhs_key) const {
145  return Compare()(lhs.key, rhs_key);
146  }
147  constexpr bool operator()(const Key& lhs_key, const KeyIndex& rhs) const {
148  return Compare()(lhs_key, rhs.key);
149  }
150  };
151 
153  std::vector<T> m_items;
154 };
155 
156 // implementation FlatSet
157 
158 template<typename T, typename Compare, typename Container>
159 template<typename U>
160 inline const typename FlatSet<T, Compare, Container>::value_type&
162  auto pos = find(std::forward<U>(u));
163  if (pos == end()) {
164  throw std::out_of_range("The requested element does not exists");
165  }
166  return *pos;
167 }
168 
169 template<typename T, typename Compare, typename Container>
170 inline void
172  auto pos = std::lower_bound(m_items.begin(), m_items.end(), t, Compare());
173  if (((pos != m_items.end()) and !Compare()(t, *pos))) {
174  *pos = t;
175  } else {
176  m_items.emplace(pos, t);
177  }
178 }
179 
180 template<typename T, typename Compare, typename Container>
181 template<typename U>
184  auto end = m_items.end();
185  auto pos =
186  std::lower_bound(m_items.begin(), end, std::forward<U>(u), Compare());
187  return ((pos != end) and !Compare()(std::forward<U>(u), *pos)) ? pos : end;
188 }
189 
190 template<typename T, typename Compare, typename Container>
191 template<typename U>
192 inline bool
194  return std::binary_search(
195  m_items.begin(), m_items.end(), std::forward<U>(u), Compare());
196 }
197 
198 // implementation FlatMap
199 
200 template<typename Key, typename T, typename Compare>
201 template<typename... Params>
202 inline void
203 FlatMap<Key, T, Compare>::emplace(const Key& key, Params&&... params) {
204  auto idx = m_keys.find(key);
205  if (idx != m_keys.end()) {
206  m_items[idx->index] = T(std::forward<Params>(params)...);
207  } else {
208  m_items.emplace_back(std::forward<Params>(params)...);
209  m_keys.insert_or_assign(KeyIndex{key, m_items.size() - 1});
210  }
211 }
212 
213 } // namespace dfe