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

DoubleEndedIterator implementation wrong #316

Open
Xenira opened this issue May 25, 2024 · 0 comments · May be fixed by #351
Open

DoubleEndedIterator implementation wrong #316

Xenira opened this issue May 25, 2024 · 0 comments · May be fixed by #351
Assignees
Labels
bug Something isn't working

Comments

@Xenira
Copy link
Collaborator

Xenira commented May 25, 2024

It seems the double ended iterator is not behaving as expected.

It is important to note that both back and forth work on the same range, and do not cross: iteration is over when they meet in the middle.
https://doc.rust-lang.org/std/iter/trait.DoubleEndedIterator.html

There should be two pointers one pointing to the position at the beginning and one at the end.

The code moves the one pointer instead. Next back should move the later and keep the pointer used in next untouched.

impl<'a> DoubleEndedIterator for Iter<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
let key_type = unsafe {
zend_hash_get_current_key_type_ex(
self.ht as *const ZendHashTable as *mut ZendHashTable,
&mut self.pos as *mut HashPosition,
)
};
if key_type == -1 {
return None;
}
let key = Zval::new();
unsafe {
zend_hash_get_current_key_zval_ex(
self.ht as *const ZendHashTable as *mut ZendHashTable,
&key as *const Zval as *mut Zval,
&mut self.pos as *mut HashPosition,
);
}
let value = unsafe {
&*zend_hash_get_current_data_ex(
self.ht as *const ZendHashTable as *mut ZendHashTable,
&mut self.pos as *mut HashPosition,
)
};
let key = match ArrayKey::from_zval(&key) {
Some(key) => key,
None => ArrayKey::Long(self.current_num),
};
unsafe {
zend_hash_move_backwards_ex(
self.ht as *const ZendHashTable as *mut ZendHashTable,
&mut self.pos as *mut HashPosition,
)
};
self.current_num -= 1;

If i have [1, 2, 3, 4, 5] - next() should return 1 and next_back() 5

@Xenira Xenira added the bug Something isn't working label Jan 7, 2025
@Xenira Xenira self-assigned this Jan 8, 2025
@Xenira Xenira linked a pull request Jan 9, 2025 that will close this issue
@Xenira Xenira linked a pull request Jan 9, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant