CTR Pia  4.11.3
Game Communication Engine
common_LatestMedian.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: common_LatestMedian.h
4 
5  Copyright Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 
14 
15 #pragma once
16 
17 #include <pia/common/common_definitions.h>
18 
19 #include <pia/common/common_FixedRingBuffer.h>
20 
21 #include <algorithm>
22 
23 
24 namespace nn
25 {
26 namespace pia
27 {
28 namespace common
29 {
30 
31 
32 /*!
33 @cond PRIVATE
34 @brief This class is used to find the median of the last <tt>n</tt> values.
35 
36 @date 2013-10-16 Added the <tt>GetSize</tt> function to get the number of elements.
37 @date 2013-01-22 Changed specifications so that the median of the <tt>n</tt> nearest values is also found.
38 @date 2012-06-14 Added the <tt>IsEmpty</tt> function.
39 @date 2012-05-14 Initial version.
40 */
41 template <typename T, int N>
42 class LatestMedian : public common::RootObject
43 {
44 public:
45 /*!
46 @brief Instantiates the object with default parameters (default constructor).
47 */
48  LatestMedian(void)
49  {
50  Clear();
51  }
52 
53 
54 /*!
55 @brief Destructor.
56 */
57  ~LatestMedian(void)
58  {
59  }
60 
61 
62 /*!
63 @brief Clears elements. Restores the object to its state immediately after the constructor was executed.
64 */
65  void Clear(void)
66  {
67  m_Values.Clear();
68  }
69 
70 
71 /*!
72 @brief Adds the latest value. Only <tt>n</tt> values can be maintained. The earliest value is discarded.
73 */
74  void Add(T value)
75  {
76  m_Values.PushBackForce(value);
77  }
78 
79 
80 /*!
81 @brief Gets the median. Do not call this function frequently because it performs sorting internally.
82 This function returns <tt>0</tt> if called when there are no elements.
83 */
84  T GetMedian(void) const
85  {
86  return GetMedian(N);
87  }
88 
89 
90 /*!
91 @brief Gets the median of the last <tt>n</tt> values.
92 If there are less than <tt>n</tt> values, the median of those present is calculated.
93 
94 Do not call this function frequently because it performs sorting internally.
95 This function returns <tt>0</tt> if it is called when there are no elements.
96 */
97  T GetMedian(size_t n) const
98  {
99  PIA_ASSERT(n <= N);
100 
101  // Checks the number of elements.
102  size_t sz = m_Values.GetSize();
103  PIA_ASSERT(sz <= N);
104  if (sz == 0)
105  {
106  return 0; // The function is guaranteed to return <tt>0</tt> if there are no elements.
107  }
108 
109  // If there are less than <tt>n</tt> values, the median of those present is calculated.
110  size_t num = (sz < n) ? sz : n; // <tt>num</tt> specifies the number of samples being used this time.
111 
112  // An array is prepared separately and sorted.
113  // To get the last <tt>num</tt> values, read them from the end of the array.
114  T buf[N];
115  for (u32 i = 0; i < num; ++i)
116  {
117  size_t idx = sz - 1 - i; // <tt>sz-1</tt> is the subscript of the last element in the array.
118  buf[i] = m_Values[idx];
119  }
120  std::sort(buf, buf + num);
121 
122  // Gets the subscript of the middle value. The median calculation differs depending on whether the number of elements is odd or even.
123  int midIdx = num / 2;
124  if (num % 2 == 0)
125  {
126  return static_cast<T>(buf[midIdx - 1] + buf[midIdx]) / static_cast<T>(2);
127  }
128  else
129  {
130  return buf[midIdx];
131  }
132  }
133 
134 
135 /*!
136 @brief Determines whether the array is empty.
137 */
138  bool IsEmpty(void) const
139  {
140  return m_Values.IsEmpty();
141  }
142 
143 
144 /*!
145 @brief Determines whether the container is full of elements.
146 */
147  bool IsFull(void) const
148  {
149  return m_Values.IsFull();
150  }
151 
152 
153 /*!
154 @brief Gets the number of elements.
155 */
156  size_t GetSize(void) const
157  {
158  return m_Values.GetSize();
159  }
160 
161 
162 /*!
163 @brief Prints information that is useful for debugging.
164 
165 @param[in] flag Specifies the bitwise OR of trace flags. For more information, see the <tt>@ref TraceFlag</tt> type.
166 */
167  void Trace(u64 flag) const
168  {
169  (void)flag; // Not implemented.
170  }
171 
172 
173 private:
174  FixedRingBuffer<T, N> m_Values;
175 };
176 //! @endcond
177 }
178 }
179 } // end of namespace nn::pia::common
Definition: assert.h:115