diff --git a/Sources/Algorithm+Array.swift b/Sources/Algorithm+Array.swift index 86757cc..50ae78a 100644 --- a/Sources/Algorithm+Array.swift +++ b/Sources/Algorithm+Array.swift @@ -37,10 +37,7 @@ extension Array where Element: Equatable { */ @discardableResult mutating func remove(object: Element) -> Element? { - guard let v = index(of: object) else { - return nil - } - return remove(at: v) + return index(of: object).map { self.remove(at: $0) } } /** @@ -82,10 +79,8 @@ extension Array where Element: Equatable { public func count(of elements: [Element]) -> Int { var c = 0 for e in elements { - for x in self { - if e == x { - c += 1 - } + for x in self where e == x { + c += 1 } } return c diff --git a/Sources/DoublyLinkedList.swift b/Sources/DoublyLinkedList.swift index 961a1bc..1a6e524 100644 --- a/Sources/DoublyLinkedList.swift +++ b/Sources/DoublyLinkedList.swift @@ -167,11 +167,10 @@ public struct DoublyLinkedList: CustomStringConvertible, Sequence { public func makeIterator() -> DoublyLinkedList.Iterator { var it = head return AnyIterator { - guard let e = it?.element else { - return nil - } + defer { it = it?.next - return e + } + return it?.element } } diff --git a/Sources/RedBlackTree.swift b/Sources/RedBlackTree.swift index 36e1c7c..e4f175e 100644 --- a/Sources/RedBlackTree.swift +++ b/Sources/RedBlackTree.swift @@ -75,15 +75,7 @@ public struct RedBlackTree: Probable, Collection, Custom - returns: String */ public var description: String { - var output = "[" - let l = count - 1 - for i in 0..: Probable, Collection, Custom // :returns: RedBlackTree.Generator // public func makeIterator() -> RedBlackTree.Iterator { - var index = startIndex - return AnyIterator { - if index < self.endIndex { - let i: Int = index - index += 1 - return self[i] - } - return nil - } + var i = indices.makeIterator() + return AnyIterator { i.next().map { self[$0] } } } /** diff --git a/Sources/SortedDictionary.swift b/Sources/SortedDictionary.swift index fd75e0d..901d31e 100644 --- a/Sources/SortedDictionary.swift +++ b/Sources/SortedDictionary.swift @@ -89,20 +89,12 @@ public struct SortedDictionary: Probable, Collection, Eq /// Retrieves an Array of the key values in order. public var keys: [Key] { - var s = [Key]() - for x in self { - s.append(x.key) - } - return s + return map { $0.key } } /// Retrieves an Array of the values that are sorted based public var values: [Value] { - var s = [Value]() - for x in self { - s.append(x.value!) - } - return s + return flatMap { $0.value } } /// Initializer. @@ -128,15 +120,8 @@ public struct SortedDictionary: Probable, Collection, Eq } public func makeIterator() -> SortedDictionary.Iterator { - var index = startIndex - return AnyIterator { - if index < self.endIndex { - let i: Int = index - index += 1 - return self[i] - } - return nil - } + var i = indices.makeIterator() + return AnyIterator { i.next().map { self[$0] } } } /** diff --git a/Sources/SortedMultiDictionary.swift b/Sources/SortedMultiDictionary.swift index adcd624..9d15c7d 100644 --- a/Sources/SortedMultiDictionary.swift +++ b/Sources/SortedMultiDictionary.swift @@ -89,20 +89,12 @@ public struct SortedMultiDictionary: Probable, Collectio /// Retrieves an Array of the key values in order. public var keys: [Key] { - var s = [Key]() - for x in self { - s.append(x.key) - } - return s + return map { $0.key } } /// Retrieves an Array of the values that are sorted based public var values: [Value] { - var s = [Value]() - for x in self { - s.append(x.value!) - } - return s + return flatMap { $0.value } } /// Initializer. @@ -128,15 +120,8 @@ public struct SortedMultiDictionary: Probable, Collectio } public func makeIterator() -> SortedMultiDictionary.Iterator { - var index = startIndex - return AnyIterator { - if index < self.endIndex { - let i: Int = index - index += 1 - return self[i] - } - return nil - } + var i = indices.makeIterator() + return AnyIterator { i.next().map { self[$0] } } } /** diff --git a/Sources/SortedMultiSet.swift b/Sources/SortedMultiSet.swift index 8f8eee9..37ba217 100644 --- a/Sources/SortedMultiSet.swift +++ b/Sources/SortedMultiSet.swift @@ -72,15 +72,7 @@ public struct SortedMultiSet: Probable, Collection, Equatable, Cu - returns: String */ public var description: String { - var output = "[" - let l = count - 1 - for i in 0..: Probable, Collection, Equatable, Cu // :returns: SortedMultiSet.Generator // public func makeIterator() -> SortedMultiSet.Iterator { - var index = startIndex - return AnyIterator { - if index < self.endIndex { - let i = index - index += 1 - return self[i] - } - return nil - } + var i = indices.makeIterator() + return AnyIterator { i.next().map { self[$0] } } } /** diff --git a/Sources/SortedSet.swift b/Sources/SortedSet.swift index 0e4183a..c39d336 100644 --- a/Sources/SortedSet.swift +++ b/Sources/SortedSet.swift @@ -58,11 +58,7 @@ public struct SortedSet: Probable, Collection, Equatable, CustomS :name: asArray */ public var asArray: [Element] { - var a = [Element]() - for x in self { - a.append(x) - } - return a + return map { $0 } } /** @@ -72,15 +68,7 @@ public struct SortedSet: Probable, Collection, Equatable, CustomS - returns: String */ public var description: String { - var output = "[" - let l = count - 1 - for i in 0..: Probable, Collection, Equatable, CustomS return tree.last?.value } - /** - :name: isEmpty - :description: A boolean of whether the RedBlackTree is empty. - - returns: Bool - */ - public var isEmpty: Bool { - return 0 == count - } - /** :name: startIndex :description: Conforms to the Collection Protocol. @@ -158,15 +137,8 @@ public struct SortedSet: Probable, Collection, Equatable, CustomS } public func makeIterator() -> SortedSet.Iterator { - var index = startIndex - return AnyIterator { - if index < self.endIndex { - let i = index - index += 1 - return self[i] - } - return nil - } + var i = indices.makeIterator() + return AnyIterator { i.next().map { self[$0] } } } /** @@ -283,7 +255,7 @@ public struct SortedSet: Probable, Collection, Equatable, CustomS */ mutating public func insert(_ elements: [Element]) { for x in elements { - tree.insert(value: x, for: x) + tree.insert(value: x, for: x) } count = tree.count }