/// <summary> /// Shrinking returns the <paramref name="values"/> as is when /// <see cref="Elasticity.Contraction"/> is not provided, in either the /// <paramref name="originalLength"/> or the given length, which ever is /// smallest. When <see cref="Elasticity.Contraction"/> is expected, discards all /// trailing false, or zero, bits from the collection. Maintains the assumption that the /// Most Significant Bits are the least important to keep when they are false, or zero. /// </summary> /// <param name="values"></param> /// <param name="originalLength"></param> /// <param name="elasticity"></param> /// <returns></returns> internal static IEnumerable <bool> Shrink(this IEnumerable <bool> values , int originalLength, Elasticity elasticity) { // ReSharper disable once PossibleMultipleEnumeration var valuesCount = values.Count(); // ReSharper disable once InvertIf // Allowing for the Contraction use case... if (elasticity.Contains(Elasticity.Contraction)) { var width = 0; // ReSharper disable once PossibleMultipleEnumeration while (width < valuesCount && !values.ElementAt(valuesCount - width - 1)) { width++; } // ReSharper disable once PossibleMultipleEnumeration return(values.Take(valuesCount - width)); } // ReSharper disable once PossibleMultipleEnumeration return(values); }
/// <summary> /// Truncation returns the <paramref name="values"/> as is when /// <see cref="Elasticity.Expansion"/> is provided. Otherwise, we take what is there, /// or the <paramref name="originalLength"/> if necessary, which ever is smallest. /// </summary> /// <param name="values"></param> /// <param name="originalLength"></param> /// <param name="elasticity"></param> /// <returns></returns> internal static IEnumerable <bool> Truncate(this IEnumerable <bool> values , int originalLength, Elasticity elasticity) { // ReSharper disable once PossibleMultipleEnumeration var valuesCount = values.Count(); return(elasticity.Contains(Elasticity.Expansion) // ReSharper disable once PossibleMultipleEnumeration ? values // ReSharper disable once PossibleMultipleEnumeration : values.Take(Math.Min(originalLength, valuesCount))); }
// TODO: TBD: consolidate the Set interface to the Elasticity one with default Elasticity... /// <summary> /// /// </summary> /// <param name="index"></param> /// <param name="value"></param> /// <param name="elasticity"></param> /// <inheritdoc /> public void Set(int index, bool value, Elasticity elasticity) { if (elasticity.Contains(Elasticity.Expansion) && index >= Length && _bytes.Count < index / BitCount + 1) { _bytes.AddRange(new byte[index / BitCount + 1]); // TODO: TBD: or simply pass it through Length ? _length = index + 1; } Set(index, value); }
// TODO: TBD: ditto Elastic Set / capture this interface as well... /// <inheritdoc /> public bool Get(int index, Elasticity elasticity) // TODO: TBD: what to do in the use case for Expansion ... => (elasticity.Contains(Elasticity.Expansion) || index < Length) && Get(index);