Skip to content

Commit

Permalink
Merge pull request #78 from dn-m/simplify-vertical-init
Browse files Browse the repository at this point in the history
[QoI] Simplify Beaming.Point.Vertical initialization
  • Loading branch information
jsbean authored Jul 24, 2018
2 parents b8392fe + 4da6d76 commit 65ab20c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"repositoryURL": "https://github.com/dn-m/Structure",
"state": {
"branch": null,
"revision": "95a3c7e50118006ae7d415dbe24d724936c3ad07",
"version": "0.3.1"
"revision": "dc5dbd53af383dd651a2539802b34b01279fd249",
"version": "0.4.0"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
.library(name: "StaffModel", targets: ["StaffModel"])
],
dependencies: [
.package(url: "https://github.com/dn-m/Structure", from: "0.1.3"),
.package(url: "https://github.com/dn-m/Structure", from: "0.4.0"),
.package(url: "https://github.com/dn-m/Math", from: "0.2.0"),
.package(url: "https://github.com/dn-m/Music", from: "0.1.0")
],
Expand Down
67 changes: 24 additions & 43 deletions Sources/RhythmBeamer/RhythmBeamer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,37 @@ public enum DefaultBeamer {
}
}

extension Beaming.Point.Vertical {
/// Create a `Vertical` with the given context:
///
/// - prev: Previous beaming count (if it exists, or 0 if it doesn't)
/// - cur: Current beaming count
/// - next: Next beaming count (if it exists, or 0 if it doesn't)
public init(_ prev: Int, _ cur: Int, _ next: Int) {
self = vertical(prev,cur,next)
extension Beaming {
/// Create a `Beaming` with the given amount of beams per vertical.
init(beamCounts: [Int]) {
self.init(sanitizingBeamletDirections(for: beamingVerticals(beamCounts)))
}
}

/// - Returns: An array of `BeamJunction` values for the given `leaves`.
func beamingVerticals (_ leaves: [MetricalDuration]) -> [Beaming.Point.Vertical] {
return beamingVerticals(leaves.map(beamCount))
}

/// - Returns: An array of `Point.Vertical` values for the given `counts` (amounts of beams).
func beamingVerticals (_ counts: [Int]) -> [Beaming.Point.Vertical] {
guard !counts.isEmpty else { return [] }
return zip([0] + counts.dropLast(), counts, counts.dropFirst(), fill: 0).map(vertical)
}

/// - Returns: a `Beaming.Point.Vertical` with the given context:
///
/// - prev: Previous beaming count (if it exists, or 0 if it doesn't)
/// - cur: Current beaming count
/// - next: Next beaming count (if it exists, or 0 if it doesn't)
///
/// - Note: This is wrapped up in its own function to avoid having to disambiquate `min` and `max`
/// for `Sequence` types and the free functions. Otherwise, `Swift.` must be prepended to each
/// instance, which is pretty gross.
private func vertical(_ prev: Int, _ cur: Int, _ next: Int) -> Beaming.Point.Vertical {
func vertical(_ prev: Int, _ cur: Int, _ next: Int) -> Beaming.Point.Vertical {
return .init(
maintain: min(prev,cur,next),
startOrStop: .init(start: max(0,min(cur,next)-prev), stop: max(0,min(cur,prev)-next)),
beamlets: max(0,cur-max(prev,next))
)
}

extension Beaming.Point.StartOrStop {
/// Create a `StartOrStop` with the given amounts. You can't start and stop in the same
/// `vertical`, so don't.
init(start: Int, stop: Int) {
self = start > 0 ? .start(count: start) : stop > 0 ? .stop(count: stop) : .none
}
}

extension Beaming {
/// Create a `Beaming` with the given amount of beams per vertical.
init(beamCounts: [Int]) {
self.init(sanitizingBeamletDirections(for: beamingVerticals(beamCounts)))
}
}

/// - Returns: An array of `Point.Vertical` values for the given `counts` (amounts of beams).
func beamingVerticals (_ counts: [Int]) -> [Beaming.Point.Vertical] {
return counts.indices.map { index in
let prev = counts[safe: index - 1] ?? 0
let cur = counts[index]
let next = counts[safe: index + 1] ?? 0
return Beaming.Point.Vertical(prev,cur,next)
}
}

/// - Returns: An array of `BeamJunction` values for the given `leaves`.
func beamingVerticals (_ leaves: [MetricalDuration]) -> [Beaming.Point.Vertical] {
return beamingVerticals(leaves.map(beamCount))
}

/// - Returns: Amount of beams needed to represent the given `duration`.
func beamCount(_ duration: MetricalDuration) -> Int {
let reduced = duration.reduced
Expand All @@ -93,3 +66,11 @@ func beamCount(_ duration: MetricalDuration) -> Int {
}
fatalError("\(duration) is not representable with beams")
}

extension Beaming.Point.StartOrStop {
/// Create a `StartOrStop` with the given amounts. You can't start and stop in the same
/// `vertical`, so don't.
init(start: Int, stop: Int) {
self = start > 0 ? .start(count: start) : stop > 0 ? .stop(count: stop) : .none
}
}
11 changes: 1 addition & 10 deletions Sources/SpelledRhythm/Beaming.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ extension Beaming.Point {
extension Beaming.Point {

/// Rhythm.Beaming.Point.Vertical.
public struct Vertical {
public struct Vertical: Equatable {

/// - Returns: The `Point` values contained herein.
public var points: [Beaming.Point] {
Expand Down Expand Up @@ -366,15 +366,6 @@ extension Beaming.Point {
}
}

extension Beaming.Point.Vertical: Equatable {

// MARK: - Equatable

public static func == (lhs: Beaming.Point.Vertical, rhs: Beaming.Point.Vertical) -> Bool {
return lhs.points == rhs.points
}
}

extension Beaming.Point.Vertical: CollectionWrapping {
public var base: [Beaming.Point] {
return points
Expand Down

0 comments on commit 65ab20c

Please sign in to comment.