31 #ifndef DRAIN_DICTIONARY
32 #define DRAIN_DICTIONARY "Dictionary v2.0"
41 #include <drain/Sprinter.h>
62 template <
class K,
class V>
70 typedef std::pair<K, V> entry_t;
71 typedef std::list<entry_t> container_t;
73 typedef std::list<key_t> keylist_t;
74 typedef std::list<value_t> valuelist_t;
81 Dictionary(std::initializer_list<entry_t> d) : std::list<entry_t>(d), separator(
','){
89 const container_t & getContainer()
const {
93 entry_t & add(
const K & key,
const V & value){
94 this->push_back(entry_t(key, value));
99 entry_t &
set(
const K & key,
const V & value){
100 for (entry_t & entry: *
this){
101 if (entry.first == key){
102 entry.second = value;
106 this->push_back(entry_t(key, value));
110 const V & operator[](
const K & key)
const {
114 const V & operator()(
const V & value)
const {
119 typename container_t::const_iterator findByKey(
const K & key)
const {
120 for (
typename container_t::const_iterator it = this->begin(); it != this->end(); ++it){
121 if (it->first == key)
127 typename container_t::const_iterator findByValue(
const V & value)
const {
128 for (
typename container_t::const_iterator it = this->begin(); it != this->end(); ++it){
129 if (it->second == value)
137 bool hasKey(
const K & key)
const {
138 return (findByKey(key) != this->end());
144 return (findByValue(value) != this->end());
150 typename container_t::const_iterator it = findByKey(key);
151 if (it != this->end())
161 const K &
getKey(
const V & value)
const {
162 typename container_t::const_iterator it = findByValue(value);
163 if (it != this->end())
172 const keylist_t & getKeys()
const {
177 for (
const entry_t & entry: *
this){
178 keyList.push_back(entry.first);
184 void getKeys(keylist_t & l)
const {
185 for (
const entry_t & entry: *
this){
186 l.push_back(entry.first);
191 const valuelist_t & getValues()
const {
196 for (
const entry_t & entry: *
this){
197 valueList.push_back(entry.second);
203 void getValues(keylist_t & l)
const {
204 for (
const entry_t & entry: *
this){
205 l.push_back(entry.second);
217 valuelist_t valueList;
231 template <
class K,
class V>
233 std::ostream & operator<<(std::ostream & ostr,
const Dictionary<K,V> & dict) {
236 static const SprinterLayout cmdArgLayout = {
",",
"?",
"=",
""};
239 ostr << drain::sprinter(dict.getContainer(), cmdArgLayout);
255 template <
class K,
class V>
268 void add(
const K & key,
const V & value){
272 typename parent_t::container_t::const_iterator findByValue(
const V & value)
const {
273 return parent_t::findByValue(& value);
277 const V & getValue(
const K & key)
const {
282 const K & getKey(
const V & value)
const {
Associates type info.
Definition: Dictionary.h:256
Two-way mapping between strings and objects of template class T.
Definition: Dictionary.h:63
const V & getValue(const K &key) const
Given a key, return the first value associated with it.
Definition: Dictionary.h:149
const K & getKey(const V &value) const
Given a value, return the first key associated with it.
Definition: Dictionary.h:161
bool hasValue(const V &value) const
Given a key, return the first value associated with it.
Definition: Dictionary.h:143
entry_t & set(const K &key, const V &value)
Replaces existing or adds.
Definition: Dictionary.h:99
Definition: DataSelector.cpp:1277