-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #989 from kiwix/remove-ordered-dependency
Remove OrderedCollections dependency
- Loading branch information
Showing
4 changed files
with
132 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// This file is part of Kiwix for iOS & macOS. | ||
// | ||
// Kiwix is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 3 of the License, or | ||
// any later version. | ||
// | ||
// Kiwix is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Kiwix; If not, see https://www.gnu.org/licenses/. | ||
|
||
import XCTest | ||
@testable import Kiwix | ||
|
||
final class OrderedCacheTests: XCTestCase { | ||
|
||
@MainActor | ||
func testEmpty() { | ||
let cache = OrderedCache<String, String>() | ||
XCTAssertEqual(cache.count, 0) | ||
XCTAssertNil(cache.findBy(key: "not to be found")) | ||
} | ||
|
||
@MainActor | ||
func testOneItem() { | ||
let cache = OrderedCache<String, String>() | ||
let pastDate = Date.distantPast | ||
cache.setValue("test_value", forKey: "keyOne", dated: pastDate) | ||
XCTAssertEqual(cache.count, 1) | ||
XCTAssertNil(cache.findBy(key: "not to be found")) | ||
XCTAssertEqual(cache.findBy(key: "keyOne"), "test_value") | ||
cache.removeOlderThan(pastDate) | ||
XCTAssertEqual(cache.count, 1) | ||
cache.removeOlderThan(Date.now) | ||
XCTAssertEqual(cache.count, 0) | ||
} | ||
|
||
@MainActor | ||
func testRemoveOlderThan() { | ||
let cache = OrderedCache<String, String>() | ||
let nowDate = Date.now | ||
cache.setValue("test_value", forKey: "keyOne", dated: nowDate) | ||
cache.setValue("old_value", forKey: "keyOld", dated: Date.distantPast) | ||
XCTAssertEqual(cache.count, 2) | ||
cache.removeOlderThan(nowDate.advanced(by: -1)) | ||
XCTAssertEqual(cache.count, 1) | ||
} | ||
|
||
@MainActor | ||
func testRemoveByKey() { | ||
let cache = OrderedCache<String, Int>() | ||
cache.setValue(1, forKey: "one") | ||
cache.setValue(0, forKey: "zero") | ||
cache.removeValue(forKey: "zero") | ||
XCTAssertNil(cache.findBy(key: "zero")) | ||
XCTAssertEqual(cache.findBy(key: "one"), 1) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This file is part of Kiwix for iOS & macOS. | ||
// | ||
// Kiwix is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 3 of the License, or | ||
// any later version. | ||
// | ||
// Kiwix is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Kiwix; If not, see https://www.gnu.org/licenses/. | ||
|
||
import Foundation | ||
|
||
@MainActor | ||
final class OrderedCache<Key: Hashable, Value> { | ||
|
||
private struct ValueDated<V> { | ||
let value: V | ||
let date: Date | ||
} | ||
|
||
private var dict: [Key: ValueDated<Value>] = [:] | ||
|
||
var count: Int { | ||
dict.count | ||
} | ||
|
||
func findBy(key: Key) -> Value? { | ||
if let dateValue = dict[key] { | ||
return dateValue.value | ||
} | ||
return nil | ||
} | ||
|
||
func removeAll() { | ||
dict = [:] | ||
} | ||
|
||
func removeOlderThan(_ pastDate: Date) { | ||
dict = dict.filter { (_, value: ValueDated<Value>) in | ||
value.date >= pastDate | ||
} | ||
} | ||
|
||
func setValue(_ value: Value, forKey key: Key, dated: Date = Date.now) { | ||
dict[key] = ValueDated(value: value, date: dated) | ||
} | ||
|
||
func removeValue(forKey key: Key) { | ||
dict.removeValue(forKey: key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters