ascii_chart.h

Go to the documentation of this file.
00001 #ifndef MATH_ASCII_CHART_H
00002 #define MATH_ASCII_CHART_H
00003 //
00004 // Copyright 2002, Lowell Boggs Jr.
00005 //
00006 // This file or directory, containing source code for a computer program,
00007 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00008 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00009 // source file without charge or obligation so long as you agree to
00010 // the following:
00011 //
00012 //  1.  You must indemnify Lowell Boggs against any and all financial
00013 //      obligations caused by its use, misuse, function, or malfunction.
00014 //      Further, you acknowledge that there is no warranty of any kind,
00015 //      whatsoever.
00016 //
00017 //  2.  You agree not to attempt to patent any portion of this original
00018 //      work -- though you may attempt to patent your own extensions to
00019 //      it if you so choose.
00020 //
00021 //  3.  You keep this copyright notice with the file and all copies
00022 //      of the file and do not change it anyway except language translation.
00023 //
00024 // You are responsible for enforcing your own compliance with these
00025 // conditions and may not use this source file if you cannot agree to the
00026 // above terms and conditions.
00027 //
00028 // Warning:  not all files in this directory structure are covered by the
00029 // same copyright.  Some of them are part of the GNU source distribution
00030 // and you must obey the GPL copyright for those files.
00031 
00034 
00035 #include <utility>
00036 #include <vector>
00037 #include <iostream>
00038 #include <string>
00039 
00040 namespace cxxtls
00041 {
00042 
00043 struct ascii_chart 
00066 {
00067   typedef double X_t; 
00068   typedef double Y_t; 
00069 
00070   typedef std::pair<X_t,Y_t>   point_t; 
00071   typedef std::vector<point_t> data_t;  
00072 
00073 
00074   int width_;      
00075   int height_;     
00076 
00077   bool dirty_;     
00078 
00079   char **data_;    
00080 
00081 
00082 
00083 
00084 
00085   char blank_;     
00086   char star_;      
00087 
00088   std::pair<X_t,X_t> xrange_;  
00089   std::pair<Y_t,Y_t> yrange_;  
00090 
00091   std::string title_;          
00092 
00093   ascii_chart(int width, int height, char blank=' ', char star='*');
00094  ~ascii_chart();
00095 
00096   void clear();  
00097 
00098 
00099   void plot(data_t const       &data,
00100             int                 xola=0  // ignore this parm
00101            );
00115    
00116   template<class X, class Y>
00117   void plot(std::vector< std::pair<X,Y> > const &data)
00122   {
00123 
00124     // This function copies the data into the standard plot required
00125     // form, calls plot, then translates the plot return data to the
00126     // requested form.
00127 
00128     data_t tmp(data.size());
00129 
00130     typedef std::vector< std::pair<X,Y> > input_t;
00131 
00132     data_t::iterator cur = tmp.begin();
00133 
00134     for(typename input_t::const_iterator i = data.begin(); i != data.end(); ++i, ++cur)
00135     {
00136       cur->first  = i->first;
00137       cur->second = i->second;
00138     }
00139 
00140     plot(tmp,1);  // call the non-template version
00141 
00142   }
00143 
00144   template<class Container>
00145   void plot(Container const &c) 
00146     
00147 
00148 
00149   {
00150     data_t tmp(c.size());
00151 
00152     typename Container::const_iterator first = c.begin(), 
00153                                        last  = c.end();
00154 
00155     data_t::iterator output = tmp.begin();
00156 
00157     while(first != last)
00158     {
00159       output->first = first->first;
00160       output->second= first->second;
00161 
00162       ++first;
00163       ++output;
00164     }
00165 
00166     plot(tmp);
00167 
00168   }
00169 
00170 
00171   void print_helper(std::ostream& stream) const;
00172 
00173   friend std::ostream& operator<< (std::ostream&s, ascii_chart const &a)
00174     {
00175       a.print_helper(s);
00176       return s;
00177     }
00178 
00179 
00180 private:
00181   ascii_chart& operator=(ascii_chart const &);  //< not implemented
00182   ascii_chart(ascii_chart const &);             //< not implemented
00183 
00184   void range(data_t const &data);               
00185 };
00186 
00187 
00188 
00189 } // namespace cxxtls
00190 #endif
Generated on Wed Feb 29 22:50:03 2012 for CXXUtilities by  doxygen 1.6.3