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