3#include <clingo/util/immutable_value.hh>
10namespace CppClingo::Util {
20 using vector_type = std::vector<T>;
65 vec_ = Util::make_immutable<vector_type>(std::move(vec));
78 vec_ = Util::make_immutable<vector_type>(first, last);
85 auto vec = vector_type{};
86 vec.reserve(std::distance(first, last));
87 for (
auto it = first; it != last; ++it) {
88 vec.emplace_back(conv(*it));
90 vec_ = Util::make_immutable<vector_type>(std::move(vec));
99 throw std::out_of_range(
"");
114 [[nodiscard]]
auto data() const -> T const * {
return vector_().data(); }
123 [[nodiscard]]
auto empty() const noexcept ->
bool {
return vector_().empty(); }
126 [[nodiscard]]
auto size() const noexcept ->
size_type {
return vector_().size(); }
133 return lhs.vector_() == rhs.vector_();
138 return std::lexicographical_compare_three_way(lhs.
begin(), lhs.
end(), rhs.
begin(), rhs.
end());
142 [[nodiscard]]
auto vector_() const -> std::vector<T> const & {
return vec_ ? *vec_ : empty_(); }
144 static auto empty_() -> std::vector<T>
const & {
145 static std::vector<T> res;
148 Util::immutable_value<vector_type> vec_;
164 res.reserve(
sizeof...(Ts));
165 (res.emplace_back(std::forward<Ts>(args)), ...);
An immutable array with efficient copying.
Definition immutable_array.hh:18
typename vector_type::value_type value_type
The value type.
Definition immutable_array.hh:24
typename vector_type::size_type size_type
The unsigned size type.
Definition immutable_array.hh:30
typename vector_type::const_iterator const_iterator
Theconst iterator type.
Definition immutable_array.hh:36
typename vector_type::const_reference const_reference
The reference type.
Definition immutable_array.hh:26
immutable_array(std::span< T const > span)
Construct array copying values from the given span.
Definition immutable_array.hh:57
friend auto operator<=>(immutable_array const &lhs, immutable_array const &rhs)
Compare two vectors lexicographically.
Definition immutable_array.hh:137
immutable_array(std::initializer_list< T > init)
Construct array coping from the given initializer list.
Definition immutable_array.hh:73
immutable_array(std::vector< T > &&vec)
Construct array taking ownership of the given vector.
Definition immutable_array.hh:62
auto operator[](size_type pos) const -> const_reference
Get the element at the given index.
Definition immutable_array.hh:105
immutable_array(std::vector< T > const &vec)
Construct array copying from the given vector.
Definition immutable_array.hh:70
constexpr immutable_array() noexcept=default
Construct an empty array.
auto empty() const noexcept -> bool
Check if the array is empty.
Definition immutable_array.hh:123
auto data() const -> T const *
Get a pointer to the undelying data.
Definition immutable_array.hh:114
auto front() const -> const_reference
Get a reference to the first element.
Definition immutable_array.hh:108
typename vector_type::const_pointer const_pointer
The pointer type.
Definition immutable_array.hh:34
void swap(immutable_array &other) noexcept
Swap two immutable arrays.
Definition immutable_array.hh:129
friend auto operator==(immutable_array const &lhs, immutable_array const &rhs) -> bool
Compare two vectors.
Definition immutable_array.hh:132
typename vector_type::allocator_type allocator_type
The allocator type.
Definition immutable_array.hh:28
auto at(size_type pos) const -> const_reference
Get the element at the given index.
Definition immutable_array.hh:97
immutable_array(It first, It last)
Construct array coping from the given iterator range.
Definition immutable_array.hh:76
typename vector_type::difference_type difference_type
The signed size type.
Definition immutable_array.hh:32
auto size() const noexcept -> size_type
Get the size of the array.
Definition immutable_array.hh:126
auto back() const -> const_reference
Get a reference to the last element.
Definition immutable_array.hh:111
auto end() const noexcept -> const_iterator
Get an iterator pointing to the end of the array.
Definition immutable_array.hh:120
auto begin() const noexcept -> const_iterator
Get an iterator pointing to the beginning of the array.
Definition immutable_array.hh:117
immutable_array(It first, It last, Pred conv)
Construct array coping from the given iterator range.
Definition immutable_array.hh:83
auto make_immutable_array(Ts &&...args) -> immutable_array< T >
Construct an immutable array from the given elements.
Definition immutable_array.hh:162
void swap(immutable_array< T > &lhs, immutable_array< T > &rhs) noexcept
Swap two immutable arrays.
Definition immutable_array.hh:155
auto operator(const immutable_value< X > &lhs, const immutable_value< Y > &rhs)
Compare two immutable values.
Definition immutable_value.hh:152