jding-xyz commented on code in PR #152:
URL: https://github.com/apache/arrow-swift/pull/152#discussion_r3007350234
##########
Sources/Arrow/ArrowBufferBuilder.swift:
##########
@@ -205,80 +205,79 @@ public class VariableBufferBuilder<T>:
ValuesBufferBuilder<T>, ArrowBufferBuilde
public required init() throws {
let values = ArrowBuffer.createBuffer(0, size: UInt(binaryStride))
let nulls = ArrowBuffer.createBuffer(0, size: UInt(binaryStride))
- self.offsets = ArrowBuffer.createBuffer(0, size:
UInt(MemoryLayout<Int32>.stride))
+ self.offsets = ArrowBuffer.createBuffer(1, size:
UInt(MemoryLayout<Int32>.stride))
+ self.offsets.rawPointer.storeBytes(of: Int32(0), as: Int32.self)
super.init(values: values, nulls: nulls, stride: binaryStride)
}
public func append(_ newValue: ItemType?) {
let index = UInt(self.length)
- self.length += 1
let offsetIndex = MemoryLayout<Int32>.stride * Int(index)
- if self.length >= self.offsets.length {
- self.resize(UInt( self.offsets.length + 1))
- }
- var binData: Data
- var isNull = false
- if let val = newValue {
- binData = getBytesFor(val)!
- } else {
- var nullVal = 0
- isNull = true
- binData = Data(bytes: &nullVal, count: MemoryLayout<UInt32>.size)
- }
+ let nextLength = index + 1
+ self.resize(nextLength)
- var currentIndex: Int32 = 0
- var currentOffset: Int32 = Int32(binData.count)
- if index > 0 {
- currentIndex = self.offsets.rawPointer.advanced(by:
offsetIndex).load(as: Int32.self)
- currentOffset += currentIndex
- if currentOffset > self.values.length {
- self.value_resize(UInt(currentOffset))
- }
- }
+ let currentOffset = self.offsets.rawPointer.advanced(by:
offsetIndex).load(as: Int32.self)
+ var nextOffset = currentOffset
- if isNull {
+ if let val = newValue {
+ let binData = getBytesFor(val)!
+ nextOffset += Int32(binData.count)
Review Comment:
Modified as suggested.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]