Stride Reference Manual  - generated for commit 9643b11
SliceIndexer.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include <boost/range.hpp>
24 #include <boost/range/adaptors.hpp>
25 #include <iterator>
26 
27 namespace stride {
28 namespace util {
29 
38 template <typename T, typename Key = std::string>
40 {
41 public:
42  using range_type = boost::sliced_range<T>;
43 
44 public:
46  explicit SliceIndexer(T& t) : m_map(), m_slices(), m_t(t) {}
47 
49  range_type& Get(const Key& s) { return m_slices.at(m_map.at(s)); }
50 
52  const range_type& Get(const Key& s) const { return m_slices.at(m_map.at(s)); }
53 
55  const std::vector<range_type>& Get() { return m_slices; }
56 
58  range_type& Set(std::size_t ibegin, std::size_t iend, const Key& name)
59  {
60  check(name);
61  assert((0 <= ibegin && iend <= iend && iend <= boost::size(m_t)) && "Bad subscript.");
62  m_slices.emplace_back(range_type(m_t, ibegin, iend));
63  m_map[name] = m_slices.size() - 1;
64  return m_slices.back();
65  }
66 
68  range_type& Set(std::size_t ibegin, const Key& name)
69  {
70  check(name);
71  assert(0 <= ibegin && ibegin <= boost::size(m_t) && "Bad subscript.");
72  m_slices.emplace_back(range_type(m_t, ibegin, boost::size(m_t)));
73  m_map[name] = m_slices.size() - 1;
74  return m_slices.back();
75  }
76 
77 private:
79  void check(const Key& name)
80  {
81  if (m_map.find(name) != m_map.end()) {
82  throw std::range_error("Name is a duplicate: ");
83  }
84  }
85 
86 private:
87  std::map<Key, std::size_t> m_map;
88  std::vector<range_type> m_slices;
89  T& m_t;
90 };
91 
92 //-----------------------
93 // Helpers
94 //-----------------------
95 
96 template <typename T>
98 {
99  return SliceIndexer<T>(t);
100 }
101 
102 } // namespace util
103 } // namespace stride
const std::vector< range_type > & Get()
Retrieve all the ranges,.
Definition: SliceIndexer.h:55
SliceIndexer< T > make_slice_indexer(T &t)
Definition: SliceIndexer.h:97
const range_type & Get(const Key &s) const
Retrieve const reference to a range by its Id.
Definition: SliceIndexer.h:52
T & m_t
Refernec to container that gets sliced into ranges.
Definition: SliceIndexer.h:89
range_type & Set(std::size_t ibegin, const Key &name)
Set a range, where the end is the end of the container.
Definition: SliceIndexer.h:68
boost::sliced_range< T > range_type
Definition: SliceIndexer.h:42
range_type & Get(const Key &s)
Retrieve reference to a range by its key.
Definition: SliceIndexer.h:49
Datastructure to index a container (supporting RandomAccessIterators) with slices.
Definition: SliceIndexer.h:39
range_type & Set(std::size_t ibegin, std::size_t iend, const Key &name)
Set a range. Warning: range is [ibegin, iend) i.e. half-open, iend not included!
Definition: SliceIndexer.h:58
void check(const Key &name)
Check key map for duplicate; throw iff duplicate.
Definition: SliceIndexer.h:79
SliceIndexer(T &t)
SliceIndexer holds a reference to the conatiner that it indexes.
Definition: SliceIndexer.h:46
std::map< Key, std::size_t > m_map
Maps key values to subscripts.
Definition: SliceIndexer.h:87
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
std::vector< range_type > m_slices
Contains the slices.
Definition: SliceIndexer.h:88