Skip to main content

rs_graph/collections/
queue.rs

1/*
2 * Copyright (c) 2018 Frank Fischer <frank-fischer@shadow-soft.de>
3 *
4 * This program is free software: you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see  <http://www.gnu.org/licenses/>
16 */
17
18use std::collections::VecDeque;
19
20pub trait ItemQueue<I> {
21    fn is_empty(&self) -> bool {
22        self.len() == 0
23    }
24
25    fn len(&self) -> usize;
26
27    fn clear(&mut self);
28
29    fn push(&mut self, u: I);
30
31    fn pop(&mut self) -> Option<I>;
32}
33
34impl<'a, I, D> ItemQueue<I> for &'a mut D
35where
36    D: ItemQueue<I>,
37{
38    fn is_empty(&self) -> bool {
39        (**self).is_empty()
40    }
41
42    fn len(&self) -> usize {
43        (**self).len()
44    }
45
46    fn clear(&mut self) {
47        (**self).clear()
48    }
49
50    fn push(&mut self, u: I) {
51        (**self).push(u)
52    }
53
54    fn pop(&mut self) -> Option<I> {
55        (**self).pop()
56    }
57}
58
59impl<I> ItemQueue<I> for VecDeque<I> {
60    fn is_empty(&self) -> bool {
61        VecDeque::is_empty(self)
62    }
63
64    fn len(&self) -> usize {
65        VecDeque::len(self)
66    }
67
68    fn clear(&mut self) {
69        VecDeque::clear(self)
70    }
71
72    fn push(&mut self, u: I) {
73        VecDeque::push_back(self, u)
74    }
75
76    fn pop(&mut self) -> Option<I> {
77        VecDeque::pop_front(self)
78    }
79}