Skip to content

Commit

Permalink
[FixedUnorderedMap/Set] Change Bidirectional -> Forward Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkaratarakis committed Feb 25, 2024
1 parent 01de7bf commit dc52292
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 200 deletions.
4 changes: 2 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ cc_library(
hdrs = ["include/fixed_containers/fixed_map_adapter.hpp"],
includes = ["include"],
deps = [
":bidirectional_iterator",
":erase_if",
":forward_iterator",
":source_location",
":preconditions",
":assert_or_abort",
Expand All @@ -324,8 +324,8 @@ cc_library(
hdrs = ["include/fixed_containers/fixed_set_adapter.hpp"],
includes = ["include"],
deps = [
":bidirectional_iterator",
":erase_if",
":forward_iterator",
":source_location",
":preconditions",
":assert_or_abort",
Expand Down
50 changes: 5 additions & 45 deletions include/fixed_containers/fixed_map_adapter.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "fixed_containers/assert_or_abort.hpp"
#include "fixed_containers/bidirectional_iterator.hpp"
#include "fixed_containers/emplace.hpp"
#include "fixed_containers/erase_if.hpp"
#include "fixed_containers/forward_iterator.hpp"
#include "fixed_containers/max_size.hpp"
#include "fixed_containers/preconditions.hpp"
#include "fixed_containers/source_location.hpp"
Expand Down Expand Up @@ -67,8 +67,6 @@ class FixedMapAdapter

constexpr void advance() noexcept { current_index_ = table_->next_of(current_index_); }

constexpr void recede() noexcept { current_index_ = table_->prev_of(current_index_); }

constexpr std::conditional_t<IS_CONST, const_reference, reference> get() const noexcept
{
// auto for auto const/mut
Expand All @@ -82,18 +80,12 @@ class FixedMapAdapter
}
};

template <IteratorConstness CONSTNESS, IteratorDirection DIRECTION>
using Iterator =
BidirectionalIterator<PairProvider<true>, PairProvider<false>, CONSTNESS, DIRECTION>;
template <IteratorConstness CONSTNESS>
using Iterator = ForwardIterator<PairProvider<true>, PairProvider<false>, CONSTNESS>;

public:
using const_iterator =
Iterator<IteratorConstness::CONSTANT_ITERATOR, IteratorDirection::FORWARD>;
using iterator = Iterator<IteratorConstness::MUTABLE_ITERATOR, IteratorDirection::FORWARD>;
using const_reverse_iterator =
Iterator<IteratorConstness::CONSTANT_ITERATOR, IteratorDirection::REVERSE>;
using reverse_iterator =
Iterator<IteratorConstness::MUTABLE_ITERATOR, IteratorDirection::REVERSE>;
using const_iterator = Iterator<IteratorConstness::CONSTANT_ITERATOR>;
using iterator = Iterator<IteratorConstness::MUTABLE_ITERATOR>;

using size_type = std::size_t;
using difference_type = ptrdiff_t;
Expand Down Expand Up @@ -189,25 +181,6 @@ class FixedMapAdapter
return iterator{PairProvider<false>{&table(), table().end_index()}};
}

constexpr reverse_iterator rbegin() noexcept
{
return reverse_iterator{PairProvider<false>{&table(), table().end_index()}};
}
constexpr const_reverse_iterator rbegin() const noexcept { return crbegin(); }
constexpr const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator{PairProvider<true>{&table(), table().end_index()}};
}
constexpr reverse_iterator rend() noexcept
{
return reverse_iterator{PairProvider<false>{&table(), table().begin_index()}};
}
constexpr const_reverse_iterator rend() const noexcept { return crend(); }
constexpr const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator{PairProvider<true>{&table(), table().begin_index()}};
}

[[nodiscard]] constexpr size_type max_size() const noexcept { return static_max_size(); }
[[nodiscard]] constexpr std::size_t size() const noexcept { return table().size(); }
[[nodiscard]] constexpr bool empty() const noexcept { return table().size() == 0; }
Expand Down Expand Up @@ -490,19 +463,6 @@ class FixedMapAdapter
PairProvider<true>{&table(), table().iterated_index_from(start_index)}};
}

constexpr reverse_iterator create_reverse_iterator(const TableIndex& start_index) noexcept
{
return reverse_iterator{
PairProvider<false>{&table(), table().iterated_index_from(start_index)}};
}

constexpr const_reverse_iterator create_const_reverse_iterator(
const TableIndex& start_index) const noexcept
{
return const_reverse_iterator{
PairProvider<true>{&table(), table().iterated_index_from(start_index)}};
}

constexpr void check_not_full(const std_transition::source_location& loc) const
{
if (preconditions::test(table().size() < TableImpl::CAPACITY))
Expand Down
32 changes: 4 additions & 28 deletions include/fixed_containers/fixed_set_adapter.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "fixed_containers/assert_or_abort.hpp"
#include "fixed_containers/bidirectional_iterator.hpp"
#include "fixed_containers/emplace.hpp"
#include "fixed_containers/erase_if.hpp"
#include "fixed_containers/forward_iterator.hpp"
#include "fixed_containers/preconditions.hpp"
#include "fixed_containers/source_location.hpp"

Expand Down Expand Up @@ -49,24 +49,17 @@ class FixedSetAdapter

constexpr void advance() noexcept { current_index_ = table_->next_of(current_index_); }

constexpr void recede() noexcept { current_index_ = table_->prev_of(current_index_); }

constexpr const_reference get() const noexcept { return table_->key_at(current_index_); }

constexpr bool operator==(const ReferenceProvider& other) const noexcept = default;
};

template <IteratorConstness CONSTNESS, IteratorDirection DIRECTION>
using Iterator =
BidirectionalIterator<ReferenceProvider, ReferenceProvider, CONSTNESS, DIRECTION>;
template <IteratorConstness CONSTNESS>
using Iterator = ForwardIterator<ReferenceProvider, ReferenceProvider, CONSTNESS>;

public:
using const_iterator =
Iterator<IteratorConstness::CONSTANT_ITERATOR, IteratorDirection::FORWARD>;
using const_iterator = Iterator<IteratorConstness::CONSTANT_ITERATOR>;
using iterator = const_iterator;
using const_reverse_iterator =
Iterator<IteratorConstness::CONSTANT_ITERATOR, IteratorDirection::REVERSE>;
using reverse_iterator = const_reverse_iterator;

using size_type = std::size_t;
using difference_type = ptrdiff_t;
Expand Down Expand Up @@ -105,17 +98,6 @@ class FixedSetAdapter
constexpr const_iterator begin() const noexcept { return cbegin(); }
constexpr const_iterator end() const noexcept { return cend(); }

constexpr const_reverse_iterator rbegin() const noexcept { return crbegin(); }
constexpr const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator{ReferenceProvider{&table(), table().end_index()}};
}
constexpr const_reverse_iterator rend() const noexcept { return crend(); }
constexpr const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator{ReferenceProvider{&table(), table().begin_index()}};
}

[[nodiscard]] constexpr std::size_t size() const noexcept { return table().size(); }
[[nodiscard]] constexpr bool empty() const noexcept { return table().size() == 0; }

Expand Down Expand Up @@ -340,12 +322,6 @@ class FixedSetAdapter
return iterator{ReferenceProvider{&table(), table().iterated_index_from(start_index)}};
}

constexpr iterator create_const_reverse_iterator(const TableIndex& start_index) noexcept
{
return reverse_iterator{
ReferenceProvider{&table(), table().iterated_index_from(start_index)}};
}

constexpr void check_not_full(const std_transition::source_location& loc) const
{
if (preconditions::test(table().size() < TableImpl::CAPACITY))
Expand Down
79 changes: 4 additions & 75 deletions test/fixed_unordered_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,21 @@ static_assert(TriviallyCopyAssignable<ES_1>);
static_assert(TriviallyMoveAssignable<ES_1>);
static_assert(IsStructuralType<ES_1>);

static_assert(std::bidirectional_iterator<ES_1::iterator>);
static_assert(std::bidirectional_iterator<ES_1::const_iterator>);
static_assert(std::forward_iterator<ES_1::iterator>);
static_assert(std::forward_iterator<ES_1::const_iterator>);
static_assert(!std::random_access_iterator<ES_1::iterator>);
static_assert(!std::random_access_iterator<ES_1::const_iterator>);

static_assert(std::is_trivially_copyable_v<ES_1::const_iterator>);
static_assert(std::is_trivially_copyable_v<ES_1::iterator>);
static_assert(std::is_trivially_copyable_v<ES_1::reverse_iterator>);
static_assert(std::is_trivially_copyable_v<ES_1::const_reverse_iterator>);

static_assert(std::is_same_v<std::iter_value_t<ES_1::iterator>, std::pair<const int&, int&>>);
static_assert(std::is_same_v<std::iter_reference_t<ES_1::iterator>, std::pair<const int&, int&>>);
static_assert(std::is_same_v<std::iter_difference_t<ES_1::iterator>, std::ptrdiff_t>);
static_assert(std::is_same_v<typename std::iterator_traits<ES_1::iterator>::pointer,
ArrowProxy<std::pair<const int&, int&>>>);
static_assert(std::is_same_v<typename std::iterator_traits<ES_1::iterator>::iterator_category,
std::bidirectional_iterator_tag>);
std::forward_iterator_tag>);

static_assert(
std::is_same_v<std::iter_value_t<ES_1::const_iterator>, std::pair<const int&, const int&>>);
Expand All @@ -56,7 +54,7 @@ static_assert(std::is_same_v<std::iter_difference_t<ES_1::const_iterator>, std::
static_assert(std::is_same_v<typename std::iterator_traits<ES_1::const_iterator>::pointer,
ArrowProxy<std::pair<const int&, const int&>>>);
static_assert(std::is_same_v<typename std::iterator_traits<ES_1::const_iterator>::iterator_category,
std::bidirectional_iterator_tag>);
std::forward_iterator_tag>);

static_assert(std::is_same_v<ES_1::reference, ES_1::iterator::reference>);

Expand Down Expand Up @@ -775,15 +773,6 @@ TEST(FixedUnorderedMap, IteratorBasic)
static_assert(std::next(s1.begin(), 2)->second == 30);
static_assert(std::next(s1.begin(), 3)->first == 4);
static_assert(std::next(s1.begin(), 3)->second == 40);

static_assert(std::prev(s1.end(), 1)->first == 4);
static_assert(std::prev(s1.end(), 1)->second == 40);
static_assert(std::prev(s1.end(), 2)->first == 3);
static_assert(std::prev(s1.end(), 2)->second == 30);
static_assert(std::prev(s1.end(), 3)->first == 2);
static_assert(std::prev(s1.end(), 3)->second == 20);
static_assert(std::prev(s1.end(), 4)->first == 1);
static_assert(std::prev(s1.end(), 4)->second == 10);
}

TEST(FixedUnorderedMap, IteratorTypes)
Expand Down Expand Up @@ -925,11 +914,6 @@ TEST(FixedUnorderedMap, IteratorMutableValue)
static_assert(s1.begin()->second == 40);
static_assert(std::next(s1.begin(), 1)->first == 4);
static_assert(std::next(s1.begin(), 1)->second == 80);

static_assert(std::prev(s1.end(), 1)->first == 4);
static_assert(std::prev(s1.end(), 1)->second == 80);
static_assert(std::prev(s1.end(), 2)->first == 2);
static_assert(std::prev(s1.end(), 2)->second == 40);
}

TEST(FixedUnorderedMap, IteratorComparisonOperator)
Expand All @@ -945,7 +929,6 @@ TEST(FixedUnorderedMap, IteratorComparisonOperator)
static_assert(s1.begin() != s1.cend());

static_assert(std::next(s1.begin(), 2) == s1.end());
static_assert(std::prev(s1.end(), 2) == s1.begin());
}

TEST(FixedUnorderedMap, IteratorAssignment)
Expand Down Expand Up @@ -1015,11 +998,6 @@ TEST(FixedUnorderedMap, Iterator_OffByOneIssues)
static_assert(s1.begin()->second == 10);
static_assert(std::next(s1.begin(), 1)->first == 4);
static_assert(std::next(s1.begin(), 1)->second == 40);

static_assert(std::prev(s1.end(), 1)->first == 4);
static_assert(std::prev(s1.end(), 1)->second == 40);
static_assert(std::prev(s1.end(), 2)->first == 1);
static_assert(std::prev(s1.end(), 2)->second == 10);
}

TEST(FixedUnorderedMap, Iterator_EnsureOrder)
Expand All @@ -1041,13 +1019,6 @@ TEST(FixedUnorderedMap, Iterator_EnsureOrder)
static_assert(std::next(s1.begin(), 1)->second == 30);
static_assert(std::next(s1.begin(), 2)->first == 4);
static_assert(std::next(s1.begin(), 2)->second == 40);

static_assert(std::prev(s1.end(), 1)->first == 4);
static_assert(std::prev(s1.end(), 1)->second == 40);
static_assert(std::prev(s1.end(), 2)->first == 3);
static_assert(std::prev(s1.end(), 2)->second == 30);
static_assert(std::prev(s1.end(), 3)->first == 1);
static_assert(std::prev(s1.end(), 3)->second == 10);
}

TEST(FixedUnorderedMap, DereferencedIteratorAssignability)
Expand Down Expand Up @@ -1107,48 +1078,6 @@ TEST(FixedUnorderedMap, IteratorDereferenceLiveness)
}
}

TEST(FixedUnorderedMap, ReverseIteratorBasic)
{
constexpr FixedUnorderedMap<int, int, 10> s1{{1, 10}, {2, 20}, {3, 30}, {4, 40}};

static_assert(consteval_compare::equal<4, std::distance(s1.crbegin(), s1.crend())>);

static_assert(consteval_compare::equal<4, s1.rbegin()->first>);
static_assert(consteval_compare::equal<40, s1.rbegin()->second>);
static_assert(consteval_compare::equal<3, std::next(s1.rbegin(), 1)->first>);
static_assert(consteval_compare::equal<30, std::next(s1.rbegin(), 1)->second>);
static_assert(consteval_compare::equal<2, std::next(s1.rbegin(), 2)->first>);
static_assert(consteval_compare::equal<20, std::next(s1.rbegin(), 2)->second>);
static_assert(consteval_compare::equal<1, std::next(s1.rbegin(), 3)->first>);
static_assert(consteval_compare::equal<10, std::next(s1.rbegin(), 3)->second>);

static_assert(consteval_compare::equal<1, std::prev(s1.rend(), 1)->first>);
static_assert(consteval_compare::equal<10, std::prev(s1.rend(), 1)->second>);
static_assert(consteval_compare::equal<2, std::prev(s1.rend(), 2)->first>);
static_assert(consteval_compare::equal<20, std::prev(s1.rend(), 2)->second>);
static_assert(consteval_compare::equal<3, std::prev(s1.rend(), 3)->first>);
static_assert(consteval_compare::equal<30, std::prev(s1.rend(), 3)->second>);
static_assert(consteval_compare::equal<4, std::prev(s1.rend(), 4)->first>);
static_assert(consteval_compare::equal<40, std::prev(s1.rend(), 4)->second>);
}

TEST(FixedUnorderedMap, ReverseIteratorBase)
{
constexpr auto s1 = []()
{
FixedUnorderedMap<int, int, 7> s{{1, 10}, {2, 20}, {3, 30}};
auto it = s.rbegin(); // points to 3
std::advance(it, 1); // points to 2
// https://stackoverflow.com/questions/1830158/how-to-call-erase-with-a-reverse-iterator
s.erase(std::next(it).base());
return s;
}();

static_assert(s1.size() == 2);
static_assert(s1.at(1) == 10);
static_assert(s1.at(3) == 30);
}

TEST(FixedUnorderedMap, IteratorInvalidation)
{
FixedUnorderedMap<int, int, 10> s1{{10, 100}, {20, 200}, {30, 300}, {40, 400}};
Expand Down
Loading

0 comments on commit dc52292

Please sign in to comment.