home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / lib270b.zip / emx / include / cpp / algobase.h < prev    next >
C/C++ Source or Header  |  1995-05-17  |  7KB  |  230 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. void uninitialized_fill_n(ForwardIterator first, Size n, const T& x) {
  145.     while (n--) construct(first++, x);
  146. }
  147.  
  148. template <class InputIterator, class OutputIterator>
  149. OutputIterator copy(InputIterator first, InputIterator last,
  150.             OutputIterator result) {
  151.     while (first != last) *result++ = *first++;
  152.     return result;
  153. }
  154.  
  155. template <class BidirectionalIterator1, class BidirectionalIterator2>
  156. BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, 
  157.                      BidirectionalIterator1 last, 
  158.                      BidirectionalIterator2 result) {
  159.     while (first != last) *--result = *--last;
  160.     return result;
  161. }
  162.  
  163. template <class ForwardIterator, class T>
  164. void fill(ForwardIterator first, ForwardIterator last, const T& value) {
  165.     while (first != last) *first++ = value;
  166. }
  167.  
  168. template <class OutputIterator, class Size, class T>
  169. void fill_n(OutputIterator first, Size n, const T& value) {
  170.     while (n-- > 0) *first++ = value;
  171. }
  172.  
  173. template <class InputIterator1, class InputIterator2>
  174. pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
  175.                           InputIterator1 last1,
  176.                           InputIterator2 first2) {
  177.     while (first1 != last1 && *first1 == *first2) {
  178.     ++first1;
  179.     ++first2;
  180.     }
  181.     return pair<InputIterator1, InputIterator2>(first1, first2);
  182. }
  183.  
  184. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  185. pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
  186.                           InputIterator1 last1,
  187.                           InputIterator2 first2,
  188.                           BinaryPredicate binary_pred) {
  189.     while (first1 != last1 && binary_pred(*first1, *first2)) {
  190.     ++first1;
  191.     ++first2;
  192.     }
  193.     return pair<InputIterator1, InputIterator2>(first1, first2);
  194. }
  195.  
  196. template <class InputIterator1, class InputIterator2>
  197. inline bool equal(InputIterator1 first1, InputIterator1 last1,
  198.           InputIterator2 first2) {
  199.     return mismatch(first1, last1, first2).first == last1;
  200. }
  201.  
  202. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  203. inline bool equal(InputIterator1 first1, InputIterator1 last1,
  204.           InputIterator2 first2, BinaryPredicate binary_pred) {
  205.     return mismatch(first1, last1, first2, binary_pred).first == last1;
  206. }
  207.  
  208. template <class InputIterator1, class InputIterator2>
  209. bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
  210.                  InputIterator2 first2, InputIterator2 last2) {
  211.     while (first1 != last1 && first2 != last2) {
  212.     if (*first1 < *first2) return true;
  213.     if (*first2++ < *first1++) return false;
  214.     }
  215.     return first1 == last1 && first2 != last2;
  216. }
  217.  
  218. template <class InputIterator1, class InputIterator2, class Compare>
  219. bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
  220.                  InputIterator2 first2, InputIterator2 last2,
  221.                  Compare comp) {
  222.     while (first1 != last1 && first2 != last2) {
  223.     if (comp(*first1, *first2)) return true;
  224.     if (comp(*first2++, *first1++)) return false;
  225.     }
  226.     return first1 == last1 && first2 != last2;
  227. }
  228.  
  229. #endif
  230.