Skip to content

Commit

Permalink
implemented __hash__ functions and improved __richcmp__
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Aug 9, 2023
1 parent c559ab6 commit 3ca2f32
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
16 changes: 11 additions & 5 deletions src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,21 @@ impl PyAnnotation {
self.map(|annotation| Ok(annotation.id() == Some(other)))
}

fn __richcmp__(&self, other: PyRef<Self>, op: CompareOp) -> Py<PyAny> {
let py = other.py();
fn __richcmp__(&self, other: PyRef<Self>, op: CompareOp) -> bool {
match op {
CompareOp::Eq => (self.handle == other.handle).into_py(py),
CompareOp::Ne => (self.handle != other.handle).into_py(py),
_ => py.NotImplemented(),
CompareOp::Eq => self.handle == other.handle,
CompareOp::Ne => self.handle != other.handle,
CompareOp::Lt => self.handle < other.handle,
CompareOp::Gt => self.handle > other.handle,
CompareOp::Le => self.handle <= other.handle,
CompareOp::Ge => self.handle >= other.handle,
}
}

fn __hash__(&self) -> usize {
self.handle.as_usize()
}

/// Returns a generator over all data in this annotation
fn __iter__(&self) -> PyResult<PyDataIter> {
Ok(PyDataIter {
Expand Down
23 changes: 19 additions & 4 deletions src/annotationdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use pyo3::pyclass::CompareOp;
use pyo3::types::*;
use std::hash::{Hash, Hasher};
use std::ops::FnOnce;
use std::sync::{Arc, RwLock};

Expand Down Expand Up @@ -66,12 +67,19 @@ impl PyDataKey {
fn __richcmp__(&self, other: PyRef<Self>, op: CompareOp) -> Py<PyAny> {
let py = other.py();
match op {
CompareOp::Eq => (self.handle == other.handle).into_py(py),
CompareOp::Ne => (self.handle != other.handle).into_py(py),
CompareOp::Eq => (self.set == other.set && self.handle == other.handle).into_py(py),
CompareOp::Ne => (self.set != other.set || self.handle != other.handle).into_py(py),
_ => py.NotImplemented(),
}
}

fn __hash__(&self) -> u64 {
let mut hasher = std::collections::hash_map::DefaultHasher::new();
let h = (self.set.as_usize(), self.handle.as_usize());
h.hash(&mut hasher);
hasher.finish()
}

/// Returns the AnnotationDataSet this key is part of
fn dataset(&self) -> PyResult<PyAnnotationDataSet> {
Ok(PyAnnotationDataSet {
Expand Down Expand Up @@ -424,12 +432,19 @@ impl PyAnnotationData {
fn __richcmp__(&self, other: PyRef<Self>, op: CompareOp) -> Py<PyAny> {
let py = other.py();
match op {
CompareOp::Eq => (self.handle == other.handle).into_py(py),
CompareOp::Ne => (self.handle != other.handle).into_py(py),
CompareOp::Eq => (self.set == other.set && self.handle == other.handle).into_py(py),
CompareOp::Ne => (self.set != other.set || self.handle != other.handle).into_py(py),
_ => py.NotImplemented(),
}
}

fn __hash__(&self) -> u64 {
let mut hasher = std::collections::hash_map::DefaultHasher::new();
let h = (self.set.as_usize(), self.handle.as_usize());
h.hash(&mut hasher);
hasher.finish()
}

/// Returns the AnnotationDataSet this data is part of
fn annotationset(&self) -> PyResult<PyAnnotationDataSet> {
Ok(PyAnnotationDataSet::new(self.set, &self.store))
Expand Down
16 changes: 11 additions & 5 deletions src/annotationdataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ impl PyAnnotationDataSet {
self.map(|annotationset| Ok(annotationset.id() == Some(other)))
}

fn __richcmp__(&self, other: PyRef<Self>, op: CompareOp) -> Py<PyAny> {
let py = other.py();
fn __richcmp__(&self, other: PyRef<Self>, op: CompareOp) -> bool {
match op {
CompareOp::Eq => (self.handle == other.handle).into_py(py),
CompareOp::Ne => (self.handle != other.handle).into_py(py),
_ => py.NotImplemented(),
CompareOp::Eq => self.handle == other.handle,
CompareOp::Ne => self.handle != other.handle,
CompareOp::Lt => self.handle < other.handle,
CompareOp::Gt => self.handle > other.handle,
CompareOp::Le => self.handle <= other.handle,
CompareOp::Ge => self.handle >= other.handle,
}
}

fn __hash__(&self) -> usize {
self.handle.as_usize()
}

/// Save the annotation dataset to a STAM JSON file
fn to_json_file(&self, filename: &str) -> PyResult<()> {
self.map(|annotationset| {
Expand Down
16 changes: 11 additions & 5 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ impl PyTextResource {
self.map(|res| Ok(res.id() == Some(other)))
}

fn __richcmp__(&self, other: PyRef<Self>, op: CompareOp) -> Py<PyAny> {
let py = other.py();
fn __richcmp__(&self, other: PyRef<Self>, op: CompareOp) -> bool {
match op {
CompareOp::Eq => (self.handle == other.handle).into_py(py),
CompareOp::Ne => (self.handle != other.handle).into_py(py),
_ => py.NotImplemented(),
CompareOp::Eq => self.handle == other.handle,
CompareOp::Ne => self.handle != other.handle,
CompareOp::Lt => self.handle < other.handle,
CompareOp::Gt => self.handle > other.handle,
CompareOp::Le => self.handle <= other.handle,
CompareOp::Ge => self.handle >= other.handle,
}
}

fn __hash__(&self) -> usize {
self.handle.as_usize()
}

/// Returns the full text of the resource (by value, aka a copy)
fn __str__<'py>(&self, py: Python<'py>) -> PyResult<&'py PyString> {
self.text(py)
Expand Down
8 changes: 8 additions & 0 deletions src/textselection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::pyclass::CompareOp;
use pyo3::types::*;
use std::hash::{Hash, Hasher};
use std::ops::FnOnce;
use std::sync::{Arc, RwLock};

Expand Down Expand Up @@ -108,6 +109,13 @@ impl PyTextSelection {
}
}

fn __hash__(&self) -> u64 {
let mut hasher = std::collections::hash_map::DefaultHasher::new();
let h = (self.resource_handle.as_usize(), self.textselection);
h.hash(&mut hasher);
hasher.finish()
}

/// Returns the resource this textselections points at
fn resource(&self) -> PyResult<PyTextResource> {
Ok(PyTextResource {
Expand Down

0 comments on commit 3ca2f32

Please sign in to comment.