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