-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/enumsAsArguments
- Loading branch information
Showing
5 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/// A poor man's ordered set. | ||
internal struct OrderedSet<T: Hashable> { | ||
fileprivate var values: [T] = [] | ||
|
||
init<S: Sequence>(_ sequence: S) where S.Element == T { | ||
for e in sequence where !values.contains(e) { | ||
values.append(e) | ||
} | ||
} | ||
|
||
@discardableResult | ||
mutating func remove(_ member: T) -> T? { | ||
if let index = values.index(of: member) { | ||
return values.remove(at: index) | ||
} else { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
extension OrderedSet: Equatable { | ||
static func == (_ lhs: OrderedSet, rhs: OrderedSet) -> Bool { | ||
return lhs.values == rhs.values | ||
} | ||
} | ||
|
||
extension OrderedSet: Collection { | ||
subscript(position: Int) -> T { | ||
return values[position] | ||
} | ||
|
||
var count: Int { | ||
return values.count | ||
} | ||
|
||
var isEmpty: Bool { | ||
return values.isEmpty | ||
} | ||
|
||
var startIndex: Int { | ||
return values.startIndex | ||
} | ||
|
||
var endIndex: Int { | ||
return values.endIndex | ||
} | ||
|
||
func index(after i: Int) -> Int { | ||
return values.index(after: i) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
@testable import Commandant | ||
import Foundation | ||
import Quick | ||
import Nimble | ||
|
||
class OrderedSetSpec: QuickSpec { | ||
override func spec() { | ||
describe("OrderedSet") { | ||
it("should remove duplicate entries") { | ||
let input = ["a", "c", "b", "a"] | ||
let output = OrderedSet(input) | ||
expect(output.count) == 3 | ||
} | ||
|
||
it("should preserve the order of the given input") { | ||
let input = ["a", "c", "b", "a"] | ||
expect(Set(input).joined()) != "acb" | ||
expect(OrderedSet(input).joined()) == "acb" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters