cycle_queue/
lib.rs

1
2#![allow(dead_code)]
3#![allow(unused_variables)]
4#[cfg(test)]
5mod tests {
6    #[test]
7    fn it_works() {
8        use super::cycle_queue::CycleQueue;
9        let mut cq: CycleQueue<u32> = CycleQueue::new_withcapacity(100);
10        for i in 1..101 {
11            let _ = cq.push_back(i);
12        }
13        loop {
14            let litem = cq.get_item();
15            let (stat,wrap_item) = cq.remove_front();
16            if cq.is_empty(){break;}
17            if let Some(ritem) = wrap_item {
18                assert_eq!(litem, ritem);
19            };
20        }
21    }
22}
23pub mod cycle_queue{
24    use std::ptr;
25    
26    #[derive(Debug)]
27    pub enum Status{
28        Full, Empty, Success
29    }
30    
31    #[derive(Debug,Default)]
32    pub struct CycleQueue<T> {
33        capacity: usize,
34        queue: Vec<T>,
35        head: usize,
36        tail: usize
37    }
38    
39    impl<T> CycleQueue<T> {
40        
41        #[inline]
42        pub fn new() -> Self {
43            Self {
44                capacity: 0,
45                queue: Vec::new(),
46                head: 0,
47                tail: 0
48            }
49        }
50    
51        #[inline]
52        pub fn new_withcapacity(capacity: usize) -> Self {
53            Self {
54                capacity,
55                queue: Vec::with_capacity(capacity),
56                head: 0,
57                tail: 0
58            }
59        }
60        
61        #[inline]
62        pub fn is_empty(&self) -> bool {
63            if self.head == self.tail{
64                true
65            }else{
66                false
67            }
68        }
69        
70        #[inline]
71        pub fn is_full(&self) -> bool {
72            if ((self.tail + 1) % self.capacity) == self.head {
73                true
74            }else {
75                false
76            }
77        }
78        #[inline]
79        pub fn push_back(&mut self,item: T) -> (Status,std::io::Result<String>){
80            if self.is_full() {
81                return (Status::Full,Ok(String::from("当前队列已满!")));
82            }
83            unsafe {
84                let tail = self.queue.as_mut_ptr().add(self.tail);
85                ptr::write(tail, item);
86            }
87            self.tail = (self.tail + 1) % self.capacity;
88            (Status::Success,Ok(String::from("添加到队列成功")))
89        }
90        
91        #[inline]
92        pub fn remove_front(&mut self) -> (Status,Option<T>) {
93            if self.is_empty() { 
94                return (Status::Empty,None);
95            }
96            let t;
97            unsafe {
98                {
99                    let head = self.queue.as_ptr().add(self.head);
100                    t =ptr::read(head);
101                }
102                
103            }
104            self.head = (self.head+1) % self.capacity;
105            (Status::Success,Some(t))
106        }
107        
108        pub fn clear(&mut self){
109            self.queue.clear();
110            self.head = 0;
111            self.tail = 0;
112        }
113        
114        #[inline]
115        pub fn get_item(&self) -> T {
116            self.get_itemidx(self.head)
117        }
118        
119        #[inline]
120        pub fn get_itemidx(&self,index: usize) -> T {
121            let ret;
122            unsafe {
123                let ptr = self.queue.as_ptr().add(index);
124                ret = ptr::read(ptr);
125            }
126            ret
127        }
128    
129        #[inline]
130        pub fn get_currsize(&self) -> usize {
131            (self.tail + self.capacity - self.head) % self.capacity
132        }
133    }
134}