/// <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 = DistinctMap.GetHash(CosmosString.Create(value)); added = this.stringLength24Plus.Add(uint192Value); } return(added); }
/// <summary> /// Adds a JToken to this map if it hasn't already been added. /// </summary> /// <param name="cosmosElement">The element to add.</param> /// <param name="hash">The hash of the token.</param> /// <returns>Whether or not the item was added to this Distinct Map.</returns> /// <remarks>This function assumes data is added in sorted order.</remarks> public override bool Add(CosmosElement cosmosElement, out UInt192?hash) { hash = DistinctMap.GetHash(cosmosElement); bool added; if (this.lastHash != hash) { this.lastHash = hash.Value; added = true; } else { added = false; } return(added); }
/// <summary> /// Adds a JToken to this map if it hasn't already been added. /// </summary> /// <param name="jToken">The token to add.</param> /// <param name="hash">The hash of the token.</param> /// <returns>Whether or not the item was added to this Distinct Map.</returns> /// <remarks>This function assumes data is added in sorted order.</remarks> public override bool Add(JToken jToken, out UInt192?hash) { hash = DistinctMap.GetHash(jToken); bool added; if (this.lastHash != hash) { this.lastHash = hash.Value; added = true; } else { added = false; } return(added); }
/// <summary> /// Adds an object value to the distinct map. /// </summary> /// <param name="cosmosObject">The object to add.</param> /// <returns>Whether or not the value was successfully added.</returns> private bool AddObjectValue(CosmosObject cosmosObject) { UInt192 hash = DistinctMap.GetHash(cosmosObject); return(this.objects.Add(hash)); }
/// <summary> /// Adds an array value to the distinct map. /// </summary> /// <param name="array">The array to add.</param> /// <returns>Whether or not the value was successfully added.</returns> private bool AddArrayValue(CosmosArray array) { UInt192 hash = DistinctMap.GetHash(array); return(this.arrays.Add(hash)); }