Optimize sparse set
This commit is contained in:
parent
81f61cafbb
commit
d08353fd53
|
|
@ -12,7 +12,7 @@
|
|||
/// an element from the sparse set.
|
||||
///
|
||||
/// See <https://github.com/bombela/sparseset/blob/master/src/lib.rs> for a reference implementation.
|
||||
public struct UnorderedSparseSet<Element, Key: Hashable & Codable> {
|
||||
public final class UnorderedSparseSet<Element, Key: Hashable & Codable> {
|
||||
/// An index into the dense store.
|
||||
public typealias DenseIndex = Int
|
||||
|
||||
|
|
@ -30,7 +30,7 @@ public struct UnorderedSparseSet<Element, Key: Hashable & Codable> {
|
|||
@usableFromInline var dense: DenseStore
|
||||
@usableFromInline var sparse: SparseStore
|
||||
|
||||
public init() {
|
||||
public convenience init() {
|
||||
self.init(sparse: [:], dense: [])
|
||||
}
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ public struct UnorderedSparseSet<Element, Key: Hashable & Codable> {
|
|||
/// - key: the key
|
||||
/// - Returns: true if new, false if replaced.
|
||||
@discardableResult
|
||||
public mutating func insert(_ element: Element, at key: Key) -> Bool {
|
||||
public func insert(_ element: Element, at key: Key) -> Bool {
|
||||
if let denseIndex = findIndex(at: key) {
|
||||
dense[denseIndex] = Entry(key: key, element: element)
|
||||
return false
|
||||
|
|
@ -63,7 +63,7 @@ public struct UnorderedSparseSet<Element, Key: Hashable & Codable> {
|
|||
|
||||
let nIndex = dense.count
|
||||
dense.append(Entry(key: key, element: element))
|
||||
sparse[key] = nIndex
|
||||
sparse.updateValue(nIndex, forKey: key)
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
@ -86,7 +86,7 @@ public struct UnorderedSparseSet<Element, Key: Hashable & Codable> {
|
|||
/// - Parameter key: the key
|
||||
/// - Returns: removed value or nil if key not found.
|
||||
@discardableResult
|
||||
public mutating func remove(at key: Key) -> Entry? {
|
||||
public func remove(at key: Key) -> Entry? {
|
||||
guard let denseIndex = findIndex(at: key) else {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -101,7 +101,7 @@ public struct UnorderedSparseSet<Element, Key: Hashable & Codable> {
|
|||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func removeAll(keepingCapacity: Bool = false) {
|
||||
public func removeAll(keepingCapacity: Bool = false) {
|
||||
sparse.removeAll(keepingCapacity: keepingCapacity)
|
||||
dense.removeAll(keepingCapacity: keepingCapacity)
|
||||
}
|
||||
|
|
@ -111,7 +111,7 @@ public struct UnorderedSparseSet<Element, Key: Hashable & Codable> {
|
|||
///
|
||||
/// - Parameter denseIndex: the dense index
|
||||
/// - Returns: the element entry
|
||||
private mutating func swapRemove(at denseIndex: Int) -> Entry {
|
||||
private func swapRemove(at denseIndex: Int) -> Entry {
|
||||
dense.swapAt(denseIndex, dense.count - 1)
|
||||
return dense.removeLast()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -387,7 +387,7 @@ class SparseSetTests: XCTestCase {
|
|||
|
||||
func testSparseSetDoubleRemove() {
|
||||
class AClass { }
|
||||
var set = UnorderedSparseSet<AClass, Int>()
|
||||
let set = UnorderedSparseSet<AClass, Int>()
|
||||
let a = AClass()
|
||||
let b = AClass()
|
||||
set.insert(a, at: 0)
|
||||
|
|
@ -471,7 +471,7 @@ class SparseSetTests: XCTestCase {
|
|||
}
|
||||
|
||||
func testSparseSetReduce() {
|
||||
var characters = UnorderedSparseSet<Character, Int>()
|
||||
let characters = UnorderedSparseSet<Character, Int>()
|
||||
|
||||
characters.insert("H", at: 4)
|
||||
characters.insert("e", at: 13)
|
||||
|
|
@ -497,7 +497,7 @@ class SparseSetTests: XCTestCase {
|
|||
}
|
||||
|
||||
func testSubscript() {
|
||||
var characters = UnorderedSparseSet<Character, Int>()
|
||||
let characters = UnorderedSparseSet<Character, Int>()
|
||||
|
||||
characters[4] = "H"
|
||||
characters[13] = "e"
|
||||
|
|
@ -528,7 +528,7 @@ class SparseSetTests: XCTestCase {
|
|||
}
|
||||
|
||||
func testStartEndIndex() {
|
||||
var set = UnorderedSparseSet<Character, Int>()
|
||||
let set = UnorderedSparseSet<Character, Int>()
|
||||
|
||||
set.insert("C", at: 33)
|
||||
set.insert("A", at: 11)
|
||||
|
|
@ -541,7 +541,7 @@ class SparseSetTests: XCTestCase {
|
|||
|
||||
func testAlternativeKey() {
|
||||
|
||||
var set = UnorderedSparseSet<Character, String>()
|
||||
let set = UnorderedSparseSet<Character, String>()
|
||||
|
||||
set.insert("A", at: "a")
|
||||
set.insert("C", at: "c")
|
||||
|
|
|
|||
Loading…
Reference in New Issue