Skip to content

reafactoring #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions Sources/Algorithm+Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}

/**
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions Sources/DoublyLinkedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,10 @@ public struct DoublyLinkedList<Element>: 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
}
}

Expand Down
21 changes: 3 additions & 18 deletions Sources/RedBlackTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
- returns: String
*/
public var description: String {
var output = "["
let l = count - 1
for i in 0..<count {
output += "\(self[i])"
if i != l {
output += ", "
}
}
return output + "]"
return "[" + map { "\($0)" }.joined(separator: ", ") + "]"
}

/**
Expand Down Expand Up @@ -166,15 +158,8 @@ public struct RedBlackTree<Key: Comparable, Value>: 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] } }
}

/**
Expand Down
23 changes: 4 additions & 19 deletions Sources/SortedDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,12 @@ public struct SortedDictionary<Key: Comparable, Value>: 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.
Expand All @@ -128,15 +120,8 @@ public struct SortedDictionary<Key: Comparable, Value>: 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] } }
}

/**
Expand Down
23 changes: 4 additions & 19 deletions Sources/SortedMultiDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,12 @@ public struct SortedMultiDictionary<Key: Comparable, Value>: 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.
Expand All @@ -128,15 +120,8 @@ public struct SortedMultiDictionary<Key: Comparable, Value>: 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] } }
}

/**
Expand Down
21 changes: 3 additions & 18 deletions Sources/SortedMultiSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,7 @@ public struct SortedMultiSet<T: Comparable>: Probable, Collection, Equatable, Cu
- returns: String
*/
public var description: String {
var output = "["
let l = count - 1
for i in 0..<count {
output += "\(self[i])"
if i != l {
output += ", "
}
}
return output + "]"
return "[" + map { "\($0)" }.joined(separator: ", ") + "]"
}

/**
Expand Down Expand Up @@ -165,15 +157,8 @@ public struct SortedMultiSet<T: Comparable>: 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] } }
}

/**
Expand Down
38 changes: 5 additions & 33 deletions Sources/SortedSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ public struct SortedSet<T: Comparable>: 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 }
}

/**
Expand All @@ -72,15 +68,7 @@ public struct SortedSet<T: Comparable>: Probable, Collection, Equatable, CustomS
- returns: String
*/
public var description: String {
var output = "["
let l = count - 1
for i in 0..<count {
output += "\(self[i])"
if i != l {
output += ", "
}
}
return output + "]"
return "[" + map { "\($0)" }.joined(separator: ",") + "]"
}

/**
Expand All @@ -105,15 +93,6 @@ public struct SortedSet<T: Comparable>: 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.
Expand Down Expand Up @@ -158,15 +137,8 @@ public struct SortedSet<T: Comparable>: 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] } }
}

/**
Expand Down Expand Up @@ -283,7 +255,7 @@ public struct SortedSet<T: Comparable>: 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
}
Expand Down