Skip to content

Commit

Permalink
bugfix with merge lane not sorting entries
Browse files Browse the repository at this point in the history
  • Loading branch information
yjpark committed Jan 27, 2022
1 parent c528353 commit a915202
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
14 changes: 14 additions & 0 deletions crates/notation_core/src/duration.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::Ordering;
use std::fmt::Display;
use std::ops::{Add, Sub};

Expand Down Expand Up @@ -116,6 +117,19 @@ impl Sub for Units {
Units(self.0 - rhs.0)
}
}

impl Units {
pub fn cmp(&self, other: &Self) -> std::cmp::Ordering {
if self.0 == other.0 {
Ordering::Equal
} else if self.0 < other.0 {
Ordering::Less
} else {
Ordering::Greater
}
}
}

// https://hellomusictheory.com/learn/tuplets/
#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum Duration {
Expand Down
12 changes: 3 additions & 9 deletions crates/notation_midi/src/midi_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,9 @@ impl MidiChannel {
fn ensure_sorted(&mut self) -> bool {
if self.need_sort {
dmsort::sort_by(&mut self.messages, |a, b| {
let units_a = a.effect_units().0;
let units_b = b.effect_units().0;
if units_a == units_b {
Ordering::Equal
} else if units_a < units_b {
Ordering::Less
} else {
Ordering::Greater
}
let units_a = a.effect_units();
let units_b = b.effect_units();
units_a.cmp(&units_b)
});
self.need_sort = false;
true
Expand Down
19 changes: 10 additions & 9 deletions crates/notation_model/src/bar_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,26 @@ impl BarLane {
Arc::<Self>::new_cyclic(|weak_self| {
let mut entries = Vec::new();
for entry in self.entries.iter() {
let merged_entry = LaneEntry {
props: entry.props.clone(),
lane: weak_self.clone(),
model: entry.model.clone(),
};
entries.push(Arc::new(merged_entry));
entries.push(entry.clone());
}
for entry in lane.entries.iter() {
entries.push(entry.clone());
}
entries.sort_by(|a, b| {
a.props.in_bar_pos.cmp(&b.props.in_bar_pos)
});
let entries = entries.iter().enumerate().map(|(index, entry)| {
let merged_entry = LaneEntry {
props: LaneEntryProps {
slice: entry.props.slice.clone(),
index: entries.len(),
index,
..entry.props
},
lane: weak_self.clone(),
model: entry.model.clone(),
};
entries.push(Arc::new(merged_entry));
}
Arc::new(merged_entry)
}).collect();
Self {
bar: self.bar.clone(),
kind: self.kind,
Expand Down

0 comments on commit a915202

Please sign in to comment.