-
Notifications
You must be signed in to change notification settings - Fork 4
/
query.go
111 lines (100 loc) · 1.95 KB
/
query.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
package refertable
import (
"math"
)
func query(index map[string]map[any][]int,
keys []string, values []any, count int,
) ([]int, bool) {
if len(keys) != len(values) {
return nil, false
}
// index 数据索引递增排序
tmp := [][]int{}
// key 中结果数量最少的
var startDataIdxs []int
headerDataIdx := 0
tailDataIdx := math.MaxInt
for kIdx, key := range keys {
// value map
m, ok := index[key]
if !ok {
return nil, false
}
// value -> index
dIdxs := m[values[kIdx]]
lenDIdxs := len(dIdxs)
switch lenDIdxs {
case 0:
return nil, false
default:
// max header
if dIdxs[0] > headerDataIdx {
headerDataIdx = dIdxs[0]
}
// min tail
if dIdxs[lenDIdxs-1] < tailDataIdx {
tailDataIdx = dIdxs[lenDIdxs-1]
}
if len(startDataIdxs) == 0 {
startDataIdxs = dIdxs
break
}
if len(dIdxs) < len(startDataIdxs) {
tmp = append(tmp, startDataIdxs)
startDataIdxs = dIdxs
} else {
tmp = append(tmp, dIdxs)
}
}
}
var ret []int
for _, idx := range startDataIdxs {
// 取相交段
if idx < headerDataIdx {
continue
}
if idx > tailDataIdx {
break
}
flag := true
// 取相交项
for _, idxs := range tmp {
if _, ok := binSearch(idxs, idx); !ok {
flag = false
break
}
}
if flag {
ret = append(ret, idx)
// count = 0, 取全部结果
if len(ret) == count {
break
}
}
}
if len(ret) > 0 {
return ret, true
}
return nil, false
}
func binSearch(li []int, s int) (int, bool) {
start := 0
end := len(li) - 1
for start <= end {
mid := (start + end) / 2
v := li[mid]
switch {
case s == v:
return mid, true
case s > v:
start = mid + 1
case s < v:
end = mid - 1
}
}
return 0, false
}