1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
//
// Copyright 2010 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef INCLUDED_UHD_DICT_HPP
#define INCLUDED_UHD_DICT_HPP
#include <list>
#include <vector>
#include <stdexcept>
#include <boost/foreach.hpp>
namespace uhd{
/*!
* A templated dictionary class with a python-like interface.
*/
template <class Key, class Val> class dict{
public:
typedef std::pair<Key, Val> pair_t;
/*!
* Create a new empty dictionary.
*/
dict(void){
/* NOP */
}
/*!
* Input iterator constructor:
* Makes boost::assign::map_list_of work.
* \param first the begin iterator
* \param last the end iterator
*/
template <class InputIterator>
dict(InputIterator first, InputIterator last){
for(InputIterator it = first; it != last; it++){
_map.push_back(*it);
}
}
/*!
* Destroy this dict.
*/
~dict(void){
/* NOP */
}
/*!
* Get the number of elements in this dict.
* \param the number of elements
*/
std::size_t size(void) const{
return _map.size();
}
/*!
* Get a list of the keys in this dict.
* Key order depends on insertion precedence.
* \return vector of keys
*/
std::vector<Key> get_keys(void) const{
std::vector<Key> keys;
BOOST_FOREACH(const pair_t &p, _map){
keys.push_back(p.first);
}
return keys;
}
/*!
* Get a list of the values in this dict.
* Value order depends on insertion precedence.
* \return vector of values
*/
std::vector<Val> get_vals(void) const{
std::vector<Val> vals;
BOOST_FOREACH(const pair_t &p, _map){
vals.push_back(p.second);
}
return vals;
}
/*!
* Does the dictionary contain this key?
* \param key the key to look for
* \return true if found
*/
bool has_key(const Key &key) const{
BOOST_FOREACH(const pair_t &p, _map){
if (p.first == key) return true;
}
return false;
}
/*!
* Get a value for the given key if it exists.
* If the key is not found throw an error.
* \param key the key to look for
* \return the value at the key
* \throw an exception when not found
*/
const Val &operator[](const Key &key) const{
BOOST_FOREACH(const pair_t &p, _map){
if (p.first == key) return p.second;
}
throw std::invalid_argument("key not found in dict");
}
/*!
* Set a value for the given key, however, in reality
* it really returns a reference which can be assigned to.
* \param key the key to set to
* \return a reference to the value
*/
Val &operator[](const Key &key){
BOOST_FOREACH(pair_t &p, _map){
if (p.first == key) return p.second;
}
_map.push_back(pair_t(key, Val()));
return _map.back().second;
}
/*!
* Pop an item out of the dictionary.
* \param key the item key
* \return the value of the item
* \throw an exception when not found
*/
Val pop(const Key &key){
Val val = (*this)[key];
_map.remove(pair_t(key, val));
return val;
}
private:
std::list<pair_t> _map; //private container
};
} //namespace uhd
#endif /* INCLUDED_UHD_DICT_HPP */
|