diff options
-rw-r--r-- | src/color.rs | 12 | ||||
-rw-r--r-- | src/color/source.rs | 2 | ||||
-rw-r--r-- | src/frontier.rs | 3 | ||||
-rw-r--r-- | src/frontier/image.rs | 4 | ||||
-rw-r--r-- | src/frontier/mean.rs | 4 | ||||
-rw-r--r-- | src/frontier/min.rs | 4 | ||||
-rw-r--r-- | src/hilbert.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 13 | ||||
-rw-r--r-- | src/metric.rs | 11 | ||||
-rw-r--r-- | src/metric/forest.rs | 18 | ||||
-rw-r--r-- | src/metric/soft.rs | 10 |
11 files changed, 68 insertions, 15 deletions
diff --git a/src/color.rs b/src/color.rs index 64fd82b..5d50aa1 100644 --- a/src/color.rs +++ b/src/color.rs @@ -77,8 +77,8 @@ impl ColorSpace for RgbSpace { } len += 1; } - for i in 0..3 { - sum[i] /= len as f64; + for s in &mut sum { + *s /= len as f64; } Self(sum) } @@ -197,8 +197,8 @@ impl ColorSpace for LabSpace { } len += 1; } - for i in 0..3 { - sum[i] /= len as f64; + for s in &mut sum { + *s /= len as f64; } Self(sum) } @@ -277,8 +277,8 @@ impl ColorSpace for LuvSpace { } len += 1; } - for i in 0..3 { - sum[i] /= len as f64; + for s in &mut sum { + *s /= len as f64; } Self(sum) } diff --git a/src/color/source.rs b/src/color/source.rs index 00a6326..5cc9631 100644 --- a/src/color/source.rs +++ b/src/color/source.rs @@ -55,7 +55,7 @@ impl From<RgbImage> for ImageColors { fn from(image: RgbImage) -> Self { Self { dims: [image.width() as usize, image.height() as usize], - image: image, + image, } } } diff --git a/src/frontier.rs b/src/frontier.rs index 7143cb7..c7b7ca5 100644 --- a/src/frontier.rs +++ b/src/frontier.rs @@ -23,6 +23,9 @@ pub trait Frontier { /// The number of pixels currently on the frontier. fn len(&self) -> usize; + /// Whether the frontier is empty. + fn is_empty(&self) -> bool; + /// Place the given color on the frontier, and return its position. fn place(&mut self, rgb8: Rgb8) -> Option<(u32, u32)>; } diff --git a/src/frontier/image.rs b/src/frontier/image.rs index 3655580..5642297 100644 --- a/src/frontier/image.rs +++ b/src/frontier/image.rs @@ -51,6 +51,10 @@ impl<C: ColorSpace> Frontier for ImageFrontier<C> { self.len - self.deleted } + fn is_empty(&self) -> bool { + self.len() == 0 + } + fn place(&mut self, rgb8: Rgb8) -> Option<(u32, u32)> { let color = C::from(rgb8); diff --git a/src/frontier/mean.rs b/src/frontier/mean.rs index 889c5ba..6a32b97 100644 --- a/src/frontier/mean.rs +++ b/src/frontier/mean.rs @@ -129,6 +129,10 @@ impl<C: ColorSpace> Frontier for MeanFrontier<C> { self.len - self.deleted } + fn is_empty(&self) -> bool { + self.len() == 0 + } + fn place(&mut self, rgb8: Rgb8) -> Option<(u32, u32)> { let color = C::from(rgb8); let (x, y) = self.forest.nearest(&color).map(|n| n.item.pos)?; diff --git a/src/frontier/min.rs b/src/frontier/min.rs index b22b290..269f3b7 100644 --- a/src/frontier/min.rs +++ b/src/frontier/min.rs @@ -136,6 +136,10 @@ impl<C: ColorSpace, R: Rng> Frontier for MinFrontier<C, R> { self.len - self.deleted } + fn is_empty(&self) -> bool { + self.len() == 0 + } + fn place(&mut self, rgb8: Rgb8) -> Option<(u32, u32)> { let color = C::from(rgb8); let (x, y) = self diff --git a/src/hilbert.rs b/src/hilbert.rs index c0982d4..36464dc 100644 --- a/src/hilbert.rs +++ b/src/hilbert.rs @@ -130,7 +130,7 @@ pub fn hilbert_point(index: usize, bits: &[u32], point: &mut [usize]) { l >>= 1; } - e = e ^ rotate_right(entry_point(w), d, dims); + e ^= rotate_right(entry_point(w), d, dims); d = (d + intra_direction(w) + 1) % dims; } } diff --git a/src/main.rs b/src/main.rs index d0899be..a2f5322 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,8 +17,6 @@ use image::{self, ImageError, Rgba, RgbaImage}; use rand::{self, SeedableRng}; use rand_pcg::Pcg64; -use term; - use std::cmp; use std::error::Error; use std::fs; @@ -200,7 +198,7 @@ impl Args { let arg = args.value_of("DEPTH"); let depths: Vec<_> = arg .iter() - .map(|s| s.split(",")) + .map(|s| s.split(',')) .flatten() .map(|n| n.parse().ok()) .collect(); @@ -265,10 +263,11 @@ impl Args { let animate = args.is_present("ANIMATE"); - let mut path = args.value_of("PATH").unwrap(); - if animate && args.occurrences_of("PATH") == 0 { - path = "kd-frames"; - } + let path = if animate && args.occurrences_of("PATH") == 0 { + "kd-frames" + } else { + args.value_of("PATH").unwrap() + }; let output = PathBuf::from(path); let seed = parse_arg(args.value_of("SEED"))?.unwrap_or(0); diff --git a/src/metric.rs b/src/metric.rs index d37d2bf..d9938e4 100644 --- a/src/metric.rs +++ b/src/metric.rs @@ -373,6 +373,17 @@ impl<T> ExhaustiveSearch<T> { pub fn len(&self) -> usize { self.0.len() } + + /// Check if this index is empty. + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +impl<T> Default for ExhaustiveSearch<T> { + fn default() -> Self { + Self::new() + } } impl<T> FromIterator<T> for ExhaustiveSearch<T> { diff --git a/src/metric/forest.rs b/src/metric/forest.rs index 47eb413..887ff12 100644 --- a/src/metric/forest.rs +++ b/src/metric/forest.rs @@ -50,6 +50,24 @@ where } len } + + /// Check if this forest is empty. + pub fn is_empty(&self) -> bool { + if !self.buffer.is_empty() { + return false; + } + + self.trees.iter().flatten().next().is_none() + } +} + +impl<T, U> Default for Forest<U> +where + U: FromIterator<T> + IntoIterator<Item = T>, +{ + fn default() -> Self { + Self::new() + } } impl<T, U> Extend<T> for Forest<U> diff --git a/src/metric/soft.rs b/src/metric/soft.rs index 0d7dcdb..d443bfd 100644 --- a/src/metric/soft.rs +++ b/src/metric/soft.rs @@ -82,6 +82,16 @@ where } } +impl<T, U> Default for SoftSearch<U> +where + T: SoftDelete, + U: FromIterator<T> + IntoIterator<Item = T>, +{ + fn default() -> Self { + Self::new() + } +} + impl<T, U: Extend<T>> Extend<T> for SoftSearch<U> { fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { self.0.extend(iter); |