Diff

public struct Diff<T, C>: Collection where
T: Equatable & Hashable,
C: RangeReplaceableCollection,
C.Element: Collection,
C.Element.Element == T,
C.Index == Int,
C.Element.Index == C.Index
extension Diff: MutableCollection

Diff is a concept of a container for multiple collections and provides methods to access elements of those collections Example:

       let names = ["Adams", "Bryant", "Channing"]
       let cars = ["Ford", "Dodge"]
       let collection = Diff([names,cars])
       let subSequence = collection[1]
       print(subSequence)
       // ["Ford", "Dodge"]

       let element = collection[IndexPath(row: 0,section: 1)]
       print(element)
       // "Ford"

Contains a very powerful extension for Diffwhere C == [CollectionBin<[T]>] that provides logic for updating element and putting it in propper subcollections of type CollectionBin<[T]>

An update operation returns DiffCollectionSnapshot<T> containing modifications made to the collection and its subcollections.

Usage Example

  • Define a model

      //let's have a model like
      struct TestObject: Hashable, Equatable {
           var value: String
           var status: ObjectStatus
           var rank: Int
    
           func hash(into hasher: inout Hasher) {
               hasher.combine(value)
               hasher.combine(rank)
           }
    
          // -Note: since value and rank are a part of unique value of the object the only part that
          // cab be changed is the status
           static func == (lhs: Self, rhs: Self) -> Bool {
               lhs.status  == rhs.status
           }
      }
    
  • Define filters for sections:

       let startsARankSorted = DiffCollectionFilter<TestObject>(name: "Starts with a",
                                                                filter: { $0.value.starts(with: "a") },
                                                                sort: { $0.rank > $1.rank })

       let startedBValueSorted = DiffCollectionFilter<TestObject>(name: "Starts with b",
                                                                  filter: { $0.value.starts(with: "b") },
                                                                  sort: { $0.value > $1.value })
  • initialize Diff
       var diffCollection = [startsARankSorted, startedBValueSorted]
  • Start using by calling upade function
       let elementA = TestObject(value: "Arm",
                                 status: .new,
                                 rank: 100)
       let snapshot = diffCollection.update(with: elementA)
       debugPrint(snapshot.changes)
       // updatedIndexes = [], removedIndexes = [], addedIndexes = [IndexPath(row: 0, section: 0)]
  • Start index of the C

    Declaration

    Swift

    public var startIndex: C.Index { get }
  • End index of the C

    Declaration

    Swift

    public var endIndex: C.Index { get }
  • Declaration

    Swift

    public typealias Element = C.Element
  • Undocumented

    Declaration

    Swift

    public typealias IndexType = C.Index
  • Undocumented

    Declaration

    Swift

    public init(collection: C)
  • Geter and setter of the element specified by an IndexType.

    let names = ["Adams", "Bryant", "Channing"]
    let cars = ["Ford", "Dodge"]
    let collection = Diff([names,cars])
    let element = collection[1]
    print(element)
    // ["Ford", "Dodge"]
    

    Complexity

    O(n)

    Declaration

    Swift

    public subscript(position: IndexType) -> C.Element { get set }

    Parameters

    Index

    A index of the element(C). The index must be valid.

  • Returns the position immediately after the given index.

    The successor of an index must be well defined. For an index i into a collection c, calling c.index(after: i) returns the same index every time.

    Declaration

    Swift

    public func index(after i: IndexType) -> IndexType

    Parameters

    i

    A valid index of the collection. i must be less than endIndex.

    Return Value

    The index value immediately after i.

  • Accesses the element specified by an IndexPath.

    let names = ["Adams", "Bryant", "Channing"]
    let cars = ["Ford", "Dodge"]
    let collection = Diff([names,cars])
    let element = collection[IndexPath(row: 0,section: 1)]
    print(element)
    // "Ford"
    

    Complexity

    O(n)

    Declaration

    Swift

    public subscript(position: IndexPath) -> T? { get }

    Parameters

    Index

    A index of the element(C). The index must be valid.

  • Retuns number of elements in collection at IndexType

    Declaration

    Swift

    public func numberOfElements(at index: IndexType) -> Int

    Parameters

    index

    IndexType

    Return Value

    Int

Available where C == [CollectionBin<[T]>]