home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / dreamscape / source / Dreamscape / Headers / std / h / algorithm next >
Encoding:
Text File  |  1996-07-17  |  2.5 KB  |  129 lines

  1. // algorithm.h
  2. // A few of the STL algorithms components
  3. //
  4. // By Paul Field
  5. // Based on the STL implementation: (c) 1994 Hewlett-Packard Company
  6. // v1.10 - 26/2/1996
  7.  
  8. // Modified by Mark Seaborn 16/5/1996 - all functions now inlined
  9.  
  10. // Algorithms have not been tested.
  11. // Other STL algorithms could be implemented.
  12.  
  13.  
  14. #ifndef algorithm_h
  15. #define algorithm_h
  16.  
  17.  
  18. template <class T>
  19. inline const T min(const T a, const T b)
  20.  { return b < a ? b : a;
  21.  }
  22.  
  23.  
  24.  
  25.  
  26. template <class T>
  27. inline const T max(const T a, const T b)
  28.  { return  a < b ? b : a;
  29.  }
  30.  
  31.  
  32.  
  33.  
  34. template <class InputIterator, class T>
  35. inline InputIterator find(InputIterator first, InputIterator last, const T& value)
  36.  { while (first != last && *first != value)
  37.     { ++first;
  38.     }
  39.    return first;
  40.  }
  41.  
  42.  
  43.  
  44. template <class ForwardIterator>
  45. inline ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last)
  46.  { if (first != last)
  47.     { ForwardIterator next = first;
  48.       while(++next != last)
  49.        { if (*first == *next)
  50.           { return first;
  51.           }
  52.          first = next;
  53.        }
  54.     }
  55.    return last;
  56.  }
  57.  
  58.  
  59.  
  60. template <class InputIterator, class T, class Size>
  61. inline void count(InputIterator first, InputIterator last, const T& value, Size& n)
  62.  { while (first != last)
  63.     { if (*first++ == value)
  64.        { ++n;
  65.        }
  66.     }
  67.  }
  68.  
  69.  
  70.  
  71. template <class ForwardIterator, class T>
  72. inline void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value)
  73.  { while (first != last)
  74.     { if (*first == old_value)
  75.        { *first = new_value;
  76.        }
  77.       ++first;
  78.     }
  79.  }
  80.  
  81.  
  82.  
  83. template <class InputIterator, class OutputIterator, class T>
  84. inline OutputIterator replace_copy
  85.  ( InputIterator first, InputIterator last,
  86.    OutputIterator result,
  87.    const T& old_value,
  88.    const T& new_value
  89.  )
  90.  { while (first != last)
  91.     { *result++ = *first == old_value ? new_value : *first;
  92.       ++first;
  93.     }
  94.    return result;
  95.  }
  96.  
  97.  
  98.  
  99. template <class InputIterator, class OutputIterator, class T>
  100. inline OutputIterator remove_copy
  101.  ( InputIterator first, InputIterator last,
  102.    OutputIterator result,
  103.    const T& value
  104.  )
  105.  { while (first != last)
  106.     { if (*first != value)
  107.        { *result++ = *first;
  108.        }
  109.       ++first;
  110.     }
  111.    return result;
  112.  }
  113.  
  114.  
  115.  
  116. template <class ForwardIterator, class T>
  117. inline ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value)
  118.  { first = find(first, last, value);
  119.    ForwardIterator next = first;
  120.    return first == last ? first : remove_copy(++next, last, first, value);
  121.  }
  122.  
  123.  
  124.  
  125.  
  126.  
  127. #endif
  128.  
  129.