use std::any::Any;
use std::sync::Arc;
use arrow_array::UInt32Array;
use async_trait::async_trait;
use ordered_float::OrderedFloat;
pub mod builder;
pub mod persisted;
use crate::Result;
pub use persisted::*;
#[async_trait]
pub trait Graph {
async fn distance(&self, a: usize, b: usize) -> Result<f32>;
async fn distance_to(&self, query: &[f32], idx: usize) -> Result<f32>;
async fn neighbors(&self, id: usize) -> Result<Arc<UInt32Array>>;
}
pub trait Vertex: Clone {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn vector(&self) -> &[f32];
}
pub trait VertexSerDe<V: Vertex> {
fn size(&self) -> usize;
fn serialize(&self, vertex: &V) -> Vec<u8>;
fn deserialize(&self, data: &[u8]) -> Result<V>;
}
pub struct VertexWithDistance {
pub id: usize,
pub distance: OrderedFloat<f32>,
}
impl VertexWithDistance {
pub fn new(id: usize, distance: f32) -> Self {
Self {
id,
distance: OrderedFloat(distance),
}
}
}
impl PartialEq for VertexWithDistance {
fn eq(&self, other: &Self) -> bool {
self.distance.eq(&other.distance)
}
}
impl Eq for VertexWithDistance {}
impl PartialOrd for VertexWithDistance {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.distance.partial_cmp(&other.distance)
}
}
impl Ord for VertexWithDistance {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.distance.cmp(&other.distance)
}
}