Skip to content

write a circular array (jazz::buffer::Loop)#16

Merged
micromho merged 4 commits into
mainfrom
nausicaa/ring-buffer
May 9, 2026
Merged

write a circular array (jazz::buffer::Loop)#16
micromho merged 4 commits into
mainfrom
nausicaa/ring-buffer

Conversation

@micromho
Copy link
Copy Markdown
Collaborator

@micromho micromho commented May 8, 2026

two considerations:

  • i like fanciful names and all, but v willing to rename this buffer::CircularBuffer or smth for absolute clarity
  • division / modulo is bad. maybe we just make this always a power-of-two in length and use bitmasks for indexing?

@micromho micromho requested review from abcdchop and glittershark May 8, 2026 19:24
@micromho micromho changed the title write a circular array (buffer::Loop) write a circular array (jazz::buffer::Loop) May 8, 2026
@micromho micromho force-pushed the nausicaa/ring-buffer branch from dcdbf38 to 0439756 Compare May 9, 2026 14:51
Comment thread lib/libjazz/include/libjazz/buffer.hpp Outdated
std::array<Sample, Size> buffer_;
std::ptrdiff_t zero_;

std::size_t real_index_(std::ptrdiff_t i) const {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this seems like a slightly odd function:

  • the underscore suffix, for one, makes me think it should just be private
  • maybe it should actually just be something that returns a const reference to the backing array, which you can then index into (or iterate over) at your heart's content?

that, or we should just delete it (and not test it)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

added a long comment explaining why it exists and what it does

Comment thread lib/libjazz/include/libjazz/buffer.hpp Outdated
* contain the item we just wrote.
*/
template <typename Sample, const std::size_t Size>
class Loop {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

can you define an ostream& << for this guy, so we can debug-print it?

Comment thread lib/libjazz/include/libjazz/buffer.hpp Outdated
Iterator(Loop* loop, std::ptrdiff_t i) : loop_(loop), i_(i) {}

public:
// required by std::iterator_traits to know how this thing works
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

probably you want to static_assert the iterator concept to ensure you got it right (and ensure it is not broken in the future)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

added some comments re. why that static_assert is a bit broken, but also added a static_assert somewhere that it's not as broken; see when i pooosh

Comment thread lib/libjazz/include/libjazz/buffer.hpp Outdated
Comment on lines +103 to +115
Sample Push(Sample s) {
zero_ = real_index_(-1);
Sample old = buffer_[zero_];
buffer_[zero_] = s;
return old;
}

Sample PushBack(Sample s) {
Sample old = buffer_[zero_];
buffer_[zero_] = s;
zero_ = real_index_(1);
return old;
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

the "returns the element we shoved out the other end" behavior of this seems Right, but is perhaps non-intuitive, and so is worth documenting right next to the method imo

Comment thread lib/libjazz/include/libjazz/buffer.hpp Outdated

constexpr std::size_t size() const { return Size; }

Sample Push(Sample s) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

should this return std::optional or something? Presumably we don't end up getting an element pushed out if the array was not yet full...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

right now, the array is always full of default-constructed Samples (soon to be Ts), so there's always something to push. i could change this but it fits our current use case so whatevs

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

...and in fact, if we want this, we could make T itself std::optional.

Could you, however, document this somewhere?

@micromho micromho requested a review from glittershark May 9, 2026 17:01
Copy link
Copy Markdown
Owner

@glittershark glittershark left a comment

Choose a reason for hiding this comment

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

wahoo

@micromho micromho force-pushed the nausicaa/ring-buffer branch from c346c83 to 47f713e Compare May 9, 2026 17:19
@micromho micromho merged commit f679988 into main May 9, 2026
1 check passed
@micromho micromho deleted the nausicaa/ring-buffer branch May 9, 2026 17:19
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