Twilight Princess
Decompilation of The Legend of Zelda: Twilight Princess
Loading...
Searching...
No Matches
algorithm.h
Go to the documentation of this file.
1#ifndef MSL_ALGORITHM_H_
2#define MSL_ALGORITHM_H_
3
4#include <iterator.h>
5#include <string.h>
6#include <functional.h>
7
8namespace std {
9
10template <class ForwardIterator, class T, typename Predicate>
11inline ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& val, Predicate p) {
12 typedef typename iterator_traits<ForwardIterator>::difference_type difference_type;
13 difference_type len = std::distance(first, last);
14
15 while (len > 0) {
16 ForwardIterator i = first;
17 difference_type step = len / 2;
18 std::advance(i, step);
19
20 if (p(*i, val)) {
21 first = ++i;
22 len -= step + 1;
23 } else {
24 len = step;
25 }
26 }
27
28 return first;
29}
30
31template <class ForwardIterator, class T>
32ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& val) {
33 // For some reason, calling the other lower_bound matches for debug, but not for retail:
34 #if DEBUG
35 return lower_bound(first, last, val, std::detail::less<T, T>());
36 #else
37
38 typedef typename iterator_traits<ForwardIterator>::difference_type difference_type;
39 difference_type len = std::distance(first, last);
40
41 while (len > 0) {
42 ForwardIterator i = first;
43 difference_type step = len / 2;
44 std::advance(i, step);
45
46 if (*i < val) {
47 first = ++i;
48 len -= step + 1;
49 } else {
50 len = step;
51 }
52 }
53
54 return first;
55 #endif
56}
57
58template <class ForwardIterator, class T, class Predicate>
59ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T& val, Predicate p) {
60 typedef typename iterator_traits<ForwardIterator>::difference_type difference_type;
61 difference_type len = std::distance(first, last);
62
63 while (len > 0) {
64 ForwardIterator i = first;
65 difference_type step = len / 2;
66 std::advance(i, step);
67
68 if (!p(val, *i)) {
69 first = ++i;
70 len -= step + 1;
71 } else {
72 len = step;
73 }
74 }
75
76 return first;
77}
78
79// should be inline, but breaks JStudio/JStudio/ctb weak function order
80template<class InputIt, class UnaryPredicate>
81InputIt find_if(InputIt first, InputIt last, UnaryPredicate p) {
82 while (first != last && !p(*first)) {
83 ++first;
84 }
85 return first;
86}
87
88// fakematch: val should be a const reference, but that breaks JMessage::TResource::toMessageIndex_messageID
89template<class ForwardIterator, class T>
90inline ForwardIterator find(ForwardIterator first, ForwardIterator last, T& val) {
91 while (first != last && !(*first == val)) {
92 ++first;
93 }
94 return first;
95}
96
97/*
98template<class OutputIt, class Size, int A2>
99struct __fill_n {
100 OutputIt fill_n(OutputIt first, Size count, const unsigned long& value);
101};
102
103template<>
104unsigned long* __fill_n<unsigned long, long, 0>::fill_n(unsigned long* first, long count, const unsigned long& value) {
105 for (; count > 0; count--) {
106 *first++ = value;
107 }
108 return first;
109}
110
111template<class OutputIt, class Size, class T>
112OutputIt fill_n(OutputIt first, Size count, const T& value) {
113 return __fill_n::fill_n(first, count, value);
114}
115
116
117template<class ForwardIt, class T>
118void __fill(ForwardIt first, ForwardIt last, const T& value, std::random_access_iterator_tag param_3) {
119 fill_n(first, last - first, value);
120}
121*/
122
123template<class ForwardIt, class T>
124inline void fill(ForwardIt first, ForwardIt last, const T& value) {
125 for (; first != last; ++first){
126 *first = value;
127 }
128}
129
130template<class InputIt, class OutputIt>
131inline OutputIt copy(InputIt first, InputIt last,
132 OutputIt d_first) {
133 for (; first < last; ++first, ++d_first) {
134 *d_first = *first;
135 }
136 return d_first;
137}
138
139template <class BidirectionalIterator1, class BidirectionalIterator2>
140inline BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result) {
141 while (last != first)
142 *--result = *--last;
143 return result;
144}
145
146template <class T, bool A>
148{
149 static T* copy_backward(T* first, T* last, T* result)
150 {
151 while (last > first)
152 *--result = *--last;
153 return result;
154 }
155};
156
157template <class T>
158struct __copy_backward<T, true>
159{
160 static T* copy_backward(T* first, T* last, T* result)
161 {
162#if DEBUG
163 size_t n = static_cast<size_t>(last - first);
164 result -= n;
165 memmove(result, first, n*sizeof(T));
166 return result;
167#else
168 while (last > first)
169 *--result = *--last;
170 return result;
171#endif
172 }
173};
174
175template <class T>
176inline T* copy_backward(T* first, T* last, T* result) {
177 return __copy_backward<T, true>::copy_backward(first, last, result);
178}
179
180} // namespace std
181
182#endif
p
Definition e_acos.c:98
n
Definition e_fmod.c:112
int i
Definition e_pow.c:165
Definition JMATrigonometric.cpp:12
iterator_traits< InputIterator >::difference_type distance(InputIterator first, InputIterator last)
Definition iterator.h:96
OutputIt copy(InputIt first, InputIt last, OutputIt d_first)
Definition algorithm.h:131
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
Definition algorithm.h:81
void advance(InputIterator &i, Distance n)
Definition iterator.h:67
BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result)
Definition algorithm.h:140
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T &val, Predicate p)
Definition algorithm.h:11
ForwardIterator find(ForwardIterator first, ForwardIterator last, T &val)
Definition algorithm.h:90
void fill(ForwardIt first, ForwardIt last, const T &value)
Definition algorithm.h:124
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T &val, Predicate p)
Definition algorithm.h:59
void * memmove(void *dst, const void *src, size_t n)
Definition mem.c:3
static T * copy_backward(T *first, T *last, T *result)
Definition algorithm.h:160
Definition algorithm.h:148
static T * copy_backward(T *first, T *last, T *result)
Definition algorithm.h:149
Definition functional.h:9
Iterator::difference_type difference_type
Definition iterator.h:15