src/foundation/ZUtil_STL.h

00001 /*  @(#) $Id: ZUtil_STL.h,v 1.12 2006/10/30 06:13:19 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2002 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZUtil_STL__
00026 #define __ZUtil_STL__
00027 #include "zconfig.h"
00028 
00029 #include "ZCompat_algorithm.h"
00030 #include "ZDebug.h"
00031 
00032 #include <map>
00033 #include <set>
00034 #include <vector>
00035 
00036 namespace ZUtil_STL {
00037 
00038 // ==================================================
00039 
00041 template <class InputIterator>
00042 void sDeleteAll(InputIterator begin, InputIterator end)
00043         {
00044         while (begin != end)
00045                 {
00046                 delete *begin;
00047                 ++begin;
00048                 }
00049         }
00050 
00051 // ==================================================
00052 
00055 template <typename T, typename S>
00056 bool sContains(const std::vector<T>& iVector, S iElement)
00057         { return iVector.end() != find(iVector.begin(), iVector.end(), iElement); }
00058 
00059 
00061 template <typename T, typename S>
00062 bool sContains(const std::set<T>& iSet, S iElement)
00063         { return iSet.end() != iSet.find(iElement); }
00064 
00065 
00067 template <typename K, typename V>
00068 bool sContains(const std::map<K, V>& iMap, K iKey)
00069         { return iMap.end() != iMap.find(iKey); }
00070 
00071 
00075 template <typename T>
00076 bool sPushBackIfNotContains(std::vector<T>& ioVector, T iElement)
00077         {
00078         typename std::vector<T>::iterator i = find(ioVector.begin(), ioVector.end(), iElement);
00079         if (i != ioVector.end())
00080                 return false;
00081         ioVector.push_back(iElement);
00082         return true;
00083         }
00084 
00085 
00088 template <typename T>
00089 void sPushBackMustNotContain(const int iDebugLevel, std::vector<T>& ioVector, T iElement)
00090         {
00091         ZAssertStop(iDebugLevel, ioVector.end() == find(ioVector.begin(), ioVector.end(), iElement));
00092         ioVector.push_back(iElement);
00093         }
00094 
00095 
00098 template <typename T>
00099 void sInsertMustNotContain(const int iDebugLevel, std::set<T>& ioSet, T iElement)
00100         {
00101         ZAssertStop(iDebugLevel, ioSet.end() == ioSet.find(iElement));
00102         ioSet.insert(iElement);
00103         }
00104 
00105 
00107 template <typename T>
00108 bool sInsertIfNotContains(std::set<T>& ioSet, T iElement)
00109         {
00110         typename std::set<T>::iterator i = ioSet.lower_bound(iElement);
00111         if (ioSet.end() == i || *i != iElement)
00112                 {
00113                 ioSet.insert(i, iElement);
00114                 return true;
00115                 }
00116         return false;
00117         }
00118 
00119 
00123 template <typename T>
00124 bool sEraseIfContains(std::vector<T>& ioVector, T iElement)
00125         {
00126         typename std::vector<T>::iterator i = find(ioVector.begin(), ioVector.end(), iElement);
00127         if (i == ioVector.end())
00128                 return false;
00129         ioVector.erase(i);
00130         return true;
00131         }
00132 
00133 
00136 template <typename T>
00137 bool sEraseIfContains(std::set<T>& ioSet, T iElement)
00138         {
00139         typename std::set<T>::iterator i = ioSet.find(iElement);
00140         if (i != ioSet.end())
00141                 {
00142                 ioSet.erase(i);
00143                 return true;
00144                 }
00145         return false;
00146         }
00147 
00148 
00151 template <typename T>
00152 typename std::vector<T>::iterator sEraseMustContain(
00153         const int iDebugLevel, std::vector<T>& ioVector, T iElement)
00154         {
00155         typename std::vector<T>::iterator i = find(ioVector.begin(), ioVector.end(), iElement);
00156         ZAssertStop(iDebugLevel, i != ioVector.end());
00157         return ioVector.erase(i);
00158         }
00159 
00160 
00162 template <typename T>
00163 void sEraseMustContain(const int iDebugLevel, std::set<T>& ioSet, T iElement)
00164         {
00165         typename std::set<T>::iterator i = ioSet.find(iElement);
00166         ZAssertStop(iDebugLevel, i != ioSet.end());
00167         ioSet.erase(i);
00168         }
00169 
00170 // ==================================================
00171 
00174 template <typename T>
00175 bool sSortedContains(const std::vector<T>& iVector, T iElement)
00176         {
00177         typename std::vector<T>::const_iterator i
00178                 = lower_bound(iVector.begin(), iVector.end(), iElement);
00179         return i != iVector.end() && *i == iElement;
00180         }
00181 
00182 
00187 template <typename T>
00188 bool sSortedInsertIfNotContains(std::vector<T>& iVector, T iElement)
00189         {
00190         typename std::vector<T>::iterator i = lower_bound(iVector.begin(), iVector.end(), iElement);
00191         if (i != iVector.end() && *i == iElement)
00192                 return false;
00193         iVector.insert(i, iElement);
00194         return true;
00195         }
00196 
00197 
00201 template <typename T>
00202 bool sSortedEraseIfContains(std::vector<T>& iVector, T iElement)
00203         {
00204         typename std::vector<T>::iterator i = lower_bound(iVector.begin(), iVector.end(), iElement);
00205         if (i == iVector.end() || *i != iElement)
00206                 return false;
00207         iVector.erase(i);
00208         return true;
00209         }
00210 
00211 
00215 template <typename T>
00216 void sSortedInsertMustNotContain(const int iDebugLevel, std::vector<T>& iVector, T iElement)
00217         {
00218         typename std::vector<T>::iterator i = lower_bound(iVector.begin(), iVector.end(), iElement);
00219         ZAssertStop(iDebugLevel, i == iVector.end() || *i != iElement);
00220         iVector.insert(i, iElement);
00221         }
00222 
00223 
00227 template <typename T>
00228 void sSortedEraseMustContain(const int iDebugLevel, std::vector<T>& iVector, T iElement)
00229         {
00230         typename std::vector<T>::iterator i = lower_bound(iVector.begin(), iVector.end(), iElement);
00231         ZAssertStop(iDebugLevel, i != iVector.end() && *i == iElement);
00232         iVector.erase(i);
00233         }
00234 
00235 // ==================================================
00236 
00237 template <typename K, typename V>
00238 bool sEraseIfContains(std::map<K, V>& ioMap, K iKey)
00239         {
00240         typename std::map<K, V>::iterator i = ioMap.find(iKey);
00241         if (i != ioMap.end())
00242                 {
00243                 ioMap.erase(i);
00244                 return true;
00245                 }
00246         return false;
00247         }
00248 
00249 
00250 template <typename K, typename V>
00251 void sEraseMustContain(const int iDebugLevel, std::map<K, V>& ioMap, K iKey)
00252         {
00253         typename std::map<K, V>::iterator i = ioMap.find(iKey);
00254         ZAssertStop(iDebugLevel, i != ioMap.end());
00255         ioMap.erase(i);
00256         }
00257 
00258 
00259 template <typename K, typename V>
00260 V sEraseAndReturn(const int iDebugLevel, std::map<K, V>& ioMap, K iKey)
00261         {       
00262         typename std::map<K, V>::iterator iter = ioMap.find(iKey);
00263         ZAssertStop(iDebugLevel, ioMap.end() != iter);
00264         V result = (*iter).second;
00265         ioMap.erase(iter);
00266         return result;
00267         }
00268 
00269 
00270 template <typename K, typename V>
00271 void sInsertMustNotContain(const int iDebugLevel, std::map<K, V>& ioMap, K iKey, V iValue)
00272         {
00273         ZAssertStop(iDebugLevel, ioMap.end() == ioMap.find(iKey));
00274         ioMap.insert(typename std::map<K, V>::value_type(iKey, iValue));
00275         }
00276 
00277 } // namespace ZUtil_STL
00278 
00279 #endif // __ZUtil_STL__

Generated on Thu Jul 26 11:21:52 2007 for ZooLib by  doxygen 1.4.7