-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect hash-equivalent literals in iteration-over-set
- Loading branch information
1 parent
443fd3b
commit 44402e2
Showing
5 changed files
with
84 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::hash::Hash; | ||
|
||
use crate::{Expr, Number}; | ||
|
||
use crate::comparable::{ComparableExpr, ExprBoolLiteral}; | ||
|
||
/// Wrapper around [`Expr`] that implements [`Hash`] and [`PartialEq`] according to Python | ||
/// semantics: | ||
/// | ||
/// > Values that compare equal (such as 1, 1.0, and True) can be used interchangeably to index the | ||
/// > same dictionary entry. | ||
/// | ||
/// For example, considers `True`, `1`, and `1.0` to be equal, as they hash to the same value | ||
/// in Python, along with `False`, `0`, and `0.0`. | ||
/// | ||
/// See: <https://docs.python.org/3/library/stdtypes.html#mapping-types-dict> | ||
pub struct HashComparableExpr<'a>(ComparableExpr<'a>); | ||
|
||
impl Hash for HashComparableExpr<'_> { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
self.0.hash(state); | ||
} | ||
} | ||
|
||
impl PartialEq<Self> for HashComparableExpr<'_> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.0 == other.0 | ||
} | ||
} | ||
|
||
impl Eq for HashComparableExpr<'_> {} | ||
|
||
impl<'a> From<&'a Expr> for HashComparableExpr<'a> { | ||
fn from(expr: &'a Expr) -> Self { | ||
as_bool(expr) | ||
.map(ExprBoolLiteral::from) | ||
.map(ComparableExpr::BoolLiteral) | ||
.map(Self) | ||
.unwrap_or_else(|| Self(ComparableExpr::from(expr))) | ||
} | ||
} | ||
|
||
/// Returns the `bool` value of the given expression, if it has an equivalent hash to `True` or `False`. | ||
fn as_bool(expr: &Expr) -> Option<bool> { | ||
let Expr::NumberLiteral(expr) = expr else { | ||
return None; | ||
}; | ||
match &expr.value { | ||
Number::Int(int) => match int.as_u8() { | ||
Some(0) => Some(false), | ||
Some(1) => Some(true), | ||
_ => None, | ||
}, | ||
Number::Float(float) => match float { | ||
0.0 => Some(false), | ||
1.0 => Some(true), | ||
_ => None, | ||
}, | ||
Number::Complex { real, imag } => match (real, imag) { | ||
(0.0, 0.0) => Some(false), | ||
(1.0, 0.0) => Some(true), | ||
_ => None, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters