Skip to content

Commit

Permalink
Cleanup struct homogeneous_vector
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Oct 23, 2024
1 parent b33a3d1 commit 2e976bf
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 123 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Procedures for each standard are provided by the following R7RS-style libraries:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cd build
make package
sudo apt install build/meevax_0.5.244_amd64.deb
sudo apt install build/meevax_0.5.245_amd64.deb
```

or
Expand Down Expand Up @@ -122,9 +122,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|-------------|-------------
| `all` | Build shared-library `libmeevax.0.5.244.so` and executable `meevax`
| `all` | Build shared-library `libmeevax.0.5.245.so` and executable `meevax`
| `test` | Test executable `meevax`
| `package` | Generate debian package `meevax_0.5.244_amd64.deb`
| `package` | Generate debian package `meevax_0.5.245_amd64.deb`
| `install` | Copy files into `/usr/local` directly

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.244
0.5.245
83 changes: 18 additions & 65 deletions include/meevax/kernel/homogeneous_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,79 +28,42 @@ namespace meevax
inline namespace kernel
{
template <typename T>
struct homogeneous_vector
struct homogeneous_vector : private std::valarray<T>
{
std::valarray<T> valarray;
using std::valarray<T>::operator [];
using std::valarray<T>::size;
using std::valarray<T>::valarray;
using std::valarray<T>::value_type;

homogeneous_vector() = default;
auto valarray() -> decltype(auto) { return static_cast<std::valarray<T> &>(*this); }
auto valarray() const -> decltype(auto) { return static_cast<std::valarray<T> const&>(*this); }

// list->@vector
explicit homogeneous_vector(object xs)
: valarray(length(xs))
explicit homogeneous_vector(from_list_tag, let xs)
: std::valarray<T>(length(xs))
{
std::generate(std::begin(valarray), std::end(valarray), [&]() mutable
std::generate(std::begin(*this), std::end(*this), [&]() mutable
{
let const x = car(xs);
xs = cdr(xs);
return input_cast(x);
});
}

// make-@vector
explicit homogeneous_vector(std::size_t size, object const& x)
: valarray(input_cast(x), size)
{}

// @vector-copy
explicit homogeneous_vector(homogeneous_vector const& v, std::size_t begin, std::size_t end)
: valarray(v.valarray[std::slice(begin, begin < end ? end - begin : 0, 1)])
{}

// @vector-copy
explicit homogeneous_vector(homogeneous_vector const& v, std::size_t begin = 0)
: homogeneous_vector { v, begin, v.valarray.size() }
{}

// @vector-append
explicit homogeneous_vector(homogeneous_vector const& a, homogeneous_vector const& b)
: valarray(a.valarray.size() + b.valarray.size())
static auto tag() -> auto const&
{
slice(0, a.valarray.size()) = a.valarray;
slice(b.valarray.size(), valarray.size()) = b.valarray;
}

// string->u8vector
explicit homogeneous_vector(T const* data, std::size_t size)
: valarray(data, size)
{}

// get-output-u8vector
explicit homogeneous_vector(std::vector<T> const& v)
: valarray(v.data(), v.size())
{}

template <typename Iterator>
explicit homogeneous_vector(Iterator begin, Iterator end)
: valarray(std::distance(begin, end))
{
std::copy(begin, end, std::begin(valarray));
}

static auto tag() -> decltype(auto)
{
auto static const tag = lexical_cast<std::string>(std::is_integral_v<T> ? std::is_signed_v<T> ? 's' : 'u' : 'f', sizeof(T) * CHAR_BIT);
auto static const tag = (std::is_integral_v<T> ? std::is_signed_v<T> ? "s" : "u" : "f") + std::to_string(sizeof(T) * CHAR_BIT);
return tag;
}

template <auto I = 0>
static auto input_cast(object const& x) -> T
{
using Us = std::tuple<exact_integer, float, double>;
using types = std::tuple<exact_integer, float, double>;

if constexpr (I < std::tuple_size_v<Us>)
if constexpr (I < std::tuple_size_v<types>)
{
using U = std::tuple_element_t<I, Us>;
return x.is<U>() ? static_cast<T>(x.as<U>()) : input_cast<I + 1>(x);
using type = std::tuple_element_t<I, types>;
return x.is<type>() ? static_cast<T>(x.as<type>()) : input_cast<I + 1>(x);
}
else
{
Expand All @@ -112,16 +75,6 @@ inline namespace kernel
{
return make<std::conditional_t<std::is_floating_point_v<T>, T, exact_integer>>(x);
}

auto slice(std::size_t begin, std::size_t end, std::size_t stride = 1) -> decltype(auto)
{
return valarray[std::slice(begin, end - begin, stride)];
}

auto slice(std::size_t begin = 0) -> decltype(auto)
{
return slice(begin, valarray.size());
}
};

template <typename T>
Expand All @@ -133,7 +86,7 @@ inline namespace kernel

auto whitespace = "";

for (auto const& value : datum.valarray)
for (auto value : datum.valarray())
{
output << std::exchange(whitespace, " ") << cyan(homogeneous_vector<T>::output_cast(value));
}
Expand All @@ -149,7 +102,7 @@ inline namespace kernel
return std::all_of(std::begin(xs), std::end(xs), [](auto x) { return x; });
};

return check(a.valarray == b.valarray);
return check(a.valarray() == b.valarray());
}

using s8vector = homogeneous_vector<std::int8_t>;
Expand Down
5 changes: 3 additions & 2 deletions include/meevax/kernel/input_homogeneous_vector_port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ inline namespace kernel
std::deque<T> deque;

explicit input_homogeneous_vector_port(homogeneous_vector<T> const& v)
: deque(std::begin(v.valarray), std::end(v.valarray))
: deque(std::begin(v.valarray()), std::end(v.valarray()))
{}

auto close() -> void override
Expand Down Expand Up @@ -61,7 +61,8 @@ inline namespace kernel
}
else
{
let const v = make<homogeneous_vector<T>>(deque.begin(), std::next(deque.begin(), size));
let const v = make<homogeneous_vector<T>>(direct_initialization, size);
std::copy(deque.begin(), std::next(deque.begin(), size), std::begin(v.as<homogeneous_vector<T>>().valarray()));
deque.erase(deque.begin(), std::next(deque.begin(), size));
return v;
}
Expand Down
7 changes: 7 additions & 0 deletions include/meevax/kernel/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ namespace meevax
{
inline namespace kernel
{
struct from_list_tag
{
explicit from_list_tag() = default;
};

inline constexpr from_list_tag from_list {};

inline auto list = [](auto&&... xs) constexpr
{
return (std::forward<decltype(xs)>(xs) | ... | nullptr);
Expand Down
2 changes: 1 addition & 1 deletion include/meevax/kernel/output_homogeneous_vector_port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ inline namespace kernel

auto put(u8vector const& v) -> void override
{
std::copy(std::begin(v.valarray), std::end(v.valarray), std::back_inserter(vector));
std::copy(std::begin(v.valarray()), std::end(v.valarray()), std::back_inserter(vector));
}
};

Expand Down
28 changes: 25 additions & 3 deletions include/meevax/memory/collector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ inline namespace memory
{
using view = std::pair<void const*, std::size_t>; // TODO Adapt to C++20's std::range concept

struct direct_initialization_tag
{
explicit direct_initialization_tag() = default;
};

inline constexpr direct_initialization_tag direct_initialization {};

struct list_initialization_tag
{
explicit list_initialization_tag() = default;
};

inline constexpr list_initialization_tag list_initialization {};

/*
This mark-and-sweep garbage collector is based on the implementation of
gc_ptr written by William E. Kempf and posted to CodeProject.
Expand Down Expand Up @@ -91,11 +105,19 @@ inline namespace memory

static inline auto allocator = allocator_type();

template <typename... Us>
explicit constexpr binder(direct_initialization_tag, Us&&... xs)
: std::conditional_t<std::is_base_of_v<Top, Bound> and std::is_constructible_v<Top, Us...>, Top, Bound>(std::forward<decltype(xs)>(xs)...)
{}

template <typename... Us>
explicit constexpr binder(list_initialization_tag, Us&&... xs)
: std::conditional_t<std::is_base_of_v<Top, Bound> and std::is_constructible_v<Top, Us...>, Top, Bound> { std::forward<decltype(xs)>(xs)... }
{}

template <typename... Us>
explicit constexpr binder(Us&&... xs)
: std::conditional_t<std::is_base_of_v<Top, Bound> and std::is_constructible_v<Top, Us...>, Top, Bound> {
std::forward<decltype(xs)>(xs)...
}
: binder { list_initialization, std::forward<decltype(xs)>(xs)... }
{}

~binder() override = default;
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/binary_input_file_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ inline namespace kernel
{
if (auto buffer = std::vector<std::uint8_t>(size); ifstream.read(reinterpret_cast<char *>(buffer.data()), size))
{
return make<u8vector>(buffer);
return make<u8vector>(direct_initialization, buffer.data(), buffer.size());
}
else
{
buffer.resize(ifstream.gcount());
return make<u8vector>(buffer);
return make<u8vector>(direct_initialization, buffer.data(), buffer.size());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/kernel/binary_output_file_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ inline namespace kernel

auto binary_output_file_port::put(u8vector const& v) -> void
{
for (auto u8 : v.valarray)
for (auto u8 : v.valarray())
{
ofstream.write(reinterpret_cast<char const*>(&u8), 1);
}
Expand Down
Loading

0 comments on commit 2e976bf

Please sign in to comment.