Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistent collections updates #174

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//===----------------------------------------------------------------------===//

//extension PersistentDictionary: CVarArg {
// public var _cVarArgEncoding: [Int] {
// <#code#>
// }
// public var _cVarArgEncoding: [Int] {
// <#code#>
// }
//}
72 changes: 36 additions & 36 deletions Sources/PersistentCollections/PersistentDictionary+Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,43 @@
//===----------------------------------------------------------------------===//

extension PersistentDictionary: Collection {
public typealias Index = PersistentDictionaryIndex

///
/// Manipulating Indices
///

public var startIndex: Self.Index { PersistentDictionaryIndex(value: 0) }

public var endIndex: Self.Index { PersistentDictionaryIndex(value: count) }

public func index(after i: Self.Index) -> Self.Index {
return i + 1
}

///
/// Returns the index for the given key.
///
public func index(forKey key: Key) -> Self.Index? {
return rootNode.index(key, computeHash(key), 0, 0)
}

///
/// Accesses the key-value pair at the specified position.
///
public subscript(position: Self.Index) -> Self.Element {
return rootNode.get(position: position, 0, position.value)
}
public typealias Index = PersistentDictionaryIndex
///
/// Manipulating Indices
///
public var startIndex: Self.Index { PersistentDictionaryIndex(value: 0) }
public var endIndex: Self.Index { PersistentDictionaryIndex(value: count) }
public func index(after i: Self.Index) -> Self.Index {
return i + 1
}
///
/// Returns the index for the given key.
///
public func index(forKey key: Key) -> Self.Index? {
return rootNode.index(key, computeHash(key), 0, 0)
}
///
/// Accesses the key-value pair at the specified position.
///
public subscript(position: Self.Index) -> Self.Element {
return rootNode.get(position: position, 0, position.value)
}
}

public struct PersistentDictionaryIndex: Comparable {
let value: Int

public static func < (lhs: Self, rhs: Self) -> Bool {
return lhs.value < rhs.value
}

public static func +(lhs: Self, rhs: Int) -> Self {
return PersistentDictionaryIndex(value: lhs.value + rhs)
}
let value: Int
public static func < (lhs: Self, rhs: Self) -> Bool {
return lhs.value < rhs.value
}
public static func +(lhs: Self, rhs: Int) -> Self {
return PersistentDictionaryIndex(value: lhs.value + rhs)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//===----------------------------------------------------------------------===//

//extension PersistentDictionary: CustomDebugStringConvertible {
// public var debugDescription: String {
// <#code#>
// }
// public var debugDescription: String {
// <#code#>
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//===----------------------------------------------------------------------===//

//extension PersistentDictionary: CustomReflectable {
// public var customMirror: Mirror {
// <#code#>
// }
// public var customMirror: Mirror {
// <#code#>
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension PersistentDictionary: CustomStringConvertible {
public var description: String {
guard count > 0 else {
return "[:]"
}

var result = "["
var first = true
for (key, value) in self {
if first {
first = false
} else {
result += ", "
}
result += "\(key): \(value)"
}
result += "]"
return result
public var description: String {
guard count > 0 else {
return "[:]"
}

var result = "["
var first = true
for (key, value) in self {
if first {
first = false
} else {
result += ", "
}
result += "\(key): \(value)"
}
result += "]"
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//===----------------------------------------------------------------------===//

//extension PersistentDictionary: Decodable where Key: Decodable, Value: Decodable {
// public init(from decoder: Decoder) throws {
// <#code#>
// }
// public init(from decoder: Decoder) throws {
// <#code#>
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//===----------------------------------------------------------------------===//

//extension PersistentDictionary: Encodable where Key: Encodable, Value: Encodable {
// public func encode(to encoder: Encoder) throws {
// <#code#>
// }
// public func encode(to encoder: Encoder) throws {
// <#code#>
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift project authors
// Copyright (c) 2019 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension PersistentDictionary: Equatable where Value: Equatable {
public static func == (lhs: PersistentDictionary<Key, Value>, rhs: PersistentDictionary<Key, Value>) -> Bool {
lhs.rootNode === rhs.rootNode || lhs.rootNode == rhs.rootNode
}
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.rootNode === rhs.rootNode || lhs.rootNode == rhs.rootNode
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift project authors
// Copyright (c) 2019 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension PersistentDictionary: ExpressibleByDictionaryLiteral {
@inlinable
@inline(__always)
public init(dictionaryLiteral elements: (Key, Value)...) {
self.init(uniqueKeysWithValues: elements)
}
@inlinable
@inline(__always)
public init(dictionaryLiteral elements: (Key, Value)...) {
self.init(uniqueKeysWithValues: elements)
}
}
20 changes: 10 additions & 10 deletions Sources/PersistentCollections/PersistentDictionary+Hashable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift project authors
// Copyright (c) 2019 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension PersistentDictionary: Hashable where Value: Hashable {
public func hash(into hasher: inout Hasher) {
var commutativeHash = 0
for (key, value) in self {
var elementHasher = hasher
elementHasher.combine(key)
elementHasher.combine(value)
commutativeHash ^= elementHasher.finalize()
}
hasher.combine(commutativeHash)
public func hash(into hasher: inout Hasher) {
var commutativeHash = 0
for (key, value) in self {
var elementHasher = hasher
elementHasher.combine(key)
elementHasher.combine(value)
commutativeHash ^= elementHasher.finalize()
}
hasher.combine(commutativeHash)
}
}
18 changes: 7 additions & 11 deletions Sources/PersistentCollections/PersistentDictionary+Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@

// TODO: implement a custom `Keys` view rather than relying on an array representation
extension PersistentDictionary {
///
/// A view of a dictionary’s keys.
///
public typealias Keys = [Key]

///
/// A collection containing just the keys of the dictionary.
///
public var keys: Self.Keys /* { get } */ {
self.map { $0.key }
}
/// A view of a dictionary’s keys.
public typealias Keys = [Key]

/// A collection containing just the keys of the dictionary.
public var keys: Self.Keys /* { get } */ {
self.map { $0.key }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift project authors
// Copyright (c) 2019 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension PersistentDictionary: Sequence {
public typealias Element = DictionaryKeyValueTupleIterator<Key, Value>.Element

public __consuming func makeIterator() -> DictionaryKeyValueTupleIterator<Key, Value> {
return DictionaryKeyValueTupleIterator(rootNode: rootNode)
}
public typealias Element = (key: Key, value: Value)
public typealias Iterator = DictionaryKeyValueTupleIterator<Key, Value>

public __consuming func makeIterator() -> Iterator {
return DictionaryKeyValueTupleIterator(rootNode: rootNode)
}
}
16 changes: 6 additions & 10 deletions Sources/PersistentCollections/PersistentDictionary+Values.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@

// TODO: implement a custom `Values` view rather than relying on an array representation
extension PersistentDictionary {
///
/// A view of a dictionary’s values.
///
public typealias Values = [Value]
/// A view of a dictionary’s values.
public typealias Values = [Value]

///
/// A collection containing just the values of the dictionary.
///
public var values: Self.Values /* { get set } */ {
self.map { $0.value }
}
/// A collection containing just the values of the dictionary.
public var values: Self.Values /* { get set } */ {
self.map { $0.value }
}
}
Loading