Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
BSetMap.h
00001 /**************************************************************************
00002  * Copyright (c) 2007 Clifford Conley Owens III                           *
00003  *                                                                        *
00004  * This file is part of Biorithm.                                         *
00005  *                                                                        *
00006  * Biorithm is free software: you can redistribute it and/or modify       *
00007  * it under the terms of the GNU General Public License as published by   *
00008  * the Free Software Foundation, either version 3 of the License, or      *
00009  * (at your option) any later version.                                    *
00010  *                                                                        *
00011  * Biorithm is distributed in the hope that it will be useful,            *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00014  * GNU General Public License for more details.                           *
00015  *                                                                        *
00016  * You should have received a copy of the GNU General Public License      *
00017  * along with Biorithm.  If not, see <http://www.gnu.org/licenses/>.      *
00018  *                                                                        *
00019  **************************************************************************/
00020 
00031 #ifndef ITEMSETMAP_H
00032 #define ITEMSETMAP_H
00033 
00034 // ---------------------------- External Headers ---------------------------- //
00035 
00036 #include <vector>
00037 #include <ext/hash_map>
00038 
00039 // ---------------------------- Internal Headers ---------------------------- //
00040 
00041 #include "ItemSetCompare.h"
00042 
00050 template <typename t> class ItemSetMap
00051 {
00052     // ----------------------------- Definitions ---------------------------- //
00053     typedef __gnu_cxx::hash_map<ItemSet*, unsigned int, hash<const ItemSet*>,
00054         eqis> h_map;
00055     typedef h_map::iterator h_iter;
00056 
00057     private:
00058         // This is the ordered vector of the sets
00059         vector<ItemSet*> vsets;
00060 
00061         // This is the ordered vector of values
00062         vector<t*> vvals;
00063 
00064         // This is the hash table of the sets
00065         h_map hsets;
00066 
00067     public:
00068         ItemSetMap()
00069         {
00070 
00071         }
00072 
00073         ItemSetMap(unsigned int size)
00074         {
00075             vsets.resize(size);
00076         }
00077 
00078         ItemSetMap(const ItemSetMap& src)
00079         {
00080             *this = src;
00081         }
00082 
00083         ~ItemSetMap()
00084         {
00085 
00086         }
00087 
00088         ItemSetMap& operator =(const ItemSetMap& rhs)
00089         {
00090             vsets = rhs.vsets;
00091             vvals = rhs.vvals;
00092             hsets = rhs.hsets;
00093             return *this;
00094         }
00095 
00096         ItemSet*& operator [](unsigned int index)
00097         {
00098             return vsets[index];
00099         }
00100 
00101         ItemSet*& back()
00102         {
00103             return vsets.back();
00104         }
00105 
00106         void clear()
00107         {
00108             vsets.clear();
00109             vvals.clear();
00110             hsets.clear();
00111         }
00112 
00113         void destroy()
00114         {
00115             for (unsigned int i = 0; i < vsets.size(); i++)
00116             {
00117                 delete vsets[i];
00118                 delete vvals[i];
00119             }
00120         }
00121 
00122         bool empty() const
00123         {
00124             return vsets.empty();
00125         }
00126 
00127         bool contains(ItemSet& is) const
00128         {
00129             return contains(&is);
00130         }
00131 
00132         bool contains(ItemSet* is) const
00133         {
00134             return hsets.find(is) != hsets.end();
00135         }
00136 
00137         void insert(ItemSet* is, const t& value)
00138         {
00139             insert(is, new t(value));
00140         }
00141 
00142         void insert(ItemSet* is, t* value = new t)
00143         {
00144             hsets[is] = vsets.size();
00145             vsets.push_back(is);
00146             vvals.push_back(value);
00147         }
00148 
00149         unsigned int size() const
00150         {
00151             return vsets.size();
00152         }
00153 
00154         ItemSet* getItemSet(ItemSet* is)
00155         {
00156             return (*hsets.find(is)).first;
00157         }
00158 
00159         ItemSet* getItemSet(ItemSet& is)
00160         {
00161             return getValue(&is);
00162         }
00163 
00164         ItemSet* getItemSet (unsigned int index)
00165         {
00166             return vsets[index];
00167         }
00168 
00169         t& getValue(ItemSet* is)
00170         {
00171             return getValue((*hsets.find(is)).second);
00172         }
00173 
00174         t& getValue(ItemSet& is)
00175         {
00176             return getValue(&is);
00177         }
00178 
00179         t& getValue(unsigned int index)
00180         {
00181             return *vvals[index];
00182         }
00183 };
00184 
00185 #endif
 All Classes Functions Variables Typedefs Friends