home *** CD-ROM | disk | FTP | other *** search
- // algorithm.h
- // A few of the STL algorithms components
- //
- // By Paul Field
- // Based on the STL implementation: (c) 1994 Hewlett-Packard Company
- // v1.10 - 26/2/1996
-
- // Modified by Mark Seaborn 16/5/1996 - all functions now inlined
-
- // Algorithms have not been tested.
- // Other STL algorithms could be implemented.
-
-
- #ifndef algorithm_h
- #define algorithm_h
-
-
- template <class T>
- inline const T min(const T a, const T b)
- { return b < a ? b : a;
- }
-
-
-
-
- template <class T>
- inline const T max(const T a, const T b)
- { return a < b ? b : a;
- }
-
-
-
-
- template <class InputIterator, class T>
- inline InputIterator find(InputIterator first, InputIterator last, const T& value)
- { while (first != last && *first != value)
- { ++first;
- }
- return first;
- }
-
-
-
- template <class ForwardIterator>
- inline ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last)
- { if (first != last)
- { ForwardIterator next = first;
- while(++next != last)
- { if (*first == *next)
- { return first;
- }
- first = next;
- }
- }
- return last;
- }
-
-
-
- template <class InputIterator, class T, class Size>
- inline void count(InputIterator first, InputIterator last, const T& value, Size& n)
- { while (first != last)
- { if (*first++ == value)
- { ++n;
- }
- }
- }
-
-
-
- template <class ForwardIterator, class T>
- inline void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value)
- { while (first != last)
- { if (*first == old_value)
- { *first = new_value;
- }
- ++first;
- }
- }
-
-
-
- template <class InputIterator, class OutputIterator, class T>
- inline OutputIterator replace_copy
- ( InputIterator first, InputIterator last,
- OutputIterator result,
- const T& old_value,
- const T& new_value
- )
- { while (first != last)
- { *result++ = *first == old_value ? new_value : *first;
- ++first;
- }
- return result;
- }
-
-
-
- template <class InputIterator, class OutputIterator, class T>
- inline OutputIterator remove_copy
- ( InputIterator first, InputIterator last,
- OutputIterator result,
- const T& value
- )
- { while (first != last)
- { if (*first != value)
- { *result++ = *first;
- }
- ++first;
- }
- return result;
- }
-
-
-
- template <class ForwardIterator, class T>
- inline ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value)
- { first = find(first, last, value);
- ForwardIterator next = first;
- return first == last ? first : remove_copy(++next, last, first, value);
- }
-
-
-
-
-
- #endif
-
-