2021-08-17 03:56:50 -07:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* \section COPYRIGHT
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 Software Radio Systems Limited
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-08-17 06:24:17 -07:00
|
|
|
#ifndef SRSRAN_OPTIONAL_ARRAY_H
|
|
|
|
#define SRSRAN_OPTIONAL_ARRAY_H
|
2021-08-17 03:56:50 -07:00
|
|
|
|
|
|
|
#include "optional.h"
|
|
|
|
#include "srsran/common/srsran_assert.h"
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
namespace srsran {
|
|
|
|
|
2021-08-17 07:38:36 -07:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename Vec>
|
|
|
|
class base_optional_vector
|
2021-08-17 03:56:50 -07:00
|
|
|
{
|
2021-08-17 07:38:36 -07:00
|
|
|
using base_t = base_optional_vector<Vec>;
|
|
|
|
using T = typename Vec::value_type::value_type;
|
|
|
|
|
|
|
|
protected:
|
2021-08-17 03:56:50 -07:00
|
|
|
template <typename Obj>
|
|
|
|
class iterator_impl
|
|
|
|
{
|
|
|
|
using It = iterator_impl<Obj>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
|
using value_type = Obj;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using pointer = Obj*;
|
|
|
|
using reference = Obj&;
|
|
|
|
|
|
|
|
iterator_impl() = default;
|
2021-08-17 07:38:36 -07:00
|
|
|
iterator_impl(base_t* parent_, size_t idx_) : parent(parent_), idx(idx_)
|
2021-08-17 03:56:50 -07:00
|
|
|
{
|
2021-08-17 07:38:36 -07:00
|
|
|
if (idx < parent->vec.size() and not parent->contains(idx)) {
|
2021-08-17 03:56:50 -07:00
|
|
|
++(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
It& operator++()
|
|
|
|
{
|
2021-08-17 07:38:36 -07:00
|
|
|
while (++idx < parent->vec.size() and not parent->contains(idx)) {
|
2021-08-17 03:56:50 -07:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
It& operator--()
|
|
|
|
{
|
2021-08-17 07:38:36 -07:00
|
|
|
while (--idx < parent->vec.size() and not parent->contains(idx)) {
|
2021-08-17 03:56:50 -07:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
reference operator*() { return parent->operator[](idx); }
|
|
|
|
pointer operator->() { return &parent->operator[](idx); }
|
|
|
|
|
|
|
|
bool operator==(const It& other) const { return idx == other.idx and parent == other.parent; }
|
|
|
|
bool operator!=(const It& other) const { return not(*this == other); }
|
|
|
|
|
|
|
|
protected:
|
2021-08-17 07:38:36 -07:00
|
|
|
friend base_t;
|
2021-08-17 03:56:50 -07:00
|
|
|
|
2021-08-17 07:38:36 -07:00
|
|
|
base_t* parent = nullptr;
|
|
|
|
size_t idx = std::numeric_limits<size_t>::max();
|
2021-08-17 03:56:50 -07:00
|
|
|
};
|
|
|
|
|
2021-08-17 07:38:36 -07:00
|
|
|
size_t nof_elems = 0;
|
|
|
|
Vec vec;
|
|
|
|
|
2021-08-17 03:56:50 -07:00
|
|
|
public:
|
|
|
|
using iterator = iterator_impl<T>;
|
|
|
|
using const_iterator = iterator_impl<const T>;
|
|
|
|
|
2021-08-17 07:38:36 -07:00
|
|
|
base_optional_vector() = default;
|
|
|
|
base_optional_vector(const base_optional_vector&) = default;
|
|
|
|
base_optional_vector(base_optional_vector&& other) noexcept : vec(std::move(other.vec)), nof_elems(other.nof_elems)
|
2021-08-17 03:56:50 -07:00
|
|
|
{
|
|
|
|
other.nof_elems = 0;
|
|
|
|
}
|
2021-08-17 07:38:36 -07:00
|
|
|
base_optional_vector& operator=(const base_optional_vector&) = default;
|
|
|
|
base_optional_vector& operator =(base_optional_vector&& other) noexcept
|
2021-08-17 03:56:50 -07:00
|
|
|
{
|
|
|
|
vec = std::move(other.vec);
|
|
|
|
nof_elems = other.nof_elems;
|
|
|
|
nof_elems = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find first position that is empty
|
2021-08-17 06:24:17 -07:00
|
|
|
size_t find_first_empty(size_t start_guess = 0)
|
2021-08-17 03:56:50 -07:00
|
|
|
{
|
2021-08-17 07:38:36 -07:00
|
|
|
if (nof_elems == vec.size()) {
|
|
|
|
return vec.size();
|
2021-08-17 03:56:50 -07:00
|
|
|
}
|
2021-08-17 07:38:36 -07:00
|
|
|
for (size_t i = start_guess; i < vec.size(); ++i) {
|
2021-08-17 03:56:50 -07:00
|
|
|
if (not vec[i].has_value()) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 07:38:36 -07:00
|
|
|
return vec.size();
|
2021-08-17 03:56:50 -07:00
|
|
|
}
|
2021-08-17 07:38:36 -07:00
|
|
|
|
|
|
|
bool contains(size_t idx) const { return idx < vec.size() and vec[idx].has_value(); }
|
|
|
|
|
2021-08-17 03:56:50 -07:00
|
|
|
void erase(size_t idx)
|
|
|
|
{
|
|
|
|
if (contains(idx)) {
|
|
|
|
nof_elems--;
|
|
|
|
vec[idx].reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void erase(iterator it) { erase(it.idx); }
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
nof_elems = 0;
|
|
|
|
for (auto& e : *this) {
|
|
|
|
e.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](size_t idx) { return *vec[idx]; }
|
|
|
|
const T& operator[](size_t idx) const { return *vec[idx]; }
|
|
|
|
|
2021-08-17 07:38:36 -07:00
|
|
|
bool empty() const { return nof_elems == 0; }
|
|
|
|
size_t size() const { return nof_elems; }
|
2021-08-17 03:56:50 -07:00
|
|
|
|
|
|
|
iterator begin() { return iterator{this, 0}; }
|
2021-08-17 07:38:36 -07:00
|
|
|
iterator end() { return iterator{this, vec.size()}; }
|
2021-08-17 03:56:50 -07:00
|
|
|
const_iterator begin() const { return const_iterator{this, 0}; }
|
2021-08-17 07:38:36 -07:00
|
|
|
const_iterator end() const { return const_iterator{this, vec.size()}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
2021-08-17 03:56:50 -07:00
|
|
|
|
2021-08-17 07:38:36 -07:00
|
|
|
/**
|
|
|
|
* Array of optional items. The iteration is in order of indexes and correctly skips non-present items
|
|
|
|
* Pointer/References/Iterators remain valid throughout the object lifetime
|
|
|
|
* NOTE: The sorted iteration and pointer validation guarantees add some overhead if the array is very fragmented
|
|
|
|
* @tparam T type of objects
|
|
|
|
* @tparam N static size of max nof items
|
|
|
|
*/
|
|
|
|
template <typename T, size_t N>
|
|
|
|
class optional_array : public detail::base_optional_vector<std::array<optional<T>, N> >
|
|
|
|
{
|
|
|
|
using base_t = detail::base_optional_vector<std::array<optional<T>, N> >;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <typename U>
|
|
|
|
void insert(size_t idx, U&& u)
|
|
|
|
{
|
|
|
|
this->nof_elems += this->contains(idx) ? 0 : 1;
|
|
|
|
this->vec[idx] = std::forward<U>(u);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contrarily to optional_array, this class may allocate and cause pointer/reference/iterator invalidation.
|
|
|
|
* However, the indexes will remain valid.
|
|
|
|
* @tparam T
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class optional_vector : public detail::base_optional_vector<std::vector<optional<T> > >
|
|
|
|
{
|
|
|
|
using base_t = detail::base_optional_vector<std::vector<optional<T> > >;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// May allocate and cause pointer invalidation
|
|
|
|
template <typename U>
|
|
|
|
void insert(size_t idx, U&& u)
|
|
|
|
{
|
|
|
|
if (not this->contains(idx)) {
|
|
|
|
this->nof_elems++;
|
|
|
|
this->vec.resize(std::max(idx + 1, this->vec.size()));
|
|
|
|
}
|
|
|
|
this->vec[idx] = std::forward<U>(u);
|
|
|
|
}
|
2021-08-17 03:56:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace srsran
|
|
|
|
|
2021-08-17 06:24:17 -07:00
|
|
|
#endif // SRSRAN_OPTIONAL_ARRAY_H
|