summaryrefslogtreecommitdiff
path: root/Userland/DevTools/Profiler/Histogram.h
blob: 3380e7741ae39756c3e3c719ce1ec9ed582524c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Vector.h>

template<typename Timestamp = u64, typename Value = u16, size_t inline_capacity = 4096>
class Histogram {
public:
    Histogram(Timestamp start, Timestamp end, size_t bucket_count)
        : m_start(start)
        , m_end(end)
    {
        m_buckets.resize(bucket_count);
    }

    void insert(Timestamp const& timestamp, Value value = 1)
    {
        VERIFY(timestamp >= m_start && timestamp <= m_end);
        auto bucket = (timestamp - m_start) * (m_buckets.size() - 1) / (m_end - m_start);
        m_buckets[bucket] += value;
    }

    Value at(size_t index) const
    {
        return m_buckets[index];
    }

    size_t size() const
    {
        return m_buckets.size();
    }

private:
    Timestamp m_start { 0 };
    Timestamp m_end { 0 };
    Vector<Value, inline_capacity> m_buckets;
};