diff --git a/Bond.podspec b/Bond.podspec index a902c5bb..25278879 100644 --- a/Bond.podspec +++ b/Bond.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "Bond" - s.version = "3.4.0" + s.version = "3.4.1" s.summary = "A Swift binding framework" s.description = <<-DESC @@ -18,7 +18,7 @@ Pod::Spec.new do |s| s.social_media_url = "http://twitter.com/srdanrasic" s.ios.deployment_target = "8.0" s.osx.deployment_target = "10.10" - s.source = { :git => "https://github.com/SwiftBond/Bond.git", :tag => "v3.4.0" } + s.source = { :git => "https://github.com/SwiftBond/Bond.git", :tag => "v3.4.1" } s.source_files = "Bond" s.osx.exclude_files = "Bond/Bond+UI*" s.framework = 'SystemConfiguration' diff --git a/Bond/Bond+Arrays.swift b/Bond/Bond+Arrays.swift index 9a88c362..4c6e36d4 100644 --- a/Bond/Bond+Arrays.swift +++ b/Bond/Bond+Arrays.swift @@ -242,95 +242,18 @@ public struct DynamicArrayGenerator: GeneratorType { } } -// MARK: Dynamic Array Map Cache - -protocol DynamicArrayMapCache { - typealias T - init(count: Int, repeatedValue: T?) - mutating func append(element: T?) - mutating func insert(element: T?, atIndex: Int) - mutating func removeAtIndex(index: Int) - subscript(index: Int) -> T? { get set } -} - -struct DynamicArrayMapCacheValue: DynamicArrayMapCache { - var array: [T?] - - init(count: Int, repeatedValue: T?) { - array = Array(count: count, repeatedValue: repeatedValue) - } - - mutating func append(element: T?) { - array.append(element) - } - - mutating func insert(element: T?, atIndex: Int) { - array.insert(element, atIndex: atIndex) - } - - mutating func removeAtIndex(index: Int) { - array.removeAtIndex(index) - } - - subscript(index: Int) -> T? { - get { - return array[index] - } - set { - array[index] = newValue - } - } -} - -struct WeakBox { - weak var unbox: T? - init(_ object: T?) { unbox = object } -} - -struct DynamicArrayMapCacheObject: DynamicArrayMapCache { - var array: [WeakBox] - - init(count: Int, repeatedValue: T?) { - array = Array(count: count, repeatedValue: WeakBox(repeatedValue)) - } - - mutating func append(element: T?) { - array.append(WeakBox(element)) - } - - mutating func insert(element: T?, atIndex: Int) { - array.insert(WeakBox(element), atIndex: atIndex) - } - - mutating func removeAtIndex(index: Int) { - array.removeAtIndex(index) - } - - subscript(index: Int) -> T? { - get { - return array[index].unbox - } - set { - array[index].unbox = newValue - } - } -} - - // MARK: Dynamic Array Map Proxy -private class DynamicArrayMapProxy: DynamicArray { +private class DynamicArrayMapProxy: DynamicArray { private unowned var sourceArray: DynamicArray private var mapf: (T, Int) -> U private let bond: ArrayBond - private var cache: C - private init(sourceArray: DynamicArray, mapf: (T, Int) -> U, cache: C) { + private init(sourceArray: DynamicArray, mapf: (T, Int) -> U) { self.sourceArray = sourceArray self.mapf = mapf self.bond = ArrayBond() self.bond.bind(sourceArray, fire: false) - self.cache = cache super.init([]) bond.willInsertListener = { [unowned self] array, i in @@ -338,10 +261,6 @@ private class DynamicArrayMapProxy } bond.didInsertListener = { [unowned self] array, i in - for idx in i { - self.cache.insert(nil, atIndex: idx) - } - self.dispatchDidInsert(i) } @@ -350,10 +269,6 @@ private class DynamicArrayMapProxy } bond.didRemoveListener = { [unowned self] array, i in - for idx in reverse(i) { - self.cache.removeAtIndex(idx) - } - self.dispatchDidRemove(i) } @@ -362,10 +277,6 @@ private class DynamicArrayMapProxy } bond.didUpdateListener = { [unowned self] array, i in - for idx in i { - self.cache[idx] = nil - } - self.dispatchDidUpdate(i) } } @@ -383,32 +294,16 @@ private class DynamicArrayMapProxy } override var first: U? { - if self.count == 0 { - return nil - } - - if let first = self.cache[0] { - return first - } else if let first = sourceArray.first { - let e = mapf(first, 0) - self.cache[0] = e - return e + if let first = sourceArray.first { + return mapf(first, 0) } else { return nil } } override var last: U? { - if self.count == 0 { - return nil - } - - if let last = self.cache[sourceArray.count - 1] { - return last - } else if let last = sourceArray.last { - let e = mapf(last, sourceArray.count - 1) - self.cache[sourceArray.count - 1] = e - return e + if let last = sourceArray.last { + return mapf(last, sourceArray.count - 1) } else { return nil } @@ -444,13 +339,7 @@ private class DynamicArrayMapProxy override subscript(index: Int) -> U { get { - if let e = self.cache[index] { - return e - } else { - let e = mapf(sourceArray[index], index) - self.cache[index] = e - return e - } + return mapf(sourceArray[index], index) } set(newObject) { fatalError("Modifying proxy array is not supported!") @@ -675,21 +564,12 @@ private class DynamicArrayFilterProxy: DynamicArray { public extension DynamicArray { public func map(f: (T, Int) -> U) -> DynamicArray { - return _map(self, f, DynamicArrayMapCacheValue(count: self.count, repeatedValue: nil)) + return _map(self, f) } public func map(f: T -> U) -> DynamicArray { let mapf = { (o: T, i: Int) -> U in f(o) } - return _map(self, mapf, DynamicArrayMapCacheValue(count: self.count, repeatedValue: nil)) - } - - public func map(f: (T, Int) -> U) -> DynamicArray { - return _map(self, f, DynamicArrayMapCacheObject(count: self.count, repeatedValue: nil)) - } - - public func map(f: T -> U) -> DynamicArray { - let mapf = { (o: T, i: Int) -> U in f(o) } - return _map(self, mapf, DynamicArrayMapCacheObject(count: self.count, repeatedValue: nil)) + return _map(self, mapf) } public func filter(f: T -> Bool) -> DynamicArray { @@ -699,8 +579,8 @@ public extension DynamicArray // MARK: Map -private func _map(dynamicArray: DynamicArray, f: (T, Int) -> U, cache: C) -> DynamicArrayMapProxy { - return DynamicArrayMapProxy(sourceArray: dynamicArray, mapf: f, cache: cache) +private func _map(dynamicArray: DynamicArray, f: (T, Int) -> U) -> DynamicArrayMapProxy { + return DynamicArrayMapProxy(sourceArray: dynamicArray, mapf: f) } // MARK: Filter diff --git a/Bond/Info.plist b/Bond/Info.plist index 69af4b10..5f4d502d 100644 --- a/Bond/Info.plist +++ b/Bond/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 3.4.0 + 3.4.1 CFBundleSignature ???? CFBundleVersion diff --git a/BondTests/ArrayTests.swift b/BondTests/ArrayTests.swift index 6d288312..f7843dbd 100644 --- a/BondTests/ArrayTests.swift +++ b/BondTests/ArrayTests.swift @@ -278,85 +278,7 @@ class ArrayTests: XCTestCase { XCTAssert(mapped.count == 0) } - func testArrayMapToValueCache() { - var callCount: Int = 0 - let array = DynamicArray([]) - let mapped = array.map { e, i -> Int in - callCount++ - return e * 2 - } - - XCTAssert(mapped.count == 0) - XCTAssert(callCount == 0) - - array.append(1) - XCTAssert(callCount == 0) - - XCTAssert(mapped[0] == 2) - XCTAssert(callCount == 1, "Should cache value") - - XCTAssert(mapped[0] == 2) - XCTAssert(callCount == 1, "Shoud use cached value") - - array.insert(2, atIndex: 0) - XCTAssert(callCount == 1) - - XCTAssert(mapped[1] == 2) - XCTAssert(callCount == 1, "Shoud use cached value") - - XCTAssert(mapped[0] == 4) - XCTAssert(callCount == 2, "Should cache value") - - XCTAssert(mapped[0] == 4) - XCTAssert(callCount == 2, "Shoud use cached value") - - array.removeAtIndex(0) - XCTAssert(callCount == 2) - - XCTAssert(mapped[0] == 2) - XCTAssert(callCount == 2, "Shoud use cached value") - - array.removeLast() - XCTAssert(callCount == 2) - - array.splice([1, 2, 3, 4], atIndex: 0) - XCTAssert(callCount == 2) - - XCTAssert(mapped[1] == 4) - XCTAssert(callCount == 3, "Should cache value") - - array.removeAtIndex(1) - XCTAssert(callCount == 3) - - XCTAssert(mapped[1] == 6) - XCTAssert(callCount == 4, "Should cache value") - - array.insert(2, atIndex: 1) - XCTAssert(callCount == 4) - - XCTAssert(mapped[2] == 6) - XCTAssert(callCount == 4, "Shoud use cached value") - - XCTAssert(mapped[1] == 4) - XCTAssert(callCount == 5, "Should cache value") - - XCTAssert(mapped.last == 8) - XCTAssert(callCount == 6, "Should cache value") - - XCTAssert(mapped.last == 8) - XCTAssert(callCount == 6, "Shoud use cached value") - - XCTAssert(mapped.first == 2) - XCTAssert(callCount == 7, "Should cache value") - - XCTAssert(mapped.first == 2) - XCTAssert(callCount == 7, "Shoud use cached value") - - array.removeAll(true) - XCTAssert(callCount == 7) - } - - func testArrayMapToObjectCache() { + func testArrayMapCallCount() { class Test { var value: Int init(_ value: Int) { self.value = value } @@ -376,28 +298,28 @@ class ArrayTests: XCTestCase { XCTAssert(callCount == 0) XCTAssert(mapped[0].value == 1) - XCTAssert(callCount == 1, "Should cache value") + XCTAssert(callCount == 1, "Should call") XCTAssert(mapped[0].value == 1) - XCTAssert(callCount == 2, "Shoud cache value") + XCTAssert(callCount == 2, "Should call") array.insert(2, atIndex: 0) XCTAssert(callCount == 2) XCTAssert(mapped[1].value == 1) - XCTAssert(callCount == 3, "Shoud cache value") + XCTAssert(callCount == 3, "Should call") XCTAssert(mapped[0].value == 2) - XCTAssert(callCount == 4, "Should cache value") + XCTAssert(callCount == 4, "Should call") XCTAssert(mapped[0].value == 2) - XCTAssert(callCount == 5, "Shoud cache value") + XCTAssert(callCount == 5, "Should call") array.removeAtIndex(0) XCTAssert(callCount == 5) XCTAssert(mapped[0].value == 1) - XCTAssert(callCount == 6, "Shoud cache value") + XCTAssert(callCount == 6, "Should call") array.removeLast() XCTAssert(callCount == 6) @@ -406,61 +328,39 @@ class ArrayTests: XCTestCase { XCTAssert(callCount == 6) XCTAssert(mapped[1].value == 2) - XCTAssert(callCount == 7, "Should cache value") + XCTAssert(callCount == 7, "Should call") array.removeAtIndex(1) XCTAssert(callCount == 7) XCTAssert(mapped[1].value == 3) - XCTAssert(callCount == 8, "Should cache value") + XCTAssert(callCount == 8, "Should call") array.insert(2, atIndex: 1) XCTAssert(callCount == 8) XCTAssert(mapped[2].value == 3) - XCTAssert(callCount == 9, "Shoud cache value") + XCTAssert(callCount == 9, "Should call") XCTAssert(mapped[1].value == 2) - XCTAssert(callCount == 10, "Should cache value") + XCTAssert(callCount == 10, "Should call") XCTAssert(mapped.last!.value == 4) - XCTAssert(callCount == 11, "Should cache value") + XCTAssert(callCount == 11, "Should call") XCTAssert(mapped.last!.value == 4) - XCTAssert(callCount == 12, "Shoud cache value") + XCTAssert(callCount == 12, "Should call") XCTAssert(mapped.first!.value == 1) - XCTAssert(callCount == 13, "Should cache value") + XCTAssert(callCount == 13, "Should call") XCTAssert(mapped.first!.value == 1) - XCTAssert(callCount == 14, "Shoud cache value") + XCTAssert(callCount == 14, "Should call") array.removeAll(true) XCTAssert(callCount == 14) } - func testArrayMapToObjectCacheRetaining() { - class Test { - var value: Int - init(_ value: Int) { self.value = value } - } - - let array = DynamicArray([]) - let mapped = array.map { e, i -> Test in - return Test(e) - } - - array.append(1) - var element: Test? = mapped[0] - XCTAssert(element != nil && element!.value == 1) - - weak var elementWeak: Test? = mapped[0] - XCTAssertNotNil(elementWeak) - - element = nil - XCTAssertNil(elementWeak, "Should not be retained by the array") - } - func testFilterMapChain() { let array = DynamicArray([]) let filtered = array.filter { e in e > 2 } diff --git a/BondTests/Info.plist b/BondTests/Info.plist index eec331d3..8f7366b7 100644 --- a/BondTests/Info.plist +++ b/BondTests/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 3.4.0 + 3.4.1 CFBundleSignature ???? CFBundleVersion