-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathout.go
189 lines (155 loc) · 3.06 KB
/
out.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import "fmt"
type Person struct {
name string
age int
}
func (p Person) String() string {
return fmt.Sprintf("%s is %d years old", p.name, p.age)
}
func (p Person) Name() string {
return p.name
}
func (p Person) Age() int {
return p.age
}
func Min_int() func(int, int) bool {
return func(x, y int) bool { return x < y }
}
type Heap_int struct {
elems []int
less func(int, int) bool
}
func (h *Heap_int) Size() int {
return len(h.elems)
}
func (h *Heap_int) Push(x int) {
h.elems = append(h.elems, x)
j := len(h.elems) - 1
for {
i := (j - 1) / 2
if i == j || !h.less(h.elems[j], h.elems[i]) {
break
}
h.elems[i], h.elems[j] = h.elems[j], h.elems[i]
j = i
}
}
func (h *Heap_int) Top() (top int, ok bool) {
if len(h.elems) == 0 {
ok = false
return
}
return h.elems[0], true
}
func (h *Heap_int) Pop() (top int, ok bool) {
if len(h.elems) == 0 {
ok = false
return
}
n := len(h.elems) - 1
h.elems[0], h.elems[n] = h.elems[n], h.elems[0]
i := 0
for {
j1 := 2*i + 1
if j1 >= n || j1 < 0 {
break
}
j := j1
if j2 := j1 + 1; j2 < n && h.less(h.elems[j2], h.elems[j1]) {
j = j2
}
if !h.less(h.elems[j], h.elems[i]) {
break
}
h.elems[i], h.elems[j] = h.elems[j], h.elems[i]
i = j
}
top = h.elems[n]
h.elems = h.elems[:n]
return top, true
}
func NewHeap_int(less func(x, y int) bool) *Heap_int { return &Heap_int{less: less} }
func MaxBy_int_Person(by func(Person) int) func(Person, Person) bool {
return func(x, y Person) bool { return by(x) > by(y) }
}
type Heap_Person struct {
elems []Person
less func(Person, Person) bool
}
func (h *Heap_Person) Size() int {
return len(h.elems)
}
func (h *Heap_Person) Push(x Person) {
h.elems = append(h.elems, x)
j := len(h.elems) - 1
for {
i := (j - 1) / 2
if i == j || !h.less(h.elems[j], h.elems[i]) {
break
}
h.elems[i], h.elems[j] = h.elems[j], h.elems[i]
j = i
}
}
func (h *Heap_Person) Top() (top Person, ok bool) {
if len(h.elems) == 0 {
ok = false
return
}
return h.elems[0], true
}
func (h *Heap_Person) Pop() (top Person, ok bool) {
if len(h.elems) == 0 {
ok = false
return
}
n := len(h.elems) - 1
h.elems[0], h.elems[n] = h.elems[n], h.elems[0]
i := 0
for {
j1 := 2*i + 1
if j1 >= n || j1 < 0 {
break
}
j := j1
if j2 := j1 + 1; j2 < n && h.less(h.elems[j2], h.elems[j1]) {
j = j2
}
if !h.less(h.elems[j], h.elems[i]) {
break
}
h.elems[i], h.elems[j] = h.elems[j], h.elems[i]
i = j
}
top = h.elems[n]
h.elems = h.elems[:n]
return top, true
}
func NewHeap_Person(less func(x, y Person) bool) *Heap_Person { return &Heap_Person{less: less} }
func main() {
numbers := NewHeap_int(Min_int())
for x := 10; x >= 1; x-- {
numbers.Push(x)
}
for {
x, ok := numbers.Pop()
if !ok {
break
}
fmt.Print(x, " ")
}
fmt.Println()
fmt.Println()
people := NewHeap_Person(MaxBy_int_Person(Person.Age))
for _, person := range []Person{{"Michal", 23}, {"Viktória", 20}, {"Jano", 21}, {"Martin", 18}} {
people.Push(person)
}
for {
p, ok := people.Pop()
if !ok {
break
}
fmt.Println(p)
}
}