music21/sorting.rs
1//! music21's `sorting`: the rules of its `SortTuple`, the six numbers that
2//! decide which of two things comes first in a stream.
3//!
4//! The tuple itself is a Python class; what is here is what it decides
5//! that is not plain tuple order: how one compares against a bare offset,
6//! how two combine, and how one is written short.
7
8/// The tuple's fields, in music21's order, which is the order they are
9/// compared in.
10pub const SORT_TUPLE_FIELDS: [&str; 6] = [
11 "atEnd",
12 "offset",
13 "priority",
14 "classSortOrder",
15 "isNotGrace",
16 "insertIndex",
17];
18
19/// A comparison of a sort tuple against a bare offset.
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub enum Comparison {
22 /// `<`
23 Less,
24 /// `<=`
25 LessEqual,
26 /// `==`
27 Equal,
28 /// `!=`
29 NotEqual,
30 /// `>`
31 Greater,
32 /// `>=`
33 GreaterEqual,
34}
35
36/// What a tuple stored at the end of its stream answers when compared
37/// against a bare offset: it stands after every offset but infinity, and
38/// equals infinity alone.
39///
40/// ```
41/// use music21::sorting::{Comparison, at_end_compared};
42///
43/// assert!(at_end_compared(Comparison::Greater, false));
44/// assert!(at_end_compared(Comparison::Equal, true));
45/// assert!(!at_end_compared(Comparison::Less, true));
46/// ```
47pub fn at_end_compared(comparison: Comparison, other_is_infinity: bool) -> bool {
48 let equal = other_is_infinity;
49 let greater = !other_is_infinity;
50 match comparison {
51 Comparison::Less => false,
52 Comparison::LessEqual | Comparison::Equal => equal,
53 Comparison::NotEqual => !equal,
54 Comparison::Greater => greater,
55 Comparison::GreaterEqual => greater || equal,
56 }
57}
58
59/// Whether a field combines with another tuple's by the larger of the two
60/// in `add` and the smaller in `sub`, as `atEnd` and `isNotGrace` do,
61/// rather than by adding or subtracting.
62///
63/// ```
64/// use music21::sorting::is_flag_field;
65///
66/// assert!(is_flag_field("atEnd"));
67/// assert!(!is_flag_field("offset"));
68/// ```
69pub fn is_flag_field(field: &str) -> bool {
70 matches!(field, "atEnd" | "isNotGrace")
71}
72
73/// A tuple written short, each part already written as Python writes it:
74/// `1.0 <0.20.323>`, or `End <4.7.[Grace].200>` for a grace note stored at
75/// the end, whose offset is left out.
76///
77/// ```
78/// use music21::sorting::short_repr;
79///
80/// assert_eq!(short_repr(false, "1.0", "0", "20", true, "323"), "1.0 <0.20.323>");
81/// assert_eq!(short_repr(true, "1.0", "4", "7", false, "200"), "End <4.7.[Grace].200>");
82/// ```
83pub fn short_repr(
84 at_end: bool,
85 offset: &str,
86 priority: &str,
87 class_sort_order: &str,
88 not_grace: bool,
89 insert_index: &str,
90) -> String {
91 let start = if at_end { "End" } else { offset };
92 let grace = if not_grace { "" } else { ".[Grace]" };
93 format!("{start} <{priority}.{class_sort_order}{grace}.{insert_index}>")
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99
100 /// `<=` and `>=` at the end are `<` or `==` and `>` or `==`.
101 #[test]
102 fn at_end_or_equal_comparisons() {
103 assert!(at_end_compared(Comparison::LessEqual, true));
104 assert!(!at_end_compared(Comparison::LessEqual, false));
105 assert!(at_end_compared(Comparison::GreaterEqual, true));
106 assert!(at_end_compared(Comparison::GreaterEqual, false));
107 assert!(at_end_compared(Comparison::NotEqual, false));
108 assert!(!at_end_compared(Comparison::NotEqual, true));
109 }
110
111 /// A grace note not at the end keeps its offset.
112 #[test]
113 fn grace_short_repr_keeps_offset() {
114 assert_eq!(
115 short_repr(false, "2.5", "-inf", "0", false, "0"),
116 "2.5 <-inf.0.[Grace].0>"
117 );
118 }
119}