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
135
136
137
138
139
140
141
142
use core::fmt::{self, Debug, Formatter};
pub(crate) const MAX_PARAMS: usize = 32;
#[derive(Default)]
pub struct Params {
subparams: [u8; MAX_PARAMS],
params: [u16; MAX_PARAMS],
current_subparams: u8,
len: usize,
}
impl Params {
#[inline]
pub fn len(&self) -> usize {
self.len
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
#[inline]
pub fn iter(&self) -> ParamsIter<'_> {
ParamsIter::new(self)
}
#[inline]
pub(crate) fn is_full(&self) -> bool {
self.len == MAX_PARAMS
}
#[inline]
pub(crate) fn clear(&mut self) {
self.current_subparams = 0;
self.len = 0;
}
#[inline]
pub(crate) fn push(&mut self, item: u16) {
self.subparams[self.len - self.current_subparams as usize] = self.current_subparams + 1;
self.params[self.len] = item;
self.current_subparams = 0;
self.len += 1;
}
#[inline]
pub(crate) fn extend(&mut self, item: u16) {
self.params[self.len] = item;
self.current_subparams += 1;
self.len += 1;
}
}
impl<'a> IntoIterator for &'a Params {
type IntoIter = ParamsIter<'a>;
type Item = &'a [u16];
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
pub struct ParamsIter<'a> {
params: &'a Params,
index: usize,
}
impl<'a> ParamsIter<'a> {
fn new(params: &'a Params) -> Self {
Self { params, index: 0 }
}
}
impl<'a> Iterator for ParamsIter<'a> {
type Item = &'a [u16];
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.params.len() {
return None;
}
let num_subparams = self.params.subparams[self.index];
let param = &self.params.params[self.index..self.index + num_subparams as usize];
self.index += num_subparams as usize;
Some(param)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.params.len() - self.index;
(remaining, Some(remaining))
}
}
impl Debug for Params {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "[")?;
for (i, param) in self.iter().enumerate() {
if i != 0 {
write!(f, ";")?;
}
for (i, subparam) in param.iter().enumerate() {
if i != 0 {
write!(f, ":")?;
}
subparam.fmt(f)?;
}
}
write!(f, "]")
}
}