CollectionBin

public struct CollectionBin<Backstorage> : Collection where Backstorage : RangeReplaceableCollection

Represents a Facade for RangeReplaceableCollection that is a Collection itself

Provides updating method to find the element if it exists and update or remove it based on the rules provided by CollectionFilter<Element>

If Backstorage doesn’t contain the element and the element complies to the rules, updating method will append the element

Every call of updating method returns BinUpdateResult<CollectionBin> that contains info about the update operation

CollectionBin can be initialized with filter and sorting closures specifying the rules how to treat the elements.

Example

 //let's have a model  like
 struct CollectionTestObjectMock: 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: CollectionTestObjectMock, rhs: CollectionTestObjectMock) -> Bool {
          lhs.status  == rhs.status
      }
 }

Then a basic initizlization will be:

     //Will contain object with status == .cold and sorted by rank descending
     DiffCollectionFilter<CollectionTestObjectMock>(name: "Status is new or old",
                                                    filter: { $0.status == .cold},
                                                    sort: { $0.rank > $1.rank })
  • Declaration

    Swift

    public typealias Index = Backstorage.Index
  • Declaration

    Swift

    public typealias Element = Backstorage.Element
  • Start index of the Backstorage

    Declaration

    Swift

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

    Declaration

    Swift

    public var endIndex: Backstorage.Index { get }
  • Main constructor for the struct

    Declaration

    Swift

    public init(collection: Backstorage,
                filter: CollectionFilter<Element>? = nil,
                sort: CollectionSort<Element>? = nil)

    Parameters

    collection

    Any RangeReplaceableCollection

    filter

    Provides rules to insert/delete/update elements

    sort

    Provides sorting rules for injecting elements.

  • 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: Index) -> Backstorage.Index

    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 Index.

    let names = CollectionBin(["Adams", "Bryant", "Channing", "Douglas", "Evarts"])
    let name = streets[2]
    print(streetsSlice)
    // "Channing"
    

    Complexity

    O(1)

    Declaration

    Swift

    public subscript(position: Index) -> Element { get }

    Parameters

    Index

    A index of the element. The index must be valid.

  • The core function of this struct.

    Usage Example:

     //let's have a model  like
     struct CollectionTestObjectMock: Hashable, Equatable {
          let value: String
          let status: ObjectStatus
          let rank: Int
     }
    
    

    Then the call of the function might look like:

     let newElement = CollectionTestObjectMock(value: "TEST",
                                               status: .new,
                                               rank: 150)
     let result = source.updating(newElement,
                                  whereUnique: { $0.value == "TEST" },
                                  whereCompare: { $0.status == .new })
    

    NOTE: The method will try to look for an element with “TEST” value.

    • if the element exist then based on compare closure it will update an element or will do noting
    • if the element exist but doest pass filter function of the Bin, the element will be removed

    Declaration

    Swift

    public func updating(_ element: Element,
                         whereUnique unique: CollectionFilter<Element>,
                         whereCompare compare: CollectionFilter<Element>) -> BinUpdateResult<CollectionBin>

    Parameters

    element

    Element

    unique

    CollectionFilter closure that defines the uniqueness of the element

    compare

    CollectionFilter closure that defines the changes of the element

  • Recreates a new CollectionBin using the same Filter and Sorting but new collection of elelemts

    Declaration

    Swift

    public func updating(with elements: Backstorage) -> CollectionBin

    Parameters

    elements

    Collection of elements of type Backstorage

    Return Value

    CollectionBin

Available where Element: Equatable & Hashable

  • The core function of this struct.

    Note:

    • Hash value is used to idenfy the unique element
    • Conformance to Equatable is used to identify if an element has changed or not

    Usage Example:

      //let's have a model  like
       struct CollectionTestObjectMock: 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: CollectionTestObjectMock, rhs: CollectionTestObjectMock) -> Bool {
                lhs.status  == rhs.status
            }
       }
    
    

    Then the call of the function might look like:

     let newElement = CollectionTestObjectMock(value: "TEST",
                                               status: .new,
                                               rank: 150)
     let result = source.updating(newElement)
    

    NOTE: The method will try to look for an element with “TEST” value and rank 150 (since they participate in hash value)

    • if the element exists then based on Hash value it will update an element or will do noting
    • if the element exists but doest pass filter function of the Bin, the element will be removed
    • if the element exists, passed filter function but not equal to existed one, it will be updated, otherwise no operation is performed

    Declaration

    Swift

    func updating(_ element: Element) -> BinUpdateResult<CollectionBin>

    Parameters

    element

    Element as Hashable and Equatable

Available where Element: CustomStringConvertible

  • Provides convenient description for debug purposes

    Declaration

    Swift

    public var description: String { get }