(index<- ) ./libextra/container.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Container traits for extra
12
13 use std::container::Mutable;
14
15 /// A double-ended sequence that allows querying, insertion and deletion at both ends.
16 pub trait Deque<T> : Mutable {
17 /// Provide a reference to the front element, or None if the sequence is empty
18 fn front<'a>(&'a self) -> Option<&'a T>;
19
20 /// Provide a mutable reference to the front element, or None if the sequence is empty
21 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T>;
22
23 /// Provide a reference to the back element, or None if the sequence is empty
24 fn back<'a>(&'a self) -> Option<&'a T>;
25
26 /// Provide a mutable reference to the back element, or None if the sequence is empty
27 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T>;
28
29 /// Insert an element first in the sequence
30 fn push_front(&mut self, elt: T);
31
32 /// Insert an element last in the sequence
33 fn push_back(&mut self, elt: T);
34
35 /// Remove the last element and return it, or None if the sequence is empty
36 fn pop_back(&mut self) -> Option<T>;
37
38 /// Remove the first element and return it, or None if the sequence is empty
39 fn pop_front(&mut self) -> Option<T>;
40 }
41
42 #[cfg(test)]
43 mod bench {
44 use std::container::MutableMap;
45 use std::{vec, rand};
46 use std::rand::Rng;
47 use test::BenchHarness;
48
49 pub fn insert_rand_n<M:MutableMap<uint,uint>>(n: uint,
50 map: &mut M,
51 bh: &mut BenchHarness) {
52 // setup
53 let mut rng = rand::XorShiftRng::new();
54
55 map.clear();
56 for _ in range(0, n) {
57 map.insert(rng.gen::<uint>() % n, 1);
58 }
59
60 // measure
61 do bh.iter {
62 let k = rng.gen::<uint>() % n;
63 map.insert(k, 1);
64 map.remove(&k);
65 }
66 }
67
68 pub fn insert_seq_n<M:MutableMap<uint,uint>>(n: uint,
69 map: &mut M,
70 bh: &mut BenchHarness) {
71 // setup
72 map.clear();
73 for i in range(0u, n) {
74 map.insert(i*2, 1);
75 }
76
77 // measure
78 let mut i = 1;
79 do bh.iter {
80 map.insert(i, 1);
81 map.remove(&i);
82 i = (i + 2) % n;
83 }
84 }
85
86 pub fn find_rand_n<M:MutableMap<uint,uint>>(n: uint,
87 map: &mut M,
88 bh: &mut BenchHarness) {
89 // setup
90 let mut rng = rand::XorShiftRng::new();
91 let mut keys = vec::from_fn(n, |_| rng.gen::<uint>() % n);
92
93 for k in keys.iter() {
94 map.insert(*k, 1);
95 }
96
97 rng.shuffle_mut(keys);
98
99 // measure
100 let mut i = 0;
101 do bh.iter {
102 map.find(&(keys[i]));
103 i = (i + 1) % n;
104 }
105 }
106
107 pub fn find_seq_n<M:MutableMap<uint,uint>>(n: uint,
108 map: &mut M,
109 bh: &mut BenchHarness) {
110 // setup
111 for i in range(0u, n) {
112 map.insert(i, 1);
113 }
114
115 // measure
116 let mut i = 0;
117 do bh.iter {
118 map.find(&i);
119 i = (i + 1) % n;
120 }
121 }
122 }