1use crossbeam::atomic::AtomicCell;
5use crate::unsafe_cell_type::U;
6use std::ops::Deref;
7use std::{default::Default, usize};
8use chrono::NaiveTime;
9use crate::fasttime::b2_time::TimeExt;
10use crate::fasttime::a8_micros;
11use std::iter::{IntoIterator, Iterator, ExactSizeIterator};
12use std::ops::{Index, IndexMut};
13
14pub trait CacheData: Default + Clone {
15 fn time(&self) -> NaiveTime;
16}
17
18pub struct HighSpeedCache<T, const LEN: usize> {
19 item: U<UnsafeData<T, LEN>>,
20}
21
22impl<T, const LEN: usize> Deref for HighSpeedCache<T, LEN> {
23 type Target = UnsafeData<T, LEN>;
24 fn deref(&self) -> &Self::Target {
25 self.item.as_ref()
26 }
27}
28
29pub struct UnsafeData<T, const LEN: usize> {
30 items: [T; LEN],
31 index: usize,
32 defalut_val: T,
33 pub count: usize,
34 待补数据: AtomicCell<Option<(usize, T)>>,
35}
36
37pub struct HighSpeedCacheIter<'a, T, const LEN: usize> {
40 cache: &'a HighSpeedCache<T, LEN>,
41 current_index: usize,
42 items_seen: usize,
43 total_items: usize,
44}
45
46pub struct HighSpeedCacheIterMut<'a, T, const LEN: usize> {
49 cache: &'a mut HighSpeedCache<T, LEN>,
50 current_index: usize,
51 items_seen: usize,
52 total_items: usize,
53}
54
55trait CacheIteratorCore {
57 fn get_cache_count(&self) -> usize;
58 fn get_cache_index(&self) -> usize;
59 fn get_current_index(&self) -> usize;
60 fn get_items_seen(&self) -> usize;
61 fn get_total_items(&self) -> usize;
62 fn increment_counters(&mut self);
63
64 fn calculate_actual_index<const LEN: usize>(&self) -> Option<usize> {
66 if self.get_items_seen() >= self.get_total_items() {
67 return None;
68 }
69
70 let idx = if self.get_cache_count() <= LEN {
72 if self.get_current_index() >= self.get_cache_count() {
74 return None;
75 }
76 let newest_idx = self.get_cache_index();
78 if self.get_current_index() <= newest_idx {
79 newest_idx - self.get_current_index()
80 } else {
81 LEN - (self.get_current_index() - newest_idx)
83 }
84 } else {
85 let newest_idx = self.get_cache_index();
87 (newest_idx + LEN - self.get_current_index()) % LEN
88 };
89
90 Some(idx)
91 }
92
93 fn calculate_actual_index_for_back<const LEN: usize>(&self, back_index: usize) -> Option<usize> {
95 let cache_index = self.get_cache_index();
96 let cache_count = self.get_cache_count();
97
98 if back_index >= self.get_total_items() {
100 return None;
101 }
102
103 let actual_index = if cache_count <= LEN {
104 if back_index >= cache_count {
106 return None; }
108
109 let oldest_index = if cache_index + 1 >= cache_count {
111 0
113 } else {
114 (cache_index + 1) % LEN
116 };
117
118 (oldest_index + back_index) % LEN
120 } else {
121 (cache_index + 1 + back_index) % LEN
124 };
125
126 Some(actual_index)
127 }
128}
129
130impl<'a, T, const LEN: usize> CacheIteratorCore for HighSpeedCacheIter<'a, T, LEN> {
131 fn get_cache_count(&self) -> usize {
132 self.cache.count
133 }
134
135 fn get_cache_index(&self) -> usize {
136 self.cache.index
137 }
138
139 fn get_current_index(&self) -> usize {
140 self.current_index
141 }
142
143 fn get_items_seen(&self) -> usize {
144 self.items_seen
145 }
146
147 fn get_total_items(&self) -> usize {
148 self.total_items
149 }
150
151 fn increment_counters(&mut self) {
152 self.current_index += 1;
153 self.items_seen += 1;
154 }
155}
156
157impl<'a, T, const LEN: usize> CacheIteratorCore for HighSpeedCacheIterMut<'a, T, LEN> {
159 fn get_cache_count(&self) -> usize {
160 self.cache.count
161 }
162
163 fn get_cache_index(&self) -> usize {
164 self.cache.index
165 }
166
167 fn get_current_index(&self) -> usize {
168 self.current_index
169 }
170
171 fn get_items_seen(&self) -> usize {
172 self.items_seen
173 }
174
175 fn get_total_items(&self) -> usize {
176 self.total_items
177 }
178
179 fn increment_counters(&mut self) {
180 self.current_index += 1;
181 self.items_seen += 1;
182 }
183}
184
185impl<'a, T, const LEN: usize> Iterator for HighSpeedCacheIter<'a, T, LEN>
186where
187 T: CacheData,
188{
189 type Item = &'a T;
190
191 fn next(&mut self) -> Option<Self::Item> {
192 if let Some(idx) = self.calculate_actual_index::<LEN>() {
193 let item = &self.cache.items[idx];
194 self.increment_counters();
195 Some(item)
196 } else {
197 None
198 }
199 }
200
201 fn size_hint(&self) -> (usize, Option<usize>) {
202 let remaining = self.get_total_items() - self.get_items_seen();
203 (remaining, Some(remaining))
204 }
205}
206
207impl<'a, T, const LEN: usize> DoubleEndedIterator for HighSpeedCacheIter<'a, T, LEN>
208where
209 T: CacheData,
210{
211 fn next_back(&mut self) -> Option<Self::Item> {
212 if self.items_seen >= self.total_items {
213 return None;
214 }
215
216 let back_index = self.total_items - self.items_seen - 1;
218 self.items_seen += 1;
219 self.cache.get(back_index)
220 }
221}
222
223impl<'a, T, const LEN: usize> Iterator for HighSpeedCacheIterMut<'a, T, LEN>
225where
226 T: CacheData,
227{
228 type Item = &'a mut T;
229
230 fn next(&mut self) -> Option<Self::Item> {
231 if let Some(idx) = self.calculate_actual_index::<LEN>() {
232 let item = unsafe {
235 let items_ptr = self.cache.item_mut().items.as_mut_ptr();
236 &mut *items_ptr.add(idx)
237 };
238 self.increment_counters();
239 Some(item)
240 } else {
241 None
242 }
243 }
244
245 fn size_hint(&self) -> (usize, Option<usize>) {
246 let remaining = self.get_total_items() - self.get_items_seen();
247 (remaining, Some(remaining))
248 }
249}
250
251impl<'a, T, const LEN: usize> ExactSizeIterator for HighSpeedCacheIter<'a, T, LEN>
253where
254 T: CacheData,
255{
256 fn len(&self) -> usize {
257 self.get_total_items() - self.get_items_seen()
258 }
259}
260
261impl<'a, T, const LEN: usize> ExactSizeIterator for HighSpeedCacheIterMut<'a, T, LEN>
263where
264 T: CacheData,
265{
266 fn len(&self) -> usize {
267 self.get_total_items() - self.get_items_seen()
268 }
269}
270
271impl<'a, T, const LEN: usize> DoubleEndedIterator for HighSpeedCacheIterMut<'a, T, LEN>
273where
274 T: CacheData,
275{
276 fn next_back(&mut self) -> Option<Self::Item> {
277 if self.items_seen >= self.total_items {
278 return None;
279 }
280
281 let back_index = self.total_items - self.items_seen - 1;
283
284 if let Some(actual_index) = self.calculate_actual_index_for_back::<LEN>(back_index) {
287 let item = unsafe {
288 let items_ptr = self.cache.item_mut().items.as_mut_ptr();
289 &mut *items_ptr.add(actual_index)
290 };
291 self.items_seen += 1;
292 Some(item)
293 } else {
294 None
295 }
296 }
297}
298
299pub struct HighSpeedCacheIntoIter<T, const LEN: usize> {
301 cache: HighSpeedCache<T, LEN>,
302 current_index: usize,
303 items_seen: usize,
304 total_items: usize,
305}
306
307impl<T, const LEN: usize> CacheIteratorCore for HighSpeedCacheIntoIter<T, LEN> {
309 fn get_cache_count(&self) -> usize {
310 self.cache.count
311 }
312
313 fn get_cache_index(&self) -> usize {
314 self.cache.index
315 }
316
317 fn get_current_index(&self) -> usize {
318 self.current_index
319 }
320
321 fn get_items_seen(&self) -> usize {
322 self.items_seen
323 }
324
325 fn get_total_items(&self) -> usize {
326 self.total_items
327 }
328
329 fn increment_counters(&mut self) {
330 self.current_index += 1;
331 self.items_seen += 1;
332 }
333}
334
335impl<T, const LEN: usize> Iterator for HighSpeedCacheIntoIter<T, LEN>
336where
337 T: CacheData + Clone,
338{
339 type Item = T;
340
341 fn next(&mut self) -> Option<Self::Item> {
342 if let Some(idx) = self.calculate_actual_index::<LEN>() {
343 let item = self.cache.items[idx].clone();
344 self.increment_counters();
345 Some(item)
346 } else {
347 None
348 }
349 }
350
351 fn size_hint(&self) -> (usize, Option<usize>) {
352 let remaining = self.get_total_items() - self.get_items_seen();
353 (remaining, Some(remaining))
354 }
355}
356
357impl<T, const LEN: usize> ExactSizeIterator for HighSpeedCacheIntoIter<T, LEN>
359where
360 T: CacheData + Clone,
361{
362 fn len(&self) -> usize {
363 self.get_total_items() - self.get_items_seen()
364 }
365}
366
367impl<T, const LEN: usize> HighSpeedCache<T, LEN>
368where
369 T: CacheData,
370{
371 pub fn with_capacity(_capacity: usize) -> Self {
374 Self::new()
375 }
376
377 pub fn new() -> Self {
378 let items: [T; LEN] = std::array::from_fn(|_i| T::default());
382 let item = UnsafeData {
383 items,
384 index: 0,
385 count: 0,
386 defalut_val: T::default(),
387 待补数据: AtomicCell::new(None),
388 };
389 Self { item: item.into() }
390 }
391
392 fn item_mut(&self) -> &mut UnsafeData<T, LEN> {
393 self.item.as_mut()
394 }
395
396 pub fn push(&self, item_new: T) {
399 let self_item = self.item_mut();
400 if let Some((i, d)) = self.待补数据.swap(None) {
401 self_item.items[i] = d;
402 }
403
404 if self_item.count == 0 {
405 } else {
407 self_item.index += 1;
408 if self_item.index == LEN {
409 self_item.index = 0;
410 }
411 }
412 self_item.items[self.index] = item_new;
413 self_item.count += 1;
414 }
415
416 pub fn current(&self) -> &T {
418 if self.count == 0 {
419 return &self.item.as_ref().defalut_val;
420 }
421 &self.items[self.index]
422 }
423
424 pub fn clear(&self) {
426 let self_item = self.item_mut();
427 self_item.index = 0;
428 self_item.count = 0;
429 self_item.items[0] = T::default();
431 }
432
433 pub fn find_找之前的项(&self, 毫秒数: i64, is_找不到即取最新数据: bool) -> Option<&T> {
436 if self.count == 0 {
437 return None;
438 }
439 let capacity = self.items.len();
440 let 微秒数 = if 毫秒数 >= 0 {
442 毫秒数 as u64 * a8_micros::MICROS_PER_MILLIS
443 } else {
444 0
445 };
446
447 let ticks_当前项 = self.items[self.index].time().micros_of_day();
448 let index_当前索引 = self.index;
449
450 let mut find_最新 = Option::<&T>::None;
451 for idx in (0..index_当前索引).rev() {
452 let ticks_往前项 = self.items[idx].time().micros_of_day();
453 if ticks_往前项 == 0 {
454 continue;
455 }
456 let delta_往前微秒数 = ticks_当前项 - ticks_往前项;
457 if delta_往前微秒数 > 微秒数 {
458 return Some(&self.items[idx]);
459 }
460
461 if is_找不到即取最新数据 {
463 match &find_最新 {
464 None => {
465 find_最新 = Some(&self.items[idx]);
466 }
467 _ => (),
468 }
469 }
470 }
471
472 if self.count <= LEN {
473 return Some(find_最新.unwrap_or_else(|| &self.item.as_ref().defalut_val));
475 }
476
477 for idx in ((index_当前索引 + 1)..capacity).rev() {
479 let item = &self.items[idx];
480 let ticks_往前项 = item.time().micros_of_day();
481 let delta_往前微秒数 = ticks_当前项 - ticks_往前项;
482 if ticks_往前项 > 0 && delta_往前微秒数 > 微秒数 {
483 return Some(item);
484
485 }
492 }
493
494 return find_最新;
495 }
496
497 pub fn find_limit<F: Fn(&T, &T) -> bool>(
553 &self,
554 n: i64,
555 is_return_last: bool,
556 compare: F,
557 ) -> T {
558 let items = self.list_items(n);
559 let 补一个数据 = None;
560
561 if (items.is_empty() || items.len() == 1) && is_return_last {
562 }
565
566 if items.is_empty() {
567 return 补一个数据.unwrap_or_else(|| self.defalut_val.clone());
568 }
569
570 let mut find_v = items[0];
571 for item in items.iter().skip(1) {
572 if compare(&find_v, item) {
573 find_v = item;
574 }
575 }
576
577 if let Some(v) = &补一个数据 {
578 if compare(&find_v, v) {
579 find_v = v;
580 }
581 }
582
583 return find_v.clone();
584 }
585
586 pub fn list_items_len(&self, mut count: usize) -> Vec<&T> {
588 if self.count == 0 {
589 return vec![];
590 }
591 let mut items = vec![];
592
593 let 当前项 = &self.items[self.index];
594 items.push(当前项);
595
596 let index_当前索引 = self.index;
597 for idx in (0..index_当前索引).rev() {
598 if count == 0 {
599 return items;
600 }
601 items.push(&self.items[idx]);
602 count -= 1;
603 }
604
605 if self.count <= LEN {
606 return items;
608 }
609
610 for idx in ((index_当前索引 + 1)..self.items.len()).rev() {
611 if count == 0 {
612 return items;
613 }
614 items.push(&self.items[idx]);
615 count -= 1;
616 }
617 items
618 }
619
620 pub fn list_items(&self, 毫秒数: i64) -> Vec<&T> {
622 if self.count == 0 {
623 return vec![];
624 }
625 let mut items = vec![];
626
627 let 微秒数 = if 毫秒数 as i64 >= 0 {
629 毫秒数 as u64 * a8_micros::MICROS_PER_MILLIS
630 } else {
631 0
632 };
633
634 let 当前项 = &self.items[self.index];
635 items.push(当前项);
636
637 let ticks_当前项 = 当前项.time().micros_of_day();
638 let index_当前索引 = self.index;
639 for idx in (0..index_当前索引).rev() {
640 let ticks_往前项 = self.items[idx].time().micros_of_day();
641 let delta_往前微秒数 = ticks_当前项 - ticks_往前项;
642 if delta_往前微秒数 > 微秒数 {
643 break;
645 }
646 items.push(&self.items[idx]);
647 }
648 if self.count <= LEN {
649 return items;
651 }
652
653 for idx in ((index_当前索引 + 1)..self.items.len()).rev() {
654 let ticks_往前项 = self.items[idx].time().micros_of_day();
655 let delta_往前微秒数 = ticks_当前项 - ticks_往前项;
656 if delta_往前微秒数 > 微秒数 {
657 return items;
658 }
659 items.push(&self.items[idx]);
661 }
662 items
663 }
664
665 }
702
703impl<T: CacheData, const LEN: usize> Default for HighSpeedCache<T, LEN> {
704 fn default() -> Self {
705 Self::new()
706 }
707}
708
709impl<T, const LEN: usize> HighSpeedCache<T, LEN>
710where
711 T: CacheData,
712{
713 pub fn len(&self) -> usize {
715 self.count.min(LEN)
717 }
718
719 pub fn is_empty(&self) -> bool {
721 self.count == 0
722 }
723
724 pub fn capacity(&self) -> usize {
726 LEN
727 }
728
729 pub fn get(&self, index: usize) -> Option<&T> {
731 if index >= self.count {
732 return None;
733 }
734
735 let actual_index = if self.count <= LEN {
737 if index <= self.index {
740 self.index - index
741 } else {
742 LEN - (index - self.index)
744 }
745 } else {
746 (self.index + LEN - index) % LEN
748 };
749
750 Some(&self.item.as_ref().items[actual_index])
751 }
752
753 pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
755 if index >= self.count {
756 return None;
757 }
758
759 let actual_index = if self.count <= LEN {
761 if index >= self.count {
763 return None; }
765 if index <= self.index {
767 self.index - index
768 } else {
769 LEN - (index - self.index)
771 }
772 } else {
773 (self.index + LEN - index) % LEN
775 };
776
777 Some(&mut self.item_mut().items[actual_index])
778 }
779
780 pub fn iter(&self) -> HighSpeedCacheIter<T, LEN> {
782 HighSpeedCacheIter {
783 cache: self,
784 current_index: 0,
785 items_seen: 0,
786 total_items: self.len(),
787 }
788 }
789
790 pub fn iter_mut(&mut self) -> HighSpeedCacheIterMut<T, LEN> {
792 let total = self.len();
794 HighSpeedCacheIterMut {
795 cache: self,
796 current_index: 0,
797 items_seen: 0,
798 total_items: total,
799 }
800 }
801
802 pub fn to_vec(&self) -> Vec<T>
804 where
805 T: Clone,
806 {
807 self.iter().cloned().collect()
808 }
809}
810
811impl<'a, T, const LEN: usize> IntoIterator for &'a HighSpeedCache<T, LEN>
813where
814 T: CacheData,
815{
816 type Item = &'a T;
817 type IntoIter = HighSpeedCacheIter<'a, T, LEN>;
818
819 fn into_iter(self) -> Self::IntoIter {
820 self.iter()
821 }
822}
823
824impl<'a, T, const LEN: usize> IntoIterator for &'a mut HighSpeedCache<T, LEN>
826where
827 T: CacheData,
828{
829 type Item = &'a mut T;
830 type IntoIter = HighSpeedCacheIterMut<'a, T, LEN>;
831
832 fn into_iter(self) -> Self::IntoIter {
833 self.iter_mut()
834 }
835}
836
837impl<T, const LEN: usize> Index<usize> for HighSpeedCache<T, LEN>
843where
844 T: CacheData,
845{
846 type Output = T;
847
848 fn index(&self, index: usize) -> &Self::Output {
849 self.get(index).expect("索引超出范围")
850 }
851}
852
853impl<T, const LEN: usize> IndexMut<usize> for HighSpeedCache<T, LEN>
855where
856 T: CacheData,
857{
858 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
859 self.get_mut(index).expect("索引超出范围")
860 }
861}
862
863impl<T, const LEN: usize> IntoIterator for HighSpeedCache<T, LEN>
864where
865 T: CacheData + Clone,
866{
867 type Item = T;
868 type IntoIter = HighSpeedCacheIntoIter<T, LEN>;
869
870 fn into_iter(self) -> Self::IntoIter {
871 let total = self.len();
872 HighSpeedCacheIntoIter {
873 cache: self,
874 current_index: 0,
875 items_seen: 0,
876 total_items: total,
877 }
878 }
879}
880
881#[cfg(test)]
882mod test_高频数据 {
883 use chrono::NaiveTime;
884 use crate::fasttime::b2_time::TimeExt;
885
886 use super::{CacheData, HighSpeedCache};
887
888 #[derive(Debug, Default, Clone, PartialEq, Eq)]
889 struct Demo_高频数据 {
890 pub time_发生时间: NaiveTime,
891 pub time_接收时间: NaiveTime,
892 pub val: i32,
893 }
894
895 impl CacheData for Demo_高频数据 {
896 fn time(&self) -> NaiveTime {
897 self.time_发生时间
898 }
899 }
900
901 #[test]
902 fn test_demo_高频数据() {
903 let HighSpeedCache = HighSpeedCache::<Demo_高频数据, 1000>::new();
904
905 let mut micros = NaiveTime::from_hmsi_friendly_unsafe(93000_000).micros_of_day();
907 for val in 1..1000 {
908 let time_发生时间 = NaiveTime::from_micros_day_unsafe(micros);
909 micros += 100; HighSpeedCache.push(Demo_高频数据 {
912 time_发生时间,
913 time_接收时间: time_发生时间,
914 val,
915 });
916 _ = HighSpeedCache.find_找之前的项(30_000, false);
917 }
918 }
919
920 #[test]
921 fn test_高频数据_迭代器() {
922 let cache_empty = HighSpeedCache::<Demo_高频数据, 10>::new();
924 assert_eq!(cache_empty.len(), 0);
925 assert!(cache_empty.is_empty());
926 assert_eq!(cache_empty.capacity(), 10);
927
928 let mut iter_count = 0;
930 for _ in &cache_empty {
931 iter_count += 1;
932 }
933 assert_eq!(iter_count, 0, "空缓存不应该有任何元素可迭代");
934 assert_eq!(cache_empty.get(0), None, "空缓存的get应返回None");
935
936 let cache_partial = HighSpeedCache::<Demo_高频数据, 10>::new();
938 let base_micros = NaiveTime::from_hmsi_friendly_unsafe(93000_000).micros_of_day();
939
940 for val in 1..6 {
942 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
943 cache_partial.push(Demo_高频数据 {
944 time_发生时间: time,
945 time_接收时间: time,
946 val,
947 });
948 }
949
950 assert_eq!(cache_partial.len(), 5);
951 assert!(!cache_partial.is_empty());
952
953 let items: Vec<i32> = cache_partial.iter().map(|item| item.val).collect();
955 assert_eq!(items, vec![5, 4, 3, 2, 1]);
956
957 let items_rev: Vec<i32> = cache_partial.iter().rev().map(|item| item.val).collect();
959 assert_eq!(items_rev, vec![1, 2, 3, 4, 5]);
960
961 assert_eq!(cache_partial.get(0).map(|item| item.val), Some(5)); assert_eq!(cache_partial.get(4).map(|item| item.val), Some(1)); assert_eq!(cache_partial.get(5), None); assert_eq!(cache_partial[0].val, 5);
968 assert_eq!(cache_partial[4].val, 1);
969
970 let cache_full = HighSpeedCache::<Demo_高频数据, 3>::new();
972
973 for val in 1..6 {
975 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
976 cache_full.push(Demo_高频数据 {
977 time_发生时间: time,
978 time_接收时间: time,
979 val,
980 });
981 }
982
983 assert_eq!(cache_full.len(), 3);
984 assert_eq!(cache_full.count, 5); let items_full: Vec<i32> = cache_full.iter().map(|item| item.val).collect();
988 assert_eq!(items_full, vec![5, 4, 3]);
989
990 let cache_with_cap = HighSpeedCache::<Demo_高频数据, 5>::with_capacity(100);
992 assert_eq!(cache_with_cap.capacity(), 5); let vec_items = cache_full.to_vec();
996 assert_eq!(vec_items.len(), 3);
997 assert_eq!(vec_items[0].val, 5);
998 assert_eq!(vec_items[2].val, 3);
999
1000 let cache_consume = HighSpeedCache::<Demo_高频数据, 3>::new();
1002
1003 for val in 1..4 {
1005 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
1006 cache_consume.push(Demo_高频数据 {
1007 time_发生时间: time,
1008 time_接收时间: time,
1009 val,
1010 });
1011 }
1012
1013 let consumed: Vec<Demo_高频数据> = cache_consume.into_iter().collect();
1015 assert_eq!(consumed.len(), 3);
1016 assert_eq!(consumed[0].val, 3); assert_eq!(consumed[2].val, 1);
1018
1019 let mut cache_mut = HighSpeedCache::<Demo_高频数据, 3>::new();
1021
1022 for val in 1..4 {
1024 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
1025 cache_mut.push(Demo_高频数据 {
1026 time_发生时间: time,
1027 time_接收时间: time,
1028 val,
1029 });
1030 }
1031
1032 cache_mut[0].val = 100;
1034 assert_eq!(cache_mut[0].val, 100);
1035
1036 let cache_exact = HighSpeedCache::<Demo_高频数据, 5>::new();
1038 for val in 1..4 {
1039 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
1040 cache_exact.push(Demo_高频数据 {
1041 time_发生时间: time,
1042 time_接收时间: time,
1043 val,
1044 });
1045 }
1046
1047 let iter = cache_exact.iter();
1048 assert_eq!(iter.len(), 3);
1049
1050 let mut iter2 = cache_exact.iter();
1051 iter2.next(); assert_eq!(iter2.len(), 2); }
1054
1055 #[test]
1056 fn test_索引器_异常处理() {
1057 {
1059 let cache = HighSpeedCache::<Demo_高频数据, 5>::new();
1060 assert_eq!(cache.len(), 0);
1063 assert!(cache.is_empty());
1064 assert_eq!(cache.get(0), None);
1066 }
1067
1068 let base_micros = NaiveTime::from_hmsi_friendly_unsafe(93000_000).micros_of_day();
1070 let cache_with_data = HighSpeedCache::<Demo_高频数据, 5>::new();
1072 for val in 1..4 {
1073 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
1074 cache_with_data.push(Demo_高频数据 {
1075 time_发生时间: time,
1076 time_接收时间: time,
1077 val,
1078 });
1079 }
1080
1081 assert_eq!(cache_with_data.get(0).map(|item| item.val), Some(3));
1083 assert_eq!(cache_with_data.get(1).map(|item| item.val), Some(2));
1084 assert_eq!(cache_with_data.get(2).map(|item| item.val), Some(1));
1085
1086 assert_eq!(cache_with_data.get(3), None, "超范围索引应该返回 None");
1088 assert_eq!(cache_with_data.get(100), None, "远超范围索引应该返回 None");
1089 }
1090
1091 #[test]
1092 fn test_iter_mut() {
1093 let base_micros = NaiveTime::from_hmsi_friendly_unsafe(93000_000).micros_of_day();
1095 let mut cache = HighSpeedCache::<Demo_高频数据, 5>::new();
1096
1097 for val in 1..6 {
1099 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
1100 cache.push(Demo_高频数据 {
1101 time_发生时间: time,
1102 time_接收时间: time,
1103 val,
1104 });
1105 }
1106
1107 for item in cache.iter_mut() {
1109 let val = &mut item.val;
1110 *val *= 10; }
1112
1113 let values: Vec<i32> = cache.iter().map(|item| item.val).collect();
1115 assert_eq!(values, vec![50, 40, 30, 20, 10]); let mut iter = cache.iter_mut();
1119 if let Some(item) = iter.next() {
1120 item.val = 100; }
1122 if let Some(item) = iter.next() {
1123 item.val = 200; }
1125
1126 assert_eq!(cache[0].val, 100); assert_eq!(cache[1].val, 200); assert_eq!(cache[2].val, 30); let mut iter_mut = cache.iter_mut();
1133 assert_eq!(iter_mut.len(), 5);
1134 iter_mut.next();
1135 assert_eq!(iter_mut.len(), 4);
1136 iter_mut.next();
1137 assert_eq!(iter_mut.len(), 3);
1138 }
1139
1140 #[test]
1141 fn test_iter_rev() {
1142 let base_micros = NaiveTime::from_hmsi_friendly_unsafe(93000_000).micros_of_day();
1144 let cache = HighSpeedCache::<Demo_高频数据, 5>::new();
1145
1146 for val in 1..6 {
1148 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
1149 cache.push(Demo_高频数据 {
1150 time_发生时间: time,
1151 time_接收时间: time,
1152 val,
1153 });
1154 }
1155
1156 let values: Vec<i32> = cache.iter().rev().map(|item| item.val).collect();
1158 assert_eq!(values, vec![1, 2, 3, 4, 5]); let mut cache_mut = HighSpeedCache::<Demo_高频数据, 5>::new();
1162
1163 for val in 1..6 {
1165 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
1166 cache_mut.push(Demo_高频数据 {
1167 time_发生时间: time,
1168 time_接收时间: time,
1169 val,
1170 });
1171 }
1172
1173 for item in cache_mut.iter_mut().rev() {
1175 let val = &mut item.val;
1176 *val *= 10; }
1178
1179 let values: Vec<i32> = cache_mut.iter().map(|item| item.val).collect();
1181 assert_eq!(values, vec![50, 40, 30, 20, 10]); let mut cache2 = HighSpeedCache::<Demo_高频数据, 3>::new();
1185 for val in 1..4 {
1186 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
1187 cache2.push(Demo_高频数据 {
1188 time_发生时间: time,
1189 time_接收时间: time,
1190 val,
1191 });
1192 }
1193
1194 for item in &mut cache2 {
1196 item.val += 100;
1197 }
1198
1199 assert_eq!(cache2[0].val, 103); assert_eq!(cache2[1].val, 102); assert_eq!(cache2[2].val, 101); }
1204
1205 #[test]
1206 fn test_完整迭代器行为() {
1207 let base_micros = NaiveTime::from_hmsi_friendly_unsafe(93000_000).micros_of_day();
1209 let mut cache = HighSpeedCache::<Demo_高频数据, 10>::new();
1210
1211 let mut original_values = Vec::new();
1213 for val in 1..11 {
1214 let time = NaiveTime::from_micros_day_unsafe(base_micros + (val as u64 * 100));
1215 let item = Demo_高频数据 {
1216 time_发生时间: time,
1217 time_接收时间: time,
1218 val,
1219 };
1220 original_values.push(item.clone());
1221 cache.push(item);
1222 }
1223
1224 assert_eq!(cache.capacity(), 10);
1226 assert_eq!(cache.len(), 10);
1227 assert!(!cache.is_empty());
1228
1229 let mut iter_values = Vec::new();
1231 for item in &cache {
1232 iter_values.push(item.val);
1233 }
1234 assert_eq!(iter_values, vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
1236
1237 let mut rev_iter_values = Vec::new();
1239 for item in cache.iter().rev() {
1240 rev_iter_values.push(item.val);
1241 }
1242 assert_eq!(rev_iter_values, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1244
1245 let mut modified_values = Vec::new();
1247 for item in cache.iter_mut() {
1248 item.val *= 2; modified_values.push(item.val);
1250 }
1251 assert_eq!(modified_values, vec![20, 18, 16, 14, 12, 10, 8, 6, 4, 2]);
1253
1254 let mut rev_modified_values = Vec::new();
1256 for item in cache.iter_mut().rev() {
1257 item.val += 5; rev_modified_values.push(item.val);
1259 }
1260 assert_eq!(rev_modified_values, vec![25, 23, 21, 19, 17, 15, 13, 11, 9, 7]);
1262
1263 for i in 0..cache.len() {
1265 assert_eq!(cache[i].val, 25 - i as i32 * 2);
1267 }
1268
1269 assert!(cache.get(cache.len()).is_none());
1271 assert!(cache.get(100).is_none());
1272
1273 let new_val = 100;
1276 let time = NaiveTime::from_micros_day_unsafe(base_micros + (new_val as u64 * 100));
1277 cache.push(Demo_高频数据 {
1278 time_发生时间: time,
1279 time_接收时间: time,
1280 val: new_val,
1281 });
1282
1283 assert_eq!(cache[0].val, new_val);
1285
1286 let values: Vec<i32> = cache.iter().map(|item| item.val).collect();
1288 assert_eq!(values.len(), 10); assert_eq!(values[0], new_val); assert_eq!(values[9], 9); let mut iter = cache.iter();
1294 assert_eq!(iter.len(), 10);
1295 iter.next();
1296 assert_eq!(iter.len(), 9);
1297
1298 let empty_cache = HighSpeedCache::<Demo_高频数据, 5>::new();
1300 assert_eq!(empty_cache.len(), 0);
1301 assert!(empty_cache.is_empty());
1302 assert_eq!(empty_cache.iter().count(), 0);
1303 assert_eq!(empty_cache.iter().rev().count(), 0);
1304 }
1305}