Skip to content

Unified: BinaryInteger/hashValueΒ #102

@oscbyspro

Description

@oscbyspro

I can return equal hash values for all binary integers with equal values:

I'm (un)reasonably pleased with this unification ( πŸ˜„ πŸš€ ✨ ).

The (#101) approach does not work past 64 bits.

Infinite and negative values collide but that's OK.

#expect(random.hashValue == IXL(load: random).hashValue)
#expect(random.hashValue == UXL(load: random).hashValue)
extension BinaryInteger {
    @inlinable public func hash(into hasher: inout Swift.Hasher) {
        self.withUnsafeBinaryIntegerElements(as: U8.self) {
            hasher.combine(bytes: $0.normalized().body.bytes())
            hasher.combine($0.appendix)
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    additionoh, so shiny!brrrsuch code, much wowsubtractionhuh? where did it go?

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions