github.com/sandwich-go/boost@v1.3.29/xcontainer/slist/gen_int32.go (about) 1 // Code generated by gotemplate. DO NOT EDIT. 2 3 // slist 包提供了一个同步的链表实现 4 // 可以产生一个带读写锁的线程安全的SyncList,也可以产生一个非线程安全的SyncList 5 // New 产生非协程安全的版本 6 // NewSync 产生协程安全的版本 7 package slist 8 9 import ( 10 "container/list" 11 "strconv" 12 13 "sync" 14 ) 15 16 //template type SyncList(VType) 17 18 type ElementInt32 = list.Element 19 20 type Int32 struct { 21 mu *localRWMutexVTypeInt32 22 list *list.List 23 } 24 25 func newWithSafeInt32(safe bool) *Int32 { 26 return &Int32{ 27 mu: newLocalRWMutexVTypeInt32(safe), 28 list: list.New(), 29 } 30 } 31 32 // New 创建非协程安全版本 33 func NewInt32() *Int32 { return newWithSafeInt32(false) } 34 35 // NewSync 创建协程安全版本 36 func NewSyncInt32() *Int32 { return newWithSafeInt32(true) } 37 38 // PushFront 队头添加 39 func (l *Int32) PushFront(v int32) (e *ElementInt32) { 40 l.mu.Lock() 41 if l.list == nil { 42 l.list = list.New() 43 } 44 e = l.list.PushFront(v) 45 l.mu.Unlock() 46 return 47 } 48 49 // PushBack 队尾添加 50 func (l *Int32) PushBack(v int32) (e *ElementInt32) { 51 l.mu.Lock() 52 if l.list == nil { 53 l.list = list.New() 54 } 55 e = l.list.PushBack(v) 56 l.mu.Unlock() 57 return 58 } 59 60 // PushFronts 队头添加多个元素 61 func (l *Int32) PushFronts(values []int32) { 62 l.mu.Lock() 63 if l.list == nil { 64 l.list = list.New() 65 } 66 for _, v := range values { 67 l.list.PushFront(v) 68 } 69 l.mu.Unlock() 70 } 71 72 // PushBacks 队尾添加多个元素 73 func (l *Int32) PushBacks(values []int32) { 74 l.mu.Lock() 75 if l.list == nil { 76 l.list = list.New() 77 } 78 for _, v := range values { 79 l.list.PushBack(v) 80 } 81 l.mu.Unlock() 82 } 83 84 // PopBack 队尾弹出元素 85 func (l *Int32) PopBack() (value int32) { 86 l.mu.Lock() 87 defer l.mu.Unlock() 88 if l.list == nil { 89 l.list = list.New() 90 return 91 } 92 if e := l.list.Back(); e != nil { 93 value = (l.list.Remove(e)).(int32) 94 } 95 return 96 } 97 98 // PopFront 队头弹出元素 99 func (l *Int32) PopFront() (value int32) { 100 l.mu.Lock() 101 defer l.mu.Unlock() 102 if l.list == nil { 103 l.list = list.New() 104 return 105 } 106 if e := l.list.Front(); e != nil { 107 value = l.list.Remove(e).(int32) 108 } 109 return 110 } 111 112 func (l *Int32) pops(max int, front bool) (values []int32) { 113 l.mu.Lock() 114 defer l.mu.Unlock() 115 if l.list == nil { 116 l.list = list.New() 117 return 118 } 119 length := l.list.Len() 120 if length > 0 { 121 if max > 0 && max < length { 122 length = max 123 } 124 values = make([]int32, length) 125 for i := 0; i < length; i++ { 126 if front { 127 values[i] = l.list.Remove(l.list.Front()).(int32) 128 } else { 129 values[i] = l.list.Remove(l.list.Back()).(int32) 130 } 131 } 132 } 133 return 134 } 135 136 // PopBacks 队尾弹出至多max个元素 137 func (l *Int32) PopBacks(max int) (values []int32) { 138 return l.pops(max, false) 139 } 140 141 // PopFronts 队头弹出至多max个元素 142 func (l *Int32) PopFronts(max int) (values []int32) { 143 return l.pops(max, true) 144 } 145 146 // PopBackAll 队尾弹出所有元素 147 func (l *Int32) PopBackAll() []int32 { 148 return l.PopBacks(-1) 149 } 150 151 // PopFrontAll 队头弹出所有元素 152 func (l *Int32) PopFrontAll() []int32 { 153 return l.PopFronts(-1) 154 } 155 156 // FrontAll 队头获取所有元素,拷贝操作 157 func (l *Int32) FrontAll() (values []int32) { 158 l.mu.RLock() 159 defer l.mu.RUnlock() 160 if l.list == nil { 161 return 162 } 163 length := l.list.Len() 164 if length > 0 { 165 values = make([]int32, length) 166 for i, e := 0, l.list.Front(); i < length; i, e = i+1, e.Next() { 167 values[i] = e.Value.(int32) 168 } 169 } 170 return 171 } 172 173 // BackAll 队尾获取所有元素,拷贝操作 174 func (l *Int32) BackAll() (values []int32) { 175 l.mu.RLock() 176 defer l.mu.RUnlock() 177 if l.list == nil { 178 return 179 } 180 length := l.list.Len() 181 if length > 0 { 182 values = make([]int32, length) 183 for i, e := 0, l.list.Back(); i < length; i, e = i+1, e.Prev() { 184 values[i] = e.Value.(int32) 185 } 186 } 187 return 188 } 189 190 // FrontValue 获取队头元素 191 func (l *Int32) FrontValue() (value int32) { 192 l.mu.RLock() 193 defer l.mu.RUnlock() 194 if l.list == nil { 195 return 196 } 197 if e := l.list.Front(); e != nil { 198 value = e.Value.(int32) 199 } 200 return 201 } 202 203 // BackValue 获取队尾元素 204 func (l *Int32) BackValue() (value int32) { 205 l.mu.RLock() 206 defer l.mu.RUnlock() 207 if l.list == nil { 208 return 209 } 210 if e := l.list.Back(); e != nil { 211 value = e.Value.(int32) 212 } 213 return 214 } 215 216 // Front returns the first element of list l or nil if the list is empty. 217 func (l *Int32) Front() (e *ElementInt32) { 218 l.mu.RLock() 219 defer l.mu.RUnlock() 220 if l.list == nil { 221 return 222 } 223 e = l.list.Front() 224 return 225 } 226 227 // Back returns the last element of list l or nil if the list is empty. 228 func (l *Int32) Back() (e *ElementInt32) { 229 l.mu.RLock() 230 defer l.mu.RUnlock() 231 if l.list == nil { 232 return 233 } 234 e = l.list.Back() 235 return 236 } 237 238 // Len 获取长度,空返回0 239 func (l *Int32) Len() (length int) { 240 l.mu.RLock() 241 defer l.mu.RUnlock() 242 if l.list == nil { 243 return 244 } 245 length = l.list.Len() 246 return 247 } 248 249 // Size Len的alias方法 250 func (l *Int32) Size() int { 251 return l.Len() 252 } 253 254 // MoveBefore moves element e to its new position before mark. 255 // If e or mark is not an element of l, or e == mark, the list is not modified. 256 // The element and mark must not be nil. 257 func (l *Int32) MoveBefore(e, mark *ElementInt32) { 258 l.mu.Lock() 259 defer l.mu.Unlock() 260 if l.list == nil { 261 l.list = list.New() 262 } 263 l.list.MoveBefore(e, mark) 264 } 265 266 // MoveAfter moves element <e> to its new position after <p>. 267 // If <e> or <p> is not an element of <l>, or <e> == <p>, the list is not modified. 268 // The element and <p> must not be nil. 269 func (l *Int32) MoveAfter(e, p *ElementInt32) { 270 l.mu.Lock() 271 defer l.mu.Unlock() 272 if l.list == nil { 273 l.list = list.New() 274 } 275 l.list.MoveAfter(e, p) 276 } 277 278 // MoveToFront moves element <e> to the front of list <l>. 279 // If <e> is not an element of <l>, the list is not modified. 280 // The element must not be nil. 281 func (l *Int32) MoveToFront(e *ElementInt32) { 282 l.mu.Lock() 283 defer l.mu.Unlock() 284 if l.list == nil { 285 l.list = list.New() 286 } 287 l.list.MoveToFront(e) 288 } 289 290 // MoveToBack moves element <e> to the back of list <l>. 291 // If <e> is not an element of <l>, the list is not modified. 292 // The element must not be nil. 293 func (l *Int32) MoveToBack(e *ElementInt32) { 294 l.mu.Lock() 295 defer l.mu.Unlock() 296 if l.list == nil { 297 l.list = list.New() 298 } 299 l.list.MoveToBack(e) 300 } 301 302 // PushBackList inserts a copy of another list at the back of list l. 303 // The lists l and other may be the same. They must not be nil. 304 func (l *Int32) PushBackList(other *Int32) { 305 if l != other { 306 other.mu.RLock() 307 defer other.mu.RUnlock() 308 } 309 l.mu.Lock() 310 defer l.mu.Unlock() 311 if l.list == nil { 312 l.list = list.New() 313 } 314 l.list.PushBackList(other.list) 315 } 316 317 // PushFrontList inserts a copy of another list at the front of list l. 318 // The lists l and other may be the same. They must not be nil. 319 func (l *Int32) PushFrontList(other *Int32) { 320 if l != other { 321 other.mu.RLock() 322 defer other.mu.RUnlock() 323 } 324 l.mu.Lock() 325 defer l.mu.Unlock() 326 if l.list == nil { 327 l.list = list.New() 328 } 329 l.list.PushFrontList(other.list) 330 } 331 332 // InsertAfter inserts a new element e with value v immediately after mark and returns e. 333 // If mark is not an element of l, the list is not modified. 334 // The mark must not be nil. 335 func (l *Int32) InsertAfter(p *ElementInt32, v int32) (e *ElementInt32) { 336 l.mu.Lock() 337 defer l.mu.Unlock() 338 if l.list == nil { 339 l.list = list.New() 340 } 341 e = l.list.InsertAfter(v, p) 342 return 343 } 344 345 // InsertBefore inserts a new element e with value v immediately before mark and returns e. 346 // If mark is not an element of l, the list is not modified. 347 // The mark must not be nil. 348 func (l *Int32) InsertBefore(p *ElementInt32, v int32) (e *ElementInt32) { 349 l.mu.Lock() 350 defer l.mu.Unlock() 351 if l.list == nil { 352 l.list = list.New() 353 } 354 e = l.list.InsertBefore(v, p) 355 return 356 } 357 358 // Remove removes e from l if e is an element of list l. 359 // It returns the element value e.Value. 360 // The element must not be nil. 361 func (l *Int32) Remove(e *ElementInt32) (value int32) { 362 l.mu.Lock() 363 defer l.mu.Unlock() 364 if l.list == nil { 365 l.list = list.New() 366 } 367 value = l.list.Remove(e).(int32) 368 return 369 } 370 371 // Removes 删除多个元素,底层调用Remove 372 func (l *Int32) Removes(es []*ElementInt32) { 373 l.mu.Lock() 374 defer l.mu.Unlock() 375 if l.list == nil { 376 l.list = list.New() 377 } 378 for _, e := range es { 379 l.list.Remove(e) 380 } 381 } 382 383 // RemoveAll 删除所有元素 384 func (l *Int32) RemoveAll() { 385 l.mu.Lock() 386 l.list = list.New() 387 l.mu.Unlock() 388 } 389 390 // Clear See RemoveAll(). 391 func (l *Int32) Clear() { 392 l.RemoveAll() 393 } 394 395 // RLockFunc 读操作调用f方法 396 func (l *Int32) RLockFunc(f func(list *list.List)) { 397 l.mu.RLock() 398 defer l.mu.RUnlock() 399 if l.list != nil { 400 f(l.list) 401 } 402 } 403 404 // LockFunc 写操作调用f方法 405 func (l *Int32) LockFunc(f func(list *list.List)) { 406 l.mu.Lock() 407 defer l.mu.Unlock() 408 if l.list == nil { 409 l.list = list.New() 410 } 411 f(l.list) 412 } 413 414 // Iterator is alias of IteratorAsc. 415 func (l *Int32) Iterator(f func(e *ElementInt32) bool) { 416 l.IteratorAsc(f) 417 } 418 419 // IteratorAsc 正序遍历,如果f返回false则停止遍历 420 func (l *Int32) IteratorAsc(f func(e *ElementInt32) bool) { 421 l.mu.RLock() 422 defer l.mu.RUnlock() 423 if l.list == nil { 424 return 425 } 426 length := l.list.Len() 427 if length > 0 { 428 for i, e := 0, l.list.Front(); i < length; i, e = i+1, e.Next() { 429 if !f(e) { 430 break 431 } 432 } 433 } 434 } 435 436 // IteratorDesc 逆序遍历,如果f返回false则停止遍历 437 func (l *Int32) IteratorDesc(f func(e *ElementInt32) bool) { 438 l.mu.RLock() 439 defer l.mu.RUnlock() 440 if l.list == nil { 441 return 442 } 443 length := l.list.Len() 444 if length > 0 { 445 for i, e := 0, l.list.Back(); i < length; i, e = i+1, e.Prev() { 446 if !f(e) { 447 break 448 } 449 } 450 } 451 } 452 453 type localRWMutexVTypeInt32 struct { 454 *sync.RWMutex 455 } 456 457 func newLocalRWMutexVTypeInt32(safe bool) *localRWMutexVTypeInt32 { 458 mu := localRWMutexVTypeInt32{} 459 if safe { 460 mu.RWMutex = new(sync.RWMutex) 461 } 462 return &mu 463 } 464 465 func (mu *localRWMutexVTypeInt32) IsSafe() bool { 466 return mu.RWMutex != nil 467 } 468 469 func (mu *localRWMutexVTypeInt32) Lock() { 470 if mu.RWMutex != nil { 471 mu.RWMutex.Lock() 472 } 473 } 474 475 func (mu *localRWMutexVTypeInt32) Unlock() { 476 if mu.RWMutex != nil { 477 mu.RWMutex.Unlock() 478 } 479 } 480 481 func (mu *localRWMutexVTypeInt32) RLock() { 482 if mu.RWMutex != nil { 483 mu.RWMutex.RLock() 484 } 485 } 486 487 func (mu *localRWMutexVTypeInt32) RUnlock() { 488 if mu.RWMutex != nil { 489 mu.RWMutex.RUnlock() 490 } 491 } 492 493 //template format 494 var __formatToInt32 = func(i interface{}) int32 { 495 switch ii := i.(type) { 496 case int: 497 return int32(ii) 498 case int8: 499 return int32(ii) 500 case int16: 501 return int32(ii) 502 case int32: 503 return int32(ii) 504 case int64: 505 return int32(ii) 506 case uint: 507 return int32(ii) 508 case uint8: 509 return int32(ii) 510 case uint16: 511 return int32(ii) 512 case uint32: 513 return int32(ii) 514 case uint64: 515 return int32(ii) 516 case float32: 517 return int32(ii) 518 case float64: 519 return int32(ii) 520 case string: 521 iv, err := strconv.ParseInt(ii, 10, 64) 522 if err != nil { 523 panic(err) 524 } 525 return int32(iv) 526 default: 527 panic("unknown type") 528 } 529 }