libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/tools_utils.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef TOOLS_UTILS_HPP_
00034 #define TOOLS_UTILS_HPP_
00035 
00036 #include <map>
00037 #include <string>
00038 #include <sstream>
00039 
00040 using namespace std;
00041 
00042 namespace ebl {
00043 
00044   template <typename T1, typename T2>
00045   string map_to_string(map<T1,T2> &m) {
00046     ostringstream s;
00047     typename map<T1,T2>::iterator j;
00048     for (j = m.begin(); j != m.end(); ++j)
00049       s << "(" << j->first << ", " << j->second << ") ";
00050     return s.str();
00051   }
00052 
00053   template <typename T1, typename T2>
00054   string map_to_string2(map<T1,T2> &m) {
00055     ostringstream s;
00056     typename map<T1,T2>::iterator j;
00057     for (j = m.begin(); j != m.end(); ++j)
00058       s << j->first << ": " << j->second << endl;
00059     return s.str();
00060   }
00061 
00062   template <typename T>
00063   void list_to_vector(list<T> &l, vector<T> &v) {
00064     v.resize(l.size());
00065     typename vector<T>::iterator iv = v.begin();
00066     typename list<T>::iterator il = l.begin();
00067     for ( ; il != l.end(); ++iv, ++il) {
00068       *iv = *il;
00069     }
00070   }
00071 
00072   template <typename T>
00073   idx<T> string_to_idx(const char *s_, char sep) {
00074     idx<T> d(1);
00075     string s = s_;
00076     int k = 0;
00077     bool found = false;
00078     while (s.size()) {
00079       uint j;
00080       for (j = 0; j < s.size(); ++j)
00081         if (s[j] == sep)
00082           break ;
00083       string s0 = s.substr(0, j);
00084       if (j >= s.size())
00085         s = "";
00086       else
00087         s = s.substr(j + 1, s.size());
00088       if (!s0.empty()) {
00089         if (!found)
00090           d.set(string_to_number<T>(s0.c_str()), 0); // 1st element
00091         else {
00092           d.resize(d.dim(0) + 1);
00093           d.set(string_to_number<T>(s0.c_str()), d.dim(0) - 1);
00094         }
00095         found = true;
00096         k++;
00097       }
00098     }
00099     if (!found)
00100       eblerror("expected at least 1 number in: " << s_);
00101     return d;
00102   }
00103   
00104   template <typename T>
00105   T string_to_number(const char *s_) {
00106     return (T) string_to_double(s_);
00107   }
00108   
00109 } // end namespace ebl
00110 
00111 #endif /* TOOLS_UTILS_HPP_ */
00112