Const conversions for edict and kanjidic objects. Removal of obsolete dictionary...
[jben.git] / boosthmm.h
blob5ca6ebd9366b83bae8fa408067f04131d00c8aef
1 /*
2 Project: J-Ben
3 Author: Paul Goins
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)
8 File: boosthmm.h
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/>
24 #ifndef boosthmm_h
25 #define boosthmm_h
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>
32 #ifdef DEBUG
33 #include <iostream>
34 #endif
36 using namespace std;
37 using namespace boost;
38 using namespace boost::multi_index;
40 template<typename key, typename value>
41 class BoostHMM : public
42 multi_index_container<
43 pair<key, value>,
44 indexed_by<
45 hashed_non_unique<
46 member<
47 pair<key, value>,
48 key,
49 &pair<key, value>::first
53 > {
54 public:
55 const value& operator[](const key& k);
56 const value& operator[](const key& k) const;
57 bool assign(const key& k, const value& v);
58 private:
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);
75 bool bOk=false;
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. */
83 if(!bOk) {
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. */
88 if(i->first!=k) {
89 i = this->end(); /* Makes sure we exit on a GOOD note. */
90 break;
92 /* If we get here, then the key and value
93 both match. BREAK. */
94 if(i->second==v) {
95 break;
98 if(i==this->end()) bOk = true;
101 /* If no match exists, proceed with the insertion. */
102 if(bOk) {
103 pair<typename BoostHMM<key,value>::iterator, bool> p = this->insert(newEntry);
104 if(!p.second) {
105 #ifdef DEBUG
106 cerr << "Error in hash multimap! This should never happen!" << endl;
107 #endif
108 return false;
112 return bOk;
115 #endif