forked from URI-ABD/clam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: added instrumentation for counting the numbers of distance compu…
…tations
- Loading branch information
Showing
18 changed files
with
635 additions
and
345 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! The Cosine distance function. | ||
use std::sync::{Arc, RwLock}; | ||
|
||
use abd_clam::{metric::ParMetric, Metric}; | ||
use distances::number::Float; | ||
|
||
use super::{CountingMetric, ParCountingMetric}; | ||
|
||
/// The Cosine distance function. | ||
pub struct Cosine(Arc<RwLock<usize>>, bool); | ||
|
||
impl Cosine { | ||
/// Creates a new `Euclidean` distance metric. | ||
pub fn new() -> Self { | ||
Self(Arc::new(RwLock::new(0)), false) | ||
} | ||
} | ||
|
||
impl<I: AsRef<[T]>, T: Float> Metric<I, T> for Cosine { | ||
fn distance(&self, a: &I, b: &I) -> T { | ||
if self.1 { | ||
<Self as CountingMetric<I, T>>::increment(self); | ||
} | ||
distances::vectors::cosine(a.as_ref(), b.as_ref()) | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"cosine" | ||
} | ||
|
||
fn has_identity(&self) -> bool { | ||
true | ||
} | ||
|
||
fn has_non_negativity(&self) -> bool { | ||
true | ||
} | ||
|
||
fn has_symmetry(&self) -> bool { | ||
true | ||
} | ||
|
||
fn obeys_triangle_inequality(&self) -> bool { | ||
true | ||
} | ||
|
||
fn is_expensive(&self) -> bool { | ||
false | ||
} | ||
} | ||
|
||
impl<I: AsRef<[U]>, U: Float> CountingMetric<I, U> for Cosine { | ||
fn disable_counting(&mut self) { | ||
self.1 = false; | ||
} | ||
|
||
fn enable_counting(&mut self) { | ||
self.1 = true; | ||
} | ||
|
||
#[allow(clippy::unwrap_used)] | ||
fn count(&self) -> usize { | ||
*self.0.read().unwrap() | ||
} | ||
|
||
#[allow(clippy::unwrap_used)] | ||
fn reset_count(&self) -> usize { | ||
let mut count = self.0.write().unwrap(); | ||
let old = *count; | ||
*count = 0; | ||
old | ||
} | ||
|
||
#[allow(clippy::unwrap_used)] | ||
fn increment(&self) { | ||
*self.0.write().unwrap() += 1; | ||
} | ||
} | ||
|
||
impl<I: AsRef<[U]> + Send + Sync, U: Float> ParMetric<I, U> for Cosine {} | ||
|
||
impl<I: AsRef<[U]> + Send + Sync, U: Float> ParCountingMetric<I, U> for Cosine {} |
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,83 @@ | ||
//! The `Euclidean` distance metric. | ||
use std::sync::{Arc, RwLock}; | ||
|
||
use abd_clam::{metric::ParMetric, Metric}; | ||
use distances::number::Float; | ||
|
||
use super::{CountingMetric, ParCountingMetric}; | ||
|
||
/// The `Euclidean` distance metric. | ||
pub struct Euclidean(Arc<RwLock<usize>>, bool); | ||
|
||
impl Euclidean { | ||
/// Creates a new `Euclidean` distance metric. | ||
pub fn new() -> Self { | ||
Self(Arc::new(RwLock::new(0)), true) | ||
} | ||
} | ||
|
||
impl<I: AsRef<[T]>, T: Float> Metric<I, T> for Euclidean { | ||
fn distance(&self, a: &I, b: &I) -> T { | ||
if self.1 { | ||
<Self as CountingMetric<I, T>>::increment(self); | ||
} | ||
distances::vectors::euclidean(a.as_ref(), b.as_ref()) | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"euclidean" | ||
} | ||
|
||
fn has_identity(&self) -> bool { | ||
true | ||
} | ||
|
||
fn has_non_negativity(&self) -> bool { | ||
true | ||
} | ||
|
||
fn has_symmetry(&self) -> bool { | ||
true | ||
} | ||
|
||
fn obeys_triangle_inequality(&self) -> bool { | ||
true | ||
} | ||
|
||
fn is_expensive(&self) -> bool { | ||
false | ||
} | ||
} | ||
|
||
impl<I: AsRef<[U]>, U: Float> CountingMetric<I, U> for Euclidean { | ||
fn disable_counting(&mut self) { | ||
self.1 = false; | ||
} | ||
|
||
fn enable_counting(&mut self) { | ||
self.1 = true; | ||
} | ||
|
||
#[allow(clippy::unwrap_used)] | ||
fn count(&self) -> usize { | ||
*self.0.read().unwrap() | ||
} | ||
|
||
#[allow(clippy::unwrap_used)] | ||
fn reset_count(&self) -> usize { | ||
let mut count = self.0.write().unwrap(); | ||
let old = *count; | ||
*count = 0; | ||
old | ||
} | ||
|
||
#[allow(clippy::unwrap_used)] | ||
fn increment(&self) { | ||
*self.0.write().unwrap() += 1; | ||
} | ||
} | ||
|
||
impl<I: AsRef<[U]> + Send + Sync, U: Float> ParMetric<I, U> for Euclidean {} | ||
|
||
impl<I: AsRef<[U]> + Send + Sync, U: Float> ParCountingMetric<I, U> for Euclidean {} |
Oops, something went wrong.