blob: d301b9977b2a034a0d95692e8aef0e6682bb22da [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
Dominik Laskowski98041832019-08-01 18:35:59 -070024namespace android::scheduler {
Ady Abraham09bd3922019-04-08 10:44:56 -070025
Dominik Laskowski98041832019-08-01 18:35:59 -070026// Opaque handle to scheduler connection.
27struct ConnectionHandle {
28 using Id = std::uintptr_t;
29 static constexpr Id INVALID_ID = static_cast<Id>(-1);
30
31 Id id = INVALID_ID;
32
33 explicit operator bool() const { return id != INVALID_ID; }
34};
35
36inline bool operator==(ConnectionHandle lhs, ConnectionHandle rhs) {
37 return lhs.id == rhs.id;
38}
39
Ana Krulec434c22d2018-11-28 13:48:36 +010040// Calculates the statistical mean (average) in the data structure (array, vector). The
41// function does not modify the contents of the array.
42template <typename T>
43auto calculate_mean(const T& v) {
44 using V = typename T::value_type;
Ady Abraham09bd3922019-04-08 10:44:56 -070045 V sum = std::accumulate(v.begin(), v.end(), static_cast<V>(0));
Ana Krulec434c22d2018-11-28 13:48:36 +010046 return sum / static_cast<V>(v.size());
47}
48
49// Calculates the statistical median in the vector. Return 0 if the vector is empty. The
50// function modifies the vector contents.
51int64_t calculate_median(std::vector<int64_t>* v);
52
53// Calculates the statistical mode in the vector. Return 0 if the vector is empty.
Ana Krulecfefd6ae2019-02-13 17:53:08 -080054template <typename T>
55auto calculate_mode(const T& v) {
56 if (v.empty()) {
57 return 0;
58 }
59
60 // Create a map with all the counts for the indivicual values in the vector.
61 std::unordered_map<int64_t, int> counts;
62 for (int64_t value : v) {
63 counts[value]++;
64 }
65
66 // Sort the map, and return the number with the highest count. If two numbers have
67 // the same count, first one is returned.
68 using ValueType = const decltype(counts)::value_type&;
69 const auto compareCounts = [](ValueType l, ValueType r) { return l.second <= r.second; };
70 return static_cast<int>(std::max_element(counts.begin(), counts.end(), compareCounts)->first);
71}
Ana Krulec434c22d2018-11-28 13:48:36 +010072
Dominik Laskowski98041832019-08-01 18:35:59 -070073} // namespace android::scheduler
74
75namespace std {
76
77template <>
78struct hash<android::scheduler::ConnectionHandle> {
79 size_t operator()(android::scheduler::ConnectionHandle handle) const {
80 return hash<android::scheduler::ConnectionHandle::Id>()(handle.id);
81 }
82};
83
84} // namespace std