risingwave_common_estimate_size/collections/
vecdeque.rs1use std::collections::VecDeque;
16
17use educe::Educe;
18
19use crate::{EstimateSize, KvSize};
20
21#[derive(Educe)]
22#[educe(Default)]
23pub struct EstimatedVecDeque<T: EstimateSize> {
24 inner: VecDeque<T>,
25 heap_size: KvSize,
26}
27
28impl<T: EstimateSize> EstimatedVecDeque<T> {
29 pub fn pop_back(&mut self) -> Option<T> {
30 self.inner.pop_back().inspect(|v| {
31 self.heap_size.sub_val(v);
32 })
33 }
34
35 pub fn pop_front(&mut self) -> Option<T> {
36 self.inner.pop_front().inspect(|v| {
37 self.heap_size.sub_val(v);
38 })
39 }
40
41 pub fn push_back(&mut self, value: T) {
42 self.heap_size.add_val(&value);
43 self.inner.push_back(value)
44 }
45
46 pub fn push_front(&mut self, value: T) {
47 self.heap_size.add_val(&value);
48 self.inner.push_front(value)
49 }
50
51 pub fn get(&self, index: usize) -> Option<&T> {
52 self.inner.get(index)
53 }
54
55 pub fn front(&self) -> Option<&T> {
56 self.inner.front()
57 }
58
59 pub fn len(&self) -> usize {
60 self.inner.len()
61 }
62
63 pub fn is_empty(&self) -> bool {
64 self.inner.is_empty()
65 }
66}
67
68impl<T: EstimateSize> std::ops::Index<usize> for EstimatedVecDeque<T> {
69 type Output = T;
70
71 fn index(&self, index: usize) -> &Self::Output {
72 &self.inner[index]
73 }
74}
75
76impl<T: EstimateSize> EstimateSize for EstimatedVecDeque<T> {
77 fn estimated_heap_size(&self) -> usize {
78 self.heap_size.size()
81 }
82}