-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add SoAVec::swap_remove
#36
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1002,6 +1002,55 @@ impl<T: SoAble> SoAVec<T> { | |
unsafe { T::TupleRepr::drop_in_place(T::TupleRepr::get_pointers(ptr, 0, cap), len) }; | ||
} | ||
} | ||
|
||
/// Removes an element from the vector and returns it. | ||
/// | ||
/// The removed element is replaced by the last element of the vector. | ||
/// | ||
/// This does not preserve ordering of the remaining elements, but is *O*(n_fields). | ||
/// If you need to preserve the element order, use [`remove`] instead. | ||
/// | ||
/// [`remove`]: SoAVec::remove | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use soavec::soavec; | ||
/// | ||
/// let mut v = soavec![("foo", "foo"), ("bar", "bar"), ("baz", "baz"), ("qux", "qux")].unwrap(); | ||
/// | ||
/// assert_eq!(v.swap_remove(1).unwrap(), ("bar", "bar")); | ||
/// assert_eq!(v, soavec![("foo", "foo"), ("qux", "qux"), ("baz", "baz")].unwrap()); | ||
/// | ||
/// assert_eq!(v.swap_remove(0).unwrap(), ("foo", "foo")); | ||
/// assert_eq!(v, soavec![("baz", "baz"), ("qux", "qux")].unwrap()); | ||
/// ``` | ||
pub fn swap_remove(&mut self, index: u32) -> Result<T, InsertError> { | ||
#[cold] | ||
fn assert_index() -> IndexOutOfBoundsError { | ||
IndexOutOfBoundsError | ||
} | ||
|
||
let len = self.len(); | ||
if index > len { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: This should be |
||
return Err(assert_index().into()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: I was going to suggest using eg. gdb or equivalent to check out the assembly generated here but I went and checked it out using Compiler Explorer: turns out we don't need the IndexOutOfBoundsError check here at all; the compiler does understand that returning So, in that case it's not necessary to add the |
||
} | ||
|
||
let ptr = self.buf.as_mut_ptr(); | ||
let cap = self.capacity(); | ||
|
||
unsafe { | ||
let value = T::from_tuple(T::TupleRepr::read(ptr, index, cap)); | ||
|
||
let src = T::TupleRepr::get_pointers(ptr, len - 1, cap); | ||
let dst = T::TupleRepr::get_pointers(ptr, index, cap); | ||
T::TupleRepr::copy(src, dst, 1); | ||
|
||
self.buf.set_len(len - 1); | ||
|
||
Ok(value) | ||
} | ||
} | ||
} | ||
|
||
impl<T: SoAble> Drop for SoAVec<T> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: I don't think InsertError is a good choice here since this can never allocate and thus doesn't need those errors. Let's just use
IndexOutOfBoundsError
directly.