@@ -246,17 +246,44 @@ internal struct _BufferingHasher<Core: _HasherCore> {
246
246
}
247
247
}
248
248
249
+ /// Represents the universal hash function used by `Set` and `Dictionary`.
250
+ ///
251
+ /// `Hasher` can be used to map an arbitrary sequence of bytes to an integer
252
+ /// hash value. You can feed data to the hasher using a series of calls to
253
+ /// mutating `combine` methods. When you've finished feeding the hasher, the
254
+ /// hash value can be retrieved by calling `finalize()`:
255
+ ///
256
+ /// var hasher = Hasher()
257
+ /// hasher.combine(23)
258
+ /// hasher.combine("Hello")
259
+ /// let hashValue = hasher.finalize()
260
+ ///
261
+ /// Within the execution of a Swift program, `Hasher` guarantees that
262
+ /// `finalize()` will always return the same value as long as it is fed the
263
+ /// exact same sequence of bytes. However, the underlying hash algorithm is
264
+ /// designed to exhibit avalanche effects: slight changes to the seed or the
265
+ /// input byte sequence will typically produce drastic changes in the generated
266
+ /// hash value.
267
+ ///
268
+ /// - Note: Do not save or otherwise reuse hash values across executions of your
269
+ /// program. `Hasher` is usually randomly seeded, which means it will return
270
+ /// different values on every new execution of your program. The hash
271
+ /// algorithm implemented by `Hasher` may itself change between any two
272
+ /// versions of the standard library.
249
273
@_fixed_layout // FIXME: Should be resilient (rdar://problem/38549901)
250
274
public struct Hasher {
251
275
internal typealias Core = _BufferingHasher < _SipHash13Core >
252
276
253
277
internal var _core : Core
254
278
279
+ /// Initialize a new hasher. The hasher uses a per-execution seed value that
280
+ /// is set during process startup, usually from a high-quality random source.
255
281
@effects ( releasenone)
256
282
public init ( ) {
257
283
self . _core = Core ( seed: Hasher . _seed)
258
284
}
259
285
286
+ /// Initialize a new hasher using the specified seed value.
260
287
@usableFromInline
261
288
@effects ( releasenone)
262
289
internal init ( _seed seed: ( UInt64 , UInt64 ) ) {
@@ -295,6 +322,8 @@ public struct Hasher {
295
322
}
296
323
}
297
324
325
+ /// Feed `value` to this hasher, mixing its essential parts into
326
+ /// the hasher state.
298
327
@inlinable
299
328
@inline ( __always)
300
329
public mutating func combine< H: Hashable > ( _ value: H ) {
@@ -331,16 +360,22 @@ public struct Hasher {
331
360
_core. combine ( value)
332
361
}
333
362
363
+ //FIXME: Convert to @usableFromInline internal once integers hash correctly.
334
364
@effects ( releasenone)
335
365
public mutating func _combine( bytes value: UInt64 , count: Int ) {
336
366
_core. combine ( bytes: value, count: count)
337
367
}
338
368
369
+ /// Feed the contents of `buffer` into this hasher, mixing it into the hasher
370
+ /// state.
339
371
@effects ( releasenone)
340
372
public mutating func combine( bytes: UnsafeRawBufferPointer ) {
341
373
_core. combine ( bytes: bytes)
342
374
}
343
375
376
+ /// Finalize the hasher state and return the hash value.
377
+ /// Finalizing invalidates the hasher; additional bits cannot be combined
378
+ /// into it, and it cannot be finalized again.
344
379
@effects ( releasenone)
345
380
public mutating func finalize( ) -> Int {
346
381
return Int ( truncatingIfNeeded: _core. finalize ( ) )
0 commit comments