blob: 9e6e8c7af685fffb6a8eddd7c7c696359f323ef3 [file] [log] [blame]
Ana Krulec434c22d2018-11-28 13:48:36 +01001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cinttypes>
20#include <numeric>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080021#include <unordered_map>
Ana Krulec434c22d2018-11-28 13:48:36 +010022#include <vector>
23
24namespace android {
25namespace scheduler {
26// This number is used to set the size of the arrays in scheduler that hold information
27// about layers.
28static constexpr size_t ARRAY_SIZE = 30;
29
Ana Krulecb43429d2019-01-09 14:28:51 -080030// This number is used to have a place holder for when the screen is not NORMAL/ON. Currently
31// the config is not visible to SF, and is completely maintained by HWC. However, we would
32// still like to keep track of time when the device is in this config.
33static constexpr int SCREEN_OFF_CONFIG_ID = -1;
34
Ana Krulec434c22d2018-11-28 13:48:36 +010035// Calculates the statistical mean (average) in the data structure (array, vector). The
36// function does not modify the contents of the array.
37template <typename T>
38auto calculate_mean(const T& v) {
39 using V = typename T::value_type;
40 V sum = std::accumulate(v.begin(), v.end(), 0);
41 return sum / static_cast<V>(v.size());
42}
43
44// Calculates the statistical median in the vector. Return 0 if the vector is empty. The
45// function modifies the vector contents.
46int64_t calculate_median(std::vector<int64_t>* v);
47
48// Calculates the statistical mode in the vector. Return 0 if the vector is empty.
Ana Krulecfefd6ae2019-02-13 17:53:08 -080049template <typename T>
50auto calculate_mode(const T& v) {
51 if (v.empty()) {
52 return 0;
53 }
54
55 // Create a map with all the counts for the indivicual values in the vector.
56 std::unordered_map<int64_t, int> counts;
57 for (int64_t value : v) {
58 counts[value]++;
59 }
60
61 // Sort the map, and return the number with the highest count. If two numbers have
62 // the same count, first one is returned.
63 using ValueType = const decltype(counts)::value_type&;
64 const auto compareCounts = [](ValueType l, ValueType r) { return l.second <= r.second; };
65 return static_cast<int>(std::max_element(counts.begin(), counts.end(), compareCounts)->first);
66}
Ana Krulec434c22d2018-11-28 13:48:36 +010067
68} // namespace scheduler
69} // namespace android