diff --git a/TinySTL/UninitializedFunctions.h b/TinySTL/UninitializedFunctions.h new file mode 100644 index 0000000..8869b1e --- /dev/null +++ b/TinySTL/UninitializedFunctions.h @@ -0,0 +1,102 @@ +#ifndef _UNINITIALIZED_FUNCTIONS_H_ +#define _UNINITIALIZED_FUNCTIONS_H_ + +#include "Construct.h" +#include "Iterator.h" +#include "TypeTraits.h" + +#include + +namespace TinySTL{ + + /***************************************************************************/ + template + ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, + ForwardIterator result, _true_type); + template + ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, + ForwardIterator result, _false_type); + + template + ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result){ + typedef typename _type_traits::value_type>::is_POD_type isPODType; + return _uninitialized_copy_aux(first, last, result, isPODType()); + } + template + ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, + ForwardIterator result, _true_type){ + memcpy(result, first, (last - first) * sizeof(*first)); + return result + (last - first); + } + template + ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, + ForwardIterator result, _false_type){ + int i = 0; + for (; first != last; ++first, ++i){ + construct((result + i), *first); + } + return (result + i); + } + + /***************************************************************************/ + template + void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, + const T& value, _true_type); + template + void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, + const T& value, _false_type); + + template + void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& value){ + typedef typename _type_traits::is_POD_type isPODType; + _uninitialized_fill_aux(first, last, value, isPODType()); + } + template + void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, + const T& value, _true_type){ + //TODO + //换成自己的fill函数 + std::fill(first, last, value); + } + template + void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, + const T& value, _false_type){ + int i = 0; + for (; first != last; ++first, ++i){ + construct(first, value); + } + } + + /***************************************************************************/ + template + ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, + Size n, const T& x, _true_type); + template + ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, + Size n, const T& x, _false_type); + + template + inline ForwardIterator uninitialized_fill_n(ForwardIterator first, + Size n, const T& x){ + typedef typename _type_traits::is_POD_type isPODType; + return _uninitialized_n_fill_aux(first, n, x, isPODType()); + } + template + ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, + Size n, const T& x, _true_type){ + //TODO + //换成自己的fill_n函数 + return std::fill_n(first, n, x); + } + template + ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, + Size n, const T& x, _false_type){ + int i = 0; + for (; i != n;){ + construct((first + i), x); + } + return (first + i); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index d56e9f6..224c42f 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -17,7 +17,7 @@ int main(){ auto last = p + 100; int array[100]; TinySTL::uninitialized_fill_n(array, 100, 88); - for (auto n : array){ cout << n << endl; } + for (auto n : array){ cout << n << endl; } system("pause"); return 0; } \ No newline at end of file