github.com/sandwich-go/boost@v1.3.29/xcontainer/sarray/gen_uint64.go (about) 1 // Code generated by gotemplate. DO NOT EDIT. 2 3 package sarray 4 5 import ( 6 "encoding/json" 7 "fmt" 8 "strconv" 9 10 "math/rand" 11 "sort" 12 "sync" 13 ) 14 15 //template type SyncArray(VType) 16 17 type Uint64 struct { 18 mu *localRWMutexVTypeUint64 19 array []uint64 20 } 21 22 // New 创建非协程安全版本 23 func NewUint64() *Uint64 { return newWithSafeUint64(false) } 24 25 // NewSync 创建协程安全版本 26 func NewSyncUint64() *Uint64 { return newWithSafeUint64(true) } 27 28 func newWithSafeUint64(safe bool) *Uint64 { 29 return &Uint64{ 30 mu: newLocalRWMutexVTypeUint64(safe), 31 array: make([]uint64, 0), 32 } 33 } 34 35 // At 返回指定位置元素,如果越界则返回默认空值 36 func (a *Uint64) At(index int) (value uint64) { 37 value, _ = a.Get(index) 38 return 39 } 40 41 // Get 返回指定位置元素,found标识元素是否存在 42 func (a *Uint64) Get(index int) (value uint64, found bool) { 43 a.mu.RLock() 44 defer a.mu.RUnlock() 45 if index < 0 || index >= len(a.array) { 46 return 47 } 48 return a.array[index], true 49 } 50 51 func (a *Uint64) errorIndexOutRangeUnLock(index int) error { 52 return fmt.Errorf("index %d out of array range %d", index, len(a.array)) 53 } 54 55 // Set 设定指定位置数据 56 func (a *Uint64) Set(index int, value uint64) error { 57 a.mu.Lock() 58 defer a.mu.Unlock() 59 if index < 0 || index >= len(a.array) { 60 return a.errorIndexOutRangeUnLock(index) 61 } 62 a.array[index] = value 63 return nil 64 } 65 66 // SetArray 替换底层存储 67 func (a *Uint64) SetArray(array []uint64) *Uint64 { 68 a.mu.Lock() 69 defer a.mu.Unlock() 70 a.array = array 71 return a 72 } 73 74 // Replace 替换指定位置元素 75 func (a *Uint64) Replace(given []uint64) *Uint64 { 76 a.mu.Lock() 77 defer a.mu.Unlock() 78 max := len(given) 79 if max > len(a.array) { 80 max = len(a.array) 81 } 82 for i := 0; i < max; i++ { 83 a.array[i] = given[i] 84 } 85 return a 86 } 87 88 // SortFunc 根据指定的方法进行排序 89 func (a *Uint64) SortFunc(less func(v1, v2 uint64) bool) *Uint64 { 90 a.mu.Lock() 91 defer a.mu.Unlock() 92 sort.Slice(a.array, func(i, j int) bool { 93 return less(a.array[i], a.array[j]) 94 }) 95 return a 96 } 97 98 // InsertBefore 在index位置前插入数据 99 func (a *Uint64) InsertBefore(index int, value uint64) error { 100 a.mu.Lock() 101 defer a.mu.Unlock() 102 if index < 0 || index >= len(a.array) { 103 return a.errorIndexOutRangeUnLock(index) 104 } 105 rear := append([]uint64{}, a.array[index:]...) 106 a.array = append(a.array[0:index], value) 107 a.array = append(a.array, rear...) 108 return nil 109 } 110 111 // InsertAfter 在index位置后插入数据 112 func (a *Uint64) InsertAfter(index int, value uint64) error { 113 a.mu.Lock() 114 defer a.mu.Unlock() 115 if index < 0 || index >= len(a.array) { 116 return a.errorIndexOutRangeUnLock(index) 117 } 118 rear := append([]uint64{}, a.array[index+1:]...) 119 a.array = append(a.array[0:index+1], value) 120 a.array = append(a.array, rear...) 121 return nil 122 } 123 124 // Contains 是否存在value 125 func (a *Uint64) Contains(value uint64) bool { 126 return a.Search(value) != -1 127 } 128 129 // Search 查找元素,不存在返回-1 130 func (a *Uint64) Search(value uint64) int { 131 a.mu.RLock() 132 defer a.mu.RUnlock() 133 if len(a.array) == 0 { 134 return -1 135 } 136 result := -1 137 for index, v := range a.array { 138 if v == value { 139 result = index 140 break 141 } 142 } 143 return result 144 } 145 146 func (a *Uint64) DeleteValue(value uint64) (found bool) { 147 if i := a.Search(value); i != -1 { 148 _, found = a.LoadAndDelete(i) 149 return found 150 } 151 return false 152 } 153 154 // LoadAndDelete 删除元素,如果删除成功返回被删除的元素 155 func (a *Uint64) LoadAndDelete(index int) (value uint64, found bool) { 156 a.mu.Lock() 157 defer a.mu.Unlock() 158 return a.doDeleteWithoutLock(index) 159 } 160 161 // doRemoveWithoutLock 不加锁移除元素 162 func (a *Uint64) doDeleteWithoutLock(index int) (value uint64, found bool) { 163 if index < 0 || index >= len(a.array) { 164 return 165 } 166 if index == 0 { 167 value = a.array[0] 168 a.array = a.array[1:] 169 return value, true 170 } else if index == len(a.array)-1 { 171 value = a.array[index] 172 a.array = a.array[:index] 173 return value, true 174 } 175 value = a.array[index] 176 a.array = append(a.array[:index], a.array[index+1:]...) 177 return value, true 178 } 179 180 // PushLeft 头插入 181 func (a *Uint64) PushLeft(value ...uint64) *Uint64 { 182 a.mu.Lock() 183 a.array = append(value, a.array...) 184 a.mu.Unlock() 185 return a 186 } 187 188 // PushRight 尾插入 189 func (a *Uint64) PushRight(value ...uint64) *Uint64 { 190 a.mu.Lock() 191 a.array = append(a.array, value...) 192 a.mu.Unlock() 193 return a 194 } 195 196 // PopLeft 头弹出 197 func (a *Uint64) PopLeft() (value uint64, found bool) { 198 a.mu.Lock() 199 defer a.mu.Unlock() 200 if len(a.array) == 0 { 201 return 202 } 203 value = a.array[0] 204 a.array = a.array[1:] 205 return value, true 206 } 207 208 // PopRight 尾弹出 209 func (a *Uint64) PopRight() (value uint64, found bool) { 210 a.mu.Lock() 211 defer a.mu.Unlock() 212 index := len(a.array) - 1 213 if index < 0 { 214 return 215 } 216 value = a.array[index] 217 a.array = a.array[:index] 218 return value, true 219 } 220 221 // PopRand 随机弹出 222 func (a *Uint64) PopRand() (value uint64, found bool) { 223 a.mu.Lock() 224 defer a.mu.Unlock() 225 return a.doDeleteWithoutLock(rand.Intn(len(a.array))) 226 } 227 228 // PopRands 随机n个元素并弹出,如果size大于数组尺寸则全部弹出 229 func (a *Uint64) PopRands(size int) []uint64 { 230 a.mu.Lock() 231 defer a.mu.Unlock() 232 if size <= 0 || len(a.array) == 0 { 233 return nil 234 } 235 if size >= len(a.array) { 236 size = len(a.array) 237 } 238 array := make([]uint64, size) 239 for i := 0; i < size; i++ { 240 array[i], _ = a.doDeleteWithoutLock(rand.Intn(len(a.array))) 241 } 242 return array 243 } 244 245 // Append 尾添加元素 alias of PushRight 246 func (a *Uint64) Append(value ...uint64) *Uint64 { return a.PushRight(value...) } 247 248 // Len 获取长度 249 func (a *Uint64) Len() int { 250 a.mu.RLock() 251 length := len(a.array) 252 a.mu.RUnlock() 253 return length 254 } 255 256 // Slice 获取底层数据存储,如果为sync安全模式则返回一份拷贝,否则直接返回底层数据指针 257 func (a *Uint64) Slice() []uint64 { 258 array := ([]uint64)(nil) 259 if a.mu.IsSafe() { 260 a.mu.RLock() 261 defer a.mu.RUnlock() 262 array = make([]uint64, len(a.array)) 263 copy(array, a.array) 264 } else { 265 array = a.array 266 } 267 return array 268 } 269 270 // Clear 清空存储 271 func (a *Uint64) Clear() *Uint64 { 272 a.mu.Lock() 273 if len(a.array) > 0 { 274 a.array = make([]uint64, 0) 275 } 276 a.mu.Unlock() 277 return a 278 } 279 280 // LockFunc 写锁操作array 281 func (a *Uint64) LockFunc(f func(array []uint64)) *Uint64 { 282 a.mu.Lock() 283 defer a.mu.Unlock() 284 f(a.array) 285 return a 286 } 287 288 // RLockFunc 读锁操作array 289 func (a *Uint64) RLockFunc(f func(array []uint64)) *Uint64 { 290 a.mu.RLock() 291 defer a.mu.RUnlock() 292 f(a.array) 293 return a 294 } 295 296 // Rand 随机一个元素 297 func (a *Uint64) Rand() (value uint64, found bool) { 298 a.mu.RLock() 299 defer a.mu.RUnlock() 300 if len(a.array) == 0 { 301 return 302 } 303 return a.array[rand.Intn(len(a.array))], true 304 } 305 306 // WalkAsc 按照index从小到大的顺序进行遍历,并将k,v作为参数执行f。如果f执行返回false则中止 307 func (a *Uint64) WalkAsc(f func(k int, v uint64) bool) { 308 a.mu.RLock() 309 defer a.mu.RUnlock() 310 for k, v := range a.array { 311 if !f(k, v) { 312 break 313 } 314 } 315 } 316 317 // WalkDesc 按照index从大到小的顺序进行遍历,并将k,v作为参数执行f。如果f执行返回false则中止 318 func (a *Uint64) WalkDesc(f func(k int, v uint64) bool) { 319 a.mu.RLock() 320 defer a.mu.RUnlock() 321 for i := len(a.array) - 1; i >= 0; i-- { 322 if !f(i, a.array[i]) { 323 break 324 } 325 } 326 } 327 328 // MarshalJSON 序列化到json 329 func (a Uint64) MarshalJSON() ([]byte, error) { 330 a.mu.RLock() 331 defer a.mu.RUnlock() 332 return json.Marshal(a.array) 333 } 334 335 // UnmarshalJSON 由json反序列化 336 func (a *Uint64) UnmarshalJSON(b []byte) error { 337 if a.array == nil { 338 a.array = make([]uint64, 0) 339 } 340 a.mu.Lock() 341 defer a.mu.Unlock() 342 if err := json.Unmarshal(b, &a.array); err != nil { 343 return err 344 } 345 return nil 346 } 347 348 // Empty 是否为空 349 func (a *Uint64) Empty() bool { return a.Len() == 0 } 350 351 type localRWMutexVTypeUint64 struct { 352 *sync.RWMutex 353 } 354 355 func newLocalRWMutexVTypeUint64(safe bool) *localRWMutexVTypeUint64 { 356 mu := localRWMutexVTypeUint64{} 357 if safe { 358 mu.RWMutex = new(sync.RWMutex) 359 } 360 return &mu 361 } 362 363 func (mu *localRWMutexVTypeUint64) IsSafe() bool { 364 return mu.RWMutex != nil 365 } 366 367 func (mu *localRWMutexVTypeUint64) Lock() { 368 if mu.RWMutex != nil { 369 mu.RWMutex.Lock() 370 } 371 } 372 373 func (mu *localRWMutexVTypeUint64) Unlock() { 374 if mu.RWMutex != nil { 375 mu.RWMutex.Unlock() 376 } 377 } 378 379 func (mu *localRWMutexVTypeUint64) RLock() { 380 if mu.RWMutex != nil { 381 mu.RWMutex.RLock() 382 } 383 } 384 385 func (mu *localRWMutexVTypeUint64) RUnlock() { 386 if mu.RWMutex != nil { 387 mu.RWMutex.RUnlock() 388 } 389 } 390 391 //template format 392 var __formatToUint64 = func(i interface{}) uint64 { 393 switch ii := i.(type) { 394 case int: 395 return uint64(ii) 396 case int8: 397 return uint64(ii) 398 case int16: 399 return uint64(ii) 400 case int32: 401 return uint64(ii) 402 case int64: 403 return uint64(ii) 404 case uint: 405 return uint64(ii) 406 case uint8: 407 return uint64(ii) 408 case uint16: 409 return uint64(ii) 410 case uint32: 411 return uint64(ii) 412 case uint64: 413 return uint64(ii) 414 case float32: 415 return uint64(ii) 416 case float64: 417 return uint64(ii) 418 case string: 419 iv, err := strconv.ParseUint(ii, 10, 64) 420 if err != nil { 421 panic(err) 422 } 423 return uint64(iv) 424 default: 425 panic("unknown type") 426 } 427 }