11 template <
typename INT>
14 s <<
"0x" << std::hex <<
i;
19 template <
typename INT>
24 s <<
"0b" <<
b.to_string().substr(32 -
n, 32);
25 }
else if (
sizeof(
i) <= 8) {
27 s <<
"0b" <<
b.to_string().substr(64 -
n, 64);
33 template <
typename T,
size_t N>
34 constexpr
size_t array_size(
T (&)[
N]) {
39 template <
typename T,
size_t N>
43 for (
size_t i = 0;
i <
N; ++
i) {
60 _reversed(
T& _t) :
t(_t) {}
61 decltype(
t.rbegin()) begin() {
return t.rbegin(); }
62 decltype(
t.rend())
end() {
return t.rend(); }
66 details::_reversed<T> reversed(
T&
t) {
67 return details::_reversed<T>(
t);
72 template <
class STR = std::
string>
73 std::vector<STR> split_string(
const std::string&
s,
char c =
' ',
char d =
' ') {
75 const char*
str =
s.c_str();
77 const char* begin =
str;
81 }
while (0 != *
str++);
87 template <
class T1,
class T2>
88 void flatten_container(
const T1&
input, T2&
output) {
89 typename T1::const_iterator it;
90 for (it =
input.begin(); it !=
input.end(); ++it) {
97 struct is_map_of_vectors :
public std::false_type {};
99 template <
typename T1,
typename T2>
100 struct is_map_of_vectors<
std::
map<T1, std::vector<T2> > > :
public std::true_type {};
103 template <
typename Map>
104 void merge_map_into_map(
const Map& map1, Map& map2) {
106 typedef typename Map::iterator
Iterator;
107 typedef typename Map::mapped_type
Container;
109 for (
auto& kv1 : map1) {
110 std::pair<Iterator, bool>
ins = map2.insert(kv1);
113 const Container* container1 = &(kv1.second);
115 container2->insert(container2->end(), container1->begin(), container1->end());
125 template <
class ForwardIt,
class BinaryPredicate,
class BinaryOp>
126 ForwardIt adjacent_cluster(ForwardIt
first, ForwardIt
last, BinaryPredicate adjacent, BinaryOp cluster) {
143 template <
typename RandomAccessIterator,
typename Compare = std::less<> >
144 void merge_sort_merge(RandomAccessIterator
first,
145 RandomAccessIterator middle,
146 RandomAccessIterator
last,
150 typedef typename std::iterator_traits<RandomAccessIterator>::pointer pointer;
151 std::pair<pointer, std::ptrdiff_t>
p = std::get_temporary_buffer<value_type>(len);
152 pointer
buf =
p.first;
155 RandomAccessIterator first1 =
first;
156 RandomAccessIterator last1 = middle;
157 RandomAccessIterator first2 = middle;
158 RandomAccessIterator last2 =
last;
160 while (first1 != last1 && first2 != last2) {
161 if (
cmp(*first2, *first1)) {
167 while (first1 != last1) {
170 while (first2 != last2) {
176 std::return_temporary_buffer(
p.first);
180 template <
typename RandomAccessIterator,
typename Compare = std::less<> >
181 void merge_sort(RandomAccessIterator
first, RandomAccessIterator
last, Compare
cmp) {
194 template <
typename RandomAccessIterator,
typename Compare,
typename Compare3>
195 void merge_sort_merge3(RandomAccessIterator
first,
196 RandomAccessIterator one_third,
197 RandomAccessIterator two_third,
198 RandomAccessIterator
last,
203 typedef typename std::iterator_traits<RandomAccessIterator>::pointer pointer;
204 std::pair<pointer, std::ptrdiff_t>
p = std::get_temporary_buffer<value_type>(len);
205 pointer
buf =
p.first;
208 RandomAccessIterator first1 =
first;
209 RandomAccessIterator last1 = one_third;
210 RandomAccessIterator first2 = one_third;
211 RandomAccessIterator last2 = two_third;
212 RandomAccessIterator first3 = two_third;
213 RandomAccessIterator last3 =
last;
215 while (first1 != last1 && first2 != last2 && first3 != last3) {
216 int rr = cmp3(*first1, *first2, *first3);
219 }
else if (
rr == 1) {
221 }
else if (
rr == 2) {
226 if (first3 == last3) {
228 }
else if (first2 == last2) {
231 }
else if (first1 == last1) {
238 while (first1 != last1 && first2 != last2) {
239 if (
cmp(*first2, *first1)) {
245 while (first1 != last1) {
248 while (first2 != last2) {
254 std::return_temporary_buffer(
p.first);
258 template <
typename RandomAccessIterator,
typename Compare,
typename Compare3>
259 void merge_sort3(RandomAccessIterator
first, RandomAccessIterator
last, Compare
cmp, Compare3 cmp3) {
263 RandomAccessIterator two_third =
std::next(
first, (len + 2) / 3 * 2);
264 merge_sort3(
first, one_third,
cmp, cmp3);
265 merge_sort3(one_third, two_third,
cmp, cmp3);
266 merge_sort3(two_third,
last,
cmp, cmp3);
267 merge_sort_merge3(
first, one_third, two_third,
last,
cmp, cmp3);
272 template <
typename RandomAccessIterator,
typename Compare,
typename Compare3>
273 void merge_sort3_with_hint(
274 RandomAccessIterator
first, RandomAccessIterator
last, Compare
cmp, Compare3 cmp3, std::ptrdiff_t
d) {
279 merge_sort3(
first, one_third,
cmp, cmp3);
280 merge_sort3(one_third, two_third,
cmp, cmp3);
281 merge_sort3(two_third,
last,
cmp, cmp3);
282 merge_sort_merge3(
first, one_third, two_third,
last,
cmp, cmp3);