github.com/parquet-go/parquet-go@v0.21.1-0.20240501160520-b3c3a0c3ed6f/sparse/array.go (about) 1 package sparse 2 3 import ( 4 "time" 5 "unsafe" 6 ) 7 8 type Array struct{ array } 9 10 func UnsafeArray(base unsafe.Pointer, length int, offset uintptr) Array { 11 return Array{makeArray(base, uintptr(length), offset)} 12 } 13 14 func (a Array) Len() int { return int(a.len) } 15 func (a Array) Index(i int) unsafe.Pointer { return a.index(i) } 16 func (a Array) Slice(i, j int) Array { return Array{a.slice(i, j)} } 17 func (a Array) Offset(off uintptr) Array { return Array{a.offset(off)} } 18 func (a Array) BoolArray() BoolArray { return BoolArray{a.array} } 19 func (a Array) Int8Array() Int8Array { return Int8Array{a.array} } 20 func (a Array) Int16Array() Int16Array { return Int16Array{a.array} } 21 func (a Array) Int32Array() Int32Array { return Int32Array{a.array} } 22 func (a Array) Int64Array() Int64Array { return Int64Array{a.array} } 23 func (a Array) Float32Array() Float32Array { return Float32Array{a.array} } 24 func (a Array) Float64Array() Float64Array { return Float64Array{a.array} } 25 func (a Array) Uint8Array() Uint8Array { return Uint8Array{a.array} } 26 func (a Array) Uint16Array() Uint16Array { return Uint16Array{a.array} } 27 func (a Array) Uint32Array() Uint32Array { return Uint32Array{a.array} } 28 func (a Array) Uint64Array() Uint64Array { return Uint64Array{a.array} } 29 func (a Array) Uint128Array() Uint128Array { return Uint128Array{a.array} } 30 func (a Array) StringArray() StringArray { return StringArray{a.array} } 31 func (a Array) TimeArray() TimeArray { return TimeArray{a.array} } 32 33 type array struct { 34 ptr unsafe.Pointer 35 len uintptr 36 off uintptr 37 } 38 39 func makeArray(base unsafe.Pointer, length, offset uintptr) array { 40 return array{ptr: base, len: length, off: offset} 41 } 42 43 func (a array) index(i int) unsafe.Pointer { 44 if uintptr(i) >= a.len { 45 panic("index out of bounds") 46 } 47 return unsafe.Add(a.ptr, a.off*uintptr(i)) 48 } 49 50 func (a array) slice(i, j int) array { 51 if uintptr(i) > a.len || uintptr(j) > a.len || i > j { 52 panic("slice index out of bounds") 53 } 54 return array{ 55 ptr: unsafe.Add(a.ptr, a.off*uintptr(i)), 56 len: uintptr(j - i), 57 off: a.off, 58 } 59 } 60 61 func (a array) offset(off uintptr) array { 62 if a.ptr == nil { 63 panic("offset of nil array") 64 } 65 return array{ 66 ptr: unsafe.Add(a.ptr, off), 67 len: a.len, 68 off: a.off, 69 } 70 } 71 72 type BoolArray struct{ array } 73 74 func MakeBoolArray(values []bool) BoolArray { 75 return BoolArray{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 1)} 76 } 77 78 func UnsafeBoolArray(base unsafe.Pointer, length int, offset uintptr) BoolArray { 79 return BoolArray{makeArray(base, uintptr(length), offset)} 80 } 81 82 func (a BoolArray) Len() int { return int(a.len) } 83 func (a BoolArray) Index(i int) bool { return *(*byte)(a.index(i)) != 0 } 84 func (a BoolArray) Slice(i, j int) BoolArray { return BoolArray{a.slice(i, j)} } 85 func (a BoolArray) Uint8Array() Uint8Array { return Uint8Array{a.array} } 86 func (a BoolArray) UnsafeArray() Array { return Array{a.array} } 87 88 type Int8Array struct{ array } 89 90 func MakeInt8Array(values []int8) Int8Array { 91 return Int8Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 8)} 92 } 93 94 func UnsafeInt8Array(base unsafe.Pointer, length int, offset uintptr) Int8Array { 95 return Int8Array{makeArray(base, uintptr(length), offset)} 96 } 97 98 func (a Int8Array) Len() int { return int(a.len) } 99 func (a Int8Array) Index(i int) int8 { return *(*int8)(a.index(i)) } 100 func (a Int8Array) Slice(i, j int) Int8Array { return Int8Array{a.slice(i, j)} } 101 func (a Int8Array) Uint8Array() Uint8Array { return Uint8Array{a.array} } 102 func (a Int8Array) UnsafeArray() Array { return Array{a.array} } 103 104 type Int16Array struct{ array } 105 106 func MakeInt16Array(values []int16) Int16Array { 107 return Int16Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 8)} 108 } 109 110 func UnsafeInt16Array(base unsafe.Pointer, length int, offset uintptr) Int16Array { 111 return Int16Array{makeArray(base, uintptr(length), offset)} 112 } 113 114 func (a Int16Array) Len() int { return int(a.len) } 115 func (a Int16Array) Index(i int) int16 { return *(*int16)(a.index(i)) } 116 func (a Int16Array) Slice(i, j int) Int16Array { return Int16Array{a.slice(i, j)} } 117 func (a Int16Array) Int8Array() Int8Array { return Int8Array{a.array} } 118 func (a Int16Array) Uint8Array() Uint8Array { return Uint8Array{a.array} } 119 func (a Int16Array) Uint16Array() Uint16Array { return Uint16Array{a.array} } 120 func (a Int16Array) UnsafeArray() Array { return Array{a.array} } 121 122 type Int32Array struct{ array } 123 124 func MakeInt32Array(values []int32) Int32Array { 125 return Int32Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 4)} 126 } 127 128 func UnsafeInt32Array(base unsafe.Pointer, length int, offset uintptr) Int32Array { 129 return Int32Array{makeArray(base, uintptr(length), offset)} 130 } 131 132 func (a Int32Array) Len() int { return int(a.len) } 133 func (a Int32Array) Index(i int) int32 { return *(*int32)(a.index(i)) } 134 func (a Int32Array) Slice(i, j int) Int32Array { return Int32Array{a.slice(i, j)} } 135 func (a Int32Array) Int8Array() Int8Array { return Int8Array{a.array} } 136 func (a Int32Array) Int16Array() Int16Array { return Int16Array{a.array} } 137 func (a Int32Array) Uint8Array() Uint8Array { return Uint8Array{a.array} } 138 func (a Int32Array) Uint16Array() Uint16Array { return Uint16Array{a.array} } 139 func (a Int32Array) Uint32Array() Uint32Array { return Uint32Array{a.array} } 140 func (a Int32Array) UnsafeArray() Array { return Array{a.array} } 141 142 type Int64Array struct{ array } 143 144 func MakeInt64Array(values []int64) Int64Array { 145 return Int64Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 8)} 146 } 147 148 func UnsafeInt64Array(base unsafe.Pointer, length int, offset uintptr) Int64Array { 149 return Int64Array{makeArray(base, uintptr(length), offset)} 150 } 151 152 func (a Int64Array) Len() int { return int(a.len) } 153 func (a Int64Array) Index(i int) int64 { return *(*int64)(a.index(i)) } 154 func (a Int64Array) Slice(i, j int) Int64Array { return Int64Array{a.slice(i, j)} } 155 func (a Int64Array) Int8Array() Int8Array { return Int8Array{a.array} } 156 func (a Int64Array) Int16Array() Int16Array { return Int16Array{a.array} } 157 func (a Int64Array) Int32Array() Int32Array { return Int32Array{a.array} } 158 func (a Int64Array) Uint8Array() Uint8Array { return Uint8Array{a.array} } 159 func (a Int64Array) Uint16Array() Uint16Array { return Uint16Array{a.array} } 160 func (a Int64Array) Uint32Array() Uint32Array { return Uint32Array{a.array} } 161 func (a Int64Array) Uint64Array() Uint64Array { return Uint64Array{a.array} } 162 func (a Int64Array) UnsafeArray() Array { return Array{a.array} } 163 164 type Float32Array struct{ array } 165 166 func MakeFloat32Array(values []float32) Float32Array { 167 return Float32Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 4)} 168 } 169 170 func UnsafeFloat32Array(base unsafe.Pointer, length int, offset uintptr) Float32Array { 171 return Float32Array{makeArray(base, uintptr(length), offset)} 172 } 173 174 func (a Float32Array) Len() int { return int(a.len) } 175 func (a Float32Array) Index(i int) float32 { return *(*float32)(a.index(i)) } 176 func (a Float32Array) Slice(i, j int) Float32Array { return Float32Array{a.slice(i, j)} } 177 func (a Float32Array) Array() Array { return Array{a.array} } 178 func (a Float32Array) Uint32Array() Uint32Array { return Uint32Array{a.array} } 179 func (a Float32Array) UnsafeArray() Array { return Array{a.array} } 180 181 type Float64Array struct{ array } 182 183 func MakeFloat64Array(values []float64) Float64Array { 184 return Float64Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 8)} 185 } 186 187 func UnsafeFloat64Array(base unsafe.Pointer, length int, offset uintptr) Float64Array { 188 return Float64Array{makeArray(base, uintptr(length), offset)} 189 } 190 191 func (a Float64Array) Len() int { return int(a.len) } 192 func (a Float64Array) Index(i int) float64 { return *(*float64)(a.index(i)) } 193 func (a Float64Array) Slice(i, j int) Float64Array { return Float64Array{a.slice(i, j)} } 194 func (a Float64Array) Uint64Array() Uint64Array { return Uint64Array{a.array} } 195 func (a Float64Array) UnsafeArray() Array { return Array{a.array} } 196 197 type Uint8Array struct{ array } 198 199 func MakeUint8Array(values []uint8) Uint8Array { 200 return Uint8Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 8)} 201 } 202 203 func UnsafeUint8Array(base unsafe.Pointer, length int, offset uintptr) Uint8Array { 204 return Uint8Array{makeArray(base, uintptr(length), offset)} 205 } 206 207 func (a Uint8Array) Len() int { return int(a.len) } 208 func (a Uint8Array) Index(i int) uint8 { return *(*uint8)(a.index(i)) } 209 func (a Uint8Array) Slice(i, j int) Uint8Array { return Uint8Array{a.slice(i, j)} } 210 func (a Uint8Array) UnsafeArray() Array { return Array{a.array} } 211 212 type Uint16Array struct{ array } 213 214 func MakeUint16Array(values []uint16) Uint16Array { 215 return Uint16Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 8)} 216 } 217 218 func UnsafeUint16Array(base unsafe.Pointer, length int, offset uintptr) Uint16Array { 219 return Uint16Array{makeArray(base, uintptr(length), offset)} 220 } 221 222 func (a Uint16Array) Len() int { return int(a.len) } 223 func (a Uint16Array) Index(i int) uint16 { return *(*uint16)(a.index(i)) } 224 func (a Uint16Array) Slice(i, j int) Uint16Array { return Uint16Array{a.slice(i, j)} } 225 func (a Uint16Array) Uint8Array() Uint8Array { return Uint8Array{a.array} } 226 func (a Uint16Array) UnsafeArray() Array { return Array{a.array} } 227 228 type Uint32Array struct{ array } 229 230 func MakeUint32Array(values []uint32) Uint32Array { 231 return Uint32Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 4)} 232 } 233 234 func UnsafeUint32Array(base unsafe.Pointer, length int, offset uintptr) Uint32Array { 235 return Uint32Array{makeArray(base, uintptr(length), offset)} 236 } 237 238 func (a Uint32Array) Len() int { return int(a.len) } 239 func (a Uint32Array) Index(i int) uint32 { return *(*uint32)(a.index(i)) } 240 func (a Uint32Array) Slice(i, j int) Uint32Array { return Uint32Array{a.slice(i, j)} } 241 func (a Uint32Array) Uint8Array() Uint8Array { return Uint8Array{a.array} } 242 func (a Uint32Array) Uint16Array() Uint16Array { return Uint16Array{a.array} } 243 func (a Uint32Array) UnsafeArray() Array { return Array{a.array} } 244 245 type Uint64Array struct{ array } 246 247 func MakeUint64Array(values []uint64) Uint64Array { 248 return Uint64Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 8)} 249 } 250 251 func UnsafeUint64Array(base unsafe.Pointer, length int, offset uintptr) Uint64Array { 252 return Uint64Array{makeArray(base, uintptr(length), offset)} 253 } 254 255 func (a Uint64Array) Len() int { return int(a.len) } 256 func (a Uint64Array) Index(i int) uint64 { return *(*uint64)(a.index(i)) } 257 func (a Uint64Array) Slice(i, j int) Uint64Array { return Uint64Array{a.slice(i, j)} } 258 func (a Uint64Array) Uint8Array() Uint8Array { return Uint8Array{a.array} } 259 func (a Uint64Array) Uint16Array() Uint16Array { return Uint16Array{a.array} } 260 func (a Uint64Array) Uint32Array() Uint32Array { return Uint32Array{a.array} } 261 func (a Uint64Array) UnsafeArray() Array { return Array{a.array} } 262 263 type Uint128Array struct{ array } 264 265 func MakeUint128Array(values [][16]byte) Uint128Array { 266 return Uint128Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 16)} 267 } 268 269 func UnsafeUint128Array(base unsafe.Pointer, length int, offset uintptr) Uint128Array { 270 return Uint128Array{makeArray(base, uintptr(length), offset)} 271 } 272 273 func (a Uint128Array) Len() int { return int(a.len) } 274 func (a Uint128Array) Index(i int) [16]byte { return *(*[16]byte)(a.index(i)) } 275 func (a Uint128Array) Slice(i, j int) Uint128Array { return Uint128Array{a.slice(i, j)} } 276 func (a Uint128Array) Uint8Array() Uint8Array { return Uint8Array{a.array} } 277 func (a Uint128Array) Uint16Array() Uint16Array { return Uint16Array{a.array} } 278 func (a Uint128Array) Uint32Array() Uint32Array { return Uint32Array{a.array} } 279 func (a Uint128Array) Uint64Array() Uint64Array { return Uint64Array{a.array} } 280 func (a Uint128Array) UnsafeArray() Array { return Array{a.array} } 281 282 type StringArray struct{ array } 283 284 func MakeStringArray(values []string) StringArray { 285 const sizeOfString = unsafe.Sizeof("") 286 return StringArray{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), sizeOfString)} 287 } 288 289 func UnsafeStringArray(base unsafe.Pointer, length int, offset uintptr) StringArray { 290 return StringArray{makeArray(base, uintptr(length), offset)} 291 } 292 293 func (a StringArray) Len() int { return int(a.len) } 294 func (a StringArray) Index(i int) string { return *(*string)(a.index(i)) } 295 func (a StringArray) Slice(i, j int) StringArray { return StringArray{a.slice(i, j)} } 296 func (a StringArray) UnsafeArray() Array { return Array{a.array} } 297 298 type TimeArray struct{ array } 299 300 func MakeTimeArray(values []time.Time) TimeArray { 301 const sizeOfTime = unsafe.Sizeof(time.Time{}) 302 return TimeArray{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), sizeOfTime)} 303 } 304 305 func UnsafeTimeArray(base unsafe.Pointer, length int, offset uintptr) TimeArray { 306 return TimeArray{makeArray(base, uintptr(length), offset)} 307 } 308 309 func (a TimeArray) Len() int { return int(a.len) } 310 func (a TimeArray) Index(i int) time.Time { return *(*time.Time)(a.index(i)) } 311 func (a TimeArray) Slice(i, j int) TimeArray { return TimeArray{a.slice(i, j)} } 312 func (a TimeArray) UnsafeArray() Array { return Array{a.array} }