Generic.h

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #if !defined(_GENERIC_H)
00019 #define _GENERIC_H
00020 
00021 #include <stdint.h>
00022 
00023 #include <list>
00024 #include <iostream>
00025 #include <ostream>
00026 #include <utility>
00027 #include <vector>
00028 
00029 template <typename T>
00030 inline T abs(T x)
00031 {
00032     return (x < 0) ? -x : x;
00033 }
00034 
00035 //
00036 // this is safe for signed/unsigned:
00037 //
00038 template <typename T>
00039 inline T absDiff(T x, T y)
00040 {
00041     return (x < y) ? (y - x) : (x - y);
00042 }
00043 
00044 //
00045 //
00046 template <typename T>
00047 inline T in(T x, T y, T z)
00048 {
00049     return (x >= y && x < z);
00050 }
00051 
00052 //
00053 // These overloaded operators and functions are largely
00054 // for diagnostic debug printing.  The underlying problem
00055 // is that gdb is unable to decipher any STL use, let alone
00056 // complex STL use.  printf debugging is a poor second choice,
00057 // but these functions at least make it practical to do rapidly.
00058 //
00059 
00060 //
00061 // Write a std::pair to a stream
00062 //
00063 template <typename A, typename B>
00064 std::ostream &operator << (std::ostream &stream, std::pair<A, B> p)
00065 {
00066     stream << "(" << p.first << ", " << p.second << ")";
00067     return stream;
00068 }
00069 
00070 //
00071 // generic vector print -- in normal use, you should
00072 // be able to simply do foostream << somevector, and get
00073 // sane results, provided that the vector elements themselves
00074 // can be written to the stream.
00075 //
00076 // Example code is in Generic.cpp
00077 //
00078 template <typename T>
00079 std::ostream &operator << (std::ostream &stream, std::vector<T> v)
00080 {
00081 
00082     typename std::vector<T>::iterator i;
00083     for (i = v.begin(); i != v.end(); i++)
00084     {
00085         stream << (i - v.begin()) << ": " << *i << std::endl;
00086     }
00087     return stream;
00088 }
00089 
00090 //
00091 // same overload as above, except for std::list
00092 //
00093 template <typename T>
00094 std::ostream &operator << (std::ostream &stream, std::list<T> l)
00095 {
00096 
00097     typename std::list<T>::iterator i;
00098     int j = 0;
00099     for (i = l.begin(); i != l.end(); i++, j++)
00100     {
00101         stream << j << ": " << *i << std::endl;
00102     }
00103     return stream;
00104 }
00105 
00106 template <typename TITLE, typename ITEM, typename EXPECT, typename GOT>
00107 void check(int &returnCode, TITLE title, ITEM item, EXPECT expect, GOT got)
00108 {
00109     if (expect!=got)
00110     {
00111         std::cout << "Test " << title << ": expect " << item << " = " << expect << ", but got " << got << "." << std::endl;
00112         returnCode = 1;
00113     }
00114 }
00115 
00116 //
00117 // read values from a stream, appending to the provided
00118 // vec.  stops when the stream is consumed.
00119 //
00120 template<typename T>
00121 std::istream &operator >> (std::istream &stream, std::vector<T> &vec)
00122 {
00123     T val;
00124     while (true)
00125     {
00126         stream >> val;
00127         if (!stream.good()) break;
00128         vec.push_back(val);
00129     }
00130     return stream;
00131 }
00132 
00133 
00134 #if 0
00135 //
00136 // generic vector of iterators print
00137 //
00138 template <typename T>
00139 std::ostream &operator << (
00140     std::ostream &stream,
00141     std::vector<
00142     std::pair< std::vector<typename T>::iterator , std::vector< typename T>::iterator >
00143     > v
00144 )
00145 {
00146 
00147     typename IteratorType i;
00148     typename std::vector<T>::iterator i;
00149     for (i = v.begin(); i != v.end(); i++)
00150     {
00151         stream << *i << std::endl;
00152     }
00153     return stream;
00154 }
00155 
00156 #endif
00157 
00158 #endif
Generated on Wed Nov 17 15:38:28 2010 for StatGen Software by  doxygen 1.6.3