/// <summary> /// Appends the <paramref name="BitNames"/> (or the indexes if no names) of all bits which are true to the /// Stringbuilder <paramref name="Sb"/> using the <paramref name="Delimiter"/>. /// There may be given special texts for the case of all bits or no bits set. /// </summary> /// <param name="Bits">the Bitarray</param> /// <param name="Sb">the Stringbuilder to which the names of the set bits are appended</param> /// <param name="BitNames">defines the name of each bit i by BitNames[i].ToString(), may be null</param> /// <param name="Delimiter">the delimiter to be used to separate names, default</param> /// <param name="All">Text if all bits are set or null (default)</param> /// <param name="Empty">Text if no Bits are set or null (default)</param> /// <returns>the argument <paramref name="Sb"/></returns> /// <exception cref="ArgumentOutOfRangeException">if </exception> internal static StringBuilder BitsToStringbuilder( this BitArray Bits, StringBuilder Sb, Object[]?BitNames = null, String Delimiter = ", ", String?All = null, String?Empty = null) { if (BitNames != null && BitNames.Length < Bits.Length) { throw new ArgumentOutOfRangeException ($"Length of {nameof(BitNames)} == {BitNames.Length} is less than the number of bits == {Bits.Length} in call of {nameof(BitsToStringbuilder)}"); } Boolean isFirst = true; if (All != null && Bits.All()) { Sb.Append(All); } else { for (Int32 i = 0; i < Bits.Length; i++) { if (Bits[i]) { if (isFirst) { isFirst = false; } else { Sb.Append(Delimiter); } if (BitNames == null) { Sb.Append(i); } else { Sb.Append(BitNames[i].ToString()); } } } if (isFirst && Empty != null) { Sb.Append(Empty); } } return(Sb); }
/// <summary> /// some tests /// </summary> public static void Test() { // tests functions of BitAray which use CopyTo and expect a special order of the returned bits var b = new BitArray(0); Debug.Assert(b.Empty()); Debug.Assert(b.All()); Debug.Assert(b.PopulationCount() == 0); b.Not(); Debug.Assert(b.Empty()); Debug.Assert(b.All()); Debug.Assert(b.PopulationCount() == 0); b = new BitArray(1); var ia = new Int32[1]; b.CopyTo(ia, 0); Debug.Assert(ia[0] == 0); Debug.Assert(b.Empty()); Debug.Assert(b.PopulationCount() == 0); b.Set(0, true); b.CopyTo(ia, 0); Debug.Assert(ia[0] == 1); Debug.Assert(b.All()); Debug.Assert(b.PopulationCount() == 1); b.Not(); Debug.Assert(b.Empty()); Debug.Assert(b.PopulationCount() == 0); b.SetAll(true); Debug.Assert(b.All()); Debug.Assert(b.PopulationCount() == 1); b = new BitArray(33); Debug.Assert(b.Empty()); Debug.Assert(b.PopulationCount() == 0); b.Not(); Debug.Assert(b.All()); Debug.Assert(b.PopulationCount() == 33); b.Not(); Debug.Assert(b.Empty()); Debug.Assert(b.PopulationCount() == 0); for (Int32 i = 0; i < 33; i++) { b[i] = true; } Debug.Assert(b.All()); Debug.Assert(b.PopulationCount() == 33); b.Not(); Debug.Assert(b.Empty()); Debug.Assert(b.PopulationCount() == 0); b.Not(); b[32] = false; Debug.Assert(!b.All()); Debug.Assert(!b.Empty()); Debug.Assert(b.PopulationCount() == 32); b.Not(); Debug.Assert(!b.All()); Debug.Assert(!b.Empty()); Debug.Assert(b.PopulationCount() == 1); }