adt: rename optional table to optional array

master
Francisco Paisana 3 years ago
parent 8c24cfebd3
commit 1596fcf349

@ -10,8 +10,8 @@
* *
*/ */
#ifndef SRSRAN_OPTIONAL_TABLE_H #ifndef SRSRAN_OPTIONAL_ARRAY_H
#define SRSRAN_OPTIONAL_TABLE_H #define SRSRAN_OPTIONAL_ARRAY_H
#include "optional.h" #include "optional.h"
#include "srsran/common/srsran_assert.h" #include "srsran/common/srsran_assert.h"
@ -27,7 +27,7 @@ namespace srsran {
* @tparam N static size of max nof items * @tparam N static size of max nof items
*/ */
template <typename T, size_t N> template <typename T, size_t N>
class optional_table class optional_array
{ {
template <typename Obj> template <typename Obj>
class iterator_impl class iterator_impl
@ -42,7 +42,7 @@ class optional_table
using reference = Obj&; using reference = Obj&;
iterator_impl() = default; iterator_impl() = default;
iterator_impl(optional_table<T, N>* parent_, size_t idx_) : parent(parent_), idx(idx_) iterator_impl(optional_array<T, N>* parent_, size_t idx_) : parent(parent_), idx(idx_)
{ {
if (idx < parent->capacity() and not parent->contains(idx)) { if (idx < parent->capacity() and not parent->contains(idx)) {
++(*this); ++(*this);
@ -69,9 +69,9 @@ class optional_table
bool operator!=(const It& other) const { return not(*this == other); } bool operator!=(const It& other) const { return not(*this == other); }
protected: protected:
friend class optional_table<T, N>; friend class optional_array<T, N>;
optional_table<T, N>* parent = nullptr; optional_array<T, N>* parent = nullptr;
size_t idx = N; size_t idx = N;
}; };
@ -79,14 +79,14 @@ public:
using iterator = iterator_impl<T>; using iterator = iterator_impl<T>;
using const_iterator = iterator_impl<const T>; using const_iterator = iterator_impl<const T>;
optional_table() {} optional_array() = default;
optional_table(const optional_table&) = default; optional_array(const optional_array&) = default;
optional_table(optional_table&& other) noexcept : vec(std::move(other.vec)), nof_elems(other.nof_elems) optional_array(optional_array&& other) noexcept : vec(std::move(other.vec)), nof_elems(other.nof_elems)
{ {
other.nof_elems = 0; other.nof_elems = 0;
} }
optional_table& operator=(const optional_table&) = default; optional_array& operator=(const optional_array&) = default;
optional_table& operator =(optional_table&& other) noexcept optional_array& operator =(optional_array&& other) noexcept
{ {
vec = std::move(other.vec); vec = std::move(other.vec);
nof_elems = other.nof_elems; nof_elems = other.nof_elems;
@ -95,12 +95,12 @@ public:
} }
// Find first position that is empty // Find first position that is empty
size_t find_first_empty() size_t find_first_empty(size_t start_guess = 0)
{ {
if (nof_elems == capacity()) { if (nof_elems == capacity()) {
return N; return N;
} }
for (size_t i = 0; i < N; ++i) { for (size_t i = start_guess; i < N; ++i) {
if (not vec[i].has_value()) { if (not vec[i].has_value()) {
return i; return i;
} }
@ -150,4 +150,4 @@ private:
} // namespace srsran } // namespace srsran
#endif // SRSRAN_OPTIONAL_TABLE_H #endif // SRSRAN_OPTIONAL_ARRAY_H

@ -62,6 +62,6 @@ add_executable(cached_alloc_test cached_alloc_test.cc)
target_link_libraries(cached_alloc_test srsran_common) target_link_libraries(cached_alloc_test srsran_common)
add_test(cached_alloc_test cached_alloc_test) add_test(cached_alloc_test cached_alloc_test)
add_executable(optional_table_test optional_table_test.cc) add_executable(optional_array_test optional_array_test.cc)
target_link_libraries(optional_table_test srsran_common) target_link_libraries(optional_array_test srsran_common)
add_test(optional_table_test optional_table_test) add_test(optional_array_test optional_array_test)

@ -10,14 +10,14 @@
* *
*/ */
#include "srsran/adt/optional_table.h" #include "srsran/adt/optional_array.h"
#include "srsran/common/test_common.h" #include "srsran/common/test_common.h"
namespace srsran { namespace srsran {
void test_slot_table() void test_slot_table()
{ {
optional_table<int, 5> table1; optional_array<int, 5> table1;
TESTASSERT(table1.size() == 0 and table1.empty()); TESTASSERT(table1.size() == 0 and table1.empty());
TESTASSERT(not table1.contains(0)); TESTASSERT(not table1.contains(0));
Loading…
Cancel
Save