github.com/tursom/GoCollections@v0.3.10/lang/atomic/Array.go (about) 1 /* 2 * Copyright (c) 2022 tursom. All rights reserved. 3 * Use of this source code is governed by a GPL-3 4 * license that can be found in the LICENSE file. 5 */ 6 7 package atomic 8 9 import "github.com/tursom/GoCollections/lang" 10 11 type ( 12 Array[T any] lang.Array[*T] 13 Int32Array lang.Array[Int32] 14 Int64Array lang.Array[Int64] 15 UInt32Array lang.Array[UInt32] 16 UInt64Array lang.Array[UInt64] 17 ) 18 19 func NewArray[T any](size int) Array[T] { 20 return make([]*T, size) 21 } 22 23 func CapArray[T any](array []*T) Array[T] { 24 return array 25 } 26 27 func NewInt32Array(size int) Int32Array { 28 return make(Int32Array, size) 29 } 30 31 func NewInt64Array(size int) Int64Array { 32 return make(Int64Array, size) 33 } 34 35 func NewUInt32Array(size int) UInt32Array { 36 return make(UInt32Array, size) 37 } 38 39 func NewUInt64Array(size int) UInt64Array { 40 return make(UInt64Array, size) 41 } 42 43 func (a Array[T]) Len() int { 44 return len(a) 45 } 46 47 func (a Array[T]) Array() []*T { 48 return a 49 } 50 51 func (a Array[T]) Get(index int) *T { 52 return GetAtomizer[T]().Load()(&a[index]) 53 } 54 55 func (a Int32Array) Get(index int) int32 { 56 return a[index].Load() 57 } 58 59 func (a Int64Array) Get(index int) int64 { 60 return a[index].Load() 61 } 62 63 func (a UInt32Array) Get(index int) uint32 { 64 return a[index].Load() 65 } 66 67 func (a UInt64Array) Get(index int) uint64 { 68 return a[index].Load() 69 } 70 71 func (a Array[T]) Set(index int, p *T) { 72 GetAtomizer[T]().Store()(&a[index], p) 73 } 74 75 func (a Int32Array) Set(index int, v int32) { 76 a[index].Store(v) 77 } 78 79 func (a Int64Array) Set(index int, v int64) { 80 a[index].Store(v) 81 } 82 83 func (a UInt32Array) Set(index int, v uint32) { 84 a[index].Store(v) 85 } 86 87 func (a UInt64Array) Set(index int, v uint64) { 88 a[index].Store(v) 89 } 90 91 func (a Array[T]) Swap(index int, p *T) (old *T) { 92 return GetAtomizer[T]().Swap()(&a[index], p) 93 } 94 95 func (a Int32Array) Swap(index int, new int32) (old int32) { 96 return a[index].Swap(new) 97 } 98 99 func (a Int64Array) Swap(index int, new int64) (old int64) { 100 return a[index].Swap(new) 101 } 102 103 func (a UInt32Array) Swap(index int, new uint32) (old uint32) { 104 return a[index].Swap(new) 105 } 106 107 func (a UInt64Array) Swap(index int, new uint64) (old uint64) { 108 return a[index].Swap(new) 109 } 110 111 func (a Array[T]) CompareAndSwap(index int, old, new *T) (swapped bool) { 112 return GetAtomizer[T]().CompareAndSwap()(&a[index], old, new) 113 } 114 115 func (a Int32Array) CompareAndSwap(index int, old, new int32) (swapped bool) { 116 return a[index].CompareAndSwap(old, new) 117 } 118 119 func (a Int64Array) CompareAndSwap(index int, old, new int64) (swapped bool) { 120 return a[index].CompareAndSwap(old, new) 121 } 122 123 func (a UInt32Array) CompareAndSwap(index int, old, new uint32) (swapped bool) { 124 return a[index].CompareAndSwap(old, new) 125 } 126 127 func (a UInt64Array) CompareAndSwap(index int, old, new uint64) (swapped bool) { 128 return a[index].CompareAndSwap(old, new) 129 } 130 131 func (a Int32Array) Add(index int, value int32) { 132 a[index].Add(value) 133 } 134 135 func (a Int64Array) Add(index int, value int64) { 136 a[index].Add(value) 137 } 138 139 func (a UInt32Array) Add(index int, value uint32) { 140 a[index].Add(value) 141 } 142 143 func (a UInt64Array) Add(index int, value uint64) { 144 a[index].Add(value) 145 } 146 147 func (a Int32Array) BitLength() int { 148 return len(a) * 32 149 } 150 151 func (a Int64Array) BitLength() int { 152 return len(a) * 64 153 } 154 155 func (a UInt32Array) BitLength() int { 156 return len(a) * 32 157 } 158 159 func (a UInt64Array) BitLength() int { 160 return len(a) * 64 161 } 162 163 func (a Int32Array) SetBit(bit int, up bool) (old bool) { 164 return a[bit/32].SetBit(bit%32, up) 165 } 166 167 func (a Int64Array) SetBit(bit int, up bool) (old bool) { 168 return a[bit/64].SetBit(bit%64, up) 169 } 170 171 func (a UInt32Array) SetBit(bit int, up bool) (old bool) { 172 return a[bit/32].SetBit(bit%32, up) 173 } 174 175 func (a UInt64Array) SetBit(bit int, up bool) (old bool) { 176 return a[bit/64].SetBit(bit%64, up) 177 } 178 179 func (a Int32Array) CompareAndSwapBit(bit int, old, new bool) (swapped bool) { 180 return a[bit/32].CompareAndSwapBit(bit%32, old, new) 181 } 182 183 func (a Int64Array) CompareAndSwapBit(bit int, old, new bool) (swapped bool) { 184 return a[bit/64].CompareAndSwapBit(bit%64, old, new) 185 } 186 187 func (a UInt32Array) CompareAndSwapBit(bit int, old, new bool) (swapped bool) { 188 return a[bit/32].CompareAndSwapBit(bit%32, old, new) 189 } 190 191 func (a UInt64Array) CompareAndSwapBit(bit int, old, new bool) (swapped bool) { 192 return a[bit/64].CompareAndSwapBit(bit%64, old, new) 193 }