1 | /*! |
2 | * \file simple_time_circular_array.hpp |
3 | * \brief file simple_time_circular_array.hpp |
4 | * |
5 | * Copyright 2019 by Intel. |
6 | * |
7 | * Contact: kevin.rogovin@gmail.com |
8 | * |
9 | * This Source Code Form is subject to the |
10 | * terms of the Mozilla Public License, v. 2.0. |
11 | * If a copy of the MPL was not distributed with |
12 | * this file, You can obtain one at |
13 | * http://mozilla.org/MPL/2.0/. |
14 | * |
15 | * \author Kevin Rogovin <kevin.rogovin@gmail.com> |
16 | * |
17 | */ |
18 | |
19 | #ifndef FASTUIDRAW_DEMO_SIMPLE_TIME_CIRCULAR_ARRAY_HPP |
20 | #define FASTUIDRAW_DEMO_SIMPLE_TIME_CIRCULAR_ARRAY_HPP |
21 | |
22 | #include "simple_time.hpp" |
23 | #include <fastuidraw/util/vecN.hpp> |
24 | |
25 | template<unsigned int N> |
26 | class simple_time_circular_array |
27 | { |
28 | public: |
29 | simple_time_circular_array(void): |
30 | m_current(0), |
31 | m_total(1) |
32 | { |
33 | } |
34 | |
35 | void |
36 | advance(void) |
37 | { |
38 | m_current = (m_current == static_cast<int>(N)) ? 0 : m_current + 1; |
39 | m_times[m_current].restart(); |
40 | ++m_total; |
41 | } |
42 | |
43 | int32_t |
44 | elapsed(int num_ago) const |
45 | { |
46 | return m_times[get_index(num_ago)].elapsed(); |
47 | } |
48 | |
49 | int64_t |
50 | elapsed_us(int num_ago) const |
51 | { |
52 | return m_times[get_index(num_ago)].elapsed_us(); |
53 | } |
54 | |
55 | int32_t |
56 | oldest_elapsed(int *num_ago) const |
57 | { |
58 | return m_times[get_oldest(num_ago)].elapsed(); |
59 | } |
60 | |
61 | int64_t |
62 | oldest_elapsed_us(int *num_ago) const |
63 | { |
64 | return m_times[get_oldest(num_ago)].elapsed_us(); |
65 | } |
66 | |
67 | private: |
68 | int |
69 | get_index(int num_ago) const |
70 | { |
71 | FASTUIDRAWassert(num_ago >= 0); |
72 | FASTUIDRAWassert(num_ago <= static_cast<int>(N)); |
73 | FASTUIDRAWassert(num_ago <= m_total); |
74 | int v(m_current - num_ago); |
75 | if (v < 0) |
76 | { |
77 | v += (N + 1u); |
78 | } |
79 | return v; |
80 | } |
81 | |
82 | int |
83 | get_oldest(int *num_ago) const |
84 | { |
85 | *num_ago = fastuidraw::t_min(N, static_cast<unsigned int>(m_total)); |
86 | return get_index(*num_ago); |
87 | } |
88 | |
89 | int m_current, m_total; |
90 | fastuidraw::vecN<simple_time, N + 1u> m_times; |
91 | }; |
92 | |
93 | #endif |
94 | |