-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.rs
More file actions
182 lines (158 loc) · 5.42 KB
/
pattern.rs
File metadata and controls
182 lines (158 loc) · 5.42 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::ops::{Index, IndexMut};
use crate::project::note_event::NoteEvent;
use crate::project::Song;
/// both row and channel are zero based. If this ever changes a lot of the implementations of
/// Pattern need to be changed, because the searching starts working differently
// don't change the Order of fields, as PartialOrd derive depends on it
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct InPatternPosition {
pub row: u16,
pub channel: u8,
}
#[cfg(test)]
mod test {
use crate::project::pattern::InPatternPosition;
#[test]
fn position_ord() {
let one_zero = InPatternPosition { row: 1, channel: 0 };
let zero_one = InPatternPosition { row: 0, channel: 1 };
assert!(one_zero > zero_one);
}
}
#[derive(Clone, Debug)]
pub struct Pattern {
rows: u16,
// Events are sorted with InPatternPosition as the key.
data: Vec<(InPatternPosition, NoteEvent)>,
}
const fn key(data: &(InPatternPosition, NoteEvent)) -> InPatternPosition {
data.0
}
impl Default for Pattern {
fn default() -> Self {
Self::new(Self::DEFAULT_ROWS)
}
}
impl Pattern {
pub const MAX_ROWS: u16 = 200;
pub const DEFAULT_ROWS: u16 = 64;
/// panics if len larger than 'Self::MAX_LEN'
pub const fn new(len: u16) -> Self {
assert!(len <= Self::MAX_ROWS);
Self {
rows: len,
data: Vec::new(),
}
}
/// panics it the new len is larger than 'Self::MAX_LEN'
/// deletes the data on higher rows
pub fn set_length(&mut self, new_len: u16) {
assert!(new_len <= Self::MAX_ROWS);
// gets the index of the first element of the first row to be removed
if new_len < self.rows {
let idx = self.data.partition_point(|(pos, _)| pos.row < new_len);
self.data.truncate(idx);
}
self.rows = new_len;
}
/// overwrites the event if the row already has an event for that channel
/// panics if the row position is larger than current amount of rows
pub fn set_event(&mut self, position: InPatternPosition, event: NoteEvent) {
assert!(position.row < self.rows);
match self.data.binary_search_by_key(&position, key) {
Ok(idx) => self.data[idx].1 = event,
Err(idx) => self.data.insert(idx, (position, event)),
}
}
pub fn get_event(&self, index: InPatternPosition) -> Option<&NoteEvent> {
self.data
.binary_search_by_key(&index, key)
.ok()
.map(|idx| &self.data[idx].1)
}
pub fn get_event_mut(&mut self, index: InPatternPosition) -> Option<&mut NoteEvent> {
self.data
.binary_search_by_key(&index, key)
.ok()
.map(|idx| &mut self.data[idx].1)
}
/// if there is no event, does nothing
pub fn remove_event(&mut self, position: InPatternPosition) {
if let Ok(index) = self.data.binary_search_by_key(&position, key) {
self.data.remove(index);
}
}
pub const fn row_count(&self) -> u16 {
self.rows
}
/// Panics if the Operation is invalid
pub fn apply_operation(&mut self, op: PatternOperation) {
match op {
PatternOperation::SetLength { new_len } => self.set_length(new_len),
PatternOperation::SetEvent { position, event } => self.set_event(position, event),
PatternOperation::RemoveEvent { position } => self.remove_event(position),
}
}
pub const fn operation_is_valid(&self, op: &PatternOperation) -> bool {
match op {
PatternOperation::SetLength { new_len } => *new_len < Self::MAX_ROWS,
PatternOperation::SetEvent { position, event: _ } => {
position.row < self.rows && position.channel as usize <= Song::MAX_CHANNELS
}
PatternOperation::RemoveEvent { position: _ } => true,
}
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
}
impl Index<u16> for Pattern {
type Output = [(InPatternPosition, NoteEvent)];
/// # Out of Bounds
/// Debug: Panic
///
/// Release: Empty slice
fn index(&self, index: u16) -> &Self::Output {
// only a debug assert because if out of bounds the output is simply empty
debug_assert!(index <= self.rows);
let start_position = self.data.partition_point(|(pos, _)| {
*pos < InPatternPosition {
row: index,
channel: 0,
}
});
// only search after start_position
let end_position =
self.data[start_position..self.data.len()].partition_point(|(pos, _)| {
*pos < InPatternPosition {
row: index + 1,
channel: 0,
}
}) + start_position;
&self.data[start_position..end_position]
}
}
impl Index<InPatternPosition> for Pattern {
type Output = NoteEvent;
fn index(&self, index: InPatternPosition) -> &Self::Output {
self.get_event(index).unwrap()
}
}
impl IndexMut<InPatternPosition> for Pattern {
fn index_mut(&mut self, index: InPatternPosition) -> &mut Self::Output {
self.get_event_mut(index).unwrap()
}
}
#[derive(Debug, Clone, Copy)]
pub enum PatternOperation {
SetLength {
new_len: u16,
},
SetEvent {
position: InPatternPosition,
event: NoteEvent,
},
RemoveEvent {
position: InPatternPosition,
},
}