home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / algobase.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  7KB  |  233 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef ALGOBASE_H
  17. #define ALGOBASE_H
  18.  
  19. #include <pair.h>
  20. #include <iterator.h>
  21.  
  22. template <class ForwardIterator1, class ForwardIterator2, class T>
  23. inline void __iter_swap(ForwardIterator1 a, ForwardIterator2 b, T*) {
  24.     T tmp = *a;
  25.     *a = *b;
  26.     *b = tmp;
  27. }
  28.  
  29. template <class ForwardIterator1, class ForwardIterator2>
  30. inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) {
  31.     __iter_swap(a, b, value_type(a));
  32. }
  33.  
  34. template <class T>
  35. inline void swap(T& a, T& b) {
  36.     T tmp = a;
  37.     a = b;
  38.     b = tmp;
  39. }
  40.  
  41. template <class T>
  42. inline const T& min(const T& a, const T& b) {
  43.     return b < a ? b : a;
  44. }
  45.  
  46. template <class T, class Compare>
  47. inline const T& min(const T& a, const T& b, Compare comp) {
  48.     return comp(b, a) ? b : a;
  49. }
  50.  
  51. template <class T>
  52. inline const T& max(const T& a, const T& b) {
  53.     return  a < b ? b : a;
  54. }
  55.  
  56. template <class T, class Compare>
  57. inline const T& max(const T& a, const T& b, Compare comp) {
  58.     return comp(a, b) ? b : a;
  59. }
  60.  
  61. template <class InputIterator, class Distance>
  62. void __distance(InputIterator first, InputIterator last, Distance& n, 
  63.         input_iterator_tag) {
  64.     while (first != last) { ++first; ++n; }
  65. }
  66.  
  67. template <class ForwardIterator, class Distance>
  68. void __distance(ForwardIterator first, ForwardIterator last, Distance& n, 
  69.         forward_iterator_tag) {
  70.     while (first != last) { ++first; ++n; }
  71. }
  72.  
  73. template <class BidirectionalIterator, class Distance>
  74. void __distance(BidirectionalIterator first, BidirectionalIterator last, 
  75.         Distance& n, bidirectional_iterator_tag) {
  76.     while (first != last) { ++first; ++n; }
  77. }
  78.  
  79. template <class RandomAccessIterator, class Distance>
  80. inline void __distance(RandomAccessIterator first, RandomAccessIterator last, 
  81.                Distance& n, random_access_iterator_tag) {
  82.     n += last - first;
  83. }
  84.  
  85. template <class InputIterator, class Distance>
  86. inline void distance(InputIterator first, InputIterator last, Distance& n) {
  87.     __distance(first, last, n, iterator_category(first));
  88. }
  89.  
  90. template <class InputIterator, class Distance>
  91. void __advance(InputIterator& i, Distance n, input_iterator_tag) {
  92.     while (n--) ++i;
  93. }
  94.  
  95. template <class ForwardIterator, class Distance>
  96. void __advance(ForwardIterator& i, Distance n, forward_iterator_tag) {
  97.     while (n--) ++i;
  98. }
  99.  
  100. template <class BidirectionalIterator, class Distance>
  101. void __advance(BidirectionalIterator& i, Distance n, 
  102.            bidirectional_iterator_tag) {
  103.     if (n >= 0)
  104.     while (n--) ++i;
  105.     else
  106.     while (n++) --i;
  107. }
  108.  
  109. template <class RandomAccessIterator, class Distance>
  110. inline void __advance(RandomAccessIterator& i, Distance n, 
  111.               random_access_iterator_tag) {
  112.     i += n;
  113. }
  114.  
  115. template <class InputIterator, class Distance>
  116. inline void advance(InputIterator& i, Distance n) {
  117.     __advance(i, n, iterator_category(i));
  118. }
  119.  
  120. template <class ForwardIterator>
  121. void destroy(ForwardIterator first, ForwardIterator last) {
  122.     while (first != last) {
  123.     /* Borland bug */
  124.     destroy(&*first);
  125.     ++first;
  126.     //destroy(&*first++);
  127.     }
  128. }
  129.  
  130. template <class InputIterator, class ForwardIterator>
  131. ForwardIterator uninitialized_copy(InputIterator first, InputIterator last,
  132.                    ForwardIterator result) {
  133.     while (first != last) construct(&*result++, *first++);
  134.     return result;
  135. }
  136.  
  137. template <class ForwardIterator, class T>
  138. void uninitialized_fill(ForwardIterator first, ForwardIterator last, 
  139.             const T& x) {
  140.     while (first != last) construct(&*first++, x);
  141. }
  142.  
  143. template <class ForwardIterator, class Size, class T>
  144. ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n,
  145.                      const T& x) {
  146.     while (n--) construct(&*first++, x);
  147.     return first;
  148. }
  149.  
  150. template <class InputIterator, class OutputIterator>
  151. OutputIterator copy(InputIterator first, InputIterator last,
  152.             OutputIterator result) {
  153.     while (first != last) *result++ = *first++;
  154.     return result;
  155. }
  156.  
  157. template <class BidirectionalIterator1, class BidirectionalIterator2>
  158. BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, 
  159.                      BidirectionalIterator1 last, 
  160.                      BidirectionalIterator2 result) {
  161.     while (first != last) *--result = *--last;
  162.     return result;
  163. }
  164.  
  165. template <class ForwardIterator, class T>
  166. void fill(ForwardIterator first, ForwardIterator last, const T& value) {
  167.     while (first != last) *first++ = value;
  168. }
  169.  
  170. template <class OutputIterator, class Size, class T>
  171. OutputIterator fill_n(OutputIterator first, Size n, const T& value) {
  172.     while (n-- > 0) *first++ = value;
  173.     return first;
  174. }
  175.  
  176. template <class InputIterator1, class InputIterator2>
  177. pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
  178.                           InputIterator1 last1,
  179.                           InputIterator2 first2) {
  180.     while (first1 != last1 && *first1 == *first2) {
  181.     ++first1;
  182.     ++first2;
  183.     }
  184.     return pair<InputIterator1, InputIterator2>(first1, first2);
  185. }
  186.  
  187. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  188. pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
  189.                           InputIterator1 last1,
  190.                           InputIterator2 first2,
  191.                           BinaryPredicate binary_pred) {
  192.     while (first1 != last1 && binary_pred(*first1, *first2)) {
  193.     ++first1;
  194.     ++first2;
  195.     }
  196.     return pair<InputIterator1, InputIterator2>(first1, first2);
  197. }
  198.  
  199. template <class InputIterator1, class InputIterator2>
  200. inline bool equal(InputIterator1 first1, InputIterator1 last1,
  201.           InputIterator2 first2) {
  202.     return mismatch(first1, last1, first2).first == last1;
  203. }
  204.  
  205. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  206. inline bool equal(InputIterator1 first1, InputIterator1 last1,
  207.           InputIterator2 first2, BinaryPredicate binary_pred) {
  208.     return mismatch(first1, last1, first2, binary_pred).first == last1;
  209. }
  210.  
  211. template <class InputIterator1, class InputIterator2>
  212. bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
  213.                  InputIterator2 first2, InputIterator2 last2) {
  214.     while (first1 != last1 && first2 != last2) {
  215.     if (*first1 < *first2) return true;
  216.     if (*first2++ < *first1++) return false;
  217.     }
  218.     return first1 == last1 && first2 != last2;
  219. }
  220.  
  221. template <class InputIterator1, class InputIterator2, class Compare>
  222. bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
  223.                  InputIterator2 first2, InputIterator2 last2,
  224.                  Compare comp) {
  225.     while (first1 != last1 && first2 != last2) {
  226.     if (comp(*first1, *first2)) return true;
  227.     if (comp(*first2++, *first1++)) return false;
  228.     }
  229.     return first1 == last1 && first2 != last2;
  230. }
  231.  
  232. #endif
  233.