mr-smidge commented on a change in pull request #7158:
URL: https://github.com/apache/arrow/pull/7158#discussion_r428556672



##########
File path: csharp/src/Apache.Arrow/ArrowBuffer.BitPackedBuilder.cs
##########
@@ -0,0 +1,268 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace Apache.Arrow
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Diagnostics;
+    using Apache.Arrow.Memory;
+
+    public partial struct ArrowBuffer
+    {
+        /// <summary>
+        /// The <see cref="ArrowBuffer.BitPackedBuilder"/> class is a 
complement to <see cref="ArrowBuffer.Builder{T}"/>
+        /// and is designed for boolean fields, which are efficiently 
bit-packed into byte-aligned memory.
+        /// </summary>
+        public class BitPackedBuilder
+        {
+            private const int DefaultBitCapacity = 8;
+
+            /// <summary>
+            /// Gets the number of bits of current capacity.
+            /// </summary>
+            public int BitCapacity { get; private set; }
+
+            /// <summary>
+            /// Gets the number of bits currently appended.
+            /// </summary>
+            public int BitCount { get; private set; }
+
+            /// <summary>
+            /// Gets the raw byte memory underpinning the builder.
+            /// </summary>
+            public Memory<byte> Memory { get; private set; }
+
+            /// <summary>
+            /// Gets the span of (bit-packed byte) memory underpinning the 
builder.
+            /// </summary>
+            public Span<byte> Span => Memory.Span;
+
+            /// <summary>
+            /// Creates an instance of the <see cref="BitPackedBuilder"/> 
class.
+            /// </summary>
+            /// <param name="bitCapacity">Number of bits of initial capacity 
to reserve.</param>
+            public BitPackedBuilder(int bitCapacity = DefaultBitCapacity)
+            {
+                Memory = new byte[BitUtility.ByteCount(bitCapacity)];
+                BitCapacity = bitCapacity;
+                BitCount = 0;
+            }
+
+            /// <summary>
+            /// Append a single bit.
+            /// </summary>
+            /// <param name="bit">Bit to append.</param>
+            /// <returns>Returns the builder (for fluent-style 
composition).</returns>
+            public BitPackedBuilder Append(bool bit)
+            {
+                if (BitCount % 8 == 0)
+                {
+                    // Append a new byte to the buffer when needed.
+                    EnsureAdditionalCapacity(1);
+                    Span[BitCount / 8] = 0;

Review comment:
       I've removed the unnecessary zero'ing out.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to