generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
71 lines (61 loc) · 1.56 KB
/
search.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
package journal
import (
"context"
)
// CompareFunc is a function that compares a record to some datum.
//
// If the record is less than the datum, cmp is negative. If the record is
// greater than the datum, cmp is positive. Otherwise, the record is considered
// equal to the datum.
type CompareFunc[T any] func(context.Context, Position, T) (cmp int, err error)
// Search performs a binary search of j within the half-open range [begin, end)
// to find the position of the record for which cmp() returns zero.
//
// It returns [ErrNotFound] if there is no such record.
func Search[T any](
ctx context.Context,
j Journal[T],
begin, end Position,
cmp CompareFunc[T],
) (pos Position, rec T, err error) {
for begin < end {
pos := (begin >> 1) + (end >> 1)
rec, err := j.Get(ctx, pos)
if err != nil {
return 0, rec, err
}
result, err := cmp(ctx, pos, rec)
if err != nil {
return 0, rec, err
}
if result > 0 {
end = pos
} else if result < 0 {
begin = pos + 1
} else {
return pos, rec, nil
}
}
return 0, rec, ErrNotFound
}
// RangeFromSearchResult invokes fn for each record in the journal, in order,
// beginning with the record for which cmp() returns zero.
//
// It returns [ErrNotFound] if there is no such record.
func RangeFromSearchResult[T any](
ctx context.Context,
j Journal[T],
begin, end Position,
cmp CompareFunc[T],
fn RangeFunc[T],
) error {
pos, rec, err := Search(ctx, j, begin, end, cmp)
if err != nil {
return err
}
ok, err := fn(ctx, pos, rec)
if !ok || err != nil {
return err
}
return j.Range(ctx, pos+1, fn)
}