]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/java/algorithm/src/main/java/org/apache/arrow/algorithm/sort/CompositeVectorComparator.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / algorithm / src / main / java / org / apache / arrow / algorithm / sort / CompositeVectorComparator.java
CommitLineData
1d09f67e
TL
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.arrow.algorithm.sort;
19
20import org.apache.arrow.vector.ValueVector;
21
22/**
23 * A composite vector comparator compares a number of vectors
24 * by a number of inner comparators.
25 * <p>
26 * It works by first using the first comparator, if a non-zero value
27 * is returned, it simply returns it. Otherwise, it uses the second comparator,
28 * and so on, until a non-zero value is produced, or all inner comparators have
29 * been used.
30 * </p>
31 */
32public class CompositeVectorComparator extends VectorValueComparator<ValueVector> {
33
34 private final VectorValueComparator[] innerComparators;
35
36 public CompositeVectorComparator(VectorValueComparator[] innerComparators) {
37 this.innerComparators = innerComparators;
38 }
39
40 @Override
41 public int compareNotNull(int index1, int index2) {
42 // short-cut for scenarios when the caller can be sure that the vectors are non-nullable.
43 for (int i = 0; i < innerComparators.length; i++) {
44 int result = innerComparators[i].compareNotNull(index1, index2);
45 if (result != 0) {
46 return result;
47 }
48 }
49 return 0;
50 }
51
52 @Override
53 public int compare(int index1, int index2) {
54 for (int i = 0; i < innerComparators.length; i++) {
55 int result = innerComparators[i].compare(index1, index2);
56 if (result != 0) {
57 return result;
58 }
59 }
60 return 0;
61 }
62
63 @Override
64 public VectorValueComparator<ValueVector> createNew() {
65 VectorValueComparator[] newInnerComparators = new VectorValueComparator[innerComparators.length];
66 for (int i = 0; i < innerComparators.length; i++) {
67 newInnerComparators[i] = innerComparators[i].createNew();
68 }
69 return new CompositeVectorComparator(newInnerComparators);
70 }
71}