Move == operators inside their adopting type

This commit is contained in:
Gwendal Roué committed 2016-09-30 06:00:50 +02:00
1 parent b01303f5ae
commit 1244b6b283
6 files changed
+118 -108

No files matched your search

+11 -11
View File
@@ -787,11 +787,11 @@ extension DatabaseFunction : Hashable {
public var hashValue: Int {
return name.hashValue ^ argumentCount.hashValue
}
}
/// Two functions are equal if they share the same name and argumentCount.
public func ==(lhs: DatabaseFunction, rhs: DatabaseFunction) -> Bool {
return lhs.name == rhs.name && lhs.argumentCount == rhs.argumentCount
/// Two functions are equal if they share the same name and argumentCount.
public static func ==(lhs: DatabaseFunction, rhs: DatabaseFunction) -> Bool {
return lhs.name == rhs.name && lhs.argumentCount == rhs.argumentCount
}
}
@@ -869,12 +869,12 @@ extension DatabaseCollation : Hashable {
// sqlite3_strnicmp SQLite function.
return 0
}
}
/// Two collations are equal if they share the same name (case insensitive)
public func ==(lhs: DatabaseCollation, rhs: DatabaseCollation) -> Bool {
// See https://www.sqlite.org/c3ref/create_collation.html
return sqlite3_stricmp(lhs.name, lhs.name) == 0
/// Two collations are equal if they share the same name (case insensitive)
public static func ==(lhs: DatabaseCollation, rhs: DatabaseCollation) -> Bool {
// See https://www.sqlite.org/c3ref/create_collation.html
return sqlite3_stricmp(lhs.name, lhs.name) == 0
}
}
+31 -22
View File
@@ -194,32 +194,41 @@ extension DatabaseValue : Hashable {
return data.hashValue
}
}
}
/// DatabaseValue adopts Equatable.
public func ==(lhs: DatabaseValue, rhs: DatabaseValue) -> Bool {
switch (lhs.storage, rhs.storage) {
case (.null, .null):
return true
case (.int64(let lhs), .int64(let rhs)):
return lhs == rhs
case (.double(let lhs), .double(let rhs)):
return lhs == rhs
case (.int64(let lhs), .double(let rhs)):
return int64EqualDouble(lhs, rhs)
case (.double(let lhs), .int64(let rhs)):
return int64EqualDouble(rhs, lhs)
case (.string(let lhs), .string(let rhs)):
return lhs == rhs
case (.blob(let lhs), .blob(let rhs)):
return lhs == rhs
default:
return false
/// Returns whether two DatabaseValues are equal.
///
/// 1.databaseValue == "foo".databaseValue // false
/// 1.databaseValue == 1.databaseValue // true
///
/// When comparing integers and doubles, the result is true if and only
/// values are equal, and if converting one type to the other does
/// not lose information:
///
/// 1.databaseValue == 1.0.databaseValue // true
public static func ==(lhs: DatabaseValue, rhs: DatabaseValue) -> Bool {
switch (lhs.storage, rhs.storage) {
case (.null, .null):
return true
case (.int64(let lhs), .int64(let rhs)):
return lhs == rhs
case (.double(let lhs), .double(let rhs)):
return lhs == rhs
case (.int64(let lhs), .double(let rhs)):
return int64EqualDouble(lhs, rhs)
case (.double(let lhs), .int64(let rhs)):
return int64EqualDouble(rhs, lhs)
case (.string(let lhs), .string(let rhs)):
return lhs == rhs
case (.blob(let lhs), .blob(let rhs)):
return lhs == rhs
default:
return false
}
}
}
/// Returns true if i and d hold exactly the same value, and if converting one
/// type into the other does not lose any information.
/// type to the other does not lose any information.
private func int64EqualDouble(_ i: Int64, _ d: Double) -> Bool {
// TODO: wait for https://github.com/apple/swift-evolution/blob/master/proposals/0080-failable-numeric-initializers.md
// Bug: https://bugs.swift.org/browse/SR-1491
+47 -47
View File
@@ -782,46 +782,47 @@ extension Row : Collection {
}
extension Row : Equatable { }
/// Returns true if and only if both rows have the same columns and values, in
/// the same order. Columns are compared in a case-sensitive way.
public func ==(lhs: Row, rhs: Row) -> Bool {
if lhs === rhs {
extension Row : Equatable {
/// Returns true if and only if both rows have the same columns and values,
/// in the same order. Columns are compared in a case-sensitive way.
public static func ==(lhs: Row, rhs: Row) -> Bool {
if lhs === rhs {
return true
}
guard lhs.count == rhs.count else {
return false
}
var liter = lhs.makeIterator()
var riter = rhs.makeIterator()
while let (lcol, lval) = liter.next(), let (rcol, rval) = riter.next() {
guard lcol == rcol else {
return false
}
guard lval == rval else {
return false
}
}
let lscopeNames = lhs.impl.scopeNames
let rscopeNames = rhs.impl.scopeNames
guard lscopeNames == rscopeNames else {
return false
}
for name in lscopeNames {
let lscope = lhs.scoped(on: name)
let rscope = rhs.scoped(on: name)
guard lscope == rscope else {
return false
}
}
return true
}
guard lhs.count == rhs.count else {
return false
}
var liter = lhs.makeIterator()
var riter = rhs.makeIterator()
while let (lcol, lval) = liter.next(), let (rcol, rval) = riter.next() {
guard lcol == rcol else {
return false
}
guard lval == rval else {
return false
}
}
let lscopeNames = lhs.impl.scopeNames
let rscopeNames = rhs.impl.scopeNames
guard lscopeNames == rscopeNames else {
return false
}
for name in lscopeNames {
let lscope = lhs.scoped(on: name)
let rscope = rhs.scoped(on: name)
guard lscope == rscope else {
return false
}
}
return true
}
/// Row adopts CustomStringConvertible.
@@ -843,15 +844,14 @@ extension Row: CustomStringConvertible {
public struct RowIndex : Comparable {
let index: Int
init(_ index: Int) { self.index = index }
}
/// Equatable implementation for RowIndex
public func ==(lhs: RowIndex, rhs: RowIndex) -> Bool {
return lhs.index == rhs.index
}
public func <(lhs: RowIndex, rhs: RowIndex) -> Bool {
return lhs.index < rhs.index
public static func ==(lhs: RowIndex, rhs: RowIndex) -> Bool {
return lhs.index == rhs.index
}
public static func <(lhs: RowIndex, rhs: RowIndex) -> Bool {
return lhs.index < rhs.index
}
}
+12 -12
View File
@@ -309,10 +309,10 @@ public struct SQLUnaryOperator : Hashable {
public var hashValue: Int {
return sql.hashValue
}
}
public func ==(lhs: SQLUnaryOperator, rhs: SQLUnaryOperator) -> Bool {
return lhs.sql == rhs.sql
public static func ==(lhs: SQLUnaryOperator, rhs: SQLUnaryOperator) -> Bool {
return lhs.sql == rhs.sql
}
}
/// This type is an implementation detail of the query interface.
@@ -397,10 +397,10 @@ public struct SQLBinaryOperator : Hashable {
}
return SQLBinaryOperator(negatedSQL, negated: sql)
}
}
public func ==(lhs: SQLBinaryOperator, rhs: SQLBinaryOperator) -> Bool {
return lhs.sql == rhs.sql
public static func ==(lhs: SQLBinaryOperator, rhs: SQLBinaryOperator) -> Bool {
return lhs.sql == rhs.sql
}
}
/// This type is an implementation detail of the query interface.
@@ -611,10 +611,10 @@ public struct SQLFunctionName : Hashable {
public var hashValue: Int {
return sql.hashValue
}
}
public func ==(lhs: SQLFunctionName, rhs: SQLFunctionName) -> Bool {
return lhs.sql == rhs.sql
public static func ==(lhs: SQLFunctionName, rhs: SQLFunctionName) -> Bool {
return lhs.sql == rhs.sql
}
}
/// This type is an implementation detail of the query interface.
+13 -13
View File
@@ -700,12 +700,12 @@ private struct InsertQuery {
extension InsertQuery : Hashable {
var hashValue: Int { return tableName.hashValue }
}
private func == (lhs: InsertQuery, rhs: InsertQuery) -> Bool {
if lhs.tableName != rhs.tableName { return false }
if lhs.onConflict != rhs.onConflict { return false }
return lhs.insertedColumns == rhs.insertedColumns
static func == (lhs: InsertQuery, rhs: InsertQuery) -> Bool {
if lhs.tableName != rhs.tableName { return false }
if lhs.onConflict != rhs.onConflict { return false }
return lhs.insertedColumns == rhs.insertedColumns
}
}
extension InsertQuery {
@@ -740,13 +740,13 @@ private struct UpdateQuery {
extension UpdateQuery : Hashable {
var hashValue: Int { return tableName.hashValue }
}
private func == (lhs: UpdateQuery, rhs: UpdateQuery) -> Bool {
if lhs.tableName != rhs.tableName { return false }
if lhs.onConflict != rhs.onConflict { return false }
if lhs.updatedColumns != rhs.updatedColumns { return false }
return lhs.conditionColumns == rhs.conditionColumns
static func == (lhs: UpdateQuery, rhs: UpdateQuery) -> Bool {
if lhs.tableName != rhs.tableName { return false }
if lhs.onConflict != rhs.onConflict { return false }
if lhs.updatedColumns != rhs.updatedColumns { return false }
return lhs.conditionColumns == rhs.conditionColumns
}
}
extension UpdateQuery {
@@ -37,9 +37,10 @@ private struct CustomStruct: DatabaseValueConvertible {
return CustomStruct(number: number)
}
}
extension CustomStruct: Equatable { }
private func ==(lhs: CustomStruct, rhs: CustomStruct) -> Bool {
return lhs.number == rhs.number
extension CustomStruct: Equatable {
static func ==(lhs: CustomStruct, rhs: CustomStruct) -> Bool {
return lhs.number == rhs.number
}
}
class FetchRequestTests: GRDBTestCase {