Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. /// A sequence of weakly held elements.
  2. ///
  3. /// Iterating over this sequence yields the elements that still exist at the
  4. /// moment of iteration. Subsequent iterations may not yield the same elements,
  5. /// as some elements may have been removed from memory.
  6. public struct WeakSequence<Element: AnyObject> : Sequence {
  7. var _contents: [Box] = []
  8.  
  9. /// A box for weakly holding a class instance.
  10. class Box {
  11. weak var value: Element?
  12.  
  13. init(_ value: Element) {
  14. self.value = value
  15. }
  16. }
  17.  
  18. /// An iterator over a sequence of weakly held instances.
  19. public struct Iterator : IteratorProtocol {
  20. var _contents: [Box]
  21. var _current: Int = 0
  22.  
  23. init(_ _weakSequence: WeakSequence) {
  24. self._contents = _weakSequence._contents
  25. }
  26.  
  27. public mutating func next() -> Element? {
  28. while _current != _contents.endIndex {
  29. defer { _current += 1 }
  30. if let result = _contents[_current].value {
  31. return result
  32. }
  33. }
  34. return nil
  35. }
  36. }
  37.  
  38. public func makeIterator() -> Iterator {
  39. return Iterator(self)
  40. }
  41.  
  42. /// The number of elements in the sequence that are still in memory.
  43. public var count: Int {
  44. return _contents.lazy.filter({ $0.value != nil }).count
  45. }
  46.  
  47. /// The number of weakly-held instances that are referenced by this sequence.
  48. public var weakCount: Int {
  49. return _contents.count
  50. }
  51.  
  52. /// Removes any already released references from the sequence.
  53. public mutating func compress() {
  54. _contents = _contents.filter { $0.value != nil }
  55. }
  56.  
  57. public init() {}
  58.  
  59. public init<S: Sequence>(_ elements: S) where S.Element == Element {
  60. _contents = elements.map(Box.init)
  61. }
  62.  
  63. public mutating func append(_ element: Element) {
  64. _contents.append(Box(element))
  65. }
  66.  
  67. public mutating func append<S: Sequence>(contentsOf elements: S)
  68. where S.Element == Element
  69. {
  70. _contents.append(contentsOf: elements.map(Box.init))
  71. }
  72.  
  73. public mutating func prepend(_ element: Element) {
  74. _contents.insert(Box(element), at: 0)
  75. }
  76.  
  77. public mutating func prepend<S: Sequence>(contentsOf elements: S)
  78. where S.Element == Element
  79. {
  80. _contents.replaceSubrange(0..<0, with: elements.map(Box.init))
  81. }
  82.  
  83. public mutating func remove(_ element: Element) -> Element? {
  84. if let i = _contents.index(where: { $0.value === element }) {
  85. return _contents.remove(at: i).value
  86. }
  87. return nil
  88. }
  89.  
  90. public mutating func removeAll() {
  91. _contents.removeAll()
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement