Sierra Toolkit  Version of the Day
utility_rdestl.h
1 #ifndef RDESTL_UTILITY_H
2 #define RDESTL_UTILITY_H
3 
4 #include <stk_util/util/rdestl_common.h>
5 #include <new>
6 
7 namespace rde
8 {
9 namespace internal
10 {
11  template<typename T>
12  void copy_n(const T* first, size_t n, T* result, int_to_type<false>)
13  {
14  const T* last = first + n;
15  //while (first != last)
16  // *result++ = *first++;
17  switch (n & 0x3)
18  {
19  case 0:
20  while (first != last)
21  {
22  *result++ = *first++;
23  case 3: *result++ = *first++;
24  case 2: *result++ = *first++;
25  case 1: *result++ = *first++;
26  }
27  }
28  }
29  template<typename T>
30  void copy_n(const T* first, size_t n, T* result, int_to_type<true>)
31  {
32  RDE_ASSERT(result >= first + n || result < first);
33  Sys::MemCpy(result, first, n * sizeof(T));
34  }
35 
36  template<typename T>
37  void copy(const T* first, const T* last, T* result, int_to_type<false>)
38  {
39  while (first != last)
40  *result++ = *first++;
41  }
42  template<typename T>
43  void copy(const T* first, const T* last, T* result, int_to_type<true>)
44  {
45  const size_t n = reinterpret_cast<const char*>(last) - reinterpret_cast<const char*>(first);
46  Sys::MemCpy(result, first, n);
47  }
48 
49  template<typename T> RDE_FORCEINLINE
50  void move_n(const T* from, size_t n, T* result, int_to_type<false>)
51  {
52  for (int i = int(n) - 1; i >= 0; --i)
53  result[i] = from[i];
54  }
55  template<typename T> RDE_FORCEINLINE
56  void move_n(const T* first, size_t n, T* result, int_to_type<true>)
57  {
58  Sys::MemMove(result, first, n * sizeof(T));
59  }
60 
61  template<typename T> RDE_FORCEINLINE
62  void move(const T* first, const T* last, T* result, int_to_type<false>)
63  {
64  while (--last >= first)
65  *result++ = *last;
66  }
67  template<typename T> RDE_FORCEINLINE
68  void move(const T* first, const T* last, T* result, int_to_type<true>)
69  {
70  // Meh, MSVC does pretty stupid things here.
71  //memmove(result, first, (last - first) * sizeof(T));
72  const size_t n = reinterpret_cast<const char*>(last) - reinterpret_cast<const char*>(first);
73  //const size_t n = (last - first) * sizeof(T);
74  Sys::MemMove(result, first, n);
75  }
76 
77 
78  template<typename T>
79  void copy_construct_n(const T* first, size_t n, T* result, int_to_type<false>)
80  {
81  for (size_t i = 0; i < n; ++i)
82  new (result + i) T(first[i]);
83  }
84  template<typename T>
85  void copy_construct_n(const T* first, size_t n, T* result, int_to_type<true>)
86  {
87  RDE_ASSERT(result >= first + n || result < first);
88  Sys::MemCpy(result, first, n * sizeof(T));
89  }
90 
91  template<typename T>
92  void destruct_n(T* first, size_t n, int_to_type<false>)
93  {
94  // For unknown reason MSVC cant see reference to first here...
95  sizeof(first);
96  for (size_t i = 0; i < n; ++i)
97  (first + i)->~T();
98  }
99  template<typename T> RDE_FORCEINLINE
100  void destruct_n(T*, size_t, int_to_type<true>)
101  {
102  // Nothing to do, no destructor needed.
103  }
104 
105  template<typename T>
106  void destruct(T* mem, int_to_type<false>)
107  {
109  mem->~T();
110  }
111  template<typename T> RDE_FORCEINLINE
112  void destruct(T*, int_to_type<true>)
113  {
114  // Nothing to do, no destructor needed.
115  }
116 
117  template<typename T>
118  void construct(T* mem, int_to_type<false>)
119  {
120  new (mem) T();
121  }
122  template<typename T> RDE_FORCEINLINE
123  void construct(T*, int_to_type<true>)
124  {
125  // Nothing to do
126  }
127 
128  template<typename T> RDE_FORCEINLINE
129  void copy_construct(T* mem, const T& orig, int_to_type<false>)
130  {
131  new (mem) T(orig);
132  }
133  template<typename T> RDE_FORCEINLINE
134  void copy_construct(T* mem, const T& orig, int_to_type<true>)
135  {
136  mem[0] = orig;
137  }
138 
139  template<typename T>
140  void construct_n(T* to, size_t count, int_to_type<false>)
141  {
142  sizeof(to);
143  for (size_t i = 0; i < count; ++i)
144  new (to + i) T();
145  }
146  template<typename T> inline
147  void construct_n(T*, int, int_to_type<true>)
148  {
149  // trivial ctor, nothing to do.
150  }
151 
152  // Tests if all elements in range are ordered according to pred.
153  template<class TIter, class TPred>
154  void test_ordering(TIter first, TIter last, const TPred& pred)
155  {
156 #if RDE_DEBUG
157  if (first != last)
158  {
159  TIter next = first;
160  if (++next != last)
161  {
162  RDE_ASSERT(pred(*first, *next));
163  first = next;
164  }
165  }
166 #else
167  sizeof(first); sizeof(last); sizeof(pred);
168 #endif
169  }
170 
171  template<typename T1, typename T2, class TPred> inline
172  bool debug_pred(const TPred& pred, const T1& a, const T2& b)
173  {
174 #if RDE_DEBUG
175  if (pred(a, b))
176  {
177  RDE_ASSERT(!pred(b, a));
178  return true;
179  }
180  else
181  {
182  return false;
183  }
184 #else
185  return pred(a, b);
186 #endif
187  }
188 } // namespace internal
189 
190 } // namespace rde
191 
192 //-----------------------------------------------------------------------------
193 #endif // #ifndef RDESTL_UTILITY_H
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
eastl::iterator_traits< InputIterator >::difference_type count(InputIterator first, InputIterator last, const T &value)