-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathSelected.java
More file actions
140 lines (126 loc) · 3.89 KB
/
Selected.java
File metadata and controls
140 lines (126 loc) · 3.89 KB
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
/*
* Licensed to the 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. The 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 org.apache.orc.impl.filter;
import org.apache.orc.OrcFilterContext;
/**
* Wrapper class for the selected vector that centralizes the convenience functions
* @since 1.7.0
*/
public class Selected {
// Sorted array of row indices
int[] sel;
int selSize;
Selected(int[] sel) {
this.sel = sel;
this.selSize = 0;
}
Selected() {
this(new int[1024]);
}
void clear() {
this.selSize = 0;
}
void selectAll(Selected src) {
System.arraycopy(src.sel, 0, this.sel, 0, src.selSize);
this.selSize = src.selSize;
}
/**
* Initialize the selected vector from the supplied filter context
*
* @param fc Input filterContext
*/
void initialize(OrcFilterContext fc) {
ensureSize(fc.getSelectedSize());
selSize = fc.getSelectedSize();
if (fc.isSelectedInUse()) {
System.arraycopy(fc.getSelected(), 0, sel, 0, selSize);
} else {
for (int i = 0; i < selSize; i++) {
sel[i] = i;
}
}
}
/**
* Only adjust the size and don't worry about the state, if required this is handled before
* this is
* called.
*
* @param size Desired size
*/
void ensureSize(int size) {
if (size > sel.length) {
sel = new int[size];
selSize = 0;
}
}
void set(Selected inBound) {
ensureSize(inBound.selSize);
System.arraycopy(inBound.sel, 0, sel, 0, inBound.selSize);
selSize = inBound.selSize;
}
/**
* Expects the elements the src to be disjoint with respect to this and is not validated.
*
* @param src The disjoint selection indices that should be merged into this.
*/
void unionDisjoint(Selected src) {
// merge from the back to avoid the need for an intermediate store
int writeIdx = src.selSize + this.selSize - 1;
int srcIdx = src.selSize - 1;
int thisIdx = this.selSize - 1;
while (thisIdx >= 0 || srcIdx >= 0) {
if (srcIdx < 0 || (thisIdx >= 0 && src.sel[srcIdx] < this.sel[thisIdx])) {
// src is exhausted or this is larger
this.sel[writeIdx--] = this.sel[thisIdx--];
} else {
this.sel[writeIdx--] = src.sel[srcIdx--];
}
}
this.selSize += src.selSize;
}
/**
* Remove the elements of src from this.
*
* @param src The selection indices that should be removed from the current selection.
*/
void minus(Selected src) {
int writeidx = 0;
int evalIdx = 0;
int srcIdx = 0;
while (srcIdx < src.selSize && evalIdx < this.selSize) {
if (this.sel[evalIdx] < src.sel[srcIdx]) {
// Evaluation is smaller so retain this
this.sel[writeidx] = this.sel[evalIdx];
evalIdx += 1;
writeidx += 1;
} else if (this.sel[evalIdx] > src.sel[srcIdx]) {
// Evaluation is larger cannot decide, navigate src forward
srcIdx += 1;
} else {
// Equal should be ignored so move both evalIdx and srcIdx forward
evalIdx += 1;
srcIdx += 1;
}
}
if (evalIdx < this.selSize) {
System.arraycopy(this.sel, evalIdx, this.sel, writeidx, this.selSize - evalIdx);
writeidx += this.selSize - evalIdx;
}
this.selSize = writeidx;
}
}