YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
algorithm.hpp
浏览该文件的文档.
1 /*
2  © 2010-2014 FrankHB.
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(is_pod<remove_reference_t<_type>>::value,
59  "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(is_pod<remove_reference_t<_type>>::value,
84  "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(is_pod<remove_reference_t<_type>>::value,
102  "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 
128 template<typename _tFwd>
129 _tFwd
130 stable_range_unique(_tFwd first, _tFwd last)
131 {
132  _tFwd result(first);
133 
134  for(_tFwd i(first); i != last; ++i)
135  if(std::find(first, result, *i) == result)
136  {
137  using std::swap;
138 
139  swap(*i, *result);
140  ++result;
141  }
142  return result;
143 }
144 
145 } // namespace ystdex;
146 
147 #endif
148 
_tFwd stable_range_unique(_tFwd first, _tFwd last)
去除迭代器指定的范围中的重复元素,且不改变元素之间的相对顺序。
Definition: algorithm.hpp:130
static auto first(const _tIterator &i) -> decltype((i->first))
Definition: iterator.hpp:759
typename remove_reference< _type >::type remove_reference_t
Definition: type_op.hpp:234
_type * pod_fill(_type *first, _type *last, const _type &value)
Definition: algorithm.hpp:56
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
_type * pod_copy_n(const _type *first, size_t n, _type *result)
Definition: algorithm.hpp:81
_type * pod_move_n(const _type *first, size_t n, _type *result)
Definition: algorithm.hpp:99
_type * pod_move(const _type *first, const _type *last, _type *result)
Definition: algorithm.hpp:110
_type * pod_copy(const _type *first, const _type *last, _type *result)
Definition: algorithm.hpp:92