Skip to content

Commit de66338

Browse files
committedApr 24, 2018
[SE-0206][stdlib] Document Hasher
1 parent 45cb8b7 commit de66338

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
 

‎stdlib/public/core/Hasher.swift

+35
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,44 @@ internal struct _BufferingHasher<Core: _HasherCore> {
246246
}
247247
}
248248

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.
249273
@_fixed_layout // FIXME: Should be resilient (rdar://problem/38549901)
250274
public struct Hasher {
251275
internal typealias Core = _BufferingHasher<_SipHash13Core>
252276

253277
internal var _core: Core
254278

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.
255281
@effects(releasenone)
256282
public init() {
257283
self._core = Core(seed: Hasher._seed)
258284
}
259285

286+
/// Initialize a new hasher using the specified seed value.
260287
@usableFromInline
261288
@effects(releasenone)
262289
internal init(_seed seed: (UInt64, UInt64)) {
@@ -295,6 +322,8 @@ public struct Hasher {
295322
}
296323
}
297324

325+
/// Feed `value` to this hasher, mixing its essential parts into
326+
/// the hasher state.
298327
@inlinable
299328
@inline(__always)
300329
public mutating func combine<H: Hashable>(_ value: H) {
@@ -331,16 +360,22 @@ public struct Hasher {
331360
_core.combine(value)
332361
}
333362

363+
//FIXME: Convert to @usableFromInline internal once integers hash correctly.
334364
@effects(releasenone)
335365
public mutating func _combine(bytes value: UInt64, count: Int) {
336366
_core.combine(bytes: value, count: count)
337367
}
338368

369+
/// Feed the contents of `buffer` into this hasher, mixing it into the hasher
370+
/// state.
339371
@effects(releasenone)
340372
public mutating func combine(bytes: UnsafeRawBufferPointer) {
341373
_core.combine(bytes: bytes)
342374
}
343375

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.
344379
@effects(releasenone)
345380
public mutating func finalize() -> Int {
346381
return Int(truncatingIfNeeded: _core.finalize())

0 commit comments

Comments
 (0)
Please sign in to comment.