Statistics
| Branch: | Tag: | Revision:

root / host / include / uhd / types / dict.hpp @ ed5cb33e

History | View | Annotate | Download (4.4 KB)

1 9fff25f4 Josh Blum
//
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 b71d0cbe Josh Blum
#ifndef INCLUDED_UHD_TYPES_DICT_HPP
19
#define INCLUDED_UHD_TYPES_DICT_HPP
20 9fff25f4 Josh Blum
21 1295df8c Josh Blum
#include <uhd/config.hpp>
22 259f5bab Josh Blum
#include <boost/foreach.hpp>
23 1295df8c Josh Blum
#include <stdexcept>
24
#include <vector>
25
#include <list>
26 9fff25f4 Josh Blum
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 259f5bab Josh Blum
        typedef std::pair<Key, Val> pair_t;
35
36 9fff25f4 Josh Blum
        /*!
37
         * Create a new empty dictionary.
38
         */
39
        dict(void){
40
            /* NOP */
41
        }
42
43
        /*!
44 d1ecc555 Josh Blum
         * 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 9fff25f4 Josh Blum
         * Destroy this dict.
58
         */
59
        ~dict(void){
60
            /* NOP */
61
        }
62
63
        /*!
64 7590f187 Josh Blum
         * Get the number of elements in this dict.
65 57f1f769 Josh Blum
         * \return the number of elements
66 7590f187 Josh Blum
         */
67
        std::size_t size(void) const{
68
            return _map.size();
69
        }
70
71
        /*!
72 9fff25f4 Josh Blum
         * Get a list of the keys in this dict.
73 52dc7389 Josh Blum
         * Key order depends on insertion precedence.
74 9fff25f4 Josh Blum
         * \return vector of keys
75
         */
76 ed5cb33e Josh Blum
        const std::vector<Key> keys(void) const{
77 9fff25f4 Josh Blum
            std::vector<Key> keys;
78 52dc7389 Josh Blum
            BOOST_FOREACH(const pair_t &p, _map){
79 9fff25f4 Josh Blum
                keys.push_back(p.first);
80
            }
81
            return keys;
82
        }
83
84
        /*!
85
         * Get a list of the values in this dict.
86 52dc7389 Josh Blum
         * Value order depends on insertion precedence.
87 9fff25f4 Josh Blum
         * \return vector of values
88
         */
89 ed5cb33e Josh Blum
        const std::vector<Val> vals(void) const{
90 9fff25f4 Josh Blum
            std::vector<Val> vals;
91 52dc7389 Josh Blum
            BOOST_FOREACH(const pair_t &p, _map){
92 9fff25f4 Josh Blum
                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 52dc7389 Josh Blum
            BOOST_FOREACH(const pair_t &p, _map){
104 9fff25f4 Josh Blum
                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 52dc7389 Josh Blum
            BOOST_FOREACH(const pair_t &p, _map){
118
                if (p.first == key) return p.second;
119 9fff25f4 Josh Blum
            }
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 52dc7389 Josh Blum
            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 9fff25f4 Josh Blum
        }
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 52dc7389 Josh Blum
        Val pop(const Key &key){
144
            Val val = (*this)[key];
145
            _map.remove(pair_t(key, val));
146
            return val;
147 9fff25f4 Josh Blum
        }
148
149
    private:
150 52dc7389 Josh Blum
        std::list<pair_t> _map; //private container
151 9fff25f4 Josh Blum
    };
152
153
} //namespace uhd
154
155 b71d0cbe Josh Blum
#endif /* INCLUDED_UHD_TYPES_DICT_HPP */