CoCoALib-0.9905 date: 23 May 2007


tmp.H

Go to the documentation of this file.
00001 #ifndef CoCoA_tmp_H
00002 #define CoCoA_tmp_H
00003 
00004 //   Copyright (c)  2006  John Abbott
00005 
00006 //   This file is part of the source of CoCoALib, the CoCoA Library.
00007 
00008 //   CoCoALib is free software; you can redistribute it and/or modify
00009 //   it under the terms of the GNU General Public License (version 2)
00010 //   as published by the Free Software Foundation.  A copy of the full
00011 //   licence may be found in the file COPYING in this directory.
00012 
00013 //   CoCoALib is distributed in the hope that it will be useful,
00014 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 //   GNU General Public License for more details.
00017 
00018 //   You should have received a copy of the GNU General Public License
00019 //   along with CoCoA; if not, write to the Free Software
00020 //   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022 
00023 // This contains sundry things which may eventually become stable parts of
00024 // the CoCoA library.
00025 
00026 
00027 #include "CoCoA/error.H"
00028 
00029 #include <list>
00030 //using std::list
00031 #include <vector>
00032 //using std::vector;
00033 #include <algorithm>
00034 //using std::for_each;
00035 
00036 namespace CoCoA
00037 {
00038 
00039   ////////////////////////////////////////////////////////////
00040   // Functions for computing products of lists and vectors.
00041 
00042   // This class is simply to permit use of std::for_each.
00043   template <typename arith_t>
00044   class SeqProd
00045   {
00046   public:
00047     SeqProd(const arith_t& InitialValue): myProdSoFar(InitialValue) {}
00048     // Default copy ctor, assignment, and dtor are all OK.
00049     void operator()(const arith_t& NextValue) { myProdSoFar *= NextValue; }
00050     const arith_t& myCurrentValue() const { return myProdSoFar; }
00051   private:
00052     arith_t myProdSoFar;
00053   };
00054 
00055   template <typename InputIterator, typename arith_t>
00056   arith_t product(InputIterator begin, InputIterator end, const arith_t& InitialValue)
00057   {
00058     return std::for_each(begin, end, SeqProd<arith_t>(InitialValue)).myCurrentValue();
00059   }
00060 
00061   // The next two are essentially identical; can they be unified???
00062   // Should there be an "inline" somewhere???
00063   template <typename arith_t>
00064   arith_t product(const std::list<arith_t>& L)
00065   {
00066     if (L.empty()) CoCoA_ERROR("Untyped empty product", "product(list)");
00067     return product(++L.begin(), L.end(), arith_t(*L.begin()));
00068   }
00069 
00070   template <typename arith_t>
00071   arith_t product(const std::vector<arith_t>& V)
00072   {
00073     if (V.empty()) CoCoA_ERROR("Untyped empty product", "product(vector)");
00074     return product(++V.begin(), V.end(), arith_t(V[0]));
00075   }
00076 
00077 
00078   ////////////////////////////////////////////////////////////
00079   // Functions for computing sums of lists and vectors.
00080 
00081   // This class is simply to permit use of for_each.
00082   template <typename arith_t>
00083   class SeqSum
00084   {
00085   public:
00086     SeqSum(const arith_t& InitialValue): mySumSoFar(InitialValue) {}
00087     // Default copy ctor, assignment, and dtor are all OK.
00088     void operator()(const arith_t& NextValue) { mySumSoFar += NextValue; }
00089     const arith_t& myCurrentValue() const { return mySumSoFar; }
00090   private:
00091     arith_t mySumSoFar;
00092   };
00093 
00094   template <typename InputIterator, typename arith_t>
00095   arith_t sum(InputIterator begin, InputIterator end, const arith_t& InitialValue)
00096   {
00097     return std::for_each(begin, end, SeqSum<arith_t>(InitialValue)).myCurrentValue();
00098   }
00099 
00100   // The next two are essentially identical; can they be unified???
00101   // Should there be an "inline" somewhere???
00102 // template <typename container_t>
00103 // container_t::value_t sum(const container_t& L)
00104 // {
00105 //   if (L.empty()) CoCoA_ERROR("Untyped empty sum", "sum(container)");
00106 //   return sum(++L.begin(), L.end(), container::value_t(*L.begin()));
00107 // }
00108   template <typename arith_t>
00109   arith_t sum(const std::list<arith_t>& L)
00110   {
00111     if (L.empty()) CoCoA_ERROR("Untyped empty sum", "sum(list)");
00112     return sum(++L.begin(), L.end(), arith_t(*L.begin()));
00113   }
00114 
00115   template <typename arith_t>
00116   arith_t sum(const std::vector<arith_t>& V)
00117   {
00118     if (V.empty()) CoCoA_ERROR("Untyped empty sum", "sum(vector)");
00119     return sum(++V.begin(), V.end(), arith_t(V[0]));
00120   }
00121 
00122 
00123 } // end of namespace CoCoA
00124 
00125 
00126 
00127 // RCS header/log in the next few lines.
00128 // $Header: /Volumes/Home/cocoa/cvs-repository/CoCoALib-0.99/include/CoCoA/tmp.H,v 1.1.1.1 2007/03/09 15:16:11 abbott Exp $
00129 // $Log: tmp.H,v $
00130 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
00131 // Imported files
00132 //
00133 // Revision 1.3  2006/12/06 17:18:40  cocoa
00134 // -- removed #include "config.H"
00135 //
00136 // Revision 1.2  2006/10/06 14:04:15  cocoa
00137 // Corrected position of #ifndef in header files.
00138 // Separated CoCoA_ASSERT into assert.H from config.H;
00139 // many minor consequential changes (have to #include assert.H).
00140 // A little tidying of #include directives (esp. in Max's code).
00141 //
00142 // Revision 1.1.1.1  2006/05/30 11:39:37  cocoa
00143 // Imported files
00144 //
00145 // Revision 1.3  2006/03/27 16:15:39  cocoa
00146 // Checking in functions for printing lists and vectors of things,
00147 // so that GPoly.C will compile.  Will reorganize things another day.
00148 //
00149 // Revision 1.2  2006/03/27 12:21:25  cocoa
00150 // Minor silly changes to reduce number of complaints from some compiler or other.
00151 //
00152 // Revision 1.1  2006/01/20 17:44:20  cocoa
00153 // Friday afternoon check-in: you have been warned!
00154 // Fixed ref counting bug in SmartPtrIRC.
00155 // Added sum/product functions in tmp.H.
00156 // Minor tidying here and there.
00157 //
00158 
00159 #endif

Generated on Wed May 23 13:43:36 2007 for CoCoALib by  doxygen 1.4.6