ilang  1.1.4
ILAng: A Modeling and Verification Platform for SoCs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros
container.h
Go to the documentation of this file.
1 
4 #ifndef ILANG_UTIL_CONTAINER_H__
5 #define ILANG_UTIL_CONTAINER_H__
6 
7 #include <map>
8 #include <memory>
9 #include <set>
10 #include <vector>
11 
13 namespace ilang {
14 
16 typedef enum { END, FOUND } KeyVecItVal;
17 
19 template <class Key, class T> class KeyVecIt {
20 public:
22  typedef std::shared_ptr<KeyVecIt> KeyVecItPtr;
23 
24  // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- //
26  KeyVecIt() : result(KeyVecItVal::END), first(), second() {}
27 
28  // ------------------------- METHODS -------------------------------------- //
30  friend bool operator==(const KeyVecItPtr lhs, const KeyVecItPtr rhs) {
31  return lhs->result == rhs->result;
32  }
34  friend bool operator!=(const KeyVecItPtr lhs, const KeyVecItPtr rhs) {
35  return lhs->result != rhs->result;
36  }
37 
38  // ------------------------- MEMBERS -------------------------------------- //
42  Key first;
44  T second;
45 
46 }; // KeyVecIt
47 
49 template <class Key, class T> class KeyVec {
50 public:
52  typedef std::shared_ptr<KeyVecIt<Key, T>> KeyVecItPtr;
53 
54  // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- //
56  KeyVec() {
57  it_ = std::make_shared<KeyVecIt<Key, T>>();
58  clear();
59  }
61  ~KeyVec() { clear(); }
62 
63  // ------------------------- METHODS -------------------------------------- //
65  bool push_back(const Key& key, const T& data) {
66  auto idx = vec_.size();
67  auto [it, status] = map_.try_emplace(key, idx);
68  if (status) {
69  vec_.push_back(data);
70  }
71  return status;
72  }
73 
75  T operator[](const size_t& idx) const { return vec_.at(idx); }
76 
78  size_t size() const { return vec_.size(); }
79 
81  bool empty() const { return size() == 0; }
82 
84  void clear() {
85  vec_.clear();
86  map_.clear();
87  }
88 
90  KeyVecItPtr find(const Key& key) const {
91  auto pos = map_.find(key);
92  if (pos == map_.end())
93  return end_;
94  else {
95  auto idx = pos->second;
96  T data = vec_[idx];
97  it_->result = KeyVecItVal::FOUND;
98  it_->first = key;
99  it_->second = data;
100  return it_;
101  }
102  }
103 
105  KeyVecItPtr end() const { return end_; }
106 
107 private:
108  // ------------------------- MEMBERS -------------------------------------- //
110  std::vector<T> vec_;
112  std::map<Key, size_t> map_;
113 
115  KeyVecItPtr it_;
117  static KeyVecItPtr end_;
118 }; // class KeyVec
119 
121 template <class Key, class T>
122 std::shared_ptr<KeyVecIt<Key, T>>
123  KeyVec<Key, T>::end_ = std::make_shared<KeyVecIt<Key, T>>();
124 
126 template <class Key, class T> class MapSet {
127 public:
129  typedef std::set<T> SetT;
130 
131  // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- //
133  MapSet() {}
135  ~MapSet() {}
136 
137  // ------------------------- METHODS -------------------------------------- //
139  void insert(const Key& k, const T& t) { map_[k].insert(t); }
140 
142  SetT get(const Key& k) const { return map_.at(k); }
143 
145  typename std::map<Key, std::set<T>>::iterator begin() { return map_.begin(); }
146 
148  typename std::map<Key, std::set<T>>::iterator end() { return map_.end(); }
149 
150 private:
152  std::map<Key, std::set<T>> map_;
153 
154 }; // class MapSet
155 
156 } // namespace ilang
157 
158 #endif // ILANG_UTIL_CONTAINER_H__
bool empty() const
Return whether is empty.
Definition: container.h:81
std::shared_ptr< KeyVecIt > KeyVecItPtr
Pointer type for the KeyVec iterator.
Definition: container.h:22
std::map< Key, std::set< T > >::iterator begin()
Return the iterator at the starting point.
Definition: container.h:145
The container that support key search and index access.
Definition: container.h:49
KeyVecItVal
KeyVecItVal.
Definition: container.h:16
std::set< T > SetT
Set type for data T.
Definition: container.h:129
A pseudo-iterator for the key-search vector.
Definition: container.h:19
KeyVec()
Default constructor.
Definition: container.h:56
A map for sets.
Definition: container.h:126
KeyVecIt()
Constructor.
Definition: container.h:26
friend bool operator!=(const KeyVecItPtr lhs, const KeyVecItPtr rhs)
Overload comparison != for pointer.
Definition: container.h:34
~KeyVec()
Default destructor.
Definition: container.h:61
T operator[](const size_t &idx) const
Get the data by index.
Definition: container.h:75
KeyVecItVal result
Iterator value for checking whether a data is found.
Definition: container.h:40
size_t size() const
Return the number of data stored.
Definition: container.h:78
std::shared_ptr< KeyVecIt< Key, T > > KeyVecItPtr
Iterator pointer type.
Definition: container.h:52
KeyVecItPtr find(const Key &key) const
Return whether the key has been registered.
Definition: container.h:90
void insert(const Key &k, const T &t)
Insert the pair into the mapping.
Definition: container.h:139
std::map< Key, std::set< T > >::iterator end()
Return the iterator at the ending point.
Definition: container.h:148
Key first
Key retrived.
Definition: container.h:42
bool push_back(const Key &key, const T &data)
Push back a data member. The name MUST NOT be registerd before.
Definition: container.h:65
T second
Data retrived.
Definition: container.h:44
~MapSet()
Default destructor.
Definition: container.h:135
void clear()
Clear all stored data.
Definition: container.h:84
KeyVecItPtr end() const
Return END, can be used to check whether data is found.
Definition: container.h:105
MapSet()
Default constructor.
Definition: container.h:133
friend bool operator==(const KeyVecItPtr lhs, const KeyVecItPtr rhs)
Overload comparison == for pointer.
Definition: container.h:30