Skip to content

Commit

Permalink
Merge pull request #129 from dn-m/equatable-derive
Browse files Browse the repository at this point in the history
Let Swift derive Equatable conformance
  • Loading branch information
jsbean authored Aug 7, 2018
2 parents 3a2c3d1 + 6bed532 commit fb666ef
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 99 deletions.
4 changes: 1 addition & 3 deletions Sources/DataStructures/CircularArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ extension CircularArray: RangeReplaceableCollection {
}
}

extension CircularArray: Equatable where Element: Equatable {
// MARK: - Equatable
}
extension CircularArray: Equatable where Element: Equatable { }

extension CircularArray: ExpressibleByArrayLiteral {

Expand Down
5 changes: 1 addition & 4 deletions Sources/DataStructures/DictionaryProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,4 @@ extension DictionaryProtocol where
}
}

extension Dictionary: DictionaryProtocol {

// MARK: - `DictionaryProtocol`
}
extension Dictionary: DictionaryProtocol { }
4 changes: 1 addition & 3 deletions Sources/DataStructures/Either.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@ public enum Either <Left, Right> {
}
}

extension Either: Equatable where Left: Equatable, Right: Equatable {
// MARK: - Equatable
}
extension Either: Equatable where Left: Equatable, Right: Equatable { }
16 changes: 1 addition & 15 deletions Sources/DataStructures/LinkedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,7 @@ extension LinkedList: Collection {
}
}

extension LinkedList: Equatable where Element: Equatable {

/// - returns: `true` if two `LinkedList` values are equivalent. Otherwise `false`.
public static func == (lhs: LinkedList, rhs: LinkedList) -> Bool {
switch (lhs, rhs) {
case (.end, .end):
return true
case let (.node(elementA, nextA), .node(elementB, nextB)):
return elementA == elementB && nextA == nextB
default:
return false
}
}

}
extension LinkedList: Equatable where Element: Equatable { }

extension LinkedList: ExpressibleByArrayLiteral {

Expand Down
36 changes: 1 addition & 35 deletions Sources/DataStructures/OrderedDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,7 @@ extension OrderedDictionary: Collection {
}
}

extension OrderedDictionary: Equatable where Value: Equatable {

/// - returns: `true` if all values contained in both `OrderedDictionary` values are
/// equivalent. Otherwise, `false`.
public static func == (lhs: OrderedDictionary, rhs: OrderedDictionary) -> Bool {

guard lhs.keys == rhs.keys else {
return false
}

for key in lhs.keys {

if rhs.values[key] == nil || rhs.values[key]! != lhs.values[key]! {
return false
}
}

for key in rhs.keys {

if lhs.values[key] == nil || lhs.values[key]! != rhs.values[key]! {
return false
}
}

return true
}
}
extension OrderedDictionary: Equatable where Value: Equatable { }

extension OrderedDictionary: ExpressibleByDictionaryLiteral {

Expand All @@ -153,11 +127,3 @@ extension OrderedDictionary: ExpressibleByDictionaryLiteral {
}
}
}


//public func == <K, V: Equatable> (lhs: OrderedDictionary<K,V>, rhs: OrderedDictionary<K,V>)
// -> Bool
//{
//
//
//}
14 changes: 4 additions & 10 deletions Sources/DataStructures/SortedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public struct SortedArray <Element: Comparable>:
SortedCollectionWrapping
{

// MARK: - Instance Properties

/// Underlying storage of elements contained herein.
public var base: [Element] = []

Expand Down Expand Up @@ -78,6 +80,8 @@ public struct SortedArray <Element: Comparable>:
}
}

extension SortedArray: Equatable { }

extension SortedArray {

/// - Returns: The slice of the `SortedArray` for the given `bounds`.
Expand All @@ -86,16 +90,6 @@ extension SortedArray {
}
}

extension SortedArray: Equatable {

// MARK: - Equatable

/// - returns: `true` if all elements in both arrays are equivalent. Otherwise, `false`.
public static func == <T> (lhs: SortedArray<T>, rhs: SortedArray<T>) -> Bool {
return lhs.base == rhs.base
}
}

extension SortedArray: Additive {

// MARK: - Additive
Expand Down
33 changes: 4 additions & 29 deletions Sources/DataStructures/SortedDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public struct SortedDictionary<Key, Value>: DictionaryProtocol where Key: Hashab
}
}

extension SortedDictionary: Equatable where Value: Equatable { }

extension SortedDictionary: Collection {

// MARK: - `Collection`
// MARK: - Collection

/// Index after the given `index`.
public func index(after index: Int) -> Int {
Expand Down Expand Up @@ -138,7 +140,7 @@ extension SortedDictionary {

extension SortedDictionary: ExpressibleByDictionaryLiteral {

// MARK: - `ExpressibleByDictionaryLiteral`
// MARK: - ExpressibleByDictionaryLiteral

/// Create a `SortedDictionary` with a `DictionaryLiteral`.
public init(dictionaryLiteral elements: (Key, Value)...) {
Expand All @@ -151,33 +153,6 @@ extension SortedDictionary: ExpressibleByDictionaryLiteral {
}
}

/// - returns: `true` if all values contained in both `SortedDictionary` values are
/// equivalent. Otherwise, `false`.
public func == <K, V: Equatable> (lhs: SortedDictionary<K,V>, rhs: SortedDictionary<K,V>)
-> Bool
{

guard lhs.keys == rhs.keys else {
return false
}

for key in lhs.keys {

if rhs.unsorted[key] == nil || rhs.unsorted[key]! != lhs.unsorted[key]! {
return false
}
}

for key in rhs.keys {

if lhs.unsorted[key] == nil || lhs.unsorted[key]! != rhs.unsorted[key]! {
return false
}
}

return true
}

/// - returns: `SortedOrderedDictionary` with values of two `SortedOrderedDictionary` values.
public func + <Value, Key> (
lhs: SortedDictionary<Value, Key>,
Expand Down

0 comments on commit fb666ef

Please sign in to comment.