Skip to content

Commit a915202

Browse files
committed
bugfix with merge lane not sorting entries
1 parent c528353 commit a915202

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

crates/notation_core/src/duration.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cmp::Ordering;
12
use std::fmt::Display;
23
use std::ops::{Add, Sub};
34

@@ -116,6 +117,19 @@ impl Sub for Units {
116117
Units(self.0 - rhs.0)
117118
}
118119
}
120+
121+
impl Units {
122+
pub fn cmp(&self, other: &Self) -> std::cmp::Ordering {
123+
if self.0 == other.0 {
124+
Ordering::Equal
125+
} else if self.0 < other.0 {
126+
Ordering::Less
127+
} else {
128+
Ordering::Greater
129+
}
130+
}
131+
}
132+
119133
// https://hellomusictheory.com/learn/tuplets/
120134
#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
121135
pub enum Duration {

crates/notation_midi/src/midi_state.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,9 @@ impl MidiChannel {
4949
fn ensure_sorted(&mut self) -> bool {
5050
if self.need_sort {
5151
dmsort::sort_by(&mut self.messages, |a, b| {
52-
let units_a = a.effect_units().0;
53-
let units_b = b.effect_units().0;
54-
if units_a == units_b {
55-
Ordering::Equal
56-
} else if units_a < units_b {
57-
Ordering::Less
58-
} else {
59-
Ordering::Greater
60-
}
52+
let units_a = a.effect_units();
53+
let units_b = b.effect_units();
54+
units_a.cmp(&units_b)
6155
});
6256
self.need_sort = false;
6357
true

crates/notation_model/src/bar_lane.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,26 @@ impl BarLane {
7171
Arc::<Self>::new_cyclic(|weak_self| {
7272
let mut entries = Vec::new();
7373
for entry in self.entries.iter() {
74-
let merged_entry = LaneEntry {
75-
props: entry.props.clone(),
76-
lane: weak_self.clone(),
77-
model: entry.model.clone(),
78-
};
79-
entries.push(Arc::new(merged_entry));
74+
entries.push(entry.clone());
8075
}
8176
for entry in lane.entries.iter() {
77+
entries.push(entry.clone());
78+
}
79+
entries.sort_by(|a, b| {
80+
a.props.in_bar_pos.cmp(&b.props.in_bar_pos)
81+
});
82+
let entries = entries.iter().enumerate().map(|(index, entry)| {
8283
let merged_entry = LaneEntry {
8384
props: LaneEntryProps {
8485
slice: entry.props.slice.clone(),
85-
index: entries.len(),
86+
index,
8687
..entry.props
8788
},
8889
lane: weak_self.clone(),
8990
model: entry.model.clone(),
9091
};
91-
entries.push(Arc::new(merged_entry));
92-
}
92+
Arc::new(merged_entry)
93+
}).collect();
9394
Self {
9495
bar: self.bar.clone(),
9596
kind: self.kind,

0 commit comments

Comments
 (0)