1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with 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,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 #pragma once
18 
19 #include "arrow/util/bit_util.h"
20 #include "arrow/util/endian.h"
21 #include "parquet/level_comparison.h"
22 
23 // Used to make sure ODR rule isn't violated.
24 #ifndef PARQUET_IMPL_NAMESPACE
25 #error "PARQUET_IMPL_NAMESPACE must be defined"
26 #endif
27 namespace parquet {
28 namespace internal {
29 namespace PARQUET_IMPL_NAMESPACE {
30 /// Builds a bitmap by applying predicate to the level vector provided.
31 ///
32 /// \param[in] levels Rep or def level array.
33 /// \param[in] num_levels The number of levels to process (must be [0, 64])
34 /// \param[in] predicate The predicate to apply (must have the signature `bool
35 /// predicate(int16_t)`.
36 /// \returns The bitmap using least significant "bit" ordering.
37 ///
38 template <typename Predicate>
LevelsToBitmap(const int16_t * levels,int64_t num_levels,Predicate predicate)39 inline uint64_t LevelsToBitmap(const int16_t* levels, int64_t num_levels,
40                                Predicate predicate) {
41   // Both clang and GCC can vectorize this automatically with SSE4/AVX2.
42   uint64_t mask = 0;
43   for (int x = 0; x < num_levels; x++) {
44     mask |= static_cast<uint64_t>(predicate(levels[x]) ? 1 : 0) << x;
45   }
46   return ::arrow::BitUtil::ToLittleEndian(mask);
47 }
48 
FindMinMaxImpl(const int16_t * levels,int64_t num_levels)49 inline MinMax FindMinMaxImpl(const int16_t* levels, int64_t num_levels) {
50   MinMax out{std::numeric_limits<int16_t>::max(), std::numeric_limits<int16_t>::min()};
51   for (int x = 0; x < num_levels; x++) {
52     out.min = std::min(levels[x], out.min);
53     out.max = std::max(levels[x], out.max);
54   }
55   return out;
56 }
57 
GreaterThanBitmapImpl(const int16_t * levels,int64_t num_levels,int16_t rhs)58 inline uint64_t GreaterThanBitmapImpl(const int16_t* levels, int64_t num_levels,
59                                       int16_t rhs) {
60   return LevelsToBitmap(levels, num_levels, [rhs](int16_t value) { return value > rhs; });
61 }
62 
63 }  // namespace PARQUET_IMPL_NAMESPACE
64 }  // namespace internal
65 }  // namespace parquet
66