Skip to content

Add rich comparison for dict_iterator#2203

Merged
youknowone merged 1 commit into
RustPython:masterfrom
clemado1:dict_cmp
Sep 13, 2020
Merged

Add rich comparison for dict_iterator#2203
youknowone merged 1 commit into
RustPython:masterfrom
clemado1:dict_cmp

Conversation

@clemado1

Copy link
Copy Markdown
Contributor

Modified inner_eq to inner_cmp so that dict_iterator can use and added rich comparison for dict_iterator.

Comment thread vm/src/obj/objdict.rs Outdated
if size_func(zelf.len(), other.len()) {
return Ok(false);
}
let other_cp = other.copy();

@youknowone youknowone Sep 12, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess this is not nessessary if we fix original code a bit to iterate smaller dict for instead of zelf.
Then remained size of the other side of looped one will tell it is bigger than smaller one or not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your advice. I revised it to iterate a smaller one.

Modified inner_eq to inner_cmp so that dict_iterator can use and added rich comparison for dict_iterator.
Comment thread vm/src/obj/objdict.rs
item: bool,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
if size_func(zelf.len(), other.len()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For eq and ne, this test leads the loop comparison.
But for other comparisons like lt, gt, the size comparison doesn't guarantee subset.

Assume a = {'a': 1, 'b' 2}; b = {'a': 1, 'b': 3, 'c': 4},
a.items() > b.items() is false but also b.items() > a.items() is false.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your advice! but don't the cpython also return false and false? I put it at first because it seems cpython check a length at first. link Should I change it as you said?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, I am sorry. I thought something totally wrong. Yes, it seems current behaviour is exactly as expected and also same as also what I described. Sorry for making confusion!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No problem! I really want to thank you for your help.

@youknowone youknowone merged commit b569ccf into RustPython:master Sep 13, 2020
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