UninitializedFunctions.h 3.6 KB
Newer Older
邹晓航 已提交
1 2 3
#ifndef _UNINITIALIZED_FUNCTIONS_H_
#define _UNINITIALIZED_FUNCTIONS_H_

邹晓航 已提交
4
#include "Algorithm.h"
邹晓航 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "Construct.h"
#include "Iterator.h"
#include "TypeTraits.h"

namespace TinySTL{

	/***************************************************************************/
	template<class InputIterator, class ForwardIterator>
	ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last,
		ForwardIterator result, _true_type);
	template<class InputIterator, class ForwardIterator>
	ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last,
		ForwardIterator result, _false_type);

	template<class InputIterator, class ForwardIterator>
	ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result){
		typedef typename _type_traits<iterator_traits<InputIterator>::value_type>::is_POD_type isPODType;
		return _uninitialized_copy_aux(first, last, result, isPODType());
	}
	template<class InputIterator, class ForwardIterator>
	ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last,
		ForwardIterator result, _true_type){
		memcpy(result, first, (last - first) * sizeof(*first));
		return result + (last - first);
	}
	template<class InputIterator, class ForwardIterator>
	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<class ForwardIterator, class T>
	void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
		const T& value, _true_type);
	template<class ForwardIterator, class T>
	void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
		const T& value, _false_type);

	template<class ForwardIterator, class T>
	void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& value){
		typedef typename _type_traits<T>::is_POD_type isPODType;
		_uninitialized_fill_aux(first, last, value, isPODType());
	}
	template<class ForwardIterator, class T>
	void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
		const T& value, _true_type){
邹晓航 已提交
56
		fill(first, last, value);
邹晓航 已提交
57 58 59 60 61 62 63 64 65 66 67 68
	}
	template<class ForwardIterator, class T>
	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<class ForwardIterator, class Size, class T>
69
	ForwardIterator _uninitialized_fill_n_aux(ForwardIterator first,
邹晓航 已提交
70 71
		Size n, const T& x, _true_type);
	template<class ForwardIterator, class Size, class T>
bug fix  
邹晓航 已提交
72
	ForwardIterator _uninitialized_fill_n_aux(ForwardIterator first,
邹晓航 已提交
73 74 75 76 77 78
		Size n, const T& x, _false_type);

	template<class ForwardIterator, class Size, class T>
	inline ForwardIterator uninitialized_fill_n(ForwardIterator first,
		Size n, const T& x){
		typedef typename _type_traits<T>::is_POD_type isPODType;
bug fix  
邹晓航 已提交
79
		return _uninitialized_fill_n_aux(first, n, x, isPODType());
邹晓航 已提交
80 81
	}
	template<class ForwardIterator, class Size, class T>
bug fix  
邹晓航 已提交
82
	ForwardIterator _uninitialized_fill_n_aux(ForwardIterator first,
邹晓航 已提交
83
		Size n, const T& x, _true_type){
邹晓航 已提交
84
		return fill_n(first, n, x);
邹晓航 已提交
85 86
	}
	template<class ForwardIterator, class Size, class T>
bug fix  
邹晓航 已提交
87
	ForwardIterator _uninitialized_fill_n_aux(ForwardIterator first,
邹晓航 已提交
88 89
		Size n, const T& x, _false_type){
		int i = 0;
90
		for (; i != n; ++i){
bug fix  
邹晓航 已提交
91
			construct((T*)(first + i), x);
邹晓航 已提交
92 93 94 95 96
		}
		return (first + i);
	}
}

97
#endif