github.com/vc42/parquet-go@v0.0.0-20240320194221-1a9adb5f23f5/sparse/array.go (about)

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