-
Notifications
You must be signed in to change notification settings - Fork 84
/
sort.go
131 lines (112 loc) · 3.07 KB
/
sort.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
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// Package sort provides a generic iterator that merges multiple sorted iterators.
package sort
import (
"bytes"
"container/heap"
"go.uber.org/multierr"
)
// Comparable is an interface that allows sorting of items.
type Comparable interface {
SortedField() []byte
}
// Iterator is a stream of items of Comparable type.
type Iterator[T Comparable] interface {
Next() bool
Val() T
Close() error
}
type container[T Comparable] struct {
item T
iter Iterator[T]
}
type containerHeap[T Comparable] struct {
items []*container[T]
desc bool
}
func (h containerHeap[T]) Len() int {
return len(h.items)
}
func (h containerHeap[T]) Less(i, j int) bool {
if h.desc {
return bytes.Compare(h.items[i].item.SortedField(), h.items[j].item.SortedField()) > 0
}
return bytes.Compare(h.items[i].item.SortedField(), h.items[j].item.SortedField()) < 0
}
func (h containerHeap[T]) Swap(i, j int) {
h.items[i], h.items[j] = h.items[j], h.items[i]
}
func (h *containerHeap[T]) Push(x interface{}) {
item := x.(*container[T])
h.items = append(h.items, item)
}
func (h *containerHeap[T]) Pop() interface{} {
old := h.items
n := len(old)
item := old[n-1]
h.items = old[0 : n-1]
return item
}
type itemIter[T Comparable] struct {
curr T
h *containerHeap[T]
iters []Iterator[T]
}
// NewItemIter returns a new iterator that merges multiple sorted iterators.
func NewItemIter[T Comparable](iters []Iterator[T], desc bool) Iterator[T] {
var def T
it := &itemIter[T]{
iters: iters,
h: &containerHeap[T]{items: make([]*container[T], 0), desc: desc},
curr: def,
}
it.initialize()
return it
}
func (it *itemIter[T]) initialize() {
heap.Init(it.h)
for _, iter := range it.iters {
if iter.Next() {
heap.Push(it.h, &container[T]{item: iter.Val(), iter: iter})
}
}
}
func (it *itemIter[T]) pushIterator(iter Iterator[T]) {
if iter.Next() {
heap.Push(it.h, &container[T]{item: iter.Val(), iter: iter})
}
}
func (it *itemIter[T]) Next() bool {
if it.h.Len() == 0 {
return false
}
top := heap.Pop(it.h).(*container[T])
it.curr = top.item
it.pushIterator(top.iter)
return true
}
func (it *itemIter[T]) Val() T {
return it.curr
}
func (it *itemIter[T]) Close() error {
var err error
for _, iter := range it.iters {
err = multierr.Append(err, iter.Close())
}
return err
}