YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
algorithm.hpp
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2010 - 2013.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #ifndef YB_INC_ystdex_algorithm_hpp_
29 #define YB_INC_ystdex_algorithm_hpp_ 1
30 
31 #include "../ydef.h"
32 #include <algorithm>
33 #include <cstring> // for std::memcpy, std::memmove;
34 
35 namespace ystdex
36 {
37 
54 template <class _type>
55 inline _type*
56 pod_fill(_type* first, _type* last, const _type& value)
57 {
58  static_assert(std::is_pod<typename std::remove_reference<_type>::type>
59  ::value, "Non-POD type found @ pod_fill;");
60 
61  switch((last - first) & 7)
62  {
63  case 0:
64  while(first != last)
65  {
66  *first = value; ++first;
67  case 7: *first = value; ++first;
68  case 6: *first = value; ++first;
69  case 5: *first = value; ++first;
70  case 4: *first = value; ++first;
71  case 3: *first = value; ++first;
72  case 2: *first = value; ++first;
73  case 1: *first = value; ++first;
74  }
75  }
76  return last;
77 }
78 
79 template <class _type>
80 inline _type*
81 pod_copy_n(const _type* first, size_t n, _type* result)
82 {
83  static_assert(std::is_pod<typename std::remove_reference<_type>::type>
84  ::value, "Non-POD type found @ pod_copy_n;");
85 
86  std::memcpy(result, first, sizeof(*first) * n);
87  return result + n;
88 }
89 
90 template <class _type>
91 inline _type*
92 pod_copy(const _type* first, const _type* last, _type* result)
93 {
94  return ystdex::pod_copy_n(first, last - first, result);
95 }
96 
97 template <class _type>
98 inline _type*
99 pod_move_n(const _type* first, size_t n, _type* result)
100 {
101  static_assert(std::is_pod<typename std::remove_reference<_type>::type>
102  ::value, "Non-POD type found @ pod_move_n;");
103 
104  std::memmove(result, first, sizeof(*first) * n);
105  return result + n;
106 }
107 
108 template <class _type>
109 inline _type*
110 pod_move(const _type* first, const _type* last, _type* result)
111 {
112  return ystdex::pod_move_n(first, last - first, result);
113 }
115 
116 
127 template<typename _tForward>
128 _tForward
129 stable_range_unique(_tForward first, _tForward last)
130 {
131  _tForward result(first);
132 
133  for(_tForward i(first); i != last; ++i)
134  if(std::find(first, result, *i) == result)
135  {
136  std::swap(*i, *result);
137  ++result;
138  }
139  return result;
140 }
141 
142 } // namespace ystdex;
143 
144 #endif
145