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
BackstorageDeclaration
Swift
public var startIndex: Backstorage.Index { get } -
End index of the
BackstorageDeclaration
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
collectionAny
RangeReplaceableCollectionfilterProvides rules to insert/delete/update elements
sortProvides 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
iinto a collectionc, callingc.index(after: i)returns the same index every time.Parameters
iA valid index of the collection.
imust be less thanendIndex.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)
Parameters
IndexA 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
elementElement
uniqueCollectionFilter
closure that defines the uniqueness of the element compareCollectionFilter
closure that defines the changes of the element Return Value
-
Recreates a new
CollectionBinusing the same Filter and Sorting but new collection of elelemtsDeclaration
Swift
public func updating(with elements: Backstorage) -> CollectionBinParameters
elementsCollection of elements of type
BackstorageReturn Value
CollectionBin
-
The core function of this struct.
Note:
- Hash value is used to idenfy the unique element
- Conformance to
Equatableis 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
elementElement as
HashableandEquatableReturn Value
-
Provides convenient description for debug purposes
Declaration
Swift
public var description: String { get }
CollectionBin Structure Reference