Bag

public struct Bag<T> : CustomDebugStringConvertible

Data structure that represents a bag of elements typed T.

Single element can be stored multiple times.

Time and space complexity of insertion an deletion is O(n).

It is suitable for storing small number of elements.

  • Type of identifier for inserted elements.

    Declaration

    Swift

    public typealias KeyType = BagKey
  • Creates new empty Bag.

    Declaration

    Swift

    public init()
  • Inserts value into bag.

    Declaration

    Swift

    public mutating func insert(element: T) -> BagKey

    Parameters

    element

    Element to insert.

    Return Value

    Key that can be used to remove element from bag.

  • Declaration

    Swift

    public var count: Int

    Return Value

    Number of elements in bag.

  • Removes all elements from bag and clears capacity.

    Declaration

    Swift

    public mutating func removeAll()
  • Removes element with a specific key from bag.

    Declaration

    Swift

    public mutating func removeKey(key: BagKey) -> T?

    Parameters

    key

    Key that identifies element to remove from bag.

    Return Value

    Element that bag contained, or nil in case element was already removed.

  • A textual representation of self, suitable for debugging.

    Declaration

    Swift

    public var debugDescription : String
  • Enumerates elements inside the bag.

    Declaration

    Swift

    public func forEach(@noescape action: (T) -> Void)

    Parameters

    action

    Enumeration closure.

  • Dispatches event to app observers contained inside bag.

    Declaration

    Swift

    public func on(event: Event<T.E>)

    Parameters

    action

    Enumeration closure.