Lumiera  0.pre.03
»edit your freedom«
index-table.hpp
Go to the documentation of this file.
1 /*
2  INDEX-TABLE.hpp - helper for lookup and membership check of sequence like data
3 
4  Copyright (C) Lumiera.org
5  2015, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
38 #ifndef LIB_DIFF_INDEX_TABLE_H
39 #define LIB_DIFF_INDEX_TABLE_H
40 
41 
42 #include "lib/error.hpp"
43 #include "lib/util.hpp"
44 #include "lib/format-string.hpp"
45 
46 #include <vector>
47 #include <map>
48 
49 
50 namespace lib {
51 namespace diff{
52 
53  namespace error = lumiera::error;
54 
55  using util::_Fmt;
56 
57 
58 
60  template<typename VAL>
61  class IndexTable
62  {
63  std::vector<VAL> data_;
64  std::map<VAL,size_t> idx_;
65 
66  public:
67  template<class SEQ>
68  IndexTable(SEQ const& seq)
69  {
70  size_t i = 0;
71  for (auto const& elm : seq)
72  {
73  __rejectDuplicate(elm);
74  data_.push_back (elm);
75  idx_[elm] = i++;
76  }
77  }
78 
79  /* === forwarded sequence access === */
80 
81  using iterator = typename std::vector<VAL>::iterator;
82  using const_iterator = typename std::vector<VAL>::const_iterator;
83 
84  iterator begin() { return data_.begin(); }
85  iterator end() { return data_.end(); }
86  const_iterator begin() const { return data_.begin(); }
87  const_iterator end() const { return data_.end(); }
88 
89  size_t size() const { return data_.size(); }
90 
91 
92 
93 
94  VAL const&
95  getElement (size_t i) const
96  {
97  REQUIRE (i < size());
98  return data_[i];
99  }
100 
101 
102  bool
103  contains (VAL const& elm) const
104  {
105  return pos(elm) != size();
106  }
107 
108 
109  size_t
110  pos (VAL const& elm) const
111  {
112  auto entry = idx_.find (elm);
113  return entry==idx_.end()? size()
114  : entry->second;
115  }
116 
117  private:
118  void
119  __rejectDuplicate (VAL const& elm)
120  {
121  if (util::contains (idx_, elm))
122  throw error::Logic(_Fmt("Attempt to add duplicate %s to index table") % elm);
123  }
124  };
125 
126 
127 
128 
129 }} // namespace lib::diff
130 #endif /*LIB_DIFF_INDEX_TABLE_H*/
AnyPair entry(Query< TY > const &query, typename WrapReturn< TY >::Wrapper &obj)
helper to simplify creating mock table entries, wrapped correctly
Front-end for printf-style string template interpolation.
A front-end for using printf-style formatting.
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
data snapshot and lookup table
Definition: index-table.hpp:61
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Lumiera error handling (C++ interface).