Rename `add` to `append` of Bucket
This commit is contained in:
parent
e6016c3dbe
commit
4172b62d90
|
@ -142,11 +142,11 @@
|
|||
668685F4210DD21A009305C3 /* Utils */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
OBJ_22 /* WeakSet.swift */,
|
||||
OBJ_9 /* Atomic.swift */,
|
||||
OBJ_10 /* Bucket.swift */,
|
||||
OBJ_11 /* Extensions.swift */,
|
||||
OBJ_13 /* Lock.swift */,
|
||||
OBJ_22 /* WeakSet.swift */,
|
||||
);
|
||||
name = Utils;
|
||||
sourceTree = "<group>";
|
||||
|
|
|
@ -16,7 +16,11 @@ struct BucketKey: Hashable, RawRepresentable {
|
|||
}
|
||||
|
||||
static func == (lhs: BucketKey, rhs: BucketKey) -> Bool {
|
||||
return lhs.hashValue == rhs.hashValue
|
||||
return lhs.rawValue == rhs.rawValue
|
||||
}
|
||||
|
||||
func next() -> BucketKey {
|
||||
return BucketKey(rawValue: rawValue &+ 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,11 +32,11 @@ struct Bucket<Element> {
|
|||
private var entries: [Entry] = []
|
||||
|
||||
@discardableResult
|
||||
mutating func add(_ new: Element) -> BucketKey {
|
||||
let key = nextKey
|
||||
nextKey = BucketKey(rawValue: nextKey.rawValue &+ 1)
|
||||
entries.append((key: key, element: new))
|
||||
return key
|
||||
mutating func append(_ new: Element) -> BucketKey {
|
||||
defer { nextKey = nextKey.next() }
|
||||
|
||||
entries.append((key: nextKey, element: new))
|
||||
return nextKey
|
||||
}
|
||||
|
||||
func element(for key: BucketKey) -> Element? {
|
||||
|
@ -44,10 +48,9 @@ struct Bucket<Element> {
|
|||
|
||||
@discardableResult
|
||||
mutating func removeElement(for key: BucketKey) -> Element? {
|
||||
for i in 0..<entries.count where entries[i].key == key {
|
||||
let element = entries[i].element
|
||||
entries.remove(at: i)
|
||||
return element
|
||||
for (idx, entry) in entries.enumerated() where entry.key == key {
|
||||
entries.remove(at: idx)
|
||||
return entry.element
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ public class Task {
|
|||
_iterator = schedule.makeIterator()
|
||||
_timer = DispatchSource.makeTimerSource(queue: queue)
|
||||
|
||||
_actions.add(onElapse)
|
||||
_actions.append(onElapse)
|
||||
|
||||
_timer.setEventHandler { [weak self] in
|
||||
self?.elapse()
|
||||
|
@ -257,7 +257,7 @@ public class Task {
|
|||
@discardableResult
|
||||
public func addAction(_ action: @escaping (Task) -> Void) -> ActionKey {
|
||||
return _lock.withLock {
|
||||
_actions.add(action)
|
||||
_actions.append(action)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,7 @@ final class AtomicTests: XCTestCase {
|
|||
|
||||
func testMutate() {
|
||||
let atom = Atomic<Int>(1)
|
||||
atom.mutate {
|
||||
$0 = 2
|
||||
}
|
||||
atom.mutate { $0 = 2 }
|
||||
XCTAssertEqual(atom.read(), 2)
|
||||
}
|
||||
|
||||
|
@ -33,6 +31,7 @@ final class AtomicTests: XCTestCase {
|
|||
|
||||
static var allTests = [
|
||||
("testExecute", testExecute),
|
||||
("testMutate", testMutate)
|
||||
("testMutate", testMutate),
|
||||
("testReadWrite", testReadWrite)
|
||||
]
|
||||
}
|
||||
|
|
|
@ -12,39 +12,57 @@ final class BucketTests: XCTestCase {
|
|||
|
||||
typealias Fn = () -> Int
|
||||
|
||||
func testAdd() {
|
||||
func testBucketKey() {
|
||||
let key = BucketKey(rawValue: 0)
|
||||
XCTAssertEqual(key.next().hashValue, BucketKey(rawValue: 1).hashValue)
|
||||
}
|
||||
|
||||
func testAppend() {
|
||||
var bucket = Bucket<Fn>()
|
||||
let key = bucket.add({ 1 })
|
||||
let key = bucket.append { 1 }
|
||||
let element = bucket.element(for: key)
|
||||
XCTAssertNotNil(element)
|
||||
guard let fn = element else { return }
|
||||
XCTAssertEqual(fn(), 1)
|
||||
}
|
||||
|
||||
func testMiss() {
|
||||
var bucket = Bucket<Fn>()
|
||||
let key = bucket.append { 1 }
|
||||
let element = bucket.element(for: key.next())
|
||||
XCTAssertNil(element)
|
||||
}
|
||||
|
||||
func testRemove() {
|
||||
var bucket = Bucket<Fn>()
|
||||
let k0 = bucket.add { 0 }
|
||||
bucket.add { 1 }
|
||||
bucket.add { 2 }
|
||||
XCTAssertEqual(bucket.count, 3)
|
||||
|
||||
let e0 = bucket.removeElement(for: k0)
|
||||
XCTAssertNotNil(e0)
|
||||
bucket.append { 0 }
|
||||
bucket.append { 1 }
|
||||
let k = bucket.append { 2 }
|
||||
|
||||
guard let fn0 = e0 else { return }
|
||||
XCTAssertEqual(fn0(), 0)
|
||||
let e = bucket.removeElement(for: k)
|
||||
XCTAssertNotNil(e)
|
||||
|
||||
XCTAssertEqual(bucket.count, 2)
|
||||
XCTAssertNil(bucket.removeElement(for: k.next()))
|
||||
|
||||
bucket.removeAll()
|
||||
XCTAssertEqual(bucket.count, 0)
|
||||
}
|
||||
|
||||
func testCount() {
|
||||
var bucket = Bucket<Fn>()
|
||||
|
||||
bucket.append { 0 }
|
||||
bucket.append { 1 }
|
||||
bucket.append { 2 }
|
||||
XCTAssertEqual(bucket.count, 3)
|
||||
}
|
||||
|
||||
func testSequence() {
|
||||
var bucket = Bucket<Fn>()
|
||||
bucket.add { 0 }
|
||||
bucket.add { 1 }
|
||||
bucket.add { 2 }
|
||||
bucket.append { 0 }
|
||||
bucket.append { 1 }
|
||||
bucket.append { 2 }
|
||||
|
||||
var i = 0
|
||||
for fn in bucket {
|
||||
|
@ -54,8 +72,10 @@ final class BucketTests: XCTestCase {
|
|||
}
|
||||
|
||||
static var allTests = [
|
||||
("testAdd", testAdd),
|
||||
("testAppend", testAppend),
|
||||
("testMiss", testMiss),
|
||||
("testRemove", testRemove),
|
||||
("testCount", testCount),
|
||||
("testSequence", testSequence)
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue