-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunnelsort.go
471 lines (412 loc) · 8.5 KB
/
funnelsort.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Package funnelsort implements lazy funnel sort, a cache-oblivious
// sorting algorithm.
//
// http://courses.csail.mit.edu/6.851/spring12/lectures/L09.html
// http://www.cs.amherst.edu/~ccm/cs34/papers/a2_2-brodal.pdf
//
package funnelsort
import (
"math"
"sort"
"syscall"
)
type Item interface {
Less(b Item) bool
Bytes() []byte
}
// Function funnelsort uses to create a new item from a slice of
// bytes. The function must be set before calling FunnelSort.
var NewItem func(b []byte) Item
// The maximum length of an item in bytes.
var MaxItemLength = 4096
type Reader interface {
Read() Item
}
type Writer interface {
Write(i Item)
}
type Buffer interface {
Reader
Writer
Peek() Item
Empty() bool
Full() bool
Reset()
Close()
}
type MBuffer struct {
unread uint64
buffer []byte
off int
}
func (b *MBuffer) Close() {
b.unmap()
}
func (b *MBuffer) Empty() bool {
return b.unread == 0
}
func (b *MBuffer) Full() bool {
return len(b.buffer)+MaxItemLength >= cap(b.buffer)
}
func (b *MBuffer) Reset() {
b.unread = 0
b.buffer = b.buffer[0:0]
b.off = 0
}
func (b *MBuffer) Write(a Item) {
b.unread += 1
// The following must hold: len(b.buffer) + len(a.Bytes()) <
// cap(b.buffer) which is currently ensured via MaxItemLength
b.buffer = append(b.buffer, a.Bytes()...)
}
func (b *MBuffer) Peek() (item Item) {
if b.unread > 0 {
item = NewItem(b.buffer[b.off:])
}
return
}
func (b *MBuffer) Read() (item Item) {
if b.unread > 0 {
b.unread -= 1
item = NewItem(b.buffer[b.off:])
b.off += len(item.Bytes())
}
return item
}
func (b *MBuffer) unmap() {
if cap(b.buffer) > 0 {
if err := syscall.Munmap(b.buffer[0:cap(b.buffer)]); err == nil {
b.buffer = nil
} else {
panic(err)
}
}
}
func NewMBuffer(capacity int) (buffer *MBuffer) {
if mmap, err := syscall.Mmap(-1, 0, capacity, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE); err == nil {
buffer = &MBuffer{buffer: mmap[0:0]}
} else {
panic(err)
}
return
}
type MultiBuffer struct {
max, unread uint64
buffers []Buffer
read, write int
}
func (mb *MultiBuffer) Empty() bool {
return mb.unread == 0
}
func (mb *MultiBuffer) Full() bool {
return mb.max != 0 && mb.unread == mb.max
}
func (mb *MultiBuffer) Reset() {
mb.unread = 0
mb.read = 0
mb.write = 0
for _, b := range mb.buffers {
b.Reset()
}
}
func (mb *MultiBuffer) getBuffer(i int) Buffer {
n := len(mb.buffers)
if i < n {
return mb.buffers[i]
} else if i == n {
b := NewMBuffer(1 << 28)
mb.buffers = append(mb.buffers, b)
return b
}
panic("tried to get buffer out of range")
}
func (mb *MultiBuffer) Write(a Item) {
mb.unread += 1
for w := mb.write; ; w++ {
c := mb.getBuffer(w)
if c.Full() == false {
mb.write = w
c.Write(a)
break
}
}
}
func (mb *MultiBuffer) getReadBuffer() (buffer Buffer) {
for r, n := mb.read, len(mb.buffers); r < n; r++ {
c := mb.getBuffer(r)
if c.Empty() == false {
buffer = c
mb.read = r
break
}
}
return
}
func (mb *MultiBuffer) Peek() (item Item) {
if mb.unread > 0 {
if c := mb.getReadBuffer(); c != nil {
item = c.Peek()
}
}
return
}
func (mb *MultiBuffer) Read() (item Item) {
if mb.unread > 0 {
mb.unread -= 1
if c := mb.getReadBuffer(); c != nil {
item = c.Read()
}
}
return
}
func (mb *MultiBuffer) Close() {
for _, b := range mb.buffers {
b.Close()
}
mb.buffers = nil
}
func NewBuffer() Buffer {
return &MultiBuffer{}
}
func NewBufferSize(n uint64) Buffer {
return &MultiBuffer{max: n}
}
func hyperceil(x float64) uint64 {
return uint64(math.Exp2(math.Ceil(math.Log2(x))))
}
type Funnel struct {
height uint64
index int
exhausted bool
out Buffer
left, right *Funnel
top *Funnel
bottom []*Funnel
}
func (f *Funnel) K() uint64 {
return uint64(math.Exp2(float64(f.height)))
}
func (f *Funnel) root() (root *Funnel) {
if f.top == nil {
root = f
} else {
root = f.top.root()
}
return root
}
func (f *Funnel) attach(funnel *Funnel, i int) {
if funnel.left == nil && funnel.right == nil {
funnel.left = f.bottom[2*i].root()
funnel.right = f.bottom[2*i+1].root()
} else {
f.attach(funnel.left, i<<1)
f.attach(funnel.right, i<<1+1)
}
}
func (f *Funnel) addIndex(funnel *Funnel, i int) {
if funnel.left == nil && funnel.right == nil {
funnel.index = i
} else {
f.addIndex(funnel.left, i<<1)
f.addIndex(funnel.right, i<<1+1)
}
}
func (f *Funnel) attachInput(in []Buffer, i int) {
if f.left == nil && f.right == nil {
f.left = &Funnel{out: in[2*i], exhausted: true}
f.right = &Funnel{out: in[2*i+1], exhausted: true}
} else {
f.left.attachInput(in, i<<1)
f.right.attachInput(in, i<<1+1)
}
}
func (f *Funnel) fill(out Writer) {
bout, ok := out.(Buffer)
if ok {
bout.Reset()
}
for bout == nil || bout.Full() == false {
if f.left.exhausted == false && f.left.out.Empty() {
f.left.fill(f.left.out)
}
if f.right.exhausted == false && f.right.out.Empty() {
f.right.fill(f.right.out)
}
if f.left.out.Empty() {
if f.right.out.Empty() {
f.left.out.Close()
f.right.out.Close()
f.exhausted = true
break
} else {
out.Write(f.right.out.Read())
}
} else {
if f.right.out.Empty() {
out.Write(f.left.out.Read())
} else {
if f.left.out.Peek().Less(f.right.out.Peek()) {
out.Write(f.left.out.Read())
} else {
out.Write(f.right.out.Read())
}
}
}
}
}
func (f *Funnel) Fill(in []Buffer, out Writer) {
root := f.root()
root.attachInput(in, 0)
root.fill(out)
}
func (f *Funnel) Close() {
if f.top != nil {
f.top.Close()
}
for _, b := range f.bottom {
b.Close()
}
if f.out != nil {
f.out.Close()
}
if f.left != nil {
f.left.Close()
}
if f.right != nil {
f.right.Close()
}
}
func NewFunnelK(k int) *Funnel {
kk := uint64(hyperceil(float64(k)))
h := uint64(math.Ceil(math.Log2(float64(kk))))
return NewFunnel(h)
}
func NewFunnel(height uint64) *Funnel {
f := &Funnel{height: height}
if height > 1 {
heightBottom := hyperceil(float64(height) / 2.)
heightTop := height - heightBottom
f.top = NewFunnel(heightTop)
k := f.top.K()
f.bottom = make([]*Funnel, k)
bsize := uint64(math.Ceil(math.Pow(float64(k), 1.5)))
f.top.out = NewBufferSize(bsize)
for i, _ := range f.bottom {
f.bottom[i] = NewFunnel(heightBottom)
f.bottom[i].out = NewBufferSize(bsize)
}
f.attach(f.top.root(), 0)
}
f.addIndex(f.root(), 0)
return f
}
type itemSlice []Item
func (p itemSlice) Len() int { return len(p) }
func (p itemSlice) Less(i, j int) bool { return p[i].Less(p[j]) }
func (p itemSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type itemBuffer struct {
buf itemSlice
}
func (p *itemBuffer) Close() {
}
func (p *itemBuffer) Empty() bool {
return len(p.buf) == 0
}
func (p *itemBuffer) Full() bool {
return false
}
func (p *itemBuffer) Reset() {
p.buf = p.buf[0:0]
}
func (p *itemBuffer) Write(a Item) {
p.buf = append(p.buf, a)
}
func (p *itemBuffer) Peek() (item Item) {
if len(p.buf) > 0 {
item = p.buf[0]
}
return
}
func (p *itemBuffer) Read() (item Item) {
if len(p.buf) > 0 {
item = p.buf[0]
p.buf = p.buf[1:]
}
return
}
const p = uint(16)
const kSquared = 1 << p
var itemArray [kSquared]Item
func manual(in Reader) (items itemSlice, done bool) {
items = itemSlice(itemArray[0:0])
for i := 0; i < kSquared; i++ {
if item := in.Read(); item != nil {
items = append(items, item)
} else {
done = true
break
}
}
sort.Sort(items)
return
}
var empty = &itemBuffer{make(itemSlice, 0)}
func Merge(buffers []Buffer, out Writer) {
if len(buffers) == 1 {
in := buffers[0]
for {
item := in.Read();
if item == nil {
break
} else {
out.Write(item)
}
}
} else if len(buffers) > 0 {
f := NewFunnelK(len(buffers))
// pad the remaining inputs with an empty buffer
for k := int(f.K()); len(buffers) < k; {
buffers = append(buffers, empty)
}
f.Fill(buffers, out)
f.Close()
for _, b := range buffers {
b.Close()
}
}
return
}
func FunnelSort(in Reader, out Writer) {
items, done := manual(in)
if done {
for _, item := range items {
out.Write(item)
}
return
}
var buffers []Buffer
for kMax := 1 << (p / 2); ; {
for len(buffers) < kMax {
buffer := NewBuffer()
for _, item := range items {
buffer.Write(item)
}
buffers = append(buffers, buffer)
if done {
break
} else {
items, done = manual(in)
}
}
if done {
Merge(buffers, out)
break
} else {
buffer := NewBuffer()
Merge(buffers, buffer)
buffers = buffers[0:0]
buffers = append(buffers, buffer)
kMax += (1 << (p / 2))
}
}
}