Skip to content

Commit

Permalink
clean up comparison ops on python edge
Browse files Browse the repository at this point in the history
  • Loading branch information
ljeub-pometry committed Dec 13, 2024
1 parent 2a8148b commit 2e9d831
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
46 changes: 45 additions & 1 deletion raphtory/src/db/graph/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ use crate::{
};
use raphtory_api::core::storage::arc_str::ArcStr;
use std::{
cmp::Ordering,
fmt::{Debug, Formatter},
hash::{Hash, Hasher},
sync::Arc,
};

Expand Down Expand Up @@ -132,7 +134,49 @@ impl<
> PartialEq<EdgeView<G2, GH2>> for EdgeView<G1, GH1>
{
fn eq(&self, other: &EdgeView<G2, GH2>) -> bool {
self.id() == other.id()
self.id() == other.id() && self.edge.time() == other.edge.time()
}
}

impl<'graph_1, 'graph_2, G1: GraphViewOps<'graph_1>, GH1: GraphViewOps<'graph_1>> Eq
for EdgeView<G1, GH1>
{
}

impl<
'graph_1,
'graph_2,
G1: GraphViewOps<'graph_1>,
GH1: GraphViewOps<'graph_1>,
G2: GraphViewOps<'graph_2>,
GH2: GraphViewOps<'graph_2>,
> PartialOrd<EdgeView<G2, GH2>> for EdgeView<G1, GH1>
{
fn partial_cmp(&self, other: &EdgeView<G2, GH2>) -> Option<Ordering> {
Some(
self.id()
.cmp(&other.id())
.then(self.edge.time().cmp(&other.edge.time())),
)
}
}

impl<'graph_1, 'graph_2, G1: GraphViewOps<'graph_1>, GH1: GraphViewOps<'graph_1>> Ord
for EdgeView<G1, GH1>
{
fn cmp(&self, other: &EdgeView<G1, GH1>) -> Ordering {
self.id()
.cmp(&other.id())
.then(self.edge.time().cmp(&other.edge.time()))
}
}

impl<'graph_1, 'graph_2, G1: GraphViewOps<'graph_1>, GH1: GraphViewOps<'graph_1>> Hash
for EdgeView<G1, GH1>
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.id().hash(state);
self.edge.time().hash(state);
}
}

Expand Down
14 changes: 7 additions & 7 deletions raphtory/src/python/graph/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,27 @@ impl PyEdge {
// }

fn __eq__(&self, other: Bound<PyEdge>) -> bool {
self.edge.id() == other.get().edge.id()
self.edge == other.get().edge
}

fn __ne__(&self, other: Bound<PyEdge>) -> bool {
self.edge.id() != other.get().edge.id()
self.edge != other.get().edge
}

fn __lt__(&self, other: Bound<PyEdge>) -> bool {
self.edge.id() < other.get().edge.id()
self.edge < other.get().edge
}

fn __le__(&self, other: Bound<PyEdge>) -> bool {
self.edge.id() <= other.get().edge.id()
self.edge <= other.get().edge
}

fn __gt__(&self, other: Bound<PyEdge>) -> bool {
self.edge.id() > other.get().edge.id()
self.edge > other.get().edge
}

fn __ge__(&self, other: Bound<PyEdge>) -> bool {
self.edge.id() >= other.get().edge.id()
self.edge >= other.get().edge
}

/// Returns the hash of the edge and edge properties.
Expand All @@ -193,7 +193,7 @@ impl PyEdge {
/// int: A hash of the edge.
pub fn __hash__(&self) -> u64 {
let mut s = DefaultHasher::new();
self.edge.id().hash(&mut s);
self.edge.hash(&mut s);
s.finish()
}

Expand Down
6 changes: 6 additions & 0 deletions raphtory/src/python/types/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ pub trait Repr {
fn repr(&self) -> String;
}

impl<T: Repr, const N: usize> Repr for [T; N] {
fn repr(&self) -> String {
self.as_slice().repr()
}
}

impl Repr for PyObject {
fn repr(&self) -> String {
Python::with_gil(|py| Repr::repr(self.bind(py)))
Expand Down

0 comments on commit 2e9d831

Please sign in to comment.