Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
LevItemSetMap.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 LEVITEMSETMAP_H
00032 #define LEVITEMSETMAP_H
00033 
00034 // ---------------------------- External Headers ---------------------------- //
00035 
00036 #include <vector>
00037 #include <map>
00038 
00039 // ---------------------------- Internal Headers ---------------------------- //
00040 
00041 #include "ItemSet.h"
00042 #include <iostream>
00050 template <typename t> class LevItemSetMap
00051 {
00052     // ----------------------------- Definitions ---------------------------- //
00053     typedef map<ItemSet, t> i_map;
00054     typedef typename i_map::iterator m_iter;
00055     typedef typename i_map::const_iterator m_citer;
00056 
00057     private:
00058         // This is the map of the sets
00059         i_map msets;
00060 
00061         // This is a vector of iterators denoting the starting points
00062         // of each level
00063         vector<m_iter> starts;
00064         vector<m_citer> cstarts;
00065 
00066         // The size of each level
00067         vector<unsigned int> sizes;
00068 
00069     public:
00070         LevItemSetMap()
00071         {
00072 
00073         }
00074 
00075         LevItemSetMap(const LevItemSetMap& src)
00076         {
00077             *this = src;
00078         }
00079 
00080         ~LevItemSetMap()
00081         {
00082 
00083         }
00084 
00085         LevItemSetMap& operator =(const LevItemSetMap& rhs)
00086         {
00087             msets = rhs.msets;
00088             starts = starts;
00089             return *this;
00090         }
00091 
00092         ItemSet*& back()
00093         {
00094             // TODO figure out what we are returning
00095             return *(msets.end()--);
00096         }
00097 
00098         void clear()
00099         {
00100             msets.clear();
00101             starts.clear();
00102             sizes.clear();
00103         }
00104 
00105         bool empty(unsigned int level) const
00106         {
00107             //cout << sizes.size() << endl;
00108             //cout << level << endl;
00109             //cout << sizes[level] << endl;
00110             return level >= sizes.size() || !sizes[level];
00111         }
00112 
00113         bool contains(const ItemSet& is) const
00114         {
00115             return msets.find(is) != msets.end();
00116         }
00117 
00118         void insert(const ItemSet& is, const t& value)
00119         {
00120             msets[is] = value;
00121             if (is.size() >= starts.size())
00122             {
00123                 starts.resize(is.size() + 1);
00124                 cstarts.resize(is.size() + 1);
00125                 sizes.resize(is.size() + 1, 0);
00126             }
00127             if (!sizes[is.size()])
00128             {
00129                 starts[is.size()] = msets.end();
00130                 starts[is.size()]--;
00131                 cstarts[is.size()] = starts[is.size()];
00132             }
00133             sizes[is.size()]++;
00134         }
00135 
00136         unsigned int size(unsigned int level) const
00137         {
00138             if (level >= sizes.size())
00139             {
00140                 return 0;
00141             }
00142             return sizes[level];
00143         }
00144 
00145         const ItemSet& getItemSet(const ItemSet& is)
00146         {
00147             return (*msets.find(is)).first;
00148         }
00149 
00150         const t& getValue() const
00151         {
00152             m_citer last = msets.end();
00153             last--;
00154             return (*last).second;
00155         }
00156 
00157         const t& getValue(const ItemSet& is) const 
00158         {
00159             return (*msets.find(is)).second;
00160         }
00161 
00162         void setValue(const ItemSet& is, const t& value)
00163         {
00164             (*msets.find(is)).second = value;
00165         }
00166 
00167         void setValue(const t& value)
00168         {
00169             m_iter last = msets.end();
00170             last--;
00171             (*last).second = value;
00172         }
00173 
00174         void pop()
00175         {
00176             m_iter last = msets.end();
00177             last--;
00178             sizes[(*last).first.size()]--;
00179             //cout << "level: " << (*last).first.size() << endl;
00180             msets.erase(last);
00181         }
00182 
00183         m_iter begin(unsigned int level)
00184         {
00185             //cout << starts.size() << endl;
00186             if (level < starts.size())
00187             {
00188                 return starts[level];
00189             }
00190             return msets.end();
00191         }
00192 
00193         m_iter end(unsigned int level)
00194         {
00195             return begin(level + 1);
00196         }
00197 
00198         m_citer begin(unsigned int level) const
00199         {
00200             //cout << cstarts.size() << endl;
00201             if (level < cstarts.size())
00202             {
00203                 return cstarts[level];
00204             }
00205             return msets.end();
00206 
00207         }
00208 
00209         m_citer end(unsigned int level) const
00210         {
00211             return begin(level + 1);
00212         }
00213 };
00214 
00215 #endif
 All Classes Functions Variables Typedefs Friends