forb
ordered_unique_list.hpp
1 //
2 // Created by gabriele on 16/11/18.
3 //
4 
5 #ifndef FORBCC_UNIQUES_LIST_H
6 #define FORBCC_UNIQUES_LIST_H
7 
8 #include <string>
9 #include <map>
10 #include <vector>
11 #include <type_traits>
12 
13 namespace forbcc {
15  template<typename T>
17 
18  /* *************************************************** ALIAS **************************************************** */
19  public:
21  using set_t = std::map<std::string, int>;
22 
24  using list_t = std::vector<T>;
25 
27  using const_T = typename std::add_const<T>::type;
28 
29  /* ************************************************* ATTRIBUTES ************************************************* */
30  private:
34  set_t _set;
35 
37  list_t _list;
38 
39 
40  /* ************************************************************************************************************** */
41  public:
44  virtual bool insert(std::string key, const T &value) {
45  auto mypair = make_pair(key, _list.size());
46 
47  if (_set.insert(mypair).second) {
48  // Successful insertion
49  _list.push_back(value);
50  return true;
51  }
52 
53  return false;
54  };
55 
57  const list_t &list() const {
58  return _list;
59  };
60 
62  bool contains(std::string key) const {
63  return _set.find(key) != _set.end();
64  };
65 
68  T &operator[](std::string key) {
69  return _list[_set.at(key)];
70  };
71 
74  const_T &operator[](std::string key) const {
75  return _list[_set.at(key)];
76  };
77 
78  };
79 } // namespace forbcc
80 
81 #endif //FORBCC_UNIQUES_LIST_H
typename std::add_const< parameter >::type const_T
Alias of the const version of the template type T.
Definition: ordered_unique_list.hpp:27
const list_t & list() const
Returns a const reference to the list of the elements.
Definition: ordered_unique_list.hpp:57
virtual bool insert(std::string key, const T &value)
Inserts a new value within the list if the corrisponding key is not present.
Definition: ordered_unique_list.hpp:44
A template that can be used to declare a list of unique ordered items.
Definition: ordered_unique_list.hpp:16
bool contains(std::string key) const
Returns true if the given key is present within the set of keys.
Definition: ordered_unique_list.hpp:62
T & operator[](std::string key)
Returns a reference to an element from its key.
Definition: ordered_unique_list.hpp:68
Definition: code_ostream.hpp:11
std::map< std::string, int > set_t
The type of the set of keys, used to access the list.
Definition: ordered_unique_list.hpp:21
std::vector< parameter > list_t
The type of the list.
Definition: ordered_unique_list.hpp:24
const_T & operator[](std::string key) const
Same as operator[](std::string), but returns a const_T instead, when used on a const reference to the...
Definition: ordered_unique_list.hpp:74