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

Hashing updates #747

Merged
merged 2 commits into from
May 23, 2018
Merged
Changes from all commits
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
219 changes: 212 additions & 7 deletions Sources/SwiftProtobuf/HashVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal struct HashVisitor: Visitor {
hashValue = (hashValue ^ hash) &* i_16777619
}

#if !swift(>=4.2)
private mutating func mixMap<K, V: Hashable>(map: Dictionary<K,V>) {
var mapHash = 0
for (k, v) in map {
Expand All @@ -42,7 +43,7 @@ internal struct HashVisitor: Visitor {
}
mix(mapHash)
}

#endif

init() {}

Expand Down Expand Up @@ -81,7 +82,7 @@ internal struct HashVisitor: Visitor {
}

mutating func visitSingularEnumField<E: Enum>(value: E,
fieldNumber: Int) {
fieldNumber: Int) {
mix(fieldNumber)
mix(value.hashValue)
}
Expand All @@ -91,32 +92,236 @@ internal struct HashVisitor: Visitor {
mix(value.hashValue)
}

mutating func visitRepeatedFloatField(value: [Float], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedDoubleField(value: [Double], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedInt32Field(value: [Int32], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedInt64Field(value: [Int64], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedUInt32Field(value: [UInt32], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedUInt64Field(value: [UInt64], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedSInt32Field(value: [Int32], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedSInt64Field(value: [Int64], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedFixed32Field(value: [UInt32], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedFixed64Field(value: [UInt64], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedSFixed32Field(value: [Int32], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedSFixed64Field(value: [Int64], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedBoolField(value: [Bool], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedStringField(value: [String], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedBytesField(value: [Data], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedEnumField<E: Enum>(value: [E], fieldNumber: Int) throws {
mix(fieldNumber)
for v in value {
mix(v.hashValue)
}
}

mutating func visitRepeatedMessageField<M: Message>(value: [M], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitRepeatedGroupField<G: Message>(value: [G], fieldNumber: Int) throws {
mix(fieldNumber)
#if swift(>=4.2)
mix(value.hashValue)
#else
for v in value {
mix(v.hashValue)
}
#endif
}

mutating func visitMapField<KeyType, ValueType: MapValueType>(
fieldType: _ProtobufMap<KeyType, ValueType>.Type,
value: _ProtobufMap<KeyType, ValueType>.BaseType,
fieldNumber: Int
) throws {
mix(fieldNumber)
mixMap(map: value)
#if swift(>=4.2)
mix(value.hashValue)
#else
mixMap(map: value)
#endif
}


mutating func visitMapField<KeyType, ValueType>(
fieldType: _ProtobufEnumMap<KeyType, ValueType>.Type,
value: _ProtobufEnumMap<KeyType, ValueType>.BaseType,
fieldNumber: Int
) throws where ValueType.RawValue == Int {
mix(fieldNumber)
mixMap(map: value)
#if swift(>=4.2)
mix(value.hashValue)
#else
mixMap(map: value)
#endif
}


mutating func visitMapField<KeyType, ValueType>(
fieldType: _ProtobufMessageMap<KeyType, ValueType>.Type,
value: _ProtobufMessageMap<KeyType, ValueType>.BaseType,
fieldNumber: Int
) throws {
mix(fieldNumber)
mixMap(map: value)
#if swift(>=4.2)
mix(value.hashValue)
#else
mixMap(map: value)
#endif
}
}