Skip to main content

apple_quant_algorithmic/hot/
flex.rs

1use std::ops::Range;
2
3use smallvec::SmallVec;
4
5#[derive(Default)]
6pub struct FlexHotData<
7	T: Clone,
8	const INLINE_COUNT: usize,
9>(SmallVec<[T; INLINE_COUNT]>);
10
11impl<
12	T: Clone,
13	const INLINE_COUNT: usize,
14> FlexHotData<T, INLINE_COUNT> {
15	pub fn remove_last(
16		&mut self,
17	) -> Option<T> {
18		self.0.pop()
19	}
20
21	pub fn append(
22		&mut self,
23		data: &[T],
24	) {
25		self.0.extend(data.iter().cloned());
26	}
27
28	pub fn drain_range(
29		&mut self,
30		range: Range<usize>,
31	) {
32		debug_assert!(range.start <= self.0.len());
33		debug_assert!(range.end <= self.0.len());
34		debug_assert!(range.start <= range.end);
35
36		self.0.drain(range);
37	}
38
39	pub fn retain_newest(
40		&mut self,
41		count: usize,
42	) {
43		debug_assert!(count <= self.0.len());
44		let drain_count = self.0.len() - count;
45		self.drain_range(0..drain_count);
46	}
47
48	pub fn get(
49		&self,
50		idx: usize,
51	) -> Option<&T> {
52		self.0.get(idx)
53	}
54
55	pub fn iter(
56		&self,
57	) -> impl IntoIterator<Item = &T> {
58		self.0.iter()
59	}
60}