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

In sixth, split_before / split_after may return illegal empty linked list in some corner cases. #238

Open
makisevon opened this issue May 1, 2022 · 2 comments · May be fixed by #264
Open

Comments

@makisevon
Copy link

For example:

    pub fn split_before(&mut self) -> LinkedList<T> {
        // We have this:
        //
        //     list.front -> A <-> B <- list.back
        //                   ^
        //                  cur
        //
        //
        // And we want to produce this:
        //
        //     list.front -> A <-> B <- list.back
        //                   ^
        //                  cur
        //
        //
        //     return.front -> None <- return.back
        //
        if let Some(cur) = self.cur {
            unsafe {
                let old_len = self.list.len;        // 2
                let old_idx = self.index.unwrap();  // 0
                let prev = (*cur.as_ptr()).front;   // None

                let new_len = old_len - old_idx;    // 2
                let new_front = self.cur;
                let new_back = self.list.back;
                let new_idx = Some(0);

                let output_len = old_len - new_len; // 0
                // oops...
                let output_front = self.list.front; // -> A
                let output_back = prev;             // None

                if let Some(prev) = prev {
                    (*cur.as_ptr()).front = None;
                    (*prev.as_ptr()).back = None;
                }

                self.list.len = new_len;
                self.list.front = new_front;
                self.list.back = new_back;
                self.index = new_idx;

                LinkedList {
                    front: output_front,            // -> A
                    back: output_back,              // None
                    len: output_len,                // 0
                    _boo: PhantomData,
                }
            }
        } else {
            std::mem::replace(self.list, LinkedList::new())
        }
    }
@morlinbrot
Copy link

I just found this now where I just opened a PR for this exact issue in the contain-rs/linked-list project.

It is actually quite easily fixed, I'd be happy to open a PR in this project, too. I'm just not sure how exactly the structure of this project works, e.g. how can I run the code part of src/sixth-final.rs? Sixth is also not implemented as a "proper" list in the lists folder, is there a specific reason for that?

I'd be happy to open a PR adding a sixth.rs with the full implementation and all the tests.

@makisevon
Copy link
Author

I just found this now where I just opened a PR for this exact issue in the contain-rs/linked-list project.

It is actually quite easily fixed, I'd be happy to open a PR in this project, too. I'm just not sure how exactly the structure of this project works, e.g. how can I run the code part of src/sixth-final.rs? Sixth is also not implemented as a "proper" list in the lists folder, is there a specific reason for that?

I'd be happy to open a PR adding a sixth.rs with the full implementation and all the tests.

Just copy the final code in the src/sixth-final.md, and I think it's equivalent to the lists/src/sixth.rs.

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 a pull request may close this issue.

2 participants