Skip to content

✨ Added scatter_span functionality#3

Open
PhazonicRidley wants to merge 10 commits into
mainfrom
scatter_span_base
Open

✨ Added scatter_span functionality#3
PhazonicRidley wants to merge 10 commits into
mainfrom
scatter_span_base

Conversation

@PhazonicRidley

Copy link
Copy Markdown
Contributor

Added the basic functionality for scatter span using modules and as cheaply as possible (memory and timing wise). Primarily geared towards embedded systems but can be used anywhere

@kammce kammce left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Looking at the tests, I can see that this works just like I wanted it to. I'm excited to be able to write.

m_serial->write({ header, body, footer}); 

And no longer need an extra array or to add make_scatter_array each time.

Comment thread .gitignore

# Allowed for clangd resolver script
!.vscode/settings.json No newline at end of file
!.vscode/settings.json

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather us just remove the line not allowing .vscode into the code base.

Comment thread conanfile.py
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.configure(cli_args=["-Wno-dev"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cmake.configure(cli_args=["-Wno-dev"])
cmake.configure()

Comment thread modules/core.cppm
Comment on lines +10 to +16
namespace mem::detail {
template<typename T, size_t N>
struct scatter_array_storage
{
std::array<std::span<T const>, N> m_internal_arr;
};
}; // namespace mem::detail

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
namespace mem::detail {
template<typename T, size_t N>
struct scatter_array_storage
{
std::array<std::span<T const>, N> m_internal_arr;
};
}; // namespace mem::detail
namespace {
template<typename T, size_t N>
struct scatter_array_storage
{
std::array<std::span<T const>, N> m_internal_arr;
};
}; // namespace

Since we aren't using headers anymore, we don't need to do the whole "detail" dance. You should be able to just use an anon namespace within a module interface.

Comment thread modules/core.cppm
};
}; // namespace mem::detail

export namespace mem {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export namespace mem {
namespace mem {

Refrain from exporting the whole namespace. Export each item we want available for our public APIs.

Comment thread modules/core.cppm
Comment on lines +235 to +237
* @details Works similarly to @c std::span, but the viewed elements do not
* need to be contiguous in memory. Each constituent chunk is a separate
* @c std::span<T const>. Iterating yields each chunk a span in order.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats up with the @c things within the comment?

Comment thread modules/core.cppm
constexpr scatter_span<T>(std::initializer_list<std::span<T const>> p_il)
: m_spans(p_il.begin(), p_il.size())
, m_start_pos(0)
, m_final_len(p_il.size() == 0 ? 0 : (p_il.end() - 1)->size())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, m_final_len(p_il.size() == 0 ? 0 : (p_il.end() - 1)->size())
, m_final_len([p_il]() {
if (p_il.size() == 0) { return 0; }
else { return p_il.back().size(); }
}())

This is a suggestion of what you could turn the ternery into if you'd prefer to use a lambda. But if you do stick with the ternary, can you change the API to use .back() vs end() - 1. I think its better expressed that way.

Comment thread modules/core.cppm
Comment on lines +363 to +364
size_t start_pos =
boundary ? 0 : p_args.offset - (cur_len - effective_span_size);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
size_t start_pos =
boundary ? 0 : p_args.offset - (cur_len - effective_span_size);
size_t start_pos = 0;
if (boundary) { start_pos = p_args.offset - (cur_len - effective_span_size); }

I find if statements are a bit cleaner and less obtuse than ternaries.

Comment thread modules/core.cppm
Comment on lines +53 to +54
auto const* first = m_ssp->m_spans.data();
auto const* last = m_ssp->m_spans.data() + m_ssp->m_spans.size() - 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use begin() and end() for these? Last could also just be m_ssp->m_spans.back().

expect(that % scatter_span_eq(offset_greater_than_len, {}));
};
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test to also verify that the scatter span object can be used within a ranged for loop. Currently we use that property within the sub scatter span, but if we ever change the implementation of that function to no longer use the ranged-for loop, then we might silently break it without knowing.

Comment thread modules/scatter_span.cppm
#include <cstddef>
#include <span>

export module scatter_span;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add license to to this file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants