be/src/util/algorithm_util.h
Line | Count | Source |
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 | | |
18 | | #pragma once |
19 | | |
20 | | #include <cstdint> |
21 | | #include <utility> |
22 | | |
23 | | #include "common/status.h" |
24 | | namespace doris { |
25 | | class AlgoUtil { |
26 | | public: |
27 | | // descent the value step by step not linear continuity |
28 | | // If the result is linear continuity, then the value will changed very quickly and will cost |
29 | | // a lot of CPU and cache will not stable and will hold some lock. |
30 | | // Its better to use step num to be 10, do not use 3, the divide value is not stable. |
31 | | // For example, if step num is 3, then the result will be 0.33333... 0.66666..., the double value |
32 | | // is not stable. |
33 | | static double descent_by_step(int step_num, int64_t low_bound, int64_t high_bound, |
34 | 238k | int64_t current) { |
35 | 238k | if (current <= low_bound) { |
36 | 238k | return 1; |
37 | 238k | } |
38 | 9 | if (current >= high_bound) { |
39 | 2 | return 0; |
40 | 2 | } |
41 | 7 | if (high_bound <= low_bound) { |
42 | | // Invalid |
43 | 0 | return 0; |
44 | 0 | } |
45 | | // Use floor value, so that the step size is a little smaller than the actual value. |
46 | | // And then the used step will be a little larger than the actual value. |
47 | 7 | int64_t step_size = (int64_t)std::floor((high_bound - low_bound) / (step_num * 1.0)); |
48 | 7 | int64_t used_step = (int64_t)std::ceil((current - low_bound) / (step_size * 1.0)); |
49 | | // Then the left step is smaller than actual value. |
50 | | // This elimation algo will elimate more cache than actual. |
51 | 7 | int64_t left_step = step_num - used_step; |
52 | 7 | return left_step / (step_num * 1.0); |
53 | 7 | } |
54 | | }; |
55 | | } // namespace doris |