Indexed.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Algorithms open source project
  4. //
  5. // Copyright (c) 2020 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See https://swift.org/LICENSE.txt for license information
  9. //
  10. //===----------------------------------------------------------------------===//
  11. /// A collection wrapper that iterates over the indices and elements of a
  12. /// collection together.
  13. struct IndexedCollection<Base: Collection> {
  14. /// The base collection.
  15. let base: Base
  16. init(base: Base) {
  17. self.base = base
  18. }
  19. }
  20. extension IndexedCollection: Collection {
  21. /// The element type for an `IndexedCollection` collection.
  22. typealias Element = (index: Base.Index, element: Base.Element)
  23. var startIndex: Base.Index {
  24. base.startIndex
  25. }
  26. var endIndex: Base.Index {
  27. base.endIndex
  28. }
  29. subscript(position: Base.Index) -> Element {
  30. (index: position, element: base[position])
  31. }
  32. func index(after i: Base.Index) -> Base.Index {
  33. base.index(after: i)
  34. }
  35. func index(_ i: Base.Index, offsetBy distance: Int) -> Base.Index {
  36. base.index(i, offsetBy: distance)
  37. }
  38. func index(
  39. _ i: Base.Index,
  40. offsetBy distance: Int,
  41. limitedBy limit: Base.Index
  42. ) -> Base.Index? {
  43. base.index(i, offsetBy: distance, limitedBy: limit)
  44. }
  45. func distance(from start: Base.Index, to end: Base.Index) -> Int {
  46. base.distance(from: start, to: end)
  47. }
  48. var indices: Base.Indices {
  49. base.indices
  50. }
  51. }
  52. //extension IndexedCollection: BidirectionalCollection
  53. // where Base: BidirectionalCollection
  54. //{
  55. // func index(before i: Base.Index) -> Base.Index {
  56. // base.index(before: i)
  57. // }
  58. //}
  59. //
  60. //extension IndexedCollection: RandomAccessCollection
  61. // where Base: RandomAccessCollection {}
  62. //
  63. //extension IndexedCollection: LazySequenceProtocol, LazyCollectionProtocol
  64. // where Base: LazySequenceProtocol {}
  65. //===----------------------------------------------------------------------===//
  66. // indexed()
  67. //===----------------------------------------------------------------------===//
  68. extension Collection {
  69. /// Returns a collection of pairs *(i, x)*, where *i* represents an index of
  70. /// the collection, and *x* represents an element.
  71. ///
  72. /// This example iterates over the indices and elements of a set, building an
  73. /// array consisting of indices of names with five or fewer letters.
  74. ///
  75. /// let names: Set = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]
  76. /// var shorterIndices: [Set<String>.Index] = []
  77. /// for (i, name) in names.indexed() {
  78. /// if name.count <= 5 {
  79. /// shorterIndices.append(i)
  80. /// }
  81. /// }
  82. ///
  83. /// Returns: A collection of paired indices and elements of this collection.
  84. func indexed() -> IndexedCollection<Self> {
  85. IndexedCollection(base: self)
  86. }
  87. }