ATLAS Offline Software
Ring.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
3 */
4 /*
5  */
6 /**
7  * @file CxxUtils/Ring.icc
8  * @author scott snyder <snyder@bnl.gov>
9  * @date May, 2018
10  * @brief A simple ring buffer.
11  */
12 
13 
14 namespace CxxUtils {
15 
16 
17 /**
18  * @brief Clear the buffer and set its size.
19  * @param size New size of the buffer.
20  */
21 template <class T>
22 inline
23 void Ring<T>::reset (size_t size)
24 {
25  m_data.clear();
26  m_data.resize (size);
27  m_pos = 0;
28 }
29 
30 
31 /**
32  * @brief Add a new item to the buffer.
33  * @param x Item to add.
34  */
35 template <class T>
36 inline
37 void Ring<T>::push (const T& x)
38 {
39  m_data[m_pos++] = x;
40  if (m_pos >= m_data.size()) {
41  m_pos = 0;
42  }
43 }
44 
45 
46 /**
47  * @brief Return a copy of keys in the buffer.
48  *
49  * Immediately adjacent duplicates and unfilled entries will be removed,
50  * so the result may be smaller than the size of the buffer.
51  */
52 template <class T>
53 std::vector<T>
54 Ring<T>::getKeysDedup() const
55 {
56  size_t sz = m_data.size();
57  std::vector<T> keys;
58  keys.reserve (sz);
59  T last_key = T();
60 
61  for (size_t pos = m_pos; pos < sz; ++pos) {
62  if (m_data[pos] != last_key) {
63  last_key = m_data[pos];
64  keys.push_back (last_key);
65  }
66  }
67 
68  for (size_t pos = 0; pos < m_pos; ++pos) {
69  if (m_data[pos] != last_key) {
70  last_key = m_data[pos];
71  keys.push_back (last_key);
72  }
73  }
74 
75  return keys;
76 }
77 
78 
79 } // namespace CxxUtils