4 Website: http://www.vultaire.net/software/jben/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>
27 #define BOOST_MULTI_INDEX_DISABLE_SERIALIZATION
29 #include <boost/multi_index_container.hpp>
30 #include <boost/multi_index/hashed_index.hpp>
31 #include <boost/multi_index/member.hpp>
37 using namespace boost
;
38 using namespace boost::multi_index
;
40 template<typename key
, typename value
>
41 class BoostHMM
: public
42 multi_index_container
<
49 &pair
<key
, value
>::first
55 const value
& operator[](const key
& k
);
56 const value
& operator[](const key
& k
) const;
57 bool assign(const key
& k
, const value
& v
);
61 template<typename key
, typename value
>
62 const value
& BoostHMM
<key
,value
>::operator[](const key
& k
) {
63 return this->find(k
)->second
;
66 template<typename key
, typename value
>
67 const value
& BoostHMM
<key
,value
>::operator[](const key
& k
) const {
68 return this->find(k
)->second
;
71 /* Assign will only assign if the pair does not match an existing one. */
72 template<typename key
, typename value
>
73 bool BoostHMM
<key
,value
>::assign(const key
& k
, const value
& v
) {
74 pair
<key
,value
> newEntry(k
,v
);
77 typename BoostHMM
<key
,value
>::iterator i
= this->find(k
);
78 if(i
==this->end()) bOk
=true; /* key does not exist, so we know we should
79 be able to insert this value. */
81 /* If a key existed, we need to check if there's an exact match.
82 Only proceed if no exact match is found. */
84 for(; i
!=this->end(); i
++) {
85 /* I'm assuming the container holds values
86 with the same key in a hashed index TOGETHER. Hopefully this
87 is a valid assumption. */
89 i
= this->end(); /* Makes sure we exit on a GOOD note. */
92 /* If we get here, then the key and value
98 if(i
==this->end()) bOk
= true;
101 /* If no match exists, proceed with the insertion. */
103 pair
<typename BoostHMM
<key
,value
>::iterator
, bool> p
= this->insert(newEntry
);
106 cerr
<< "Error in hash multimap! This should never happen!" << endl
;