00001 // more/algorith.h -- extensions to STL algorithm 00002 // Copyright (C) 1999--2001 Petter Urkedal (petter.urkedal@matfys.lth.se) 00003 00004 // This file 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 2 of the License, or 00007 // (at your option) any later version. 00008 00009 // This file 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, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 // As a special exception, you may use this file as part of a free 00019 // software library without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to 00022 // produce an executable, this file does not by itself cause the 00023 // resulting executable to be covered by the GNU General Public 00024 // License. This exception does not however invalidate any other 00025 // reasons why the executable file might be covered by the GNU General 00026 // Public License. 00027 00028 // $Id: algorithm.h,v 1.1 2002/05/30 18:01:37 petter_urkedal Exp $ 00029 00030 00031 #ifndef MORE_GEN_ALGORITHM_H 00032 #define MORE_GEN_ALGORITHM_H 00033 00034 #include <iterator> 00035 #include <algorithm> 00036 00037 namespace more { 00038 namespace gen { 00039 00040 template<typename InputIterator, typename OutputIterator> 00041 void copy_n(InputIterator u, int n, OutputIterator v) { 00042 while (--n) { *u = *v; ++u; ++v; } 00043 } 00044 00045 /*!\brief Insert an element into a sorted range. 00046 * \par Type requirements: 00047 * \a ForwardIterator is a model of Forward Iterator. 00048 * \pre 00049 * [\a first, \a{last}) is a sorted range. \a last is 00050 * dereferenceable. 00051 */ 00052 template<typename ForwardIterator, typename T> 00053 inline void //ForwardIterator 00054 insert_sorted(ForwardIterator first, ForwardIterator last, T const x) { 00055 while (first != last && *first < x) ++first; 00056 *last = x; 00057 while (first != last) { 00058 std::iter_swap(first, last); 00059 ++first; 00060 } 00061 ++last; 00062 //return last; 00063 // It is more useful to return iterator to the inserted value. 00064 } 00065 00066 // /*!\brief Remove elements from a sorted range. 00067 // * \par Type requirements: 00068 // * \a ForwardIterator is a model of Forward Iterator. 00069 // * \par Precondition: 00070 // * [\a first, \a last) is a sorted range. 00071 // * \return The PTE iterator of the modified range. */ 00072 // template<typename ForwardIterator> 00073 // inline ForwardIterator 00074 // remove_sorted(ForwardIterator first, ForwardIterator last, 00075 // typename iterator_traits<ForwardIterator> 00076 // ::value_type const& x) { 00077 // while(first != last && *first < x) ++first; 00078 // ForwardIterator it = first; 00079 // while(first != last && !(x < *it)) ++it; 00080 // if(first != it) 00081 // return std::copy(it, last, first); 00082 // else return last; 00083 // } 00084 00085 template<typename ForwardIterator0, typename ForwardIterator1, typename> 00086 inline bool 00087 counted_equal(ForwardIterator0 it0, ForwardIterator0 it0end, 00088 ForwardIterator1 it1, ForwardIterator1 it1end) 00089 { 00090 for (;;) { 00091 if (it0 == it0end) 00092 return it1 == it1end; 00093 if (it1 == it1end) 00094 return false; 00095 if (!(*it0 == *it1)) 00096 return false; 00097 ++it0; 00098 ++it1; 00099 } 00100 } 00101 00102 template<typename ForwardIterator0, typename ForwardIterator1, 00103 typename Equal> 00104 inline bool 00105 counted_equal(ForwardIterator0 it0, ForwardIterator0 it0end, 00106 ForwardIterator1 it1, ForwardIterator1 it1end, 00107 Equal eq) 00108 { 00109 for (;;) { 00110 if (it0 == it0end) 00111 return it1 == it1end; 00112 if (it1 == it1end) 00113 return false; 00114 if (!eq(*it0, *it1)) 00115 return false; 00116 ++it0; 00117 ++it1; 00118 } 00119 } 00120 00121 }} // more::gen 00122 00123 #endif