mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
202 lines
6.3 KiB
Swift
202 lines
6.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Generate comparison functions for tuples up to some reasonable arity.
|
|
|
|
%{
|
|
|
|
comparableOperators = [
|
|
('<', 'before'),
|
|
('<=', 'before or the same as'),
|
|
('>', 'after'),
|
|
('>=', 'after or the same as')
|
|
]
|
|
|
|
}%
|
|
|
|
/// Returns a Boolean value indicating whether the corresponding components of
|
|
/// two tuples are equal.
|
|
///
|
|
/// All arity zero tuples are equal.
|
|
///
|
|
/// - Parameters:
|
|
/// - lhs: An empty tuple.
|
|
/// - rhs: An empty tuple.
|
|
@inlinable // trivial-implementation
|
|
public func ==(lhs: (), rhs: ()) -> Bool {
|
|
return true
|
|
}
|
|
|
|
/// Returns a Boolean value indicating whether any corresponding components of
|
|
/// the two tuples are not equal.
|
|
///
|
|
/// All arity zero tuples are equal.
|
|
///
|
|
/// - Parameters:
|
|
/// - lhs: An empty tuple.
|
|
/// - rhs: An empty tuple.
|
|
@inlinable // trivial-implementation
|
|
public func !=(lhs: (), rhs: ()) -> Bool {
|
|
return false
|
|
}
|
|
|
|
/// Returns a Boolean value indicating whether the first tuple is ordered
|
|
/// before the second in a lexicographical ordering.
|
|
///
|
|
/// An arity zero tuple is never strictly before another arity zero tuple in a
|
|
/// lexicographical ordering.
|
|
///
|
|
/// - Parameters:
|
|
/// - lhs: An empty tuple.
|
|
/// - rhs: An empty tuple.
|
|
@inlinable // trivial-implementation
|
|
public func <(lhs: (), rhs: ()) -> Bool {
|
|
return false
|
|
}
|
|
|
|
/// Returns a Boolean value indicating whether the first tuple is ordered
|
|
/// before or the same as the second in a lexicographical ordering.
|
|
///
|
|
/// An arity zero tuple is always before or the same as another arity zero tuple
|
|
/// in a lexicographical ordering.
|
|
///
|
|
/// - Parameters:
|
|
/// - lhs: An empty tuple.
|
|
/// - rhs: An empty tuple.
|
|
@inlinable // trivial-implementation
|
|
public func <=(lhs: (), rhs: ()) -> Bool {
|
|
return true
|
|
}
|
|
|
|
/// Returns a Boolean value indicating whether the first tuple is ordered
|
|
/// after the second in a lexicographical ordering.
|
|
///
|
|
/// An arity zero tuple is never strictly after another arity zero tuple in a
|
|
/// lexicographical ordering.
|
|
///
|
|
/// - Parameters:
|
|
/// - lhs: An empty tuple.
|
|
/// - rhs: An empty tuple.
|
|
@inlinable // trivial-implementation
|
|
public func >(lhs: (), rhs: ()) -> Bool {
|
|
return false
|
|
}
|
|
|
|
/// Returns a Boolean value indicating whether the first tuple is ordered
|
|
/// after or the same as the second in a lexicographical ordering.
|
|
///
|
|
/// An arity zero tuple is always after or the same as another arity zero tuple
|
|
/// in a lexicographical ordering.
|
|
///
|
|
/// - Parameters:
|
|
/// - lhs: An empty tuple.
|
|
/// - rhs: An empty tuple.
|
|
@inlinable // trivial-implementation
|
|
public func >=(lhs: (), rhs: ()) -> Bool {
|
|
return true
|
|
}
|
|
|
|
% for arity in range(2,7):
|
|
% typeParams = [chr(ord("A") + i) for i in range(arity)]
|
|
% tupleT = "({})".format(",".join(typeParams))
|
|
% equatableTypeParams = ", ".join(["{}: Equatable".format(c) for c in typeParams])
|
|
|
|
% originalTuple = "(\"a\", {})".format(", ".join(map(str, range(1, arity))))
|
|
% greaterTuple = "(\"a\", {})".format(", ".join(map(str, list(range(1, arity - 1)) + [arity])))
|
|
|
|
/// Returns a Boolean value indicating whether the corresponding components of
|
|
/// two tuples are equal.
|
|
///
|
|
/// For two tuples to compare as equal, each corresponding pair of components
|
|
/// must be equal. The following example compares tuples made up of ${arity}
|
|
/// components:
|
|
///
|
|
/// let a = ${originalTuple}
|
|
/// let b = ${originalTuple}
|
|
/// print(a == b)
|
|
/// // Prints "true"
|
|
///
|
|
/// let c = ${greaterTuple}
|
|
/// print(a == c)
|
|
/// // Prints "false"
|
|
///
|
|
/// - Parameters:
|
|
/// - lhs: A tuple of `Equatable` elements.
|
|
/// - rhs: Another tuple of elements of the same type as `lhs`.
|
|
@inlinable // trivial-implementation
|
|
public func == <${equatableTypeParams}>(lhs: ${tupleT}, rhs: ${tupleT}) -> Bool {
|
|
guard lhs.0 == rhs.0 else { return false }
|
|
/*tail*/ return (
|
|
${", ".join("lhs.{}".format(i) for i in range(1, arity))}
|
|
) == (
|
|
${", ".join("rhs.{}".format(i) for i in range(1, arity))}
|
|
)
|
|
}
|
|
|
|
/// Returns a Boolean value indicating whether any corresponding components of
|
|
/// the two tuples are not equal.
|
|
///
|
|
/// For two tuples to compare as equal, each corresponding pair of components
|
|
/// must be equal. The following example compares tuples made up of ${arity}
|
|
/// components:
|
|
///
|
|
/// let a = ${originalTuple}
|
|
/// let b = ${originalTuple}
|
|
/// print(a != b)
|
|
/// // Prints "false"
|
|
///
|
|
/// let c = ${greaterTuple}
|
|
/// print(a != c)
|
|
/// // Prints "true"
|
|
///
|
|
/// - Parameters:
|
|
/// - lhs: A tuple of `Equatable` elements.
|
|
/// - rhs: Another tuple of elements of the same type as `lhs`.
|
|
@inlinable // trivial-implementation
|
|
public func != <${equatableTypeParams}>(lhs: ${tupleT}, rhs: ${tupleT}) -> Bool {
|
|
guard lhs.0 == rhs.0 else { return true }
|
|
/*tail*/ return (
|
|
${", ".join("lhs.{}".format(i) for i in range(1, arity))}
|
|
) != (
|
|
${", ".join("rhs.{}".format(i) for i in range(1, arity))}
|
|
)
|
|
}
|
|
|
|
% comparableTypeParams = ", ".join(["{}: Comparable".format(c) for c in typeParams])
|
|
% for op, phrase in comparableOperators:
|
|
/// Returns a Boolean value indicating whether the first tuple is ordered
|
|
/// ${phrase} the second in a lexicographical ordering.
|
|
///
|
|
/// Given two tuples `(a1, a2, ..., aN)` and `(b1, b2, ..., bN)`, the first
|
|
/// tuple is ${phrase} the second tuple if and only if
|
|
/// `a1 ${op.replace('=', '')} b1` or (`a1 == b1` and
|
|
/// `(a2, ..., aN) ${op} (b2, ..., bN)`).
|
|
///
|
|
/// - Parameters:
|
|
/// - lhs: A tuple of `Comparable` elements.
|
|
/// - rhs: Another tuple of elements of the same type as `lhs`.
|
|
@inlinable // trivial-implementation
|
|
public func ${op} <${comparableTypeParams}>(lhs: ${tupleT}, rhs: ${tupleT}) -> Bool {
|
|
if lhs.0 != rhs.0 { return lhs.0 ${op} rhs.0 }
|
|
/*tail*/ return (
|
|
${", ".join("lhs.{}".format(i) for i in range(1, arity))}
|
|
) ${op} (
|
|
${", ".join("rhs.{}".format(i) for i in range(1, arity))}
|
|
)
|
|
}
|
|
% end
|
|
% end
|
|
|
|
// ${'Local Variables'}:
|
|
// eval: (read-only-mode 1)
|
|
// End:
|