|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace QRCoder |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Helper methods for <see cref="BitArray"/>. |
| 10 | + /// </summary> |
| 11 | + internal static class BitArrayExtensions |
| 12 | + { |
| 13 | + /// Copies a specified number of elements from one <see cref="BitArray"/> to another starting at the specified offsets. |
| 14 | + /// </summary> |
| 15 | + /// <param name="source">The source <see cref="BitArray"/> from which elements will be copied.</param> |
| 16 | + /// <param name="sourceOffset">The zero-based index in the source <see cref="BitArray"/> at which copying begins.</param> |
| 17 | + /// <param name="destination">The destination <see cref="BitArray"/> to which elements will be copied.</param> |
| 18 | + /// <param name="destinationOffset">The zero-based index in the destination <see cref="BitArray"/> at which storing begins.</param> |
| 19 | + /// <param name="count">The number of elements to copy.</param> |
| 20 | + /// <returns>The index in the destination <see cref="BitArray"/> immediately following the last copied element.</returns> |
| 21 | + public static int CopyTo(this BitArray source, int sourceOffset, BitArray destination, int destinationOffset, int count) |
| 22 | + { |
| 23 | + for (int i = 0; i < count; i++) |
| 24 | + { |
| 25 | + destination[destinationOffset + i] = source[sourceOffset + i]; |
| 26 | + } |
| 27 | + return destinationOffset + count; |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Copies a specified number of elements from one <see cref="BitArray"/> to another starting at the specified offsets, |
| 32 | + /// and reverses every set of 8 bits during the copy operation. |
| 33 | + /// </summary> |
| 34 | + /// <param name="source">The source <see cref="BitArray"/> from which elements will be copied.</param> |
| 35 | + /// <param name="sourceOffset">The zero-based index in the source <see cref="BitArray"/> at which copying begins.</param> |
| 36 | + /// <param name="destination">The destination <see cref="BitArray"/> to which elements will be copied.</param> |
| 37 | + /// <param name="destinationOffset">The zero-based index in the destination <see cref="BitArray"/> at which storing begins.</param> |
| 38 | + /// <param name="count">The number of elements to copy. Must be a multiple of 8.</param> |
| 39 | + /// <returns>The index in the destination <see cref="BitArray"/> immediately following the last copied element.</returns> |
| 40 | + public static int CopyToRev8(this BitArray source, int sourceOffset, BitArray destination, int destinationOffset, int count) |
| 41 | + { |
| 42 | + if (count % 8 != 0) |
| 43 | + { |
| 44 | + throw new ArgumentException("Count must be a multiple of 8.", nameof(count)); |
| 45 | + } |
| 46 | + |
| 47 | + for (int i = 0; i < count; i += 8) |
| 48 | + { |
| 49 | + // Reverse the current set of 8 bits |
| 50 | + for (int j = 0; j < 8; j++) |
| 51 | + { |
| 52 | + destination[destinationOffset + i + j] = source[sourceOffset + i + (7 - j)]; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return destinationOffset + count; |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | +} |
0 commit comments