Skip to content

Commit

Permalink
Remove Number
Browse files Browse the repository at this point in the history
  • Loading branch information
robb committed Apr 17, 2015
1 parent c64b6db commit 5d4351d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 146 deletions.
6 changes: 0 additions & 6 deletions Cartography.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
54143E4E1A93991D00208182 /* Distribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5467916E1A93962000DC9BF7 /* Distribute.swift */; };
54143E511A939D7000208182 /* DistributeSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54143E4F1A939D6000208182 /* DistributeSpec.swift */; };
54143E521A939D7100208182 /* DistributeSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54143E4F1A939D6000208182 /* DistributeSpec.swift */; };
543250CD1A8131F100BE7581 /* NumberSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 543250CB1A8112D700BE7581 /* NumberSpec.swift */; };
543250CE1A8131F200BE7581 /* NumberSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 543250CB1A8112D700BE7581 /* NumberSpec.swift */; };
5435B8BF1AD8612C00B805F1 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5435B8B41AD8610300B805F1 /* Nimble.framework */; };
5435B8C01AD8612C00B805F1 /* Quick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5435B8B51AD8610300B805F1 /* Quick.framework */; };
5435B8C11AD8613500B805F1 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5435B8B81AD8610300B805F1 /* Nimble.framework */; };
Expand Down Expand Up @@ -139,7 +137,6 @@

/* Begin PBXFileReference section */
54143E4F1A939D6000208182 /* DistributeSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DistributeSpec.swift; sourceTree = "<group>"; };
543250CB1A8112D700BE7581 /* NumberSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NumberSpec.swift; sourceTree = "<group>"; };
5435B8B41AD8610300B805F1 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Nimble.framework; sourceTree = "<group>"; };
5435B8B51AD8610300B805F1 /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Quick.framework; sourceTree = "<group>"; };
5435B8B81AD8610300B805F1 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Nimble.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -319,7 +316,6 @@
546E9E921950A87600B16707 /* EdgeSpec.swift */,
545F858E1953235F00791F75 /* EdgesSpec.swift */,
545F35831ACDFF6C00B57E98 /* MemoryLeakSpec.swift */,
543250CB1A8112D700BE7581 /* NumberSpec.swift */,
54FA3A3D1951A3750094B82A /* PointSpec.swift */,
54FA3A371950FD8E0094B82A /* PrioritySpec.swift */,
54FA3A431951A9730094B82A /* SizeSpec.swift */,
Expand Down Expand Up @@ -550,7 +546,6 @@
54FA3A3E1951A3750094B82A /* PointSpec.swift in Sources */,
545F858F1953235F00791F75 /* EdgesSpec.swift in Sources */,
54FA3A381950FD8E0094B82A /* PrioritySpec.swift in Sources */,
543250CD1A8131F100BE7581 /* NumberSpec.swift in Sources */,
546E9EA21950B33D00B16707 /* DimensionSpec.swift in Sources */,
546E9E931950A87600B16707 /* EdgeSpec.swift in Sources */,
54A3889A1A7663BF003945B7 /* ConstraintGroupSpec.swift in Sources */,
Expand Down Expand Up @@ -594,7 +589,6 @@
BBAC6D1C1A22A8F400E8A3E2 /* ViewHierarchySpec.swift in Sources */,
54BF29B41A934F250066ED10 /* AlignSpec.swift in Sources */,
54143E521A939D7100208182 /* DistributeSpec.swift in Sources */,
543250CE1A8131F200BE7581 /* NumberSpec.swift in Sources */,
54F6A860195C213F00313D24 /* PointSpec.swift in Sources */,
545F35851ACDFFAF00B57E98 /* MemoryLeakSpec.swift in Sources */,
54F6A862195C213F00313D24 /* PrioritySpec.swift in Sources */,
Expand Down
35 changes: 15 additions & 20 deletions Cartography/Coefficients.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,53 @@
import Foundation

public struct Coefficients {
var multiplier: Double = 1
var constant: Double = 0
var multiplier: CGFloat = 1
var constant: CGFloat = 0

init() { }

init(_ multiplier: Double, _ constant: Double) {
init(_ multiplier: CGFloat, _ constant: CGFloat) {
self.constant = constant
self.multiplier = multiplier
}

init(_ multiplier: Number, _ constant: Number) {
self.constant = constant.doubleValue
self.multiplier = multiplier.doubleValue
}
}

// MARK: Addition

public func + (c: Number, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier, rhs.constant + c.doubleValue)
public func + (c: CGFloat, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier, rhs.constant + c)
}

public func + (lhs: Coefficients, rhs: Number) -> Coefficients {
public func + (lhs: Coefficients, rhs: CGFloat) -> Coefficients {
return rhs + lhs
}

// MARK: Subtraction

public func - (c: Number, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier, rhs.constant - c.doubleValue)
public func - (c: CGFloat, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier, rhs.constant - c)
}

public func - (lhs: Coefficients, rhs: Number) -> Coefficients {
public func - (lhs: Coefficients, rhs: CGFloat) -> Coefficients {
return rhs - lhs
}

// MARK: Multiplication

public func * (m: Number, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier * m.doubleValue, rhs.constant * m.doubleValue)
public func * (m: CGFloat, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier * m, rhs.constant * m)
}

public func * (lhs: Coefficients, rhs: Number) -> Coefficients {
public func * (lhs: Coefficients, rhs: CGFloat) -> Coefficients {
return rhs * lhs
}

// MARK: Division

public func / (m: Number, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier / m.doubleValue, rhs.constant / m.doubleValue)
public func / (m: CGFloat, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier / m, rhs.constant / m)
}

public func / (lhs: Coefficients, rhs: Number) -> Coefficients {
public func / (lhs: Coefficients, rhs: CGFloat) -> Coefficients {
return rhs / lhs
}
6 changes: 3 additions & 3 deletions Cartography/Distribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private func reduce(first: LayoutProxy, rest: [LayoutProxy], combine: (LayoutPro
///
/// :returns: An array of `NSLayoutConstraint` instances.
///
public func distribute(by amount: Double, horizontally first: LayoutProxy, rest: LayoutProxy...) -> [NSLayoutConstraint] {
public func distribute(by amount: CGFloat, horizontally first: LayoutProxy, rest: LayoutProxy...) -> [NSLayoutConstraint] {
return reduce(first, rest) { $0.trailing == $1.leading - amount }
}

Expand All @@ -48,7 +48,7 @@ public func distribute(by amount: Double, horizontally first: LayoutProxy, rest:
///
/// :returns: An array of `NSLayoutConstraint` instances.
///
public func distribute(by amount: Double, leftToRight first: LayoutProxy, rest: LayoutProxy...) -> [NSLayoutConstraint] {
public func distribute(by amount: CGFloat, leftToRight first: LayoutProxy, rest: LayoutProxy...) -> [NSLayoutConstraint] {
return reduce(first, rest) { $0.right == $1.left - amount }
}

Expand All @@ -62,6 +62,6 @@ public func distribute(by amount: Double, leftToRight first: LayoutProxy, rest:
///
/// :returns: An array of `NSLayoutConstraint` instances.
///
public func distribute(by amount: Double, vertically first: LayoutProxy, rest: LayoutProxy...) -> [NSLayoutConstraint] {
public func distribute(by amount: CGFloat, vertically first: LayoutProxy, rest: LayoutProxy...) -> [NSLayoutConstraint] {
return reduce(first, rest) { $0.bottom == $1.top - amount }
}
14 changes: 7 additions & 7 deletions Cartography/Edges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct Edges: Compound, RelativeCompoundEquality, RelativeCompoundInequal
///
/// :returns: A new expression with the inset edges.
///
public func inset(edges: Edges, all: Number) -> Expression<Edges> {
public func inset(edges: Edges, all: CGFloat) -> Expression<Edges> {
return inset(edges, all, all, all, all)
}

Expand All @@ -43,7 +43,7 @@ public func inset(edges: Edges, all: Number) -> Expression<Edges> {
///
/// :returns: A new expression with the inset edges.
///
public func inset(edges: Edges, horizontal: Number, vertical: Number) -> Expression<Edges> {
public func inset(edges: Edges, horizontal: CGFloat, vertical: CGFloat) -> Expression<Edges> {
return inset(edges, vertical, horizontal, vertical, horizontal)
}

Expand All @@ -57,11 +57,11 @@ public func inset(edges: Edges, horizontal: Number, vertical: Number) -> Express
///
/// :returns: A new expression with the inset edges.
///
public func inset(edges: Edges, top: Number, leading: Number, bottom: Number, trailing: Number) -> Expression<Edges> {
public func inset(edges: Edges, top: CGFloat, leading: CGFloat, bottom: CGFloat, trailing: CGFloat) -> Expression<Edges> {
return Expression(edges, [
Coefficients(1, top.doubleValue),
Coefficients(1, leading.doubleValue),
Coefficients(1, -bottom.doubleValue),
Coefficients(1, -trailing.doubleValue)
Coefficients(1, top),
Coefficients(1, leading),
Coefficients(1, -bottom),
Coefficients(1, -trailing)
])
}
22 changes: 0 additions & 22 deletions Cartography/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,3 @@ internal extension Dictionary {
}
}
}

public protocol Number {
var doubleValue: Double { get }
}

extension Float: Number {
public var doubleValue: Double {
return Double(self)
}
}

extension Double: Number {
public var doubleValue: Double {
return self
}
}

extension CGFloat: Number {
public var doubleValue: Double {
return Double(self)
}
}
42 changes: 21 additions & 21 deletions Cartography/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public protocol NumericalEquality : Property { }
///
/// :returns: An `NSLayoutConstraint`.
///
public func == (lhs: NumericalEquality, rhs: Number) -> NSLayoutConstraint {
public func == (lhs: NumericalEquality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs))
}

Expand Down Expand Up @@ -76,7 +76,7 @@ public protocol NumericalInequality : Property { }
///
/// :returns: An `NSLayoutConstraint`.
///
public func <= (lhs: NumericalInequality, rhs: Number) -> NSLayoutConstraint {
public func <= (lhs: NumericalInequality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs), relation: NSLayoutRelation.LessThanOrEqual)
}

Expand All @@ -88,7 +88,7 @@ public func <= (lhs: NumericalInequality, rhs: Number) -> NSLayoutConstraint {
///
/// :returns: An `NSLayoutConstraint`.
///
public func >= (lhs: NumericalInequality, rhs: Number) -> NSLayoutConstraint {
public func >= (lhs: NumericalInequality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs), relation: NSLayoutRelation.GreaterThanOrEqual)
}

Expand Down Expand Up @@ -148,62 +148,62 @@ public func >= <P: RelativeInequality>(lhs: P, rhs: Expression<P>) -> NSLayoutCo

public protocol Addition : Property { }

public func + <P: Addition>(c: Number, rhs: P) -> Expression<P> {
public func + <P: Addition>(c: CGFloat, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(1, c) ])
}

public func + <P: Addition>(lhs: P, rhs: Number) -> Expression<P> {
public func + <P: Addition>(lhs: P, rhs: CGFloat) -> Expression<P> {
return rhs + lhs
}

public func + <P: Addition>(c: Number, rhs: Expression<P>) -> Expression<P> {
public func + <P: Addition>(c: CGFloat, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 + c })
}

public func + <P: Addition>(lhs: Expression<P>, rhs: Number) -> Expression<P> {
public func + <P: Addition>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return rhs + lhs
}

public func - <P: Addition>(c: Number, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(1, -c.doubleValue) ])
public func - <P: Addition>(c: CGFloat, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(1, -c) ])
}

public func - <P: Addition>(lhs: P, rhs: Number) -> Expression<P> {
public func - <P: Addition>(lhs: P, rhs: CGFloat) -> Expression<P> {
return rhs - lhs
}

public func - <P: Addition>(c: Number, rhs: Expression<P>) -> Expression<P> {
public func - <P: Addition>(c: CGFloat, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 - c})
}

public func - <P: Addition>(lhs: Expression<P>, rhs: Number) -> Expression<P> {
public func - <P: Addition>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return rhs - lhs
}

// MARK: Multiplication

public protocol Multiplication : Property { }

public func * <P: Multiplication>(m: Number, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 * m.doubleValue })
public func * <P: Multiplication>(m: CGFloat, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 * m })
}

public func * <P: Multiplication>(lhs: Expression<P>, rhs: Number) -> Expression<P> {
public func * <P: Multiplication>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return rhs * lhs
}

public func * <P: Multiplication>(m: Number, rhs: P) -> Expression<P> {
public func * <P: Multiplication>(m: CGFloat, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(m, 0) ])
}

public func * <P: Multiplication>(lhs: P, rhs: Number) -> Expression<P> {
public func * <P: Multiplication>(lhs: P, rhs: CGFloat) -> Expression<P> {
return rhs * lhs
}

public func / <P: Multiplication>(lhs: Expression<P>, rhs: Number) -> Expression<P> {
return lhs * (1 / rhs.doubleValue)
public func / <P: Multiplication>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return lhs * (1 / rhs)
}

public func / <P: Multiplication>(lhs: P, rhs: Number) -> Expression<P> {
return lhs * (1 / rhs.doubleValue)
public func / <P: Multiplication>(lhs: P, rhs: CGFloat) -> Expression<P> {
return lhs * (1 / rhs)
}
20 changes: 10 additions & 10 deletions Cartography/Size.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ public struct Size : Compound, RelativeCompoundEquality, RelativeCompoundInequal

// MARK: Multiplication

public func * (m: Number, rhs: Expression<Size>) -> Expression<Size> {
return Expression(rhs.value, rhs.coefficients.map { $0 * m.doubleValue })
public func * (m: CGFloat, rhs: Expression<Size>) -> Expression<Size> {
return Expression(rhs.value, rhs.coefficients.map { $0 * m })
}

public func * (lhs: Expression<Size>, rhs: Number) -> Expression<Size> {
public func * (lhs: Expression<Size>, rhs: CGFloat) -> Expression<Size> {
return rhs * lhs
}

public func * (m: Number, rhs: Size) -> Expression<Size> {
return Expression(rhs, [ Coefficients(m.doubleValue, 0), Coefficients(m.doubleValue, 0) ])
public func * (m: CGFloat, rhs: Size) -> Expression<Size> {
return Expression(rhs, [ Coefficients(m, 0), Coefficients(m, 0) ])
}

public func * (lhs: Size, rhs: Number) -> Expression<Size> {
public func * (lhs: Size, rhs: CGFloat) -> Expression<Size> {
return rhs * lhs
}

// MARK: Division

public func / (lhs: Expression<Size>, rhs: Number) -> Expression<Size> {
return lhs * (1 / rhs.doubleValue)
public func / (lhs: Expression<Size>, rhs: CGFloat) -> Expression<Size> {
return lhs * (1 / rhs)
}

public func / (lhs: Size, rhs: Number) -> Expression<Size> {
return lhs * (1 / rhs.doubleValue)
public func / (lhs: Size, rhs: CGFloat) -> Expression<Size> {
return lhs * (1 / rhs)
}
Loading

0 comments on commit 5d4351d

Please sign in to comment.