stl_utils.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00003 // taken from the GNU/sgi stl extensions
00004 
00005 /*
00006  *
00007  * Copyright (c) 1994
00008  * Hewlett-Packard Company
00009  *
00010  * Permission to use, copy, modify, distribute and sell this software
00011  * and its documentation for any purpose is hereby granted without fee,
00012  * provided that the above copyright notice appear in all copies and
00013  * that both that copyright notice and this permission notice appear
00014  * in supporting documentation.  Hewlett-Packard Company makes no
00015  * representations about the suitability of this software for any
00016  * purpose.  It is provided "as is" without express or implied warranty.
00017  *
00018  *
00019  * Copyright (c) 1996
00020  * Silicon Graphics Computer Systems, Inc.
00021  *
00022  * Permission to use, copy, modify, distribute and sell this software
00023  * and its documentation for any purpose is hereby granted without fee,
00024  * provided that the above copyright notice appear in all copies and
00025  * that both that copyright notice and this permission notice appear
00026  * in supporting documentation.  Silicon Graphics makes no
00027  * representations about the suitability of this software for any
00028  * purpose.  It is provided "as is" without express or implied warranty.
00029  */
00030 
00031 
00032 #ifndef _HUGIN_UTILS_STL_UTILS_H
00033 #define _HUGIN_UTILS_STL_UTILS_H
00034 
00035 
00036 #include <functional>
00037 #include <algorithm>
00038 #include <utility>
00039 #include <string>
00040 #include <string.h>
00041 #include <ctype.h>
00042 #include <stdexcept>
00043 
00044 #include <hugin_utils/utils.h>
00045 
00046 namespace hugin_utils {
00047     
00049     inline std::string tolower(const std::string& s)
00050     {
00051         std::string result = s;
00052         std::transform<std::string::iterator,
00053             std::string::iterator,
00054             int (*)(int)>(result.begin(), result.end(),
00055                           result.begin(), ::tolower);
00056         return result;
00057     }
00058     
00059     inline std::string toupper(const std::string& s)
00060     {
00061         std::string result = s;
00062         std::transform<std::string::iterator,
00063             std::string::iterator,
00064             int (*)(int)>(result.begin(), result.end(),
00065                           result.begin(), ::toupper);
00066         return result;
00067     }
00068 }
00069 
00070 
00072 template<typename _Container>
00073 //inline bool set_contains(const _Container & c, const _Container::key_type & key)
00074 inline bool set_contains(const _Container & c, const typename _Container::key_type & key)
00075 {
00076     return c.find(key) != c.end();
00077 }
00078 
00080 template<typename _Container>
00081 inline void fill_set(_Container & c, typename _Container::key_type begin,
00082                                          typename _Container::key_type end)
00083 {
00084         for (typename _Container::key_type i=begin; i <= end; i++) {
00085                 c.insert(i);
00086         }
00087 }
00088 
00089 
00090 
00097 template<typename Map>
00098 typename Map::mapped_type & map_get(Map &m, const typename Map::key_type & key)
00099 {
00100     typename Map::iterator it = m.find(key);
00101     if (it != m.end()) {
00102         return (*it).second;
00103     } else {
00104         DEBUG_WARN("could not find " << key);
00105         throw std::out_of_range("No such element in vector");
00106     }
00107 }
00108 
00109 template<typename Map>
00110 const typename Map::mapped_type & const_map_get(const Map &m, const typename Map::key_type & key)
00111 {
00112     typename Map::const_iterator it = m.find(key);
00113     if (it != m.end()) {
00114         return (*it).second;
00115     } else {
00116         DEBUG_WARN("could not find " << key);
00117         throw std::out_of_range("No such element in vector");
00118     }
00119 }
00120 
00121 
00122 template<typename Map>
00123 typename Map::mapped_type & map_get(Map &m, const char * key)
00124 {
00125     typename Map::iterator it = m.find(key);
00126     if (it != m.end()) {
00127         return (*it).second;
00128     } else {
00129         DEBUG_WARN("could not find " << key);
00130         throw std::out_of_range("No such element in vector");
00131     }
00132 }
00133 
00134 template<typename Map>
00135 const typename Map::mapped_type & const_map_get(const Map &m, const char * key)
00136 {
00137     typename Map::const_iterator it = m.find(key);
00138     if (it != m.end()) {
00139         return (*it).second;
00140     } else {
00141         DEBUG_WARN("could not find " << key);
00142         throw std::out_of_range("No such element in vector");
00143     }
00144 }
00145 
00146 #endif // _HUGIN_UTILS_STL_UTILS_H

Generated on 27 Jul 2015 for Hugintrunk by  doxygen 1.4.7