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
use super::*;
// POOL 256
const CAPACITY: usize = 256;
/// Sized pool object with a max capacity of 256 items.
#[derive(Copy, Clone)]
pub struct SizedPool256<ItemType> {
items: [Option<ItemType>; CAPACITY],
order: [usize; CAPACITY],
count: usize,
}
impl<ItemType> SizedPool256<ItemType>
where ItemType: Copy + PartialEq
{
#[allow(unused)]
/// Creates a new pool.
pub fn new() -> Self {
let mut order = [0; CAPACITY];
for i in 0..CAPACITY { order[i] = i; }
SizedPool256 {
items: [None; CAPACITY],
order,
count: 0,
}
}
/// Returns a new SizedPool that contains copies of all of
/// the objects that where passed through the items array
/// list parameter.
///
/// All items that exceed the maximum capacity of this
/// SizedPool are negated and a full item list containing the
/// first part of the items property is returned.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool as pool;
/// use swarm_pool::tools::sized_pool::SizedPool256;
///
/// let empty_pool = SizedPool256::<usize>::new();
/// assert_eq!(pool::count(&empty_pool), &0);
///
/// let mut pool16 = SizedPool256::<usize>::from_slice(&[
/// 1,2,3,4,5,6,7,8,9,10
/// ]);
///
/// assert_eq!(pool::count(&pool16), &10);
/// assert_eq!(pool::get_ref(&pool16, 0), &Some(1));
/// assert_eq!(pool::get_ref(&pool16, 9), &Some(10));
///
/// // If an item in the pool is unused it returns None
/// // a request index larger than 15 will panic!
/// assert_eq!(pool::get_ref(&pool16, 15), &None);
/// ```
pub fn from_slice(items: &[ItemType]) -> SizedPool256<ItemType> {
let mut new_pool = SizedPool256::<ItemType>::new();
for i in 0..items.len() {
if i >= CAPACITY {
break;
} else {
push(&mut new_pool, items[i]);
}
}
new_pool
}
#[allow(unused)]
pub(crate) fn get_mut(&mut self, position: &usize) -> &mut Option<ItemType> {
&mut self.items[self.order[*position]]
}
#[allow(unused)]
pub(crate) fn get_ref(&self, position: &usize) -> &Option<ItemType> {
&self.items[self.order[*position]]
}
}
impl<ItemType> Default for SizedPool256<ItemType>
where ItemType: Copy + PartialEq
{
fn default() -> Self {
SizedPool256::<ItemType>::new()
}
}
impl<ItemType> StackPool<ItemType> for SizedPool256<ItemType> {
fn count(&self) -> &usize {
&self.count
}
fn count_mut(&mut self) -> &mut usize {
&mut self.count
}
fn ref_at(&self, item_index: &usize) -> &Option<ItemType> {
&self.items[*item_index]
}
fn ref_sorted(&self, ord_index: &usize) -> &Option<ItemType> {
&self.items[self.order[*ord_index]]
}
fn item_at(&mut self, item_index: &usize) -> &mut Option<ItemType> {
&mut self.items[*item_index]
}
fn item_last(&mut self) -> &mut Option<ItemType> {
&mut self.items[self.count]
}
fn item_sorted(&mut self, ord_index: &usize) -> &mut Option<ItemType> {
&mut self.items[self.order[*ord_index]]
}
fn order_at(&mut self, ord_index: &usize) -> &mut usize {
&mut self.order[*ord_index]
}
}