MicroModelicaCCompiler  4.5.3
table.hpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #pragma once
21 
22 #include <map>
23 
24 #include <util/util_types.hpp>
25 
26 template <typename Key, typename Value>
27 class ModelTable {
28  public:
29  ModelTable() : _map(){};
30  ~ModelTable() = default;
31  void insert(Key k, Value v)
32  {
33  _map.erase(k);
34  _map.insert(std::pair<Key, Value>(k, v));
35  }
36  Option<Value> operator[](Key k) const
37  {
38  if (_map.find(k) == _map.end()) {
39  return Option<Value>();
40  }
41  return _map.at(k);
42  }
43  void remove(Key k) { _map.erase(k); }
44  typedef typename std::map<Key, Value>::iterator iterator;
45  typedef typename std::map<Key, Value>::const_iterator const_iterator;
46  bool lookup(Key key)
47  {
48  iterator it = _map.find(key);
49  return it != _map.end();
50  }
51  inline iterator begin() { return _map.begin(); };
52  inline const_iterator begin() const { return _map.begin(); };
53  inline iterator end() { return _map.end(); };
54  inline const_iterator end() const { return _map.end(); };
55  inline Value begin(iterator& it)
56  {
57  it = _map.begin();
58  return (end(it) ? Value() : value(it));
59  };
60  inline bool end(iterator& it) { return it == _map.end(); }
61  inline Value next(iterator& it)
62  {
63  it++;
64  return (end(it) ? Value() : value(it));
65  }
66  inline Value value(iterator& it) { return it->second; }
67  Value value(int pos)
68  {
69  iterator it = _map.begin();
70  int current_pos = 0;
71  while (current_pos < pos && it != _map.end()) {
72  it++;
73  current_pos++;
74  }
75  return (end(it) ? Value() : value(it));
76  }
77  inline Key key(iterator& it) { return it->first; };
79  {
81  for (Value v = other.begin(it); !other.end(it); v = other.next(it)) {
82  insert(other.key(it), v);
83  }
84  }
85  Value first()
86  {
87  iterator it;
88  return begin(it);
89  }
90  void clear() { _map.clear(); };
91  bool empty() { return _map.empty(); };
92  const int size() const { return _map.size(); };
93 
94  std::list<Key> keys()
95  {
96  std::list<Key> ret;
98  for (Value v = begin(it); !end(it); v = next(it)) {
99  ret.push_back(key(it));
100  }
101  return ret;
102  }
103 
104  protected:
105  std::map<Key, Value> map() const { return _map; };
106 
107  private:
108  std::map<Key, Value> _map;
109 };
ModelTable
Definition: table.hpp:27
ModelTable::begin
iterator begin()
Definition: table.hpp:68
ModelTable::value
Value value(iterator &it)
Definition: table.hpp:83
ModelTable::key
Key key(iterator &it)
Definition: table.hpp:94
Option
Definition: util_types.hpp:32
ModelTable::ModelTable
ModelTable()
Definition: table.hpp:46
ModelTable::end
iterator end()
Definition: table.hpp:70
ModelTable::map
std::map< Key, Value > map() const
Definition: table.hpp:122
util_types.hpp
ModelTable::_map
std::map< Key, Value > _map
Definition: table.hpp:122
ModelTable::remove
void remove(Key k)
Definition: table.hpp:60
ModelTable::iterator
std::map< Key, Value >::iterator iterator
Definition: table.hpp:61
ModelTable::first
Value first()
Definition: table.hpp:102
ModelTable::keys
std::list< Key > keys()
Definition: table.hpp:111
ModelTable::operator[]
Option< Value > operator[](Key k) const
Definition: table.hpp:53
ModelTable::empty
bool empty()
Definition: table.hpp:108
ModelTable::lookup
bool lookup(Key key)
Definition: table.hpp:63
ModelTable::insert
void insert(Key k, Value v)
Definition: table.hpp:48
ModelTable::size
const int size() const
Definition: table.hpp:109
ModelTable::clear
void clear()
Definition: table.hpp:107
ModelTable::~ModelTable
~ModelTable()=default
ModelTable::merge
void merge(ModelTable< Key, Value > other)
Definition: table.hpp:95
ModelTable::const_iterator
std::map< Key, Value >::const_iterator const_iterator
Definition: table.hpp:62
ModelTable::next
Value next(iterator &it)
Definition: table.hpp:78