2 #ifndef _STL_ALGORITHM_H
3 #define _STL_ALGORITHM_H
7 #include "cstring" // for memmove
26 void __copy_pod(const T *first, const T *last, T *result_start)
29 static_cast<void *>(result_start),
30 static_cast<const void *>(first),
31 (last - first) * sizeof(T)
37 /* naive implementation */
38 template <class InputIterator, class OutputIterator,
39 bool IsPod = is_pod<typename iterator_traits<OutputIterator>::value_type>::value>
41 static OutputIterator run(InputIterator first, InputIterator last, OutputIterator result)
43 while (first != last){
44 *result++ = move(*first++);
50 /* pod implementation */
52 struct __move<T *, typename remove_cv<T>::type *, true> {
53 static T *run(const T *first, const T *last, typename remove_cv<T>::type *result)
55 __copy_pod(first, last, result);
56 return result + (last - first);
60 template <class InputIterator, class OutputIterator>
61 OutputIterator move(InputIterator first, InputIterator last, OutputIterator result)
63 return const_cast<OutputIterator>(__move<InputIterator, OutputIterator>::run(first, last, result));
66 /*** MOVE BACKWARD ***/
68 /* naive implementation */
69 template <class InputIterator, class OutputIterator,
70 bool IsPod = is_pod<typename iterator_traits<OutputIterator>::value_type>::value>
71 struct __move_backward {
72 static OutputIterator run(InputIterator first, InputIterator last, OutputIterator result)
74 while (last != first){
75 *--result = move(*--last);
81 /* pod implementation */
83 struct __move_backward<T *, typename remove_cv<T>::type *, true> {
84 static T *run(const T *first, const T *last, typename remove_cv<T>::type *result)
86 __copy_pod(first, last, result - (last - first));
91 template <class InputIterator, class OutputIterator>
92 OutputIterator move_backward(InputIterator first, InputIterator last, OutputIterator result)
94 return const_cast<OutputIterator>(__move_backward<InputIterator, OutputIterator>::run(first, last, result));
99 /* naive implementation */
100 template <class InputIterator, class OutputIterator,
101 bool IsPod = is_pod<typename iterator_traits<OutputIterator>::value_type>::value>
103 static OutputIterator run(InputIterator first, InputIterator last, OutputIterator result)
105 while (first != last){
106 *result++ = *first++;
112 /* pod implementation */
114 struct __copy<T *, typename remove_cv<T>::type *, true> {
115 static T *run(const T *first, const T *last, typename remove_cv<T>::type *result)
117 __copy_pod(first, last, result);
118 return result + (last - first);
122 template <class InputIterator, class OutputIterator>
123 OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
125 return const_cast<OutputIterator>(__copy<InputIterator, OutputIterator>::run(first, last, result));
128 /*** COPY BACKWARD ***/
130 /* naive implementation */
131 template <class InputIterator, class OutputIterator,
132 bool IsPod = is_pod<typename iterator_traits<OutputIterator>::value_type>::value>
133 struct __copy_backward {
134 static OutputIterator run(InputIterator first, InputIterator last, OutputIterator result)
136 while (last != first){
143 /* pod implementation */
145 struct __copy_backward<T *, typename remove_cv<T>::type *, true> {
146 static T *run(const T *first, const T *last, typename remove_cv<T>::type *result)
148 __copy_pod(first, last, result - (last - first));
153 template <class InputIterator, class OutputIterator>
154 OutputIterator copy_backward(InputIterator first, InputIterator last, OutputIterator result)
156 return const_cast<OutputIterator>(__copy_backward<InputIterator, OutputIterator>::run(first, last, result));
159 template <class OutputIterator, class Size, class T>
160 void fill_n(OutputIterator first, Size n, const T &value)
162 // naive implementation