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

TypedComparator #491

Merged
merged 2 commits into from
Sep 6, 2024
Merged
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
24 changes: 12 additions & 12 deletions Sources/Verge/Store/Changes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,15 @@ extension Changes {
@inline(__always)
public func takeIfChanged<Composed>(
_ compose: (Value) throws -> Composed,
_ comparer: some Comparison<Composed>
_ comparer: some TypedComparator<Composed>
) rethrows -> Composed? {
try _takeIfChanged(compose, comparer)
}

@inline(__always)
fileprivate func _takeIfChanged<each Element>(
_ compose: (Value) throws -> (repeat each Element),
_ comparer: consuming some Comparison<(repeat each Element)>
_ comparer: consuming some TypedComparator<(repeat each Element)>
) rethrows -> (repeat each Element)? {

let current = self.primitive
Expand Down Expand Up @@ -356,7 +356,7 @@ extension Changes {
@inline(__always)
fileprivate func _takeIfChanged_packed_nonEquatable<each Element>(
_ compose: (Value) throws -> (repeat each Element),
comparator: some Comparison<(repeat each Element)>
comparator: some TypedComparator<(repeat each Element)>
) rethrows -> (repeat each Element)? {

let current = self.primitive
Expand Down Expand Up @@ -391,7 +391,7 @@ extension Changes {
@inline(__always)
public func ifChanged<Composed, Result>(
_ compose: (Value) -> Composed,
_ comparer: some Comparison<Composed>,
_ comparer: some TypedComparator<Composed>,
_ perform: (Composed) throws -> Result
) rethrows -> Result? {
guard let result = takeIfChanged(compose, comparer) else {
Expand Down Expand Up @@ -419,7 +419,7 @@ extension Changes {
@available(*, deprecated, message: "Use another function that returns IfChangedBox")
public func ifChanged<T, Result>(
_ selector: ChangesKeyPath<T>,
_ comparer: some Comparison<T>,
_ comparer: some TypedComparator<T>,
_ perform: (T) throws -> Result
) rethrows -> Result? {
guard let value = takeIfChanged({ $0[keyPath: selector] }, comparer) else {
Expand Down Expand Up @@ -459,7 +459,7 @@ extension Changes {

public borrowing func ifChanged<each Element>(
_ compose: (borrowing Value) -> (repeat each Element),
comparator: some Comparison<(repeat each Element)>
comparator: some TypedComparator<(repeat each Element)>
) -> IfChangedBox<(repeat each Element)> {
guard let result = _takeIfChanged_packed_nonEquatable(compose, comparator: comparator) else {
return .init()
Expand Down Expand Up @@ -611,14 +611,14 @@ extension Changes {
/// Returns boolean that indicates value specified by keyPath contains changes with compared old and new.
@inline(__always)
public func hasChanges<T: Equatable>(_ keyPath: ChangesKeyPath<T>) -> Bool {
hasChanges(keyPath, EqualityComparison())
hasChanges(keyPath, EqualityComparator())
}

/// Returns boolean that indicates value specified by keyPath contains changes with compared old and new.
@inline(__always)
public func hasChanges<T>(
_ keyPath: ChangesKeyPath<T>,
_ comparer: some Comparison<T>
_ comparer: some TypedComparator<T>
) -> Bool {
hasChanges({ $0[keyPath: keyPath] }, comparer)
}
Expand All @@ -627,13 +627,13 @@ extension Changes {
public func hasChanges<Composed: Equatable>(
_ compose: (Value) -> Composed
) -> Bool {
hasChanges(compose, EqualityComparison())
hasChanges(compose, EqualityComparator())
}

@inline(__always)
public func hasChanges<Composed>(
_ compose: (Value) -> Composed,
_ comparer: some Comparison<Composed>
_ comparer: some TypedComparator<Composed>
) -> Bool {
takeIfChanged(compose, comparer) != nil
}
Expand All @@ -646,12 +646,12 @@ extension Changes {
/// Returns boolean that indicates value specified by keyPath contains **NO** changes with compared old and new.
@inline(__always)
public func noChanges<T: Equatable>(_ keyPath: ChangesKeyPath<T>) -> Bool {
!hasChanges(keyPath, EqualityComparison())
!hasChanges(keyPath, EqualityComparator())
}

/// Returns boolean that indicates value specified by keyPath contains **NO** changes with compared old and new.
@inline(__always)
public func noChanges<T>(_ keyPath: ChangesKeyPath<T>, _ comparer: some Comparison<T>) -> Bool {
public func noChanges<T>(_ keyPath: ChangesKeyPath<T>, _ comparer: some TypedComparator<T>) -> Bool {
!hasChanges(keyPath, comparer)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Verge/Utility/Edge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ extension Edge where Value : Equatable {

extension Edge {

public struct VersionComparison: Comparison {
public struct VersionComparison: TypedComparator {

public func callAsFunction(_ lhs: Edge, _ rhs: Edge) -> Bool {
lhs.globalID == rhs.globalID && lhs.version == rhs.version
Expand All @@ -237,7 +237,7 @@ extension Edge {

}

extension Comparison {
extension TypedComparator {

public static func versionEquals<T>() -> Self where Self == Edge<T>.VersionComparison {
.init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import Foundation
import os.log

// TODO: will rename as Comparator
public protocol Comparison<Input>: Sendable {
public protocol TypedComparator<Input>: Sendable {
associatedtype Input

@Sendable
Expand All @@ -32,57 +31,57 @@ public protocol Comparison<Input>: Sendable {

struct NotEqual: Error {}

extension Comparison {
extension TypedComparator {

public static func equality<T>() -> Self where Self == EqualityComparison<T> {
public static func equality<T>() -> Self where Self == EqualityComparator<T> {
.init()
}

/**
TODO: Use typed comparison instead of AnyEqualityComparison.
*/
public static func equality<each T: Equatable>() -> Self where Self == AnyEqualityComparison<(repeat each T)> {
public static func equality<each T: Equatable>() -> Self where Self == AnyEqualityComparator<(repeat each T)> {
return .init { a, b in
areEqual((repeat each a), (repeat each b))
}
}

public static func any<T>(_ isEqual: @escaping @Sendable (T, T) -> Bool) -> Self where Self == AnyEqualityComparison<T> {
public static func any<T>(_ isEqual: @escaping @Sendable (T, T) -> Bool) -> Self where Self == AnyEqualityComparator<T> {
.init(isEqual)
}

public static func any<T, U: Equatable>(selector: @escaping @Sendable (T) -> U) -> Self where Self == AnyEqualityComparison<T> {
public static func any<T, U: Equatable>(selector: @escaping @Sendable (T) -> U) -> Self where Self == AnyEqualityComparator<T> {
.init {
selector($0) == selector($1)
}
}

public static func alwaysFalse<T>() -> Self where Self == FalseComparison<T> {
public static func alwaysFalse<T>() -> Self where Self == FalseComparator<T> {
.init()
}

}

extension Comparison {
extension TypedComparator {

public func and<C: Comparison>(_ otherExpression: C) -> AndComparison<Input, Self, C> {
public func and<C: TypedComparator>(_ otherExpression: C) -> AndComparator<Input, Self, C> {
.init(self, otherExpression)
}

public func or<C: Comparison>(_ otherExpression: C) -> OrComparison<Input, Self, C> {
public func or<C: TypedComparator>(_ otherExpression: C) -> OrComparator<Input, Self, C> {
.init(self, otherExpression)
}
}

public struct FalseComparison<Input>: Comparison {
public struct FalseComparator<Input>: TypedComparator {
public init() {}

public func callAsFunction(_ lhs: Input, _ rhs: Input) -> Bool {
return false
}
}

public struct EqualityComparison<Input: Equatable>: Comparison {
public struct EqualityComparator<Input: Equatable>: TypedComparator {

public init() {}

Expand All @@ -93,7 +92,7 @@ public struct EqualityComparison<Input: Equatable>: Comparison {

}

public struct AnyEqualityComparison<Input>: Comparison {
public struct AnyEqualityComparator<Input>: TypedComparator {

private let closure: @Sendable (Input, Input) -> Bool

Expand All @@ -107,7 +106,7 @@ public struct AnyEqualityComparison<Input>: Comparison {

}

public struct AndComparison<Input, C1: Comparison, C2: Comparison>: Comparison where C1.Input == Input, C2.Input == Input {
public struct AndComparator<Input, C1: TypedComparator, C2: TypedComparator>: TypedComparator where C1.Input == Input, C2.Input == Input {

public let c1: C1
public let c2: C2
Expand All @@ -122,7 +121,7 @@ public struct AndComparison<Input, C1: Comparison, C2: Comparison>: Comparison w
}
}

public struct OrComparison<Input, C1: Comparison, C2: Comparison>: Comparison where C1.Input == Input, C2.Input == Input {
public struct OrComparator<Input, C1: TypedComparator, C2: TypedComparator>: TypedComparator where C1.Input == Input, C2.Input == Input {
public let c1: C1
public let c2: C2

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import VergeComparator

public enum NormalizedStorageComparisons<Storage: NormalizedStorageType> {
public enum NormalizedStorageComparators<Storage: NormalizedStorageType> {

/// True indicates database is not changed
public struct StorageComparison: Comparison {
public struct StorageComparator: TypedComparator {
public typealias Input = Storage

public init() {}
Expand All @@ -14,7 +14,7 @@ public enum NormalizedStorageComparisons<Storage: NormalizedStorageType> {
}

/// Returns true if the table of the entity in database has no changes.
public struct TableComparison<Table: TableType>: Comparison {
public struct TableComparator<Table: TableType>: TypedComparator {

public typealias Input = Table

Expand Down
8 changes: 4 additions & 4 deletions Sources/VergeNormalizationDerived/DispatcherType+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,11 @@ where _StorageSelector.Storage == _TableSelector.Storage {
return .new(yield(input))
}

if NormalizedStorageComparisons<Storage>.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) {
if NormalizedStorageComparators<Storage>.StorageComparator()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) {
return .noUpdates
}

if NormalizedStorageComparisons<Storage>.TableComparison<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) {
if NormalizedStorageComparators<Storage>.TableComparator<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) {
return .noUpdates
}

Expand Down Expand Up @@ -418,11 +418,11 @@ where _StorageSelector.Storage == _TableSelector.Storage {
return .new(yield(input))
}

if NormalizedStorageComparisons<Storage>.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) {
if NormalizedStorageComparators<Storage>.StorageComparator()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) {
return .noUpdates
}

if NormalizedStorageComparisons<Storage>.TableComparison<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) {
if NormalizedStorageComparators<Storage>.TableComparator<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) {
return .noUpdates
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/VergeNormalizationDerived/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct QueryPipeline<
}

// check if the storage has been updated
if NormalizedStorageComparisons<Storage>.StorageComparison()(storageSelector.select(source: input.primitive), storageSelector.select(source: previous.primitive)) {
if NormalizedStorageComparators<Storage>.StorageComparator()(storageSelector.select(source: input.primitive), storageSelector.select(source: previous.primitive)) {
return .noUpdates
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/VergeRx/Store+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ extension ObservableType where Element : ChangesType {
/// - selector:
/// - compare:
/// - Returns: Returns an observable sequence that contains only changed elements according to the `comparer`.
public func changed<S>(_ selector: @escaping (Element.Value) -> S, _ comparer: some Comparison<S>) -> Observable<S> {
public func changed<S>(_ selector: @escaping (Element.Value) -> S, _ comparer: some TypedComparator<S>) -> Observable<S> {

return flatMap { changes -> Observable<S> in
let _r = changes.asChanges().ifChanged(selector, comparer) { value in
return value
Expand All @@ -138,7 +138,7 @@ extension ObservableType where Element : ChangesType {
/// - comparer:
/// - Returns: Returns an observable sequence that contains only changed elements according to the `comparer`.
public func changed<S : Equatable>(_ selector: @escaping (Element.Value) -> S) -> Observable<S> {
return changed(selector, EqualityComparison())
return changed(selector, EqualityComparator())
}

/// Returns an observable sequence that contains only changed elements according to the `comparer`.
Expand All @@ -149,7 +149,7 @@ extension ObservableType where Element : ChangesType {
/// - selector:
/// - compare:
/// - Returns: Returns an observable sequence that contains only changed elements according to the `comparer`.
public func changedDriver<S>(_ selector: @escaping (Element.Value) -> S, _ comparer: some Comparison<S>) -> Driver<S> {
public func changedDriver<S>(_ selector: @escaping (Element.Value) -> S, _ comparer: some TypedComparator<S>) -> Driver<S> {
changed(selector, comparer)
.asDriver(onErrorRecover: { _ in .empty() })
}
Expand All @@ -163,7 +163,7 @@ extension ObservableType where Element : ChangesType {
/// - comparer:
/// - Returns: Returns an observable sequence that contains only changed elements according to the `comparer`.
public func changedDriver<S : Equatable>(_ selector: @escaping (Element.Value) -> S) -> Driver<S> {
return changedDriver(selector, EqualityComparison())
return changedDriver(selector, EqualityComparator())
}

}
Expand Down
2 changes: 1 addition & 1 deletion Tests/VergeTests/ComparerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ComparerTests: XCTestCase {

func testPerfomance_new() {

let base = EqualityComparison<String>()
let base = EqualityComparator<String>()

measure(metrics: [XCTMemoryMetric(), XCTCPUMetric()]) {
for _ in 0..<10000 {
Expand Down
4 changes: 2 additions & 2 deletions Tests/VergeTests/FilterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class FilterTests: XCTestCase {
var c = 0
}

let comparison = OrComparison<Model, _, _>(
let comparison = OrComparator<Model, _, _>(
.any { @Sendable in $0.a == 1 && $1.a == 1 },
.any { @Sendable in $0.b == 1 && $1.b == 1 }
)
Expand Down Expand Up @@ -60,7 +60,7 @@ final class FilterTests: XCTestCase {
var c = 0
}

let comparison = AndComparison<Model, _, _>(
let comparison = AndComparator<Model, _, _>(
.any { @Sendable in $0.a == 1 && $1.a == 1 },
.any { @Sendable in $0.b == 1 && $1.b == 1 }
)
Expand Down
Loading