Skip to content

Add fn replace_aliased(&self, usize, bool) -> bool to BitSlice<T, O>#284

Open
regexident wants to merge 2 commits intoferrilab:mainfrom
regexident:replace_aliased
Open

Add fn replace_aliased(&self, usize, bool) -> bool to BitSlice<T, O>#284
regexident wants to merge 2 commits intoferrilab:mainfrom
regexident:replace_aliased

Conversation

@regexident
Copy link
Copy Markdown

This PR adds the following methods to BitSlice<T, O>:

  • fn replace_aliased(&self, usize, bool) -> bool
  • fn replace_aliased_unchecked(&self, usize, bool) -> bool

They are modeled after the existing fn set_aliased()/fn set_aliased_unchecked() methods on BitSlice<T, O>.

The motivation behind these methods is to be able to use BitVec<AtomicUsize> for keeping track of "visited" nodes in a parallel graph traversal without the need of locking. As such you often need to atomically add a node to the "visited" set, while also find out if it had already been visited before (e.g. for avoiding performing redundant computations).

…iased_unchecked(…) -> bool` to `BitSlice<T, O>`
@regexident
Copy link
Copy Markdown
Author

Just noticed this other PR that sort of aims to achieve the same: #273

Copy link
Copy Markdown
Contributor

@pczarn pczarn left a comment

Choose a reason for hiding this comment

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

Okay. For consistency with standard Vec we would indeed go with replace_ since for Vec you would do mem::replace, or with swap_ since for atomics you would do fn swap.

Comment thread src/slice.rs
/// Replaces the value of a single bit, using alias-safe operations and
/// without bounds checking, returning the previous value.
///
/// This is equivalent to [`.replace_unchecked()`], except that it does not
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You link to fn replace_unchecked, but you do not link to fn replace_aliased. Please do that.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi Peter, would you mind briefly elaborating on what you mean exactly, or providing a code suggestion? I couldn't find any equivalent cross linking from .replace_unchecked() to .replace() to follow suit.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be nice to have, regardless of what existing docs say:

This is for standard slice:

"For a safe alternative see get."

Screenshot_20260418_133151_Brave.jpg

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Vincent, you may update set_aliased_unchecked and replace_unchecked as well

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've updated all unsafe _unchecked methods I could find, accordingly.

Comment thread src/slice.rs
/// Replaces the value of a single bit, using alias-safe operations and
/// without bounds checking, returning the previous value.
///
/// This is equivalent to [`.replace_unchecked()`], except that it does not
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Vincent, you may update set_aliased_unchecked and replace_unchecked as well

Copy link
Copy Markdown
Contributor

@pczarn pczarn left a comment

Choose a reason for hiding this comment

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

Thanks, Vincent

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