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

Implements move_to #83

Merged
merged 3 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/y_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,27 @@ impl YArray {
}
}

/// Moves the element from the index source to target.
pub fn move_to(&mut self, txn: &mut YTransaction, source: u32, target: u32) -> PyResult<()> {
match &mut self.0 {
SharedType::Integrated(v) => Ok(v.move_to(txn, source, target)),
SharedType::Prelim(v) if source < v.len() as u32 && target < v.len() as u32 => {
if source < 0 as u32 || target < 0 as u32 {
Err::<PyIndexError, ()>(());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Err::<PyIndexError, ()>(());
Err(PyIndexError::default_message())

Including a default message or specifically referencing whether source or target is out of bounds would be helpful.

}
if source < target {
let el = v.remove(source as usize);
v.insert((target-1) as usize, el);
} else if source > target {
let el = v.remove(source as usize);
v.insert(target as usize, el);
}
Ok(())
}
_ => Err(PyIndexError::default_message()),
}
}

pub fn __getitem__(&self, index: Index) -> PyResult<PyObject> {
// Apply index to the Array type
match index {
Expand Down
9 changes: 9 additions & 0 deletions y_py.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,15 @@ class YArray:
Deletes a range of items of given `length` from current `YArray` instance,
starting from given `index`.
"""
def move_to(self, txn: YTransaction, source: int, target: int):
"""
Moves a single item found at `source` index into `target` index position

Args:
txn: The transaction where the array is being modified.
source: The index of the element to be moved.
target: The new position of the element.
"""
def __getitem__(self, index: Union[int, slice]) -> Any:
"""
Returns:
Expand Down