Simplify atomic
This commit is contained in:
parent
926989f851
commit
4a44b9a702
|
@ -7,28 +7,22 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
class Atomic<T> {
|
||||
final class Atomic<T> {
|
||||
|
||||
private var value: T
|
||||
private var lock = Lock()
|
||||
private let lock = Lock()
|
||||
|
||||
init(_ value: T) {
|
||||
self.value = value
|
||||
}
|
||||
|
||||
func execute(_ body: (T) -> Void) {
|
||||
lock.withLock { body(value) }
|
||||
@inline(__always)
|
||||
func read<U>(_ body: (T) -> U) -> U {
|
||||
return lock.withLock { body(value) }
|
||||
}
|
||||
|
||||
func mutate(_ body: (inout T) -> Void) {
|
||||
lock.withLock { body(&value) }
|
||||
}
|
||||
|
||||
func read() -> T {
|
||||
return lock.withLock { value }
|
||||
}
|
||||
|
||||
func write(_ new: T) {
|
||||
lock.withLock { value = new }
|
||||
@inline(__always)
|
||||
func write<U>(_ body: (inout T) -> U) -> U {
|
||||
return lock.withLock { body(&value) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,19 +47,17 @@ public struct Period {
|
|||
self.nanoseconds = nanoseconds
|
||||
}
|
||||
|
||||
private static var map: Atomic<[String: Int]> = Atomic([
|
||||
private static let quantifiers: Atomic<[String: Int]> = Atomic([
|
||||
"one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6,
|
||||
"seven": 7, "eight": 8, "nine": 9, "ten": 10, "eleven": 11, "twelve": 12
|
||||
])
|
||||
|
||||
/// Register your own quantifier.
|
||||
/// Registers your own quantifier.
|
||||
///
|
||||
/// Period.registerQuantifier("fifty", for: 15)
|
||||
/// let period = Period("fifty minutes")
|
||||
public static func registerQuantifier(_ word: String, for number: Int) {
|
||||
map.mutate {
|
||||
$0[word] = number
|
||||
}
|
||||
quantifiers.write { $0[word] = number }
|
||||
}
|
||||
|
||||
/// Creates a period from a natural expression.
|
||||
|
@ -69,7 +67,7 @@ public struct Period {
|
|||
/// Period("1 year, 2 months and 3 days") => Period(years: 1, months: 2, days: 3)
|
||||
public init?(_ string: String) {
|
||||
var period = string
|
||||
for (word, number) in Period.map.read() {
|
||||
for (word, number) in Period.quantifiers.read({ $0 }) {
|
||||
period = period.replacingOccurrences(of: word, with: "\(number)")
|
||||
}
|
||||
guard let regex = try? NSRegularExpression(pattern: "( and |, )") else {
|
||||
|
|
|
@ -10,28 +10,25 @@ import XCTest
|
|||
|
||||
final class AtomicTests: XCTestCase {
|
||||
|
||||
func testExecute() {
|
||||
func testRead() {
|
||||
let atom = Atomic<Int>(1)
|
||||
atom.execute {
|
||||
atom.read {
|
||||
XCTAssertEqual($0, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func testMutate() {
|
||||
func testWrite() {
|
||||
let atom = Atomic<Int>(1)
|
||||
atom.mutate { $0 = 2 }
|
||||
XCTAssertEqual(atom.read(), 2)
|
||||
atom.write {
|
||||
$0 = 2
|
||||
}
|
||||
atom.read {
|
||||
XCTAssertEqual($0, 2)
|
||||
}
|
||||
|
||||
func testReadWrite() {
|
||||
let atom = Atomic<Int>(1)
|
||||
atom.write(3)
|
||||
XCTAssertEqual(atom.read(), 3)
|
||||
}
|
||||
|
||||
static var allTests = [
|
||||
("testExecute", testExecute),
|
||||
("testMutate", testMutate),
|
||||
("testReadWrite", testReadWrite)
|
||||
("testRead", testRead),
|
||||
("testWrite", testWrite)
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue