1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

#![allow(dead_code)]
#![allow(unused_variables)]
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        use super::cycle_queue::CycleQueue;
        let mut cq: CycleQueue<u32> = CycleQueue::new_withcapacity(100);
        for i in 1..101 {
            let _ = cq.push_back(i);
        }
        loop {
            let litem = cq.get_item();
            let (stat,wrap_item) = cq.remove_front();
            if cq.is_empty(){break;}
            if let Some(ritem) = wrap_item {
                assert_eq!(litem, ritem);
            };
        }
    }
}
pub mod cycle_queue{
    use std::ptr;
    
    #[derive(Debug)]
    pub enum Status{
        Full, Empty, Success
    }
    
    #[derive(Debug,Default)]
    pub struct CycleQueue<T> {
        capacity: usize,
        queue: Vec<T>,
        head: usize,
        tail: usize
    }
    
    impl<T> CycleQueue<T> {
        
        #[inline]
        pub fn new() -> Self {
            Self {
                capacity: 0,
                queue: Vec::new(),
                head: 0,
                tail: 0
            }
        }
    
        #[inline]
        pub fn new_withcapacity(capacity: usize) -> Self {
            Self {
                capacity,
                queue: Vec::with_capacity(capacity),
                head: 0,
                tail: 0
            }
        }
        
        #[inline]
        pub fn is_empty(&self) -> bool {
            if self.head == self.tail{
                true
            }else{
                false
            }
        }
        
        #[inline]
        pub fn is_full(&self) -> bool {
            if ((self.tail + 1) % self.capacity) == self.head {
                true
            }else {
                false
            }
        }
        #[inline]
        pub fn push_back(&mut self,item: T) -> (Status,std::io::Result<String>){
            if self.is_full() {
                return (Status::Full,Ok(String::from("当前队列已满!")));
            }
            unsafe {
                let tail = self.queue.as_mut_ptr().add(self.tail);
                ptr::write(tail, item);
            }
            self.tail = (self.tail + 1) % self.capacity;
            (Status::Success,Ok(String::from("添加到队列成功")))
        }
        
        #[inline]
        pub fn remove_front(&mut self) -> (Status,Option<T>) {
            if self.is_empty() { 
                return (Status::Empty,None);
            }
            let t;
            unsafe {
                {
                    let head = self.queue.as_ptr().add(self.head);
                    t =ptr::read(head);
                }
                
            }
            self.head = (self.head+1) % self.capacity;
            (Status::Success,Some(t))
        }
        
        pub fn clear(&mut self){
            self.queue.clear();
            self.head = 0;
            self.tail = 0;
        }
        
        #[inline]
        pub fn get_item(&self) -> T {
            self.get_itemidx(self.head)
        }
        
        #[inline]
        pub fn get_itemidx(&self,index: usize) -> T {
            let ret;
            unsafe {
                let ptr = self.queue.as_ptr().add(index);
                ret = ptr::read(ptr);
            }
            ret
        }
    
        #[inline]
        pub fn get_currsize(&self) -> usize {
            (self.tail + self.capacity - self.head) % self.capacity
        }
    }
}