-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathch8-link.cpp
165 lines (151 loc) · 3.71 KB
/
ch8-link.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include "my_bifurcate.h"
#include "my_integer.h"
#include "my_intrinsics.h"
#include "my_iterator.h"
#include "my_link.h"
#include "my_type_functions.h"
using namespace std;
template<typename I, typename S, typename Pred>
requires(ForwardIterator(I) && ForwardLinker(S) &&
IteratorType(S) == I &&
UnaryPseudoPredicate(Pred) &&
ValueType(I) == Domain(Pred))
pair< pair<I, I>, pair<I, I> >
split_linked_2(I f, I l, Pred p, S set_link)
{
// Preconditions:
// bounded_range(f, l)
I h0 = l, t0 = l;
I h1 = l, t1 = l;
linker_to_tail<S> link_to_tail(set_link);
while (f != l) {
if (p(source(f))) {
if (h1 == l) {
h1 = f;
advance_tail(t1, f);
} else {
link_to_tail(t1, f);
}
} else {
if (h0 == l) {
h0 = f;
advance_tail(t0, f);
} else {
link_to_tail(t0, f);
}
}
}
return make_pair(make_pair(h0, t0), make_pair(h1, t1));
}
template<typename T>
requires(Regular(T))
void print(const T& x)
{
cout << x << endl;
}
template<typename I>
requires(Readable(I))
struct source_less
{
bool operator()(const I& i, const I& j)
{
return source(i) < source(j);
}
};
template<typename I>
struct input_type<source_less<I>, 0>
{
typedef I type;
};
template<typename I>
requires(Readable(I))
struct source_less_than
{
typedef ValueType(I) T;
T x;
source_less_than(const T& x) : x(x) {}
bool operator()(const I& i)
{
return source(i) < x;
}
};
template<typename I>
struct input_type<source_less_than<I>, 0>
{
typedef I type;
};
template<typename I>
struct alternating
{
bool last;
alternating(bool last = false) : last(last) {}
bool operator()(const I& i)
{
last = !last;
return last;
}
};
template<typename I>
struct input_type<alternating<I>, 0>
{
typedef I type;
};
template<typename I, typename R, typename S>
requires(Readable(I) && ForwardIterator(I) &&
Relation(R) && I == Domain(R) &&
ForwardLinker(S))
pair<I, I> my_merge_sort_nonempty(I f, I l, R r, S set_link)
{
// Preconditions:
// bounded_range(f, l)
// f != l
// total_ordering(r)
typedef pair<I, I> P;
typedef triple<I, I, I> T;
if (successor(f) == l) return pair<I, I>(f, f);
pair<P, P> p = split_linked(f, l, alternating<I>(), set_link);
P p0 = my_merge_sort_nonempty(p.first.first,
p.first.second->next,
r,
set_link);
p0.second->next = NULL;
P p1 = my_merge_sort_nonempty(p.second.first,
p.second.second->next,
r,
set_link);
p1.second->next = NULL;
T t = combine_linked_nonempty(p0.first,
p0.second->next,
p1.first,
p1.second->next,
r,
set_link);
return P(t.m0, my_find_last(t.m1, t.m2));
}
template<typename T>
bool less_than(T a, T b)
{
return a < b;
}
template<typename T>
struct input_type<equal_to<T>, 0>
{
typedef T type;
};
template<typename T>
requires(Readable(T))
void print_source(T x)
{
cout << source(x) << endl;
}
int main()
{
typedef my_node<int>* C;
C r0 = new my_node<int>(4);
r0->new_left(2)->new_left(1);
r0->left->new_right(3);
r0->new_right(6)->new_left(5);
r0->right->new_right(7);
cout << weight_phased_rotating(r0) << endl;
}