Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sparta::Buffer Enhancements #527

Merged
merged 6 commits into from
Oct 1, 2024
Merged

Conversation

bdutro
Copy link
Contributor

@bdutro bdutro commented Sep 26, 2024

Makes sparta::Buffer::erase() and makes sparta::Buffer::reverse_iterator behave more like their STL counterparts.

Note that there is a quirk in the behavior of reverse_iterator:

for(int i = 0; i < 10; ++i)
{
    buf.push_back(i);
}
auto it = buf.rbegin();
std::cout << *it; // Prints out 9
buf.pop_back();
std::cout << *it; // Prints out 8

Because sparta::Buffer::end() is a constant and sparta::Buffer::rbegin() points to end(), it will always be valid after an erase or pop_back as long as the buffer is not empty. This makes using pop_back while looping over reverse iterators unintuitive. However, the following pattern works as expected:

auto it = buf.rbegin();
while(it != buf.rend())
{
    it = buf.erase(it);
}

Fixes #464

klingaard and others added 6 commits December 23, 2023 19:20
reverse_iterator
    - rbegin() iterator now correctly dereferences to the entry
      before end()
    - Minor quirk: because end() is a constant, rbegin() will always be
      valid after an erase() or pop_back() as long as the Buffer is not
      empty
Copy link
Member

@klingaard klingaard left a comment

Choose a reason for hiding this comment

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

Awesome testing. Thank you!

@bdutro bdutro merged commit 1477609 into master Oct 1, 2024
8 checks passed
@bdutro bdutro deleted the knutel/issue_464_resource_enhancements branch October 1, 2024 20:54
github-actions bot pushed a commit that referenced this pull request Oct 1, 2024
* First cut of Buffer::erase returning next itr

* Fixed bugs in tester for new erase

* Buffer::erase now returns a non-const iterator; trying to get reverse working

* Make sparta::Buffer::reverse_iterator behave more like an STL
reverse_iterator
    - rbegin() iterator now correctly dereferences to the entry
      before end()
    - Minor quirk: because end() is a constant, rbegin() will always be
      valid after an erase() or pop_back() as long as the Buffer is not
      empty

---------

Co-authored-by: Knute Lingaard <[email protected]> 1477609
github-actions bot pushed a commit to x-tinkerer/map that referenced this pull request Oct 24, 2024
* First cut of Buffer::erase returning next itr

* Fixed bugs in tester for new erase

* Buffer::erase now returns a non-const iterator; trying to get reverse working

* Make sparta::Buffer::reverse_iterator behave more like an STL
reverse_iterator
    - rbegin() iterator now correctly dereferences to the entry
      before end()
    - Minor quirk: because end() is a constant, rbegin() will always be
      valid after an erase() or pop_back() as long as the Buffer is not
      empty

---------

Co-authored-by: Knute Lingaard <[email protected]> 1477609
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.

Sparta::Buffer::erase(const iter&) should return updated iter reference
2 participants