Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members

sgi_stl_compat.h

Go to the documentation of this file.
00001 /**************************************************************************
00002 ***    
00003 *** Copyright (c) 1995-2000 Regents of the University of California,
00004 ***               Andrew E. Caldwell, Andrew B. Kahng and Igor L. Markov
00005 *** Copyright (c) 2000-2004 Regents of the University of Michigan,
00006 ***               Saurabh N. Adya, Jarrod A. Roy and Igor L. Markov
00007 ***
00008 ***  Contact author(s): abk@cs.ucsd.edu, imarkov@umich.edu
00009 ***  Original Affiliation:   UCLA, Computer Science Department,
00010 ***                          Los Angeles, CA 90095-1596 USA
00011 ***
00012 ***  Permission is hereby granted, free of charge, to any person obtaining 
00013 ***  a copy of this software and associated documentation files (the
00014 ***  "Software"), to deal in the Software without restriction, including
00015 ***  without limitation 
00016 ***  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00017 ***  and/or sell copies of the Software, and to permit persons to whom the 
00018 ***  Software is furnished to do so, subject to the following conditions:
00019 ***
00020 ***  The above copyright notice and this permission notice shall be included
00021 ***  in all copies or substantial portions of the Software.
00022 ***
00023 *** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00024 *** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00025 *** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
00026 *** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00027 *** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
00028 *** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
00029 *** THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00030 ***
00031 ***
00032 ***************************************************************************/
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 /*
00044  *
00045  * Copyright (c) 1994
00046  * Hewlett-Packard Company
00047  *
00048  * Permission to use, copy, modify, distribute and sell this software
00049  * and its documentation for any purpose is hereby granted without fee,
00050  * provided that the above copyright notice appear in all copies and
00051  * that both that copyright notice and this permission notice appear
00052  * in supporting documentation.  Hewlett-Packard Company makes no
00053  * representations about the suitability of this software for any
00054  * purpose.  It is provided "as is" without express or implied warranty.
00055  *
00056  *
00057  * Copyright (c) 1996
00058  * Silicon Graphics Computer Systems, Inc.
00059  *                 
00060  * Permission to use, copy, modify, distribute and sell this software
00061  * and its documentation for any purpose is hereby granted without fee,
00062  * provided that the above copyright notice appear in all copies and
00063  * that both that copyright notice and this permission notice appear
00064  * in supporting documentation.  Silicon Graphics makes no
00065  * representations about the suitability of this software for any
00066  * purpose.  It is provided "as is" without express or implied warranty.
00067  */
00068 
00069 #ifndef __SGI_STL_COMPAT
00070 #define __SGI_STL_COMPAT
00071 
00072 
00073 #if defined(__GNUC__)
00074   #define ported_from_sgi std
00075 #else
00076       
00077 #include <utility>
00078 #include <algorithm>
00079 #include <iterator>
00080 #include <functional>
00081 
00082 // #include <ABKCommon/SGI_STL_COMPAT/hash_map.h>
00083 
00084 #ifdef _MSC_VER
00085   //HP and SGI copyrights do not apply to these definitions of
00086   //std::min and std::max
00087 #if _MSC_VER < 1300
00088   namespace std
00089   {
00090       template<class T> const T &min(const T &x, const T &y)
00091       {
00092           if (x<y) return x;
00093           else return y;
00094       }
00095       template<class T> const T &max(const T &x, const T &y)
00096       {
00097           if (x>y) return x;
00098           else return y;
00099       }
00100   };
00101 #endif
00102 #endif
00103 
00104 namespace std
00105 {
00106 
00107 template <class _ForwardIterator, class _Tp>
00108 void
00109 iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value)
00110 {
00111   while (__first != __last)
00112     *__first++ = __value++;
00113 }       
00114 
00115 template <class _Base, class _Integer>
00116 _Base power(_Base __x, _Integer __n)
00117 {
00118   if (__n == 0) return 1;
00119   else {
00120     while ((__n & 1) == 0) {
00121       __n >>= 1;
00122       __x = __x * __x;
00123     }
00124 
00125     _Base __result = __x;
00126     __n >>= 1;
00127     while (__n != 0) {
00128       __x = __x * __x;
00129       if ((__n & 1) != 0)
00130         __result = __result * __x;
00131       __n >>= 1;
00132     }
00133     return __result;
00134   }
00135 }        
00136 
00137 
00138 
00139 template <class _ForwardIter, class _OutputIter, class _Distance>
00140 _OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last,
00141                             _OutputIter __out, const _Distance __n)
00142 {
00143   _Distance __remaining = __last - __first;
00144   _Distance __m = std::min(__n, __remaining);
00145 
00146   while (__m > 0) {
00147     if (__random_number(__remaining) < __m) {
00148       *__out = *__first;
00149       ++__out;
00150       --__m;
00151     }
00152 
00153     --__remaining;
00154     ++__first;
00155   }
00156   return __out;
00157 }
00158 
00159 template <class _ForwardIter, class _OutputIter, class _Distance,
00160           class _RandomNumberGenerator>
00161 _OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last,
00162                             _OutputIter __out, const _Distance __n,
00163                             _RandomNumberGenerator& __rand)
00164 {
00165   _Distance __remaining = __last - __first;
00166   _Distance __m = std::min(__n, __remaining);
00167 
00168   while (__m > 0) {
00169     if (__rand(__remaining) < __m) {
00170       *__out = *__first;
00171       ++__out;
00172       --__m;
00173     }
00174 
00175     --__remaining;
00176     ++__first;
00177   }
00178   return __out;
00179 }       
00180 
00181 template <class _InputIter, class _RandomAccessIter, class _Distance>
00182 _RandomAccessIter __random_sample(_InputIter __first, _InputIter __last,
00183                                   _RandomAccessIter __out,
00184                                   const _Distance __n)
00185 {
00186   _Distance __m = 0;
00187   _Distance __t = __n;
00188   for ( ; __first != __last && __m < __n; ++__m, ++__first)
00189     __out[__m] = *__first;
00190 
00191   while (__first != __last) {
00192     ++__t;
00193     _Distance __M = __random_number(__t);
00194     if (__M < __n)
00195       __out[__M] = *__first;
00196     ++__first;
00197   }
00198 
00199   return __out + __m;
00200 }                      
00201 
00202 template <class _InputIter, class _RandomAccessIter,
00203           class _RandomNumberGenerator, class _Distance>
00204 _RandomAccessIter __random_sample(_InputIter __first, _InputIter __last,
00205                                   _RandomAccessIter __out,
00206                                   _RandomNumberGenerator& __rand,
00207                                   const _Distance __n)
00208 {
00209   _Distance __m = 0;
00210   _Distance __t = __n;
00211   for ( ; __first != __last && __m < __n; ++__m, ++__first)
00212     __out[__m] = *__first;
00213 
00214   while (__first != __last) {
00215     ++__t;
00216     _Distance __M = __rand(__t);
00217     if (__M < __n)
00218       __out[__M] = *__first;
00219     ++__first;
00220   }
00221 
00222   return __out + __m;
00223 }            
00224 
00225 template <class _InputIter, class _RandomAccessIter>
00226 inline _RandomAccessIter
00227 random_sample(_InputIter __first, _InputIter __last,
00228               _RandomAccessIter __out_first, _RandomAccessIter __out_last)
00229 {
00230   return __random_sample(__first, __last,
00231                          __out_first, __out_last - __out_first);
00232 }
00233 
00234 
00235 template <class _InputIter, class _RandomAccessIter,
00236           class _RandomNumberGenerator>
00237 inline _RandomAccessIter
00238 random_sample(_InputIter __first, _InputIter __last,
00239               _RandomAccessIter __out_first, _RandomAccessIter __out_last,
00240               _RandomNumberGenerator& __rand)
00241 {
00242   return __random_sample(__first, __last,
00243                          __out_first, __rand,
00244                          __out_last - __out_first);
00245 }    
00246 
00247 
00248 template <class _ForwardIter>
00249 bool is_sorted(_ForwardIter __first, _ForwardIter __last)
00250 {
00251   if (__first == __last)
00252     return true;
00253 
00254   _ForwardIter __next = __first;
00255   for (++__next; __next != __last; __first = __next, ++__next) {
00256     if (*__next < *__first)
00257       return false;
00258   }
00259 
00260   return true;
00261 }
00262 
00263 template <class _ForwardIter, class _StrictWeakOrdering>
00264 bool is_sorted(_ForwardIter __first, _ForwardIter __last,
00265                _StrictWeakOrdering __comp)
00266 {
00267   if (__first == __last)
00268     return true;
00269 
00270   _ForwardIter __next = __first;
00271   for (++__next; __next != __last; __first = __next, ++__next) {
00272     if (__comp(*__next, *__first))
00273       return false;
00274   }
00275 
00276   return true;
00277 }            
00278 
00279 // identity_element (not part of the C++ standard).
00280 /* #ifdef __NO_SUCH_SYMBOL__
00281 
00282 template <class _Tp> inline _Tp identity_element(plus<_Tp>) 
00283 { return _Tp(0); }
00284 
00285 template <class _Tp> inline _Tp identity_element(multiplies<_Tp>)
00286 { return _Tp(1); }                
00287 
00288 #endif
00289 */
00290 
00291 
00292 // identity is an extensions: it is not part of the standard.
00293 
00294 template <class _Tp>
00295 struct _Identity : public unary_function<_Tp,_Tp> 
00296 { const _Tp& operator()(const _Tp& __x) const { return __x; } };
00297 
00298 template <class _Tp> struct identity : public _Identity<_Tp> {};
00299                           
00300 
00301 template <class Pair, class U>
00302 // JDJ (CW Pro1 doesn't like const when first_type is also const)
00303 struct __select1st_hint : public unary_function<Pair, U>
00304 { const U& operator () (const Pair& x) const { return x.first; } };
00305 
00306 template <class Pair>
00307 struct select1st : public unary_function<Pair, typename Pair::first_type>
00308 {
00309   const typename Pair::first_type& operator()(const Pair& x) const
00310   { return x.first; }
00311 };
00312 
00313 template <class Pair>
00314 struct select2nd : public unary_function<Pair, typename Pair::second_type>
00315 {
00316   const typename Pair::second_type& operator()(const Pair& x) const
00317   { return x.second; }
00318 };
00319 
00320 
00321 };    
00322 
00323 #endif
00324 
00325 #endif
00326 
00327 

Generated on Mon Apr 25 01:09:25 2005 for Parquete by doxygen 1.3.2