mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.2 KiB
C
57 lines
1.2 KiB
C
4 years ago
|
/**
|
||
4 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
4 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
4 years ago
|
*
|
||
4 years ago
|
* By using this file, you agree to the terms and conditions set
|
||
|
* forth in the LICENSE file which can be found at the top level of
|
||
|
* the distribution.
|
||
4 years ago
|
*
|
||
|
*/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_CIRCULAR_ARRAY_H
|
||
|
#define SRSRAN_CIRCULAR_ARRAY_H
|
||
4 years ago
|
|
||
|
#include <array>
|
||
4 years ago
|
#include <cstddef>
|
||
4 years ago
|
|
||
|
/**
|
||
|
*
|
||
|
* @file circular_array.h
|
||
|
*
|
||
|
* @brief Helper class to safely access elements of a std::array
|
||
|
*
|
||
|
* Protects from out-of-bounds access by applying modulo of it's length
|
||
|
* when using the [] operator for element access.
|
||
|
*
|
||
|
* This is useful for circular data structures, like TTIs or SNs.
|
||
|
*/
|
||
|
|
||
4 years ago
|
namespace srsran {
|
||
4 years ago
|
|
||
|
template <typename T, std::size_t N>
|
||
|
class circular_array
|
||
|
{
|
||
|
std::array<T, N> data{};
|
||
|
|
||
|
public:
|
||
4 years ago
|
using iterator = T*;
|
||
|
using const_iterator = const T*;
|
||
|
|
||
4 years ago
|
T& operator[](std::size_t pos) { return data[pos % N]; }
|
||
|
const T& operator[](std::size_t pos) const { return data[pos % N]; }
|
||
4 years ago
|
|
||
|
T* begin() { return data.begin(); }
|
||
|
const T* begin() const { return data.begin(); }
|
||
|
|
||
|
T* end() { return data.end(); }
|
||
|
const T* end() const { return data.end(); }
|
||
4 years ago
|
|
||
|
size_t size() const { return N; }
|
||
4 years ago
|
};
|
||
|
|
||
4 years ago
|
} // namespace srsran
|
||
4 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_CIRCULAR_ARRAY_H
|