Skip to content

Commit

Permalink
set_to -> import_bits & export_to -> export_bits
Browse files Browse the repository at this point in the history
  • Loading branch information
nessan committed Aug 1, 2024
1 parent a0de1f9 commit 55b98d7
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 218 deletions.
2 changes: 1 addition & 1 deletion docs/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ format:
- cosmo
- assets/css/theme.scss
website:
title: "Linear Algebra over GF(2)"
title: "Working in GF(2)"
image: "assets/images/logo.jpg"
favicon: "assets/images/logo.jpg"
open-graph: true
Expand Down
6 changes: 3 additions & 3 deletions docs/pages/_common.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ $$
[`vector::dot`]: /pages/vector/dot.qmd
[`vector::element`]: /pages/vector/access.qmd
[`vector::empty`]: /pages/vector/size.qmd
[`vector::export_all_to`]: /pages/vector/export_to.qmd
[`vector::export_to`]: /pages/vector/export_to.qmd
[`vector::export_all_bits`]: /pages/vector/export_bits.qmd
[`vector::export_bits`]: /pages/vector/export_bits.qmd
[`vector::empty`]: /pages/vector/size.qmd
[`vector::final_set`]: /pages/vector/first_set.qmd
[`vector::first_set`]: /pages/vector/first_set.qmd
Expand All @@ -53,6 +53,7 @@ $$
[`vector::from`]: /pages/vector/from.qmd
[`vector::front`]: /pages/vector/access.qmd
[`vector::if_set_call`]: /pages/vector/if_set_call.qmd
[`vector::import_bits`]: /pages/vector/import_bits.qmd
[`vector::join`]: /pages/vector/join.qmd
[`vector::next_set`]: /pages/vector/first_set.qmd
[`vector::none`]: /pages/vector/all.qmd
Expand Down Expand Up @@ -91,7 +92,6 @@ $$
[`vector::set_if`]: /pages/vector/set_if.qmd
[`vector::set_indices`]: /pages/vector/indices.qmd
[`vector::set`]: /pages/vector/set.qmd
[`vector::set_to`]: /pages/vector/set_to.qmd
[`vector::shrink_to_fit`]: /pages/vector/reserve.qmd
[`vector::size`]: /pages/vector/size.qmd
[`vector::stream<<`]: /pages/vector/stream.qmd
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/polynomial/reference.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Method | Description
`operator=` | Assigns a `bool` to the referenced coefficient.
`to_bool` | Return the value of the referenced coefficient as a boolean.
`operator bool` | Casts the referenced coefficient to a boolean value.
`set_to(val)` | Sets the value of the referenced polynomial coefficient to the passed argument.
`import_bits(val)` | Sets the value of the referenced polynomial coefficient to the passed argument.
`set` | Sets the referenced polynomial coefficient to 1.
`reset` | Sets the referenced polynomial coefficient to 0.
`flip` | Flips the referenced referenced polynomial coefficient from 1 to 0 and vice versa.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ The bits from a bit-vector can be exported to fill various destinations --- unsi

```cpp
template<std::unsigned_integral Dst>
constexpr void export_to(Dst &dst) const; // <1>
constexpr void export_bits(Dst &dst) const; // <1>

template<typename Iter>
constexpr void export_to(Iter b, Iter e) const; // <2>
constexpr void export_bits(Iter b, Iter e) const; // <2>

template<std::unsigned_integral Dst, std::size_t N>
constexpr void export_to(std::array<Dst, N> &dst) const; // <3>
constexpr void export_bits(std::array<Dst, N> &dst) const; // <3>

template<std::unsigned_integral Dst>
constexpr void export_to(std::vector<Dst> &dst) const; // <4>
constexpr void export_bits(std::vector<Dst> &dst) const; // <4>

template<std::size_t N>
constexpr void export_to(std::bitset<N> &dst) const; // <5>
constexpr void export_bits(std::bitset<N> &dst) const; // <5>
```
1. Initializes the word `dst` to zero and then fills it with as many bits as possible from the bit-vector.
2. Initializes an iteration of unsigned integers to zeros and fills them with as many bits as possible from the bit-vector.
Expand All @@ -37,17 +37,17 @@ Note that the destination gets initialized to all zeros.
Thus, if the source bit-vector is empty, the destination will be returned filled with zeros.
:::
In many applications, we use these `export_to` functions to flip back and forth between bit-space and word-space.
In many applications, we use these `export_bits` functions to flip back and forth between bit-space and word-space.
In word-space, the sources and destinations are often a `std::vector` or a `std::array`, so we supply specializations for those collections --- one could get by just using the versions of `export_to` that take iterators.
In word-space, the sources and destinations are often a `std::vector` or a `std::array`, so we supply specializations for those collections --- one could get by just using the versions of `export_bits` that take iterators.
## Copying ALL bits from a bit-vector
## Exporting ALL bits from a bit-vector
We also have a method that takes all the bits from a bit-vector and uses them to fill a vector of words of some unsigned type.
```cpp
template<std::unsigned_integral Dst>
constexpr void export_all_to(std::vector<Dst> &dst) const;
constexpr void export_all_bits(std::vector<Dst> &dst) const;
```
As the name suggests, this method first resizes the destination vector to accommodate all the bits in the source bit-vector and then copies them.

Expand All @@ -57,7 +57,7 @@ Parameter | Description
--------- | -----------
`Dst` | The type of unsigned integers getting set from the bits in the source bit-vector. There is _no_ requirement that `Dst` and `Block` are the same. For example, we can fill an array of 32-bit unsigned integers while the storage scheme for the bit-vector remains the default 64-bit type.
`Iter` | An iterator --- might be the type returned by any `std::cbegin(collection)` and `std::cend(collection)` `Iter::value_type` should be some unsigned integer type, but it need not match the `Block` type.
: {.bordered .hover .responsive tbl-colwidths="[10,90]"}
: {.bordered .hover .responsive tbl-colwidths="[20,80]"}


[Example --- Overwriting a unsigned word(s) with the bits from a bit-vector]{.bt}
Expand All @@ -69,42 +69,42 @@ int main()

std::cout << std::format("bit::vector: {:b}\n", v);
std::cout << "Exporting that bit-vector to words of various types:\n";
uint16_t word16; v.export_to(word16);
uint16_t word16; v.export_bits(word16);
std::cout << std::format("uint16_t: {}\n", word16);
uint32_t word32; v.export_to(word32);
uint32_t word32; v.export_bits(word32);
std::cout << std::format("uint32_t: {}\n", word32);
uint64_t word64; v.export_to(word64);
uint64_t word64; v.export_bits(word64);
std::cout << std::format("uint64_t: {}\n", word64);
std::cout << std::endl;

std::cout << std::format("bit::vector: {:b}\n", v);
std::cout << "Exporting that bit-vector to a std::array of words of various types:\n";
constexpr std::size_t N = 4;
std::array<uint16_t, N> arr16; v.export_to(arr16);
std::array<uint16_t, N> arr16; v.export_bits(arr16);
std::cout << std::format("std::array<uint16_t,4>: {}\n", arr16);
std::array<uint32_t, N> arr32; v.export_to(arr32);
std::array<uint32_t, N> arr32; v.export_bits(arr32);
std::cout << std::format("std::array<uint32_t,4>: {}\n", arr32);
std::array<uint64_t, N> arr64; v.export_to(arr64);
std::array<uint64_t, N> arr64; v.export_bits(arr64);
std::cout << std::format("std::array<uint64_t,4>: {}\n", arr64);
std::cout << std::endl;

std::cout << std::format("bit::vector: {:b}\n", v);
std::cout << "Exporting that bit-vector to a std::vector of words of various types:\n";
std::vector<uint16_t> vec16(N); v.export_to(vec16);
std::vector<uint16_t> vec16(N); v.export_bits(vec16);
std::cout << std::format("std::vector<uint16_t>: {}\n", vec16);
std::vector<uint32_t> vec32(N); v.export_to(vec32);
std::vector<uint32_t> vec32(N); v.export_bits(vec32);
std::cout << std::format("std::vector<uint32_t>: {}\n", vec32);
std::vector<uint64_t> vec64(N); v.export_to(vec64);
std::vector<uint64_t> vec64(N); v.export_bits(vec64);
std::cout << std::format("std::vector<uint64_t>: {}\n", vec64);
std::cout << std::endl;

std::cout << std::format("bit::vector: {:b}\n", v);
std::cout << "Exporting ALL of that bit-vector to a std::vector of words of various types:\n";
v.export_all_to(vec16);
v.export_all_bits(vec16);
std::cout << std::format("std::vector<uint16_t>: {}\n", vec16);
v.export_all_to(vec32);
v.export_all_bits(vec32);
std::cout << std::format("std::vector<uint32_t>: {}\n", vec32);
v.export_all_to(vec64);
v.export_all_bits(vec64);
std::cout << std::format("std::vector<uint64_t>: {}\n", vec64);
std::cout << std::endl;
}
Expand Down Expand Up @@ -139,4 +139,4 @@ std::vector<uint64_t>: [18446744073709551615, 8191]

### See Also
[`vector::append`] \
[`vector::set_to`]
[`vector::import_bits`]
91 changes: 91 additions & 0 deletions docs/pages/vector/import_bits.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
title: "`bit::vector` --- Importing Bits"
---

{{< include /pages/_common.qmd >}}

A bit-vector can be import bits sourced from an unsigned word, a collection of unsigned words, or a `std::bitset`.
By default the imported bits completely overwrite the bit-vector but you can set the `add` parameter to true to have the bits appended to the end of the bit-vector instead.

```cpp
template<std::unsigned_integral Src=>
constexpr vector& import_bits(Src src, bool add = false); // <1>

template<typename Iter>
constexpr vector& import_bits(Iter b, Iter e, bool add = false); // <2>

template<std::unsigned_integral Src>
constexpr vector& import_bits(std::initializer_list<Src> src, bool add = false); // <3>

template<std::unsigned_integral Src>
constexpr vector& import_bits(const std::vector<Src> &src, bool add = false); // <4>

template<std::unsigned_integral Src, std::size_t N>
constexpr vector& import_bits(const std::array<Src,N> &src, bool add = false); // <5>

template<std::size_t N>
constexpr vector& import_bits(const std::bitset<N> &src, bool add = false); // <6>
```
1. Imports the bits from a single word `src`, which is some unsigned integer type.
2. Imports the bits from an iteration where the `value_type` of the iterator is some unsigned integer type.
3. Imports the bits from an initializer-style list of unsigned integers.
4. Imports the bits from a vector of unsigned integers.
5. Imports the bits from a fixed array of unsigned integers.
6. Imports the bits from a `std::bitset`.
::: {.callout-note}
# These resize the destination
These functions all resize the destination bit-vector to make it absorb all the source bits.
:::
## Parameters
Parameter | Description
--------- | -----------
`Src` | The type of unsigned integers whose bits will fill the destination bit-vector. There is _no_ requirement that `Src` and `Block` are the same. For example, we can add the bits from a list of 32-bit unsigned integers while the storage scheme for the bit-vector remains the default 64-bit type.
`Iter` | An iterator --- might be the type returned by `std::cbegin(collection)` and `std::cend(collection)`. The `Iter::value_type` should be some unsigned integer type, but it need not match the `Block` type.
`add` | If `true` then we append the imported bits to the end of the bit-vector. The default value of the parameter is `false` and we first call the [`vector::clear`] method before importing the bits.
: {.bordered .hover .responsive tbl-colwidths="[20,80]"}
[Example --- Overwriting a bit-vector with the bits from unsigned words]{.bt}
```cpp
#include <bit/bit.h>
int main()
{
bit::vector v; // <1>
std::cout << "v: " << v << '\n';
v.import_bits(std::uint8_t(0)); // <2>
std::cout << "v: " << v << '\n';
v.import_bits({std::uint8_t(255), std::uint8_t(0)}); // <3>
std::cout << "v: " << v << '\n';
std::vector<std::uint8_t> vec{255, 0}; // <4>
v.import_bits(vec);
std::cout << "v: " << v << '\n';
v.import_bits(vec.cbegin(), vec.cend()); // <5>
std::cout << "v: " << v << '\n';
std::bitset<8> bs(255); // <6>
v.import_bits(bs);
std::cout << "v: " << v << '\n';
}
```
1. Default constructor makes an empty vector.
2. Fills with eight 0-bits.
3. Fill with a list of eight 1-bits and eight 0-bits.
4. Fills with a `std::vector` with eight 1-bits and eight 0-bits.
5. Fills with a `std::vector` with eight 1-bits and eight 0-bits using the usual iterators.
6. Fills with a `std::bitset` with eight 1-bits.

[Output]{.bt}
```bash
v: []
v: [0 0 0 0 0 0 0 0]
v: [1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0]
v: [1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0]
v: [1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0]
v: [1 1 1 1 1 1 1 1]
```

### See Also
[`vector::append`] \
[`vector::export_bits`] \
[`vector::export_all_bits`]
8 changes: 4 additions & 4 deletions docs/pages/vector/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ Method | Description
### Import and Export
Method | Description
------ | -----------
[`vector::export_to`] | Use the bits from the bit-vector to fill various destinations without resizing the destination.
[`vector::export_all_to`] | Resize and fill a `std::vector` of some unsigned integers type with **all** the bits from this bit-vector.
[`vector::set_to`] | Overwrite the bit-vector with bits from various sources.
: {.bordered .striped .hover .responsive tbl-colwidths="[30,70]"}
[`vector::export_bits`] | Use the bits from the bit-vector to fill various destinations without resizing the destination.
[`vector::export_all_bits`] | Resize and fill a `std::vector` of some unsigned integer type with **all** the bits from this bit-vector.
[`vector::import_bits`] | Import bits from various sources into this bit-vector. By default these methods completely overwrite the bit-vector with the imported data but can instead append to the existing elements if that is desired.
: {.bordered .striped .hover .responsive tbl-colwidths="[35,65]"}

### String Conversions
Method | Description
Expand Down
89 changes: 0 additions & 89 deletions docs/pages/vector/set_to.qmd

This file was deleted.

Loading

0 comments on commit 55b98d7

Please sign in to comment.