Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Collection/TreeMap.h
Go to the documentation of this file.
00001 /*
00002     This file is part of Simple Application Framework (Saf) library.
00003     Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com>
00004 
00005     This library is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published 
00007     by the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     Saf is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Simple Application Framework library. If not, 
00017     see <http://www.gnu.org/licenses/>.
00018 */
00019 
00028 #ifndef SAF_COLLECTION_TREEMAP_H
00029 #define SAF_COLLECTION_TREEMAP_H
00030 
00031 #include "Pair.h"
00032 #include "../Algo/Struct/RedBlackTree.h"
00033 #include "../Algo/Predicate.h"
00034 #include "../Algo/Selector.h"
00035 #include "../IndexOutOfRangeException.h"
00036 
00037 namespace Saf
00038 {
00039     namespace Collection
00040     {
00056         template <class KeyType, class ValType, class CompType = Algo::Predicate::Less<KeyType> > 
00057         class TreeMap
00058             : public Algo::Struct::RedBlackTree<KeyType,Pair<const KeyType,ValType>, 
00059                 Algo::Selector::FirstMember<Pair<const KeyType,ValType> >,CompType>
00060         {
00061         protected:
00062             typedef Pair<const KeyType,ValType> RecType;
00063             typedef Algo::Struct::RedBlackTree<KeyType,RecType,Algo::Selector::FirstMember<RecType>,CompType> BaseType;
00064             typedef TreeMap<KeyType,ValType,CompType> MyType;
00065 
00066         public:
00067             typedef typename BaseType::Iterator Iterator;
00068             typedef typename BaseType::ConstIterator ConstIterator;
00069 
00070         public:
00072             TreeMap()
00073                 : BaseType()
00074             {}
00075 
00077             TreeMap(const MyType &m) 
00078                 : BaseType(m)
00079             {}
00080 
00082             MyType& operator=(const MyType& m)
00083             {
00084                 BaseType::operator=(m);
00085                 return *this;
00086             }
00087 
00093             MyType& Swap(MyType& m)
00094             {
00095                 BaseType::Swap(m);
00096                 return *this;
00097             }
00098 
00104             ValType& operator[](const KeyType& key)
00105             {
00106                 Iterator iter = Find(key);
00107 
00108                 if (iter == this->End())
00109                 {
00110                     return Insert(RecType(key, ValType())).m_first->m_second;
00111                 }
00112 
00113                 return iter->m_second;
00114             }
00115 
00124             Iterator Update(const KeyType& key, const ValType& val)
00125             {
00126                 Pair<Iterator,bool> ins = Insert(RecType(key, val));
00127 
00128                 if (!ins.m_second)
00129                 {
00130                     ins.m_first->m_second = val.m_second;
00131                 }
00132 
00133                 return ins.m_first;
00134             }
00135 
00141             ValType& At(const KeyType& key)
00142             {
00143                 Iterator iter = Find(key);
00144 
00145                 if (iter == this->End())
00146                 {
00147                     throw IndexOutOfRangeException(SAF_SOURCE_LOCATION, "Key does not exist in the map.");
00148                 }
00149                 
00150                 return iter->m_second;
00151             }
00152 
00158             const ValType& At(const KeyType& key) const
00159             {
00160                 ConstIterator iter = Find(key);
00161 
00162                 if (iter == this->End())
00163                 {
00164                     throw IndexOutOfRangeException(SAF_SOURCE_LOCATION, "Key does not exist in the map.");
00165                 }
00166                 
00167                 return iter->m_second;
00168             }
00169         };
00170     }
00171 
00173     namespace Algo
00174     {
00175         // Swap specialization for map container.
00176         template <class KeyType, class ValType, class CompType>
00177         struct SwapFunc< Collection::TreeMap<KeyType,ValType,CompType> >
00178         {
00179             void operator()(Collection::TreeMap<KeyType,ValType,CompType>& x, 
00180                 Collection::TreeMap<KeyType,ValType,CompType>& y)
00181             {
00182                 x.Swap(y);
00183             }
00184         };
00185     }
00187 }
00188 
00189 #endif