/** * @file dict.h * @copyright (C) 2001-2006 Marcus Bjurman\n * @copyright (C) 2007-2012 Piotr Eljasiak\n * @copyright (C) 2013-2023 Uwe Scholz\n * * @copyright 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 2 of the License, or * (at your option) any later version. * * @copyright 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. * * @copyright You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ #pragma once #include #include #include template class DICT { typedef std::map KEY_COLL; typedef std::map VAL_COLL; KEY_COLL k_coll; VAL_COLL v_coll; const KEY NO_KEY; const VAL NO_VALUE; public: DICT(const KEY &no_key=KEY(), const VAL &no_val=VAL()): NO_KEY(no_key), NO_VALUE(no_val) {} void add(const KEY &k, const VAL &v); void add(const VAL &v, const KEY &k) { add(k,v); } void clear() { k_coll.clear(); v_coll.clear(); } const VAL &operator [] (const KEY &k) const; const KEY &operator [] (const VAL &v) const; }; template inline void DICT::add(const KEY &k, const VAL &v) { std::pair k_pos = k_coll.insert(std::make_pair(k,(const VAL *) NULL)); std::pair v_pos = v_coll.insert(std::make_pair(v,(const KEY *) NULL)); if (k_pos.second) k_pos.first->second = &v_pos.first->first; if (v_pos.second) v_pos.first->second = &k_pos.first->first; } template inline const VAL &DICT::operator [] (const KEY &k) const { typename KEY_COLL::const_iterator pos = k_coll.find(k); if (pos==k_coll.end()) return NO_VALUE; return pos->second ? *pos->second : NO_VALUE; } template inline const KEY &DICT::operator [] (const VAL &v) const { typename VAL_COLL::const_iterator pos = v_coll.find(v); if (pos==v_coll.end()) return NO_KEY; return pos->second ? *pos->second : NO_KEY; } template class DICT { typedef std::map T_COLL; T_COLL t_coll; const T NO_VALUE; public: explicit DICT(const T no_val = T()): NO_VALUE(no_val) {} void add(const T k, const T &v); void clear() { t_coll.clear(); } const T &operator [] (const T k) const; }; template inline void DICT::add(const T k, const T &v) { std::pair k_pos = t_coll.insert(make_pair(k,(T *) NULL)); std::pair v_pos = t_coll.insert(make_pair(v,(T *) NULL)); if (k_pos.second) k_pos.first->second = &v_pos.first->first; if (v_pos.second) v_pos.first->second = &k_pos.first->first; } template inline const T &DICT::operator [] (const T k) const { typename T_COLL::const_iterator pos = t_coll.find(k); if (pos==t_coll.end()) return NO_VALUE; return pos->second ? *pos->second : NO_VALUE; } template inline void load_data(DICT &dict, void *a, unsigned n) { if (!a) return; struct TUPLE { KEY key; VAL value; }; struct TUPLE *t = static_cast(a); for (unsigned i=0; ikey,t->value); } template inline void load_data(DICT &dict, void *a, unsigned n) { if (!a) return; struct TUPLE { KEY key; char *value; }; struct TUPLE *t = static_cast(a); for (unsigned i=0; ikey,t->value); } template inline void load_data(DICT &dict, void *a, unsigned n) { if (!a) return; struct TUPLE { char *key; VAL value; }; struct TUPLE *t = static_cast(a); for (unsigned i=0; ikey,t->value); } inline void load_data(DICT &dict, void *a, unsigned n) { if (!a) return; struct TUPLE { char *key; char *value; }; struct TUPLE *t = static_cast(a); for (unsigned i=0; ikey,t->value); }