/// <summary> /// Gets the hash of a byte array. /// </summary> /// <param name="bytes">The bytes.</param> /// <param name="seed">The seed.</param> /// <returns>The hash.</returns> public UInt192 GetHash(byte[] bytes, UInt192 seed) { Documents.UInt128 hash128 = MurmurHash3.Hash128(bytes, bytes.Length, Documents.UInt128.Create(seed.GetLow(), seed.GetMid())); ulong hash64 = MurmurHash3.Hash64(bytes, bytes.Length, seed.GetHigh()); return(UInt192.Create(hash128.GetLow(), hash128.GetHigh(), hash64)); }
/// <summary> /// Gets the hash of a byte array. /// </summary> /// <param name="bytes">The bytes.</param> /// <param name="seed">The seed.</param> /// <returns>The hash.</returns> public static UInt192 GetHash(byte[] bytes, UInt192 seed) { // TODO: Have MurmurHash3 work on Span<T> instead. Documents.UInt128 hash128 = Microsoft.Azure.Documents.Routing.MurmurHash3.Hash128( bytes, bytes.Length, Documents.UInt128.Create(seed.GetLow(), seed.GetMid())); ulong hash64 = Microsoft.Azure.Documents.Routing.MurmurHash3.Hash64(bytes, bytes.Length, seed.GetHigh()); return(UInt192.Create(hash128.GetLow(), hash128.GetHigh(), hash64)); }
/// <summary> /// Adds a string to the distinct map. /// </summary> /// <param name="value">The string to add.</param> /// <returns>Whether or not the value was successfully added.</returns> private bool AddStringValue(string value) { bool added = false; int utf8Length = Encoding.UTF8.GetByteCount(value); // If you fit the string with full fidelity in 24 bytes, then you might as well just hash the string. if (utf8Length <= UnorderdDistinctMap.UInt192Length) { // Zero out the array since you want all trailing bytes to be 0 for the conversions that happen next. Array.Clear(this.utf8Buffer, 0, this.utf8Buffer.Length); Encoding.UTF8.GetBytes(value, 0, utf8Length, this.utf8Buffer, 0); if (utf8Length == 0) { added = this.AddSimpleValue(SimpleValues.EmptyString); } else if (utf8Length <= UnorderdDistinctMap.UIntLength) { uint uintValue = BitConverter.ToUInt32(this.utf8Buffer, 0); added = this.stringsLength4.Add(uintValue); } else if (utf8Length <= UnorderdDistinctMap.ULongLength) { ulong uLongValue = BitConverter.ToUInt64(this.utf8Buffer, 0); added = this.stringLength8.Add(uLongValue); } else if (utf8Length <= UnorderdDistinctMap.UInt128Length) { UInt128 uInt128Value = UInt128.FromByteArray(this.utf8Buffer, 0); added = this.stringLength16.Add(uInt128Value); } else { UInt192 uInt192Value = UInt192.FromByteArray(this.utf8Buffer, 0); added = this.stringLength24.Add(uInt192Value); } } else { // Else the string is too large and we will just store the hash. UInt192 uint192Value = DistinctHash.GetHash(CosmosString.Create(value)); added = this.stringLength24Plus.Add(uint192Value); } return(added); }