| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::ops::Range; |
| 5 |
|
| 6 |
use crate::coord::ranged1d::{AsRangedCoord, DiscreteRanged, KeyPointHint, Ranged}; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
pub struct WithKeyPoints<Inner: Ranged> { |
| 17 |
inner: Inner, |
| 18 |
bold_points: Vec<Inner::ValueType>, |
| 19 |
light_points: Vec<Inner::ValueType>, |
| 20 |
} |
| 21 |
|
| 22 |
impl<I: Ranged> WithKeyPoints<I> { |
| 23 |
|
| 24 |
pub fn with_light_points<T: IntoIterator<Item = I::ValueType>>(mut self, iter: T) -> Self { |
| 25 |
self.light_points.clear(); |
| 26 |
self.light_points.extend(iter); |
| 27 |
self |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
pub fn bold_points(&self) -> &[I::ValueType] { |
| 32 |
self.bold_points.as_ref() |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
pub fn bold_points_mut(&mut self) -> &mut [I::ValueType] { |
| 37 |
self.bold_points.as_mut() |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
pub fn light_points(&self) -> &[I::ValueType] { |
| 42 |
self.light_points.as_ref() |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
pub fn light_points_mut(&mut self) -> &mut [I::ValueType] { |
| 47 |
self.light_points.as_mut() |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl<R: Ranged> Ranged for WithKeyPoints<R> |
| 52 |
where |
| 53 |
R::ValueType: Clone, |
| 54 |
{ |
| 55 |
type ValueType = R::ValueType; |
| 56 |
type FormatOption = R::FormatOption; |
| 57 |
|
| 58 |
fn range(&self) -> Range<Self::ValueType> { |
| 59 |
self.inner.range() |
| 60 |
} |
| 61 |
|
| 62 |
fn map(&self, value: &Self::ValueType, limit: (i32, i32)) -> i32 { |
| 63 |
self.inner.map(value, limit) |
| 64 |
} |
| 65 |
|
| 66 |
fn key_points<Hint: KeyPointHint>(&self, hint: Hint) -> Vec<Self::ValueType> { |
| 67 |
if hint.weight().allow_light_points() { |
| 68 |
self.light_points.clone() |
| 69 |
} else { |
| 70 |
self.bold_points.clone() |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
fn axis_pixel_range(&self, limit: (i32, i32)) -> Range<i32> { |
| 75 |
self.inner.axis_pixel_range(limit) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl<R: DiscreteRanged> DiscreteRanged for WithKeyPoints<R> |
| 80 |
where |
| 81 |
R::ValueType: Clone, |
| 82 |
{ |
| 83 |
fn size(&self) -> usize { |
| 84 |
self.inner.size() |
| 85 |
} |
| 86 |
fn index_of(&self, value: &Self::ValueType) -> Option<usize> { |
| 87 |
self.inner.index_of(value) |
| 88 |
} |
| 89 |
fn from_index(&self, index: usize) -> Option<Self::ValueType> { |
| 90 |
self.inner.from_index(index) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
pub trait BindKeyPoints |
| 96 |
where |
| 97 |
Self: AsRangedCoord, |
| 98 |
{ |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
fn with_key_points(self, points: Vec<Self::Value>) -> WithKeyPoints<Self::CoordDescType> { |
| 114 |
WithKeyPoints { |
| 115 |
inner: self.into(), |
| 116 |
bold_points: points, |
| 117 |
light_points: vec![], |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
impl<T: AsRangedCoord> BindKeyPoints for T {} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
pub struct WithKeyPointMethod<R: Ranged> { |
| 130 |
inner: R, |
| 131 |
bold_func: Box<dyn Fn(usize) -> Vec<R::ValueType>>, |
| 132 |
light_func: Box<dyn Fn(usize) -> Vec<R::ValueType>>, |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
pub trait BindKeyPointMethod |
| 137 |
where |
| 138 |
Self: AsRangedCoord, |
| 139 |
{ |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
fn with_key_point_func<F: Fn(usize) -> Vec<Self::Value> + 'static>( |
| 155 |
self, |
| 156 |
func: F, |
| 157 |
) -> WithKeyPointMethod<Self::CoordDescType> { |
| 158 |
WithKeyPointMethod { |
| 159 |
inner: self.into(), |
| 160 |
bold_func: Box::new(func), |
| 161 |
light_func: Box::new(|_| Vec::new()), |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
impl<T: AsRangedCoord> BindKeyPointMethod for T {} |
| 167 |
|
| 168 |
impl<R: Ranged> WithKeyPointMethod<R> { |
| 169 |
|
| 170 |
pub fn with_light_point_func<F: Fn(usize) -> Vec<R::ValueType> + 'static>( |
| 171 |
mut self, |
| 172 |
func: F, |
| 173 |
) -> Self { |
| 174 |
self.light_func = Box::new(func); |
| 175 |
self |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
impl<R: Ranged> Ranged for WithKeyPointMethod<R> { |
| 180 |
type ValueType = R::ValueType; |
| 181 |
type FormatOption = R::FormatOption; |
| 182 |
|
| 183 |
fn range(&self) -> Range<Self::ValueType> { |
| 184 |
self.inner.range() |
| 185 |
} |
| 186 |
|
| 187 |
fn map(&self, value: &Self::ValueType, limit: (i32, i32)) -> i32 { |
| 188 |
self.inner.map(value, limit) |
| 189 |
} |
| 190 |
|
| 191 |
fn key_points<Hint: KeyPointHint>(&self, hint: Hint) -> Vec<Self::ValueType> { |
| 192 |
if hint.weight().allow_light_points() { |
| 193 |
(self.light_func)(hint.max_num_points()) |
| 194 |
} else { |
| 195 |
(self.bold_func)(hint.max_num_points()) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
fn axis_pixel_range(&self, limit: (i32, i32)) -> Range<i32> { |
| 200 |
self.inner.axis_pixel_range(limit) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
impl<R: DiscreteRanged> DiscreteRanged for WithKeyPointMethod<R> { |
| 205 |
fn size(&self) -> usize { |
| 206 |
self.inner.size() |
| 207 |
} |
| 208 |
fn index_of(&self, value: &Self::ValueType) -> Option<usize> { |
| 209 |
self.inner.index_of(value) |
| 210 |
} |
| 211 |
fn from_index(&self, index: usize) -> Option<Self::ValueType> { |
| 212 |
self.inner.from_index(index) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
#[cfg(test)] |
| 217 |
mod test { |
| 218 |
use super::*; |
| 219 |
use crate::coord::ranged1d::{BoldPoints, LightPoints}; |
| 220 |
#[test] |
| 221 |
fn test_with_key_points() { |
| 222 |
let range = (0..100).with_key_points(vec![1, 2, 3]); |
| 223 |
assert_eq!(range.map(&3, (0, 1000)), 30); |
| 224 |
assert_eq!(range.range(), 0..100); |
| 225 |
assert_eq!(range.key_points(BoldPoints(100)), vec![1, 2, 3]); |
| 226 |
assert_eq!(range.key_points(LightPoints::new(100, 100)), vec![]); |
| 227 |
let range = range.with_light_points(5..10); |
| 228 |
assert_eq!(range.key_points(BoldPoints(10)), vec![1, 2, 3]); |
| 229 |
assert_eq!( |
| 230 |
range.key_points(LightPoints::new(10, 10)), |
| 231 |
(5..10).collect::<Vec<_>>() |
| 232 |
); |
| 233 |
|
| 234 |
assert_eq!(range.size(), 101); |
| 235 |
assert_eq!(range.index_of(&10), Some(10)); |
| 236 |
assert_eq!(range.from_index(10), Some(10)); |
| 237 |
|
| 238 |
assert_eq!(range.axis_pixel_range((0, 1000)), 0..1000); |
| 239 |
|
| 240 |
let mut range = range; |
| 241 |
|
| 242 |
assert_eq!(range.light_points().len(), 5); |
| 243 |
assert_eq!(range.light_points_mut().len(), 5); |
| 244 |
assert_eq!(range.bold_points().len(), 3); |
| 245 |
assert_eq!(range.bold_points_mut().len(), 3); |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn test_with_key_point_method() { |
| 250 |
let range = (0..100).with_key_point_func(|_| vec![1, 2, 3]); |
| 251 |
assert_eq!(range.map(&3, (0, 1000)), 30); |
| 252 |
assert_eq!(range.range(), 0..100); |
| 253 |
assert_eq!(range.key_points(BoldPoints(100)), vec![1, 2, 3]); |
| 254 |
assert_eq!(range.key_points(LightPoints::new(100, 100)), vec![]); |
| 255 |
let range = range.with_light_point_func(|_| (5..10).collect()); |
| 256 |
assert_eq!(range.key_points(BoldPoints(10)), vec![1, 2, 3]); |
| 257 |
assert_eq!( |
| 258 |
range.key_points(LightPoints::new(10, 10)), |
| 259 |
(5..10).collect::<Vec<_>>() |
| 260 |
); |
| 261 |
|
| 262 |
assert_eq!(range.size(), 101); |
| 263 |
assert_eq!(range.index_of(&10), Some(10)); |
| 264 |
assert_eq!(range.from_index(10), Some(10)); |
| 265 |
|
| 266 |
assert_eq!(range.axis_pixel_range((0, 1000)), 0..1000); |
| 267 |
} |
| 268 |
} |