实现的一些内存辅助操作函数

其中有一个函数调用了std::copy,本来想自己实现这个copy函数的,不过为了能先用上这些内存辅助函数来实现我的容器类和算法,只好过一段时间来实现一个自己的高效的实现copy函数

  1 #ifndef _STL_UNINITIALIZED_H_
  2 #define _STL_UNINITIALIZED_H_
  3 
  4 #include "stl_algobase.h"
  5 #include "stl_iterator.h"
  6 #include "type_traits.h"
  7 
  8 //#include <algorithm>
  9 
 10 namespace zstd
 11 {
 12     /***************************************************************************/
 13     template<class InputIterator, class ForwardIterator, class T>
 14     ForwardIterator _uninitialized_copy(InputIterator first, InputIterator last,
 15         ForwardIterator result, T*);
 16     template<class InputIterator, class ForwardIterator>
 17     ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last,
 18         ForwardIterator result, _true_type);
 19     template<class InputIterator, class ForwardIterator>
 20     ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last,
 21         ForwardIterator result, _false_type);
 22 
 23     template<class InputIterator, class ForwardIterator>
 24     ForwardIterator 
 25     inline uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result)
 26     {
 27         return _uninitialized_copy(first, last, result, value_type(result));
 28     }
 29     template<>
 30     inline char *uninitialized_copy(const char *first, const char *last, char *result)
 31     {
 32         memmove(result, first, last - first);
 33         return result + (last - first);
 34     }
 35     template<>
 36     inline char *uninitialized_copy(char *first, char *last, char *result)
 37     {
 38         memmove(result, first, last - first);
 39         return result + (last - first);
 40     }
 41     template<>
 42     inline wchar_t *uninitialized_copy(const wchar_t *first, const wchar_t *last, wchar_t *result)
 43     {
 44         memmove(result, first, (last - first) * sizeof(wchar_t));
 45         return result + (last - first);
 46     }
 47     template<>
 48     inline wchar_t *uninitialized_copy(wchar_t *first, wchar_t *last, wchar_t *result)
 49     {
 50         memmove(result, first, (last - first) * sizeof(wchar_t));
 51         return result + (last - first);
 52     }
 53     template<class InputIterator, class ForwardIterator, class T>
 54     inline ForwardIterator _uninitialized_copy(InputIterator first, InputIterator last,
 55         ForwardIterator result, T*)
 56     {
 57         typedef typename _type_traits<T>::is_POD_type is_POD_type;
 58         return _uninitialized_copy_aux(first, last, result, is_POD_type());
 59     }
 60     template<class InputIterator, class ForwardIterator>
 61     inline ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last,
 62         ForwardIterator result, _true_type)
 63     {
 64         return std::copy(first, last, result);//到时候记得自己实现copy
 65     }
 66     template<class InputIterator, class ForwardIterator>
 67     ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last,
 68         ForwardIterator result, _false_type)
 69     {
 70         ForwardIterator cur = result;
 71         try
 72         {
 73             for (; first != last; ++first, ++cur)
 74                 construct(&*cur, *first);
 75         }
 76         catch (...)
 77         {
 78             destroy(result, cur);
 79         }
 80         return cur;
 81     }
 82 
 83     /***************************************************************************/
 84     template<class ForwardIterator, class T, class T1>
 85     void _uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x, T1*);
 86     template<class ForwardIterator, class T>
 87     void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
 88         const T& x, _true_type);
 89     template<class ForwardIterator, class T>
 90     void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
 91         const T& x, _false_type);
 92 
 93     template<class ForwardIterator, class T>
 94     void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x)
 95     {
 96         _uninitialized_fill(first, last, x, value_type(first));
 97     }
 98     template<class ForwardIterator, class T, class T1>
 99     void _uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x, T1*)
100     {
101         typedef typename _type_traits<T1>::is_POD_type is_POD;
102         _uninitialized_fill_aux(first, last, x, is_POD());
103     }
104     template<class ForwardIterator, class T>
105     void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
106         const T& x, _true_type)
107     {
108         fill(first, last, x);
109     }
110     template<class ForwardIterator, class T>
111     void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
112         const T& x, _false_type)
113     {
114         ForwardIterator cur = first;
115         for (; cur != last; ++cur)
116             construct(&*cur, x);
117     }
118 
119     /***************************************************************************/
120     template<class ForwardIterator, class Size, class T, class T1>
121     ForwardIterator _uninitialized_fill_n(ForwardIterator first,
122         Size n, const T& x, T1*);
123     template<class ForwardIterator, class Size, class T>
124     ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first,
125         Size n, const T& x, _true_type);
126     template<class ForwardIterator, class Size, class T>
127     ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first,
128         Size n, const T& x, _false_type);
129 
130     template<class ForwardIterator, class Size, class T>
131     inline ForwardIterator uninitialized_fill_n(ForwardIterator first,
132         Size n, const T& x)
133     {
134         return _uninitialized_fill_n(first, n, x, value_type(first));
135     }
136     template<class ForwardIterator, class Size, class T, class T1>
137     ForwardIterator _uninitialized_fill_n(ForwardIterator first, 
138         Size n, const T& x, T1*)
139     {
140         typedef typename _type_traits<T1>::is_POD_type is_POD;
141         return _uninitialized_fill_n_aux(first, n, x, is_POD());
142     }
143     template<class ForwardIterator, class Size, class T>
144     ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, 
145         Size n, const T& x, _true_type)
146     {
147         return fill_n(first, n, x);
148     }
149     template<class ForwardIterator, class Size, class T>
150     ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, 
151         Size n, const T& x, _false_type)
152     {
153         ForwardIterator cur = first;
154         for (; n > 0; --n, ++cur)
155             construct(&*cur, x);
156         return cur;
157     }
158 
159 }
160 #endif

 

posted @ 2014-02-28 21:37  老司机  阅读(602)  评论(0编辑  收藏  举报