Skip to content

Commit 2f0e3bb

Browse files
committed
Adopt hashing API of Swift 4.2.
1 parent 6a8282b commit 2f0e3bb

File tree

13 files changed

+299
-189
lines changed

13 files changed

+299
-189
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ The following technologies are needed to build the components of the LispKit fra
193193
command-line tool, Xcode and Carthage are not strictly needed. Just for compiling the framework and trying
194194
the command-line tool in Xcode, the Swift Package Manager is not needed.
195195

196-
- [Xcode 9.4](https://developer.apple.com/xcode/)
196+
- [Xcode 10.0](https://developer.apple.com/xcode/)
197197
- [Carthage](https://github.com/Carthage/Carthage)
198198
- [Swift Package Manager](https://swift.org/package-manager/)
199-
- [Swift 4.1](https://developer.apple.com/swift/)
199+
- [Swift 4.2](https://developer.apple.com/swift/)
200200
- [NumberKit](http://github.com/objecthub/swift-numberkit)
201201
- [CommandLineKit](http://github.com/objecthub/swift-commandlinekit)

Sources/LispKit/Base/Owners.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ import Foundation
2727
public struct Owners<T: Reference>: Sequence {
2828

2929
fileprivate struct Entry: Hashable {
30-
let hashValue: Int
30+
let hash: Int
3131
weak var owner: T?
3232

3333
init(_ owner: T) {
34-
self.hashValue = Int(bitPattern: owner.identity)
34+
self.hash = Int(bitPattern: owner.identity)
3535
self.owner = owner
3636
}
3737

38+
func hash(into hasher: inout Hasher) {
39+
hasher.combine(self.hash)
40+
}
41+
3842
static func ==(lhs: Owners<T>.Entry, rhs: Owners<T>.Entry) -> Bool {
3943
return lhs.owner === rhs.owner && lhs.hashValue == rhs.hashValue
4044
}

Sources/LispKit/Base/Reference.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ open class Reference: Hashable {
3232
return String(self.identity, radix: 16)
3333
}
3434

35-
public final var hashValue: Int {
36-
return ObjectIdentifier(self).hashValue
35+
public final func hash(into hasher: inout Hasher) {
36+
hasher.combine(ObjectIdentifier(self))
3737
}
3838

3939
open var typeDescription: String {

Sources/LispKit/Compiler/Checkpointer.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,21 @@ public enum CheckpointData: Hashable, CustomStringConvertible {
110110
case valueBinding(Symbol)
111111
case expansion(Expr)
112112

113-
public var hashValue: Int {
113+
public func hash(into hasher: inout Hasher) {
114114
switch self {
115115
case .systemDefined:
116-
return 1
116+
hasher.combine(1)
117117
case .imported:
118-
return 2
118+
hasher.combine(2)
119119
case .fromGlobalEnv(let expr):
120-
return expr.hashValue &* 31 &+ 3
120+
hasher.combine(3)
121+
hasher.combine(expr)
121122
case .valueBinding(let sym):
122-
return sym.hashValue &* 31 &+ 4
123+
hasher.combine(4)
124+
hasher.combine(sym)
123125
case .expansion(let expr):
124-
return expr.hashValue &* 31 &+ 5
126+
hasher.combine(5)
127+
hasher.combine(expr)
125128
}
126129
}
127130

Sources/LispKit/Compiler/Env.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,15 @@ public enum WeakEnv: Hashable {
220220
}
221221

222222
/// Hash value of this weak environment
223-
public var hashValue: Int {
223+
public func hash(into hasher: inout Hasher) {
224224
switch self {
225225
case .global(let box):
226-
return ObjectIdentifier(box).hashValue
226+
return hasher.combine(ObjectIdentifier(box))
227227
case .local(let box):
228-
return ObjectIdentifier(box).hashValue
228+
return hasher.combine(ObjectIdentifier(box))
229229
}
230230
}
231231

232-
233232
/// Compares two weak environments. Two weak environments are the same if they have the same
234233
/// type (i.e. same enumeration case) and for local environments, the bindings groups are
235234
/// identical (same object).

Sources/LispKit/Compiler/RuntimeError.swift

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,12 @@ public class RuntimeError: Error, Hashable, CustomStringConvertible {
149149
}
150150
}
151151

152-
public var hashValue: Int {
153-
var res = 0
152+
public func hash(into hasher: inout Hasher) {
154153
for irritant in self.irritants {
155-
res = res &* 31 &+ irritant.hashValue
154+
hasher.combine(irritant)
156155
}
157-
res = res &* 31 &+ self.descriptor.hashValue
158-
return res &* 31 &+ self.pos.hashValue
156+
hasher.combine(self.descriptor)
157+
hasher.combine(self.pos)
159158
}
160159

161160
public var message: String {
@@ -554,37 +553,40 @@ public enum ErrorDescriptor: Hashable {
554553
}
555554
}
556555

557-
public var hashValue: Int {
556+
public func hash(into hasher: inout Hasher) {
558557
switch self {
559558
case .lexical(let error):
560-
return error.hashValue &* 31
559+
hasher.combine(error)
561560
case .syntax(let error):
562-
return error.hashValue &* 31 &+ 1
561+
hasher.combine(1)
562+
hasher.combine(error)
563563
case .type(let found, let expected):
564-
return (found.hashValue &* 31 &+ expected.hashValue) &* 31 &+ 2
564+
hasher.combine(2)
565+
hasher.combine(found)
566+
hasher.combine(expected)
565567
case .range(let fun, let argn, let low, let high):
566-
var res = (low.hashValue &* 31 &+ high.hashValue) &* 31
567-
if let fun = fun {
568-
res = (res + fun.hashValue) &* 31
569-
}
570-
if let argn = argn {
571-
res = (res + argn.hashValue) &* 31
572-
}
573-
return res &+ 3
568+
hasher.combine(3)
569+
hasher.combine(fun)
570+
hasher.combine(argn)
571+
hasher.combine(low)
572+
hasher.combine(high)
574573
case .argumentCount(let fun, let min, let max):
575-
var res = (min.hashValue &* 31 &+ max.hashValue) &* 31
576-
if let fun = fun {
577-
res = (res + fun.hashValue) &* 31
578-
}
579-
return res &+ 4
574+
hasher.combine(4)
575+
hasher.combine(fun)
576+
hasher.combine(min)
577+
hasher.combine(max)
580578
case .eval(let error):
581-
return error.hashValue &* 31 &+ 5
579+
hasher.combine(5)
580+
hasher.combine(error)
582581
case .os(let error):
583-
return error.hashValue &* 31 &+ 6
582+
hasher.combine(6)
583+
hasher.combine(error)
584584
case .abortion:
585-
return 7
585+
hasher.combine(7)
586586
case .custom(let kind, let message):
587-
return (kind.hashValue &* 31 &+ message.hashValue) &* 31 &+ 8
587+
hasher.combine(8)
588+
hasher.combine(kind)
589+
hasher.combine(message)
588590
}
589591
}
590592

Sources/LispKit/Compiler/SourcePosition.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ public struct SourcePosition: Equatable, Hashable, CustomStringConvertible {
5353
pos.col > UInt16.max ? SourcePosition.unknownColumn : UInt16(pos.col))
5454
}
5555

56-
public var hashValue: Int {
57-
return ((self.sourceId.hashValue &* 31) &+ self.line.hashValue) &* 31 &+ self.column.hashValue
56+
public func hash(into hasher: inout Hasher) {
57+
hasher.combine(self.sourceId)
58+
hasher.combine(self.line)
59+
hasher.combine(self.column)
5860
}
5961

6062
public var isUnknown: Bool {

Sources/LispKit/Data/Equality.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ struct Equality: Hashable {
3333
self.ref2 = ref2
3434
}
3535

36-
var hashValue: Int {
37-
return ref1.hashValue &+ ref2.hashValue
36+
func hash(into hasher: inout Hasher) {
37+
hasher.combine(ref1.hashValue &+ ref2.hashValue)
3838
}
3939

4040
static func ==(lhs: Equality, rhs: Equality) -> Bool {

Sources/LispKit/Data/Expr.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,8 @@ public enum Expr: Trackable, Hashable {
363363
}
364364
}
365365

366-
public var hashValue: Int {
367-
return equalHash(self)
366+
public func hash(into hasher: inout Hasher) {
367+
equalHash(self, into: &hasher)
368368
}
369369
}
370370

0 commit comments

Comments
 (0)