From 4a44b9a7021a051b0d6af3d5ea1b1919fba92a5c Mon Sep 17 00:00:00 2001 From: QuentinJin Date: Mon, 24 Sep 2018 20:38:54 +0800 Subject: [PATCH] Simplify atomic --- Sources/Schedule/Atomic.swift | 22 ++++++++-------------- Sources/Schedule/Period.swift | 12 +++++------- Tests/ScheduleTests/AtomicTests.swift | 25 +++++++++++-------------- 3 files changed, 24 insertions(+), 35 deletions(-) diff --git a/Sources/Schedule/Atomic.swift b/Sources/Schedule/Atomic.swift index 9c9865b..53e7a4c 100644 --- a/Sources/Schedule/Atomic.swift +++ b/Sources/Schedule/Atomic.swift @@ -7,28 +7,22 @@ import Foundation -class Atomic { +final class Atomic { 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(_ 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(_ body: (inout T) -> U) -> U { + return lock.withLock { body(&value) } } } diff --git a/Sources/Schedule/Period.swift b/Sources/Schedule/Period.swift index 982c526..576cb60 100644 --- a/Sources/Schedule/Period.swift +++ b/Sources/Schedule/Period.swift @@ -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 { diff --git a/Tests/ScheduleTests/AtomicTests.swift b/Tests/ScheduleTests/AtomicTests.swift index 55a1014..28b44f2 100644 --- a/Tests/ScheduleTests/AtomicTests.swift +++ b/Tests/ScheduleTests/AtomicTests.swift @@ -10,28 +10,25 @@ import XCTest final class AtomicTests: XCTestCase { - func testExecute() { + func testRead() { let atom = Atomic(1) - atom.execute { + atom.read { XCTAssertEqual($0, 1) } } - func testMutate() { + func testWrite() { let atom = Atomic(1) - atom.mutate { $0 = 2 } - XCTAssertEqual(atom.read(), 2) - } - - func testReadWrite() { - let atom = Atomic(1) - atom.write(3) - XCTAssertEqual(atom.read(), 3) + atom.write { + $0 = 2 + } + atom.read { + XCTAssertEqual($0, 2) + } } static var allTests = [ - ("testExecute", testExecute), - ("testMutate", testMutate), - ("testReadWrite", testReadWrite) + ("testRead", testRead), + ("testWrite", testWrite) ] }