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