Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions include/bitlib/bit-algorithms/accumulate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ constexpr auto accumulate(
using size_type = typename bit_iterator<RandomAccessIt>::size_type;
constexpr size_type digits = bitsof<word_type>();


size_type total_bits_to_op = distance(first, last);
bool keep_going = true;;
if constexpr (initial_sub_word) {
size_type sub_digits;
if constexpr (forward) {
Expand Down Expand Up @@ -90,7 +88,6 @@ constexpr auto accumulate_while(
BinaryOperationSubword binary_op_subword) {
using word_type = typename bit_iterator<RandomAccessIt>::word_type;
using size_type = typename bit_iterator<RandomAccessIt>::size_type;
using difference_type = typename bit_iterator<RandomAccessIt>::difference_type;
constexpr size_type digits = bitsof<word_type>();

size_type total_bits_to_op = distance(first, last);
Expand Down
2 changes: 1 addition & 1 deletion include/bitlib/bit-algorithms/bit_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "rotate.hpp"
#include "shift.hpp"
#include "swap_ranges.hpp"
#include "to_string.hpp"
#include "to_from_string.hpp"
#include "transform.hpp"
#include "type_traits.hpp"
// ========================================================================== //
Expand Down
29 changes: 13 additions & 16 deletions include/bitlib/bit-algorithms/shift.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ bit_iterator<RandomAccessIt> shift_left(
write_word<word_type>(new_word, first, d - n);
return first + d - n;
}
// Multiple word case
word_type first_value = *first.base();
word_type last_value = !is_last_aligned ? *last.base() : 0;

// Align first
if (!is_first_aligned) {
Expand Down Expand Up @@ -286,19 +283,19 @@ bit_iterator<RandomAccessIt> shift_right(

// Shift bit sequence to the msb
if (offset == 0) {
auto new_first = bit::bit_iterator<RandomAccessIt>(
STD_SHIFT_RIGHT(
first.base(),
last.base(),
word_shifts),
first.position()
);
// https://en.cppreference.com/w/cpp/algorithm/shift
// "Elements that are in the original range but not the new range
// are left in a valid but unspecified state."
//
//bit::fill(first, new_first, bit::bit0);
return first + n;
/*auto new_first = */
static_cast<void>(bit::bit_iterator<RandomAccessIt>(
STD_SHIFT_RIGHT(
first.base(),
last.base(),
word_shifts),
first.position()));
// https://en.cppreference.com/w/cpp/algorithm/shift
// "Elements that are in the original range but not the new range
// are left in a valid but unspecified state."
//
//bit::fill(first, new_first, bit::bit0);
return first + n;
}

if (bit::distance(first, middle) >= digits)
Expand Down
8 changes: 5 additions & 3 deletions include/bitlib/bit-algorithms/to_from_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#include "bitlib/bit-algorithms/accumulate.hpp"
#include "bitlib/bit-algorithms/count.hpp"
#include "bitlib/bit-containers/array_dynamic_extent.hpp"
#include "bitlib/bit-containers/bit_vector.hpp"
#include "bitlib/bit_concepts.hpp"

namespace bit {
Expand Down Expand Up @@ -109,6 +107,9 @@ constexpr std::string to_string(const bit_sized_range auto& bits, std::string pr
return to_string<meta>(bits.begin(), bits.end(), prefix);
}

/*
Commenting this out temporarily as the reference to bit_vector/bit_array messes up include dependency DAG

template <string::metadata_t meta = string::typical(), typename Policy = policy::typical<uintptr_t>, typename RandomAccessIt>
constexpr void from_string(
Policy,
Expand Down Expand Up @@ -145,7 +146,7 @@ constexpr bit_vector<> from_string(const char* first, const char* last) {
bits += base_bits;
}
if (bits) {
vec.append_range(array<>(bits, work));
vec.append_range(bit_array<>(bits, work));
}
}
return vec;
Expand All @@ -158,6 +159,7 @@ template <string::metadata_t meta = string::typical()>
constexpr bit_vector<> from_string(const std::string& str) {
return from_string<meta>(str.c_str(), str.c_str() + str.length());
}
*/

} // namespace bit

Expand Down
29 changes: 15 additions & 14 deletions include/bitlib/bit-containers/bit_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ template <typename T = bit_value,
std::uintptr_t,
ceil_integral<N>>,
T>,
typename Policy = policy::typical<W>>
typename Policy = policy::typical<std::remove_cvref_t<W>>>
class array;

template <std::size_t N = std::dynamic_extent,
typename W = std::conditional_t<
(N == std::dynamic_extent),
std::uintptr_t,
ceil_integral<N>>,
typename Policy = policy::typical<W>>
typename Policy = policy::typical<std::remove_cvref_t<W>>>
using bit_array = array<bit_value, N, W, Policy>;

namespace detail {
Expand All @@ -68,10 +68,11 @@ struct array_iterator_types {
} // namespace detail

template <typename T, std::size_t N, typename W, typename Policy>
class array : public array_base<array<T, N, W>, T, N, W, Policy, detail::array_iterator_types<T, W, N>> {
class array : public array_base<array<T, N, W, Policy>, T, N, W, false, Policy, detail::array_iterator_types<T, W, N>> {
public:
using base = array_base<array<T, N, W>, T, N, W, Policy, detail::array_iterator_types<T, W, N>>;
using base = array_base<array<T, N, W, Policy>, T, N, W, false, Policy, detail::array_iterator_types<T, W, N>>;
using base::end;
using base::size;
using typename base::const_iterator;
using typename base::const_pointer;
using typename base::const_reference;
Expand Down Expand Up @@ -108,13 +109,13 @@ class array : public array_base<array<T, N, W>, T, N, W, Policy, detail::array_i
this->from_integral(integral);
}

constexpr array(const array<T, N, W>& other) noexcept
: storage(other.storage) {}
constexpr array(const array<T, N, W, Policy>& other) noexcept
: base(), storage(other.storage) {}

constexpr array(const array<T, N, W>&& other) noexcept
: storage(other.storage) {}
constexpr array(const array<T, N, W, Policy>&& other) noexcept
: base(), storage(other.storage) {}

constexpr array(const bit_sized_range auto& other) {
constexpr array(const bit_sized_range auto& other) : base() {
if (other.size() != this->size()) [[unlikely]] {
throw std::invalid_argument("other bit_range contains an invalid number of bits for array.");
}
Expand All @@ -123,21 +124,21 @@ class array : public array_base<array<T, N, W>, T, N, W, Policy, detail::array_i

constexpr array(const std::initializer_list<value_type> init)
requires(!std::is_same_v<value_type, word_type>)
{
: base() {
if (init.size() != bitsof(*this)) [[unlikely]] {
throw std::invalid_argument("initialize_list contains an invalid number of bits for array.");
}
std::copy(init.begin(), init.end(), this->begin());
}

constexpr array(const std::initializer_list<bool> init) {
constexpr array(const std::initializer_list<bool> init) : base() {
if (init.size() != bitsof(*this)) [[unlikely]] {
throw std::invalid_argument("initialize_list contains an invalid number of bits for array.");
}
std::copy(init.begin(), init.end(), this->begin());
}

constexpr array(const std::initializer_list<word_type> init) : storage{} {
constexpr array(const std::initializer_list<word_type> init) : base(), storage{} {
// Make sure we handle the case where init.size() != Words
auto it = init.begin();
for (size_type i = 0; i < std::min(Words(N), init.size()); ++i, ++it) {
Expand All @@ -147,7 +148,7 @@ class array : public array_base<array<T, N, W>, T, N, W, Policy, detail::array_i

constexpr array(const std::string_view s)
requires(std::is_same_v<value_type, bit_value>)
{
: base() {
if (bitsof(*this) != static_cast<size_t>(std::count(s.begin(), s.end(), '0') + std::count(s.begin(), s.end(), '1'))) [[unlikely]] {
throw std::invalid_argument("String contains an invalid number of bits for array.");
};
Expand Down Expand Up @@ -209,7 +210,7 @@ class array : public array_base<array<T, N, W>, T, N, W, Policy, detail::array_i
/*
* Operations
*/
constexpr void swap(array<T, N, W>& other) noexcept {
constexpr void swap(array<T, N, W, Policy>& other) noexcept {
std::swap(this->storage, other.storage);
}

Expand Down
19 changes: 12 additions & 7 deletions include/bitlib/bit-containers/bit_array_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace bit {

template <typename T, typename W, typename Policy>
template <typename T, size_t N, typename W, typename Policy>
class array_ref;

template <typename T,
Expand All @@ -50,8 +50,8 @@
* @tparam Policy The policy for integral conversion (default is typical)
* @tparam Iterators A struct that provides iterator and const_iterator types
*/
template <typename Derived, typename T, size_t N, typename W, typename Policy, typename Iterators>
class array_base {
template <typename Derived, typename T, size_t N, typename W, bool resizable, typename Policy, typename Iterators>
class array_base : public detail::container_size_storage<std::size_t, resizable, N> {
protected:
constexpr Derived& derived() noexcept {
return static_cast<Derived&>(*this);
Expand All @@ -73,6 +73,11 @@
using iterator = Iterators::iterator;
using const_iterator = Iterators::const_iterator;

constexpr array_base() noexcept : detail::container_size_storage<std::size_t, resizable, N>() {}
constexpr array_base(const size_type& size) noexcept
requires(N == std::dynamic_extent)
: detail::container_size_storage<std::size_t, resizable, N>(size) {}

// Element access
constexpr reference operator[](size_type pos) {
return derived().begin()[pos];
Expand Down Expand Up @@ -163,14 +168,14 @@
* @brief Slice operations - returns a array_ref
*/
constexpr auto operator()(size_type offset, size_type right) const noexcept {
return array_ref<value_type, const word_type, Policy>(&this->at(offset), right - offset);
return array_ref<value_type, std::dynamic_extent, const word_type, Policy>(&this->at(offset), right - offset);
}

/**
* @brief Slice operations - returns a array_ref
*/
constexpr auto operator()(size_type offset, size_type right) noexcept {
return array_ref<value_type, word_type, Policy>(&this->at(offset), right - offset);
return array_ref<value_type, std::dynamic_extent, word_type, Policy>(&this->at(offset), right - offset);
}

// Common operations
Expand All @@ -193,7 +198,7 @@
::bit::copy(derived().begin(), end(), bit_pointer<U>(&integral));
}
if (derived().size() < bitsof<U>()) {
Policy::extension::template to_integral<U, N>(derived(), integral);
Policy::extension::template to_integral<U, N>(derived(), integral, detail::uninitialized);

Check warning on line 201 in include/bitlib/bit-containers/bit_array_base.hpp

View check run for this annotation

Codecov / codecov/patch

include/bitlib/bit-containers/bit_array_base.hpp#L201

Added line #L201 was not covered by tests
}
} else {
if constexpr (N > bitsof<U>()) {
Expand All @@ -202,7 +207,7 @@
::bit::copy(derived().begin(), end(), bit_pointer<U>(&integral));
}
if constexpr (N < bitsof<U>()) {
Policy::extension::template to_integral<U, N>(derived(), integral);
Policy::extension::template to_integral<U, N>(derived(), integral, detail::uninitialized);
}
}

Expand Down
Loading
Loading