Simple Application Framework
1
|
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_HASHMAP_H 00029 #define SAF_COLLECTION_HASHMAP_H 00030 00031 #include "../Algo/Hash.h" 00032 #include "../Algo/Struct/HashTable.h" 00033 #include "../Algo/Struct/HashTablePolicy.h" 00034 #include "../Algo/Predicate.h" 00035 #include "../Algo/Selector.h" 00036 #include "../IndexOutOfRangeException.h" 00037 #include "Pair.h" 00038 00039 namespace Saf 00040 { 00041 namespace Collection 00042 { 00065 template <class KeyType, class ValType, class HashFuncType = Algo::Hash<KeyType>, 00066 class HashPolicyType = Algo::Struct::HashPolicy::PrimePolicy, 00067 class CompType = Algo::Predicate::Equal<KeyType> > 00068 class HashMap 00069 : public Algo::Struct::HashTable<KeyType,Pair<const KeyType,ValType>, 00070 Algo::Selector::FirstMember<Pair<const KeyType,ValType> >, 00071 HashFuncType,HashPolicyType,CompType> 00072 { 00073 protected: 00074 typedef Pair<const KeyType,ValType> RecType; 00075 typedef Algo::Struct::HashTable<KeyType,RecType,Algo::Selector::FirstMember<RecType>,HashFuncType,HashPolicyType,CompType> BaseType; 00076 typedef HashMap<KeyType,ValType,HashFuncType,HashPolicyType,CompType> MyType; 00077 00078 public: 00079 typedef typename BaseType::Iterator Iterator; 00080 typedef typename BaseType::ConstIterator ConstIterator; 00081 00082 public: 00088 explicit HashMap(Size bucketHint = 0) 00089 : BaseType(bucketHint) 00090 {} 00091 00093 HashMap(const MyType& d) 00094 : BaseType(d) 00095 {} 00096 00098 MyType& operator=(const MyType& d) 00099 { 00100 BaseType::operator=(d); 00101 return *this; 00102 } 00103 00109 MyType& Swap(MyType& d) 00110 { 00111 BaseType::Swap(d); 00112 return *this; 00113 } 00114 00120 ValType& operator[](const KeyType& key) 00121 { 00122 Iterator iter = Find(key); 00123 00124 if (iter == this->End()) 00125 { 00126 return Insert(RecType(key, ValType())).m_first->m_second; 00127 } 00128 00129 return iter->m_second; 00130 } 00131 00140 Iterator Update(const KeyType& key, const ValType& val) 00141 { 00142 Pair<Iterator,bool> ins = Insert(RecType(key,val)); 00143 00144 if (!ins.m_second) 00145 { 00146 ins.m_first->m_second = val.m_second; 00147 } 00148 00149 return ins.m_first; 00150 } 00151 00157 ValType& At(const KeyType& key) 00158 { 00159 Iterator iter = Find(key); 00160 00161 if (iter == this->End()) 00162 { 00163 throw IndexOutOfRangeException(SAF_SOURCE_LOCATION, "Key does not exist in the dictionary."); 00164 } 00165 00166 return iter->m_second; 00167 } 00168 00174 const ValType& At(const KeyType& key) const 00175 { 00176 ConstIterator iter = Find(key); 00177 00178 if (iter == this->End()) 00179 { 00180 throw IndexOutOfRangeException(SAF_SOURCE_LOCATION, "Key does not exist in the dictionary."); 00181 } 00182 00183 return iter->m_second; 00184 } 00185 }; 00186 } 00187 00189 namespace Algo 00190 { 00191 // Swap specialization for dictionary container. 00192 template <class KeyType, class ValType, class HashFuncType, class HashPolicyType, class CompType> 00193 struct SwapFunc< Collection::HashMap<KeyType,ValType,HashFuncType,HashPolicyType,CompType> > 00194 { 00195 void operator()(Collection::HashMap<KeyType,ValType,HashFuncType,HashPolicyType,CompType>& x, 00196 Collection::HashMap<KeyType,ValType,HashFuncType,HashPolicyType,CompType>& y) 00197 { 00198 x.Swap(y); 00199 } 00200 }; 00201 } 00203 } 00204 00205 #endif