1414
1515import Foundation
1616
17- public protocol ArrowArrayProtocol < ItemType > {
17+ public protocol ArrowArrayProtocol {
1818 associatedtype ItemType
1919 subscript( _ index: Int ) -> ItemType ? { get }
2020 var offset : Int { get }
@@ -70,18 +70,32 @@ public struct ArrowArrayBoolean: ArrowArrayProtocol {
7070}
7171
7272/// An Arrow array of fixed-width types.
73- struct ArrowArrayFixed < Element , ValueBuffer> : ArrowArrayProtocol
73+ public struct ArrowArrayFixed < ValueBuffer> : ArrowArrayProtocol
7474where
75- Element : Numeric ,
76- ValueBuffer: FixedWidthBufferProtocol < Element >
75+ ValueBuffer : FixedWidthBufferProtocol ,
76+ ValueBuffer. ElementType : Numeric
7777{
78- typealias ItemType = Element
79- let offset : Int
80- let length : Int
78+ public typealias ItemType = ValueBuffer . ElementType
79+
80+ // public typealias ItemType = Element
81+ public let offset : Int
82+ public let length : Int
8183 let nullBuffer : NullBuffer
8284 let valueBuffer : ValueBuffer
8385
84- subscript( index: Int ) -> Element ? {
86+ public init (
87+ offset: Int = 0 ,
88+ length: Int ,
89+ nullBuffer: NullBuffer ,
90+ valueBuffer: ValueBuffer
91+ ) {
92+ self . offset = offset
93+ self . length = length
94+ self . nullBuffer = nullBuffer
95+ self . valueBuffer = valueBuffer
96+ }
97+
98+ public subscript( index: Int ) -> ValueBuffer . ElementType ? {
8599 precondition ( index >= 0 && index < length, " Invalid index. " )
86100 let offsetIndex = self . offset + index
87101 if !self . nullBuffer. isSet ( offsetIndex) {
90104 return valueBuffer [ offsetIndex]
91105 }
92106
93- func slice( offset: Int , length: Int ) -> Self {
107+ public func slice( offset: Int , length: Int ) -> Self {
94108 . init(
95109 offset: offset,
96110 length: length,
@@ -116,7 +130,7 @@ where
116130 let valueBuffer : ValueBuffer
117131
118132 public init (
119- offset: Int ,
133+ offset: Int = 0 ,
120134 length: Int ,
121135 nullBuffer: NullBuffer ,
122136 offsetsBuffer: OffsetsBuffer ,
@@ -162,7 +176,7 @@ where
162176{
163177 typealias ItemType = Date
164178
165- let array : ArrowArrayFixed < Date32 , ValueBuffer >
179+ let array : ArrowArrayFixed < ValueBuffer >
166180
167181 var offset : Int {
168182 array. offset
@@ -192,11 +206,11 @@ where
192206/// An Arrow array of `Date`s with a resolution of 1 second.
193207struct ArrowArrayDate64 < ValueBuffer> : ArrowArrayProtocol
194208where
195- ValueBuffer: FixedWidthBufferProtocol < Int64 >
209+ ValueBuffer: FixedWidthBufferProtocol < Date64 >
196210{
197211 typealias ItemType = Date
198212
199- let array : ArrowArrayFixed < Date64 , ValueBuffer >
213+ let array : ArrowArrayFixed < ValueBuffer >
200214
201215 var offset : Int {
202216 array. offset
@@ -259,3 +273,44 @@ where
259273 )
260274 }
261275}
276+
277+ /// An Arrow struct array.
278+ public struct ArrowStructArray : ArrowArrayProtocol {
279+ public typealias ItemType = [ String : Any ]
280+
281+ let nullBuffer : NullBuffer
282+ public let offset : Int
283+ public let length : Int
284+ public let fields : [ ( name: String , array: any ArrowArrayProtocol ) ]
285+
286+ public init (
287+ offset: Int = 0 ,
288+ length: Int ,
289+ nullBuffer: NullBuffer ,
290+ fields: [ ( name: String , array: any ArrowArrayProtocol ) ]
291+ ) {
292+ self . offset = offset
293+ self . length = length
294+ self . nullBuffer = nullBuffer
295+ self . fields = fields
296+ }
297+
298+ public subscript( index: Int ) -> ItemType ? {
299+ guard nullBuffer. isSet ( offset + index) else { return nil }
300+
301+ var result : [ String : Any ] = [ : ]
302+ for (name, array) in fields {
303+ result [ name] = array. any ( at: index)
304+ }
305+ return result
306+ }
307+
308+ public func slice( offset newOffset: Int , length newLength: Int ) -> Self {
309+ . init(
310+ offset: self . offset + newOffset,
311+ length: newLength,
312+ nullBuffer: nullBuffer,
313+ fields: fields
314+ )
315+ }
316+ }
0 commit comments