Implement Hash for SpaceObject

This commit is contained in:
2019-10-08 10:46:31 +02:00
parent ab718bf491
commit 8fcf1b74e7
4 changed files with 36 additions and 18 deletions

View File

@@ -14,7 +14,7 @@ pub struct CoreQueryParameters<'a> {
pub resolution: Option<Vec<u64>>, pub resolution: Option<Vec<u64>>,
} }
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum Properties { pub enum Properties {
Feature(String), Feature(String),
Unknown(String, String), Unknown(String, String),
@@ -50,13 +50,15 @@ impl Properties {
} }
} }
// FIXME: Which is faster, the code below or the automatically generated
// implementation?
/*
impl PartialEq for Properties { impl PartialEq for Properties {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.id() == other.id() && self.type_name() == other.type_name() self.id() == other.id() && self.type_name() == other.type_name()
} }
} }
*/
impl Eq for Properties {}
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Core { pub struct Core {

View File

@@ -5,8 +5,6 @@ mod space_index;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::hash::Hash;
use std::hash::Hasher;
use ironsea_index::Indexed; use ironsea_index::Indexed;
use ironsea_table_vector::VectorTable; use ironsea_table_vector::VectorTable;
@@ -57,13 +55,16 @@ impl PartialEq for SpaceId {
} }
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize)]
pub struct SpaceObject { pub struct SpaceObject {
pub space_id: String, pub space_id: String,
pub position: Position, pub position: Position,
pub value: Properties, pub value: Properties,
} }
// FIXME: Which is faster, the code below or the automatically generated
// implementation?
/*
impl PartialEq for SpaceObject { impl PartialEq for SpaceObject {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.space_id == other.space_id self.space_id == other.space_id
@@ -71,15 +72,7 @@ impl PartialEq for SpaceObject {
&& self.position == other.position && self.position == other.position
} }
} }
*/
impl Eq for SpaceObject {}
impl Hash for SpaceObject {
//FIXME: ADD HASHING IMPLEMENTATION, REQUIRED FOR distinct()!
fn hash<H: Hasher>(&self, state: &mut H) {
unimplemented!()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DataBase { pub struct DataBase {

View File

@@ -2,6 +2,8 @@ use std::cmp::Ordering;
use std::fmt; use std::fmt;
use std::fmt::Display; use std::fmt::Display;
use std::fmt::Formatter; use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::ops::Add; use std::ops::Add;
use std::ops::Mul; use std::ops::Mul;
use std::ops::Sub; use std::ops::Sub;
@@ -255,3 +257,22 @@ impl PartialEq for Coordinate {
self.u64() == other.u64() self.u64() == other.u64()
} }
} }
impl Hash for Coordinate {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Coordinate::CoordinateU8(v) => v.hash(state),
Coordinate::CoordinateU16(v) => v.hash(state),
Coordinate::CoordinateU32(v) => v.hash(state),
Coordinate::CoordinateU64(v) => v.hash(state),
// FIXME: Ugly workaround... 16 decimal position is enough to
// represent any mantissa of 2^53 bits.
Coordinate::CoordinateF64(v) => format!("{:.*}", 16, v).hash(state),
}
}
/*
fn hash_slice<H: Hasher>(data: &[Self], state: &mut H) where Self: Sized {
unimplemented!()
}*/
}

View File

@@ -13,7 +13,7 @@ use std::ops::SubAssign;
use super::coordinate::Coordinate; use super::coordinate::Coordinate;
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum Position { pub enum Position {
Position1(Coordinate), Position1(Coordinate),
Position2([Coordinate; 2]), Position2([Coordinate; 2]),
@@ -204,6 +204,9 @@ impl Mul for Position {
} }
} }
// FIXME: Which is faster, the code below or the automatically generated
// implementation?
/*
impl PartialEq for Position { impl PartialEq for Position {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
for i in 0..self.dimensions() { for i in 0..self.dimensions() {
@@ -214,8 +217,7 @@ impl PartialEq for Position {
true true
} }
} }
*/
impl Eq for Position {}
impl<'s> From<&'s Position> for Vec<&'s Coordinate> { impl<'s> From<&'s Position> for Vec<&'s Coordinate> {
fn from(position: &'s Position) -> Self { fn from(position: &'s Position) -> Self {