Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds '~' custom operators for compile speed #348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Cartography/Compound.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,28 @@ public protocol RelativeCompoundInequality : Compound { }
@discardableResult public func >= <P: RelativeCompoundInequality>(lhs: P, rhs: Expression<P>) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients, to: rhs.value, relation: .greaterThanOrEqual)
}

// MARK: - Aliases
@discardableResult public func ~== <P: RelativeCompoundEquality>(lhs: P, rhs: Expression<P>) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients, to: rhs.value)
}

@discardableResult public func ~== <P: RelativeCompoundEquality>(lhs: P, rhs: P) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, to: rhs)
}

@discardableResult public func ~<= <P: RelativeCompoundInequality>(lhs: P, rhs: P) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, to: rhs, relation: .lessThanOrEqual)
}

@discardableResult public func ~>= <P: RelativeCompoundInequality>(lhs: P, rhs: P) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, to: rhs, relation: .greaterThanOrEqual)
}

@discardableResult public func ~<= <P: RelativeCompoundInequality>(lhs: P, rhs: Expression<P>) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients, to: rhs.value, relation: .lessThanOrEqual)
}

@discardableResult public func ~>= <P: RelativeCompoundInequality>(lhs: P, rhs: Expression<P>) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients, to: rhs.value, relation: .greaterThanOrEqual)
}
8 changes: 8 additions & 0 deletions Cartography/Priority.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ precedencegroup CarthographyPriorityPrecedence {

infix operator ~: CarthographyPriorityPrecedence

infix operator ~==: AssignmentPrecedence
infix operator ~<=: AssignmentPrecedence
infix operator ~>=: AssignmentPrecedence
infix operator ~+: AdditionPrecedence
infix operator ~-: AdditionPrecedence
infix operator ~*: MultiplicationPrecedence
infix operator ~/: MultiplicationPrecedence

/// Sets the priority for a constraint.
///
/// - parameter lhs: The constraint to update.
Expand Down
105 changes: 105 additions & 0 deletions Cartography/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,108 @@ public func / <P: Multiplication>(lhs: Expression<P>, rhs: CGFloat) -> Expressio
public func / <P: Multiplication>(lhs: P, rhs: CGFloat) -> Expression<P> {
return lhs * (1 / rhs)
}

// MARK: - Aliases
@discardableResult public func ~== (lhs: NumericalEquality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs))
}

@discardableResult public func ~== <P: RelativeEquality>(lhs: P, rhs: Expression<P>) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs.value, coefficients: rhs.coefficients[0])
}

@discardableResult public func ~== <P: RelativeEquality>(lhs: P, rhs: P) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs)
}

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

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

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: CGFloat) -> Expression<P> {
return rhs + lhs
}

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

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

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: CGFloat) -> Expression<P> {
return rhs - lhs
}

#if os(iOS) || os(tvOS)

public func ~+ (lhs: LayoutSupport, c : CGFloat) -> Expression<LayoutSupport> {
return Expression<LayoutSupport>(lhs, [Coefficients(1, c)])
}

public func ~- (lhs: LayoutSupport, c : CGFloat) -> Expression<LayoutSupport> {
return lhs + (-c)
}

#endif

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: CGFloat) -> Expression<P> {
return rhs * lhs
}

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

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

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

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

@discardableResult public func ~<= (lhs: NumericalInequality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs), relation: .lessThanOrEqual)
}

@discardableResult public func ~>= (lhs: NumericalInequality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs), relation: .greaterThanOrEqual)
}

@discardableResult public func ~<= <P: RelativeInequality>(lhs: P, rhs: P) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs, relation: .lessThanOrEqual)
}

@discardableResult public func ~>= <P: RelativeInequality>(lhs: P, rhs: P) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs, relation: .greaterThanOrEqual)
}

@discardableResult public func ~<= <P: RelativeInequality>(lhs: P, rhs: Expression<P>) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs.value, coefficients: rhs.coefficients[0], relation: .lessThanOrEqual)
}

@discardableResult public func ~>= <P: RelativeInequality>(lhs: P, rhs: Expression<P>) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs.value, coefficients: rhs.coefficients[0], relation: .greaterThanOrEqual)
}