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