GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TGRSIMap.h
Go to the documentation of this file.
1#ifndef TGRSIMAP_H
2#define TGRSIMAP_H
3
4#include <map>
5#include <iostream>
6#include <vector>
7#include <string>
8#include <sstream>
9#include <initializer_list>
10
11/** \addtogroup Sorting
12 * * @{
13 * */
14
15////////////////////////////////////////////////////////////
16///
17/// This class re-implements std::map with more explicit
18/// exceptions replacing out-of-range exceptions.
19///
20////////////////////////////////////////////////////////////
21
22template <typename key_type>
24
25template <typename key_type, typename mapped_type, typename key_compare = std::less<key_type>,
26 typename allocator_type = std::allocator<std::pair<const key_type, mapped_type>>>
27class TGRSIMap {
28public:
29 TGRSIMap() = default;
30 TGRSIMap(const TGRSIMap&) = default;
31 TGRSIMap(TGRSIMap&&) noexcept = default;
32 TGRSIMap& operator=(const TGRSIMap&) = default;
33 TGRSIMap& operator=(TGRSIMap&&) noexcept = default;
34 ~TGRSIMap() = default;
35
36 void Print()
37 {
38 for(auto iter : fMap) {
39 std::cout << iter.first << " - " << iter.second << std::endl;
40 }
41 }
42
43 mapped_type& at(const key_type& key)
44 {
45 try {
46 return fMap.at(key);
47 } catch(std::exception& e) {
49 }
50 }
51 const mapped_type& at(const key_type& key) const
52 {
53 try {
54 return fMap.at(key);
55 } catch(std::exception& e) {
57 }
58 }
59
60 mapped_type& operator[](const key_type& key) { return fMap[key]; }
61 const mapped_type& operator[](const key_type& key) const { return fMap[key]; }
62
63 using map_t = std::map<key_type, mapped_type, key_compare, allocator_type>;
64 typename map_t::iterator begin() { return fMap.begin(); }
65 typename map_t::const_iterator begin() const { return fMap.begin(); }
66 typename map_t::iterator end() { return fMap.end(); }
67 typename map_t::const_iterator end() const { return fMap.end(); }
68
69 // capacity functions of std::map
70 bool empty() const noexcept { return fMap.empty(); }
71 size_t size() const noexcept { return fMap.size(); }
72 size_t max_size() const noexcept { return fMap.max_size(); }
73 // modifier functions of std::map
74 void clear() noexcept { fMap.clear(); }
75 // insert
76 // insert_or_assign
77 // emplace
78 template <class... Args>
79 std::pair<typename map_t::iterator, bool> emplace(Args&&... args)
80 {
81 return fMap.emplace(args...);
82 }
83 // emplace_hint
84 // try_emplace
85 void erase(typename map_t::iterator pos) { fMap.erase(pos); }
86 void erase(typename map_t::iterator first, typename map_t::iterator last) { fMap.erase(first, last); }
87 void swap(map_t& other) { fMap.swap(other); }
88 // lookup functions of std::map
89 typename map_t::size_type count(const key_type* key) const { return fMap.count(key); }
90 typename map_t::iterator find(const key_type& key) { return fMap.find(key); }
91 typename map_t::const_iterator find(const key_type& key) const { return fMap.find(key); }
92 // equal_range
93 // lower_bound
94 // upper_bound
95
96private:
97 std::map<key_type, mapped_type, key_compare, allocator_type> fMap;
98};
99
100template <typename key_type>
101class TGRSIMapException : public std::exception {
102public:
103 template <typename mapped_type, typename key_compare, typename allocator_type>
104 TGRSIMapException(const key_type key, const std::map<key_type, mapped_type, key_compare, allocator_type>& map)
105 : std::exception(), fKey(key)
106 {
107 for(auto iter : map) {
108 fKeys.push_back(iter.first);
109 }
110 }
111
112 std::string detail() const noexcept
113 {
114 std::ostringstream str;
115 str << "Key " << fKey << " not found in '";
116 for(auto key = fKeys.begin(); key != fKeys.end(); ++key) {
117 str << *key;
118 if(std::next(key) != fKeys.end()) {
119 str << ", ";
120 }
121 }
122 str << "'";
123 return str.str();
124 }
125
126 const char* what() const noexcept override { return strdup(detail().c_str()); }
127
128private:
129 key_type fKey;
130 std::vector<key_type> fKeys;
131};
132
133/*! @} */
134#endif
TGRSIMapException(const key_type key, const std::map< key_type, mapped_type, key_compare, allocator_type > &map)
Definition TGRSIMap.h:104
std::vector< key_type > fKeys
Definition TGRSIMap.h:130
const char * what() const noexcept override
Definition TGRSIMap.h:126
std::string detail() const noexcept
Definition TGRSIMap.h:112
void clear() noexcept
Definition TGRSIMap.h:74
const mapped_type & operator[](const key_type &key) const
Definition TGRSIMap.h:61
map_t::const_iterator begin() const
Definition TGRSIMap.h:65
map_t::iterator end()
Definition TGRSIMap.h:66
std::pair< typename map_t::iterator, bool > emplace(Args &&... args)
Definition TGRSIMap.h:79
size_t size() const noexcept
Definition TGRSIMap.h:71
mapped_type & at(const key_type &key)
Definition TGRSIMap.h:43
map_t::size_type count(const key_type *key) const
Definition TGRSIMap.h:89
void erase(typename map_t::iterator first, typename map_t::iterator last)
Definition TGRSIMap.h:86
map_t::iterator begin()
Definition TGRSIMap.h:64
mapped_type & operator[](const key_type &key)
Definition TGRSIMap.h:60
TGRSIMap(TGRSIMap &&) noexcept=default
std::map< key_type, mapped_type, key_compare, allocator_type > fMap
Definition TGRSIMap.h:97
size_t max_size() const noexcept
Definition TGRSIMap.h:72
void erase(typename map_t::iterator pos)
Definition TGRSIMap.h:85
map_t::const_iterator find(const key_type &key) const
Definition TGRSIMap.h:91
const mapped_type & at(const key_type &key) const
Definition TGRSIMap.h:51
TGRSIMap(const TGRSIMap &)=default
TGRSIMap()=default
bool empty() const noexcept
Definition TGRSIMap.h:70
map_t::iterator find(const key_type &key)
Definition TGRSIMap.h:90
void Print()
Definition TGRSIMap.h:36
map_t::const_iterator end() const
Definition TGRSIMap.h:67
void swap(map_t &other)
Definition TGRSIMap.h:87
std::map< key_type, mapped_type, key_compare, allocator_type > map_t
Definition TGRSIMap.h:63