github.com/sandwich-go/boost@v1.3.29/xcontainer/slist/gen_int64.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 ElementInt64 = list.Element 19 20 type Int64 struct { 21 mu *localRWMutexVTypeInt64 22 list *list.List 23 } 24 25 func newWithSafeInt64(safe bool) *Int64 { 26 return &Int64{ 27 mu: newLocalRWMutexVTypeInt64(safe), 28 list: list.New(), 29 } 30 } 31 32 // New 创建非协程安全版本 33 func NewInt64() *Int64 { return newWithSafeInt64(false) } 34 35 // NewSync 创建协程安全版本 36 func NewSyncInt64() *Int64 { return newWithSafeInt64(true) } 37 38 // PushFront 队头添加 39 func (l *Int64) PushFront(v int64) (e *ElementInt64) { 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 *Int64) PushBack(v int64) (e *ElementInt64) { 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 *Int64) PushFronts(values []int64) { 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 *Int64) PushBacks(values []int64) { 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 *Int64) PopBack() (value int64) { 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)).(int64) 94 } 95 return 96 } 97 98 // PopFront 队头弹出元素 99 func (l *Int64) PopFront() (value int64) { 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).(int64) 108 } 109 return 110 } 111 112 func (l *Int64) pops(max int, front bool) (values []int64) { 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([]int64, length) 125 for i := 0; i < length; i++ { 126 if front { 127 values[i] = l.list.Remove(l.list.Front()).(int64) 128 } else { 129 values[i] = l.list.Remove(l.list.Back()).(int64) 130 } 131 } 132 } 133 return 134 } 135 136 // PopBacks 队尾弹出至多max个元素 137 func (l *Int64) PopBacks(max int) (values []int64) { 138 return l.pops(max, false) 139 } 140 141 // PopFronts 队头弹出至多max个元素 142 func (l *Int64) PopFronts(max int) (values []int64) { 143 return l.pops(max, true) 144 } 145 146 // PopBackAll 队尾弹出所有元素 147 func (l *Int64) PopBackAll() []int64 { 148 return l.PopBacks(-1) 149 } 150 151 // PopFrontAll 队头弹出所有元素 152 func (l *Int64) PopFrontAll() []int64 { 153 return l.PopFronts(-1) 154 } 155 156 // FrontAll 队头获取所有元素,拷贝操作 157 func (l *Int64) FrontAll() (values []int64) { 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([]int64, length) 166 for i, e := 0, l.list.Front(); i < length; i, e = i+1, e.Next() { 167 values[i] = e.Value.(int64) 168 } 169 } 170 return 171 } 172 173 // BackAll 队尾获取所有元素,拷贝操作 174 func (l *Int64) BackAll() (values []int64) { 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([]int64, length) 183 for i, e := 0, l.list.Back(); i < length; i, e = i+1, e.Prev() { 184 values[i] = e.Value.(int64) 185 } 186 } 187 return 188 } 189 190 // FrontValue 获取队头元素 191 func (l *Int64) FrontValue() (value int64) { 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.(int64) 199 } 200 return 201 } 202 203 // BackValue 获取队尾元素 204 func (l *Int64) BackValue() (value int64) { 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.(int64) 212 } 213 return 214 } 215 216 // Front returns the first element of list l or nil if the list is empty. 217 func (l *Int64) Front() (e *ElementInt64) { 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 *Int64) Back() (e *ElementInt64) { 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 *Int64) 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 *Int64) 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 *Int64) MoveBefore(e, mark *ElementInt64) { 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 *Int64) MoveAfter(e, p *ElementInt64) { 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 *Int64) MoveToFront(e *ElementInt64) { 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 *Int64) MoveToBack(e *ElementInt64) { 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 *Int64) PushBackList(other *Int64) { 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 *Int64) PushFrontList(other *Int64) { 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 *Int64) InsertAfter(p *ElementInt64, v int64) (e *ElementInt64) { 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 *Int64) InsertBefore(p *ElementInt64, v int64) (e *ElementInt64) { 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 *Int64) Remove(e *ElementInt64) (value int64) { 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).(int64) 368 return 369 } 370 371 // Removes 删除多个元素,底层调用Remove 372 func (l *Int64) Removes(es []*ElementInt64) { 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 *Int64) RemoveAll() { 385 l.mu.Lock() 386 l.list = list.New() 387 l.mu.Unlock() 388 } 389 390 // Clear See RemoveAll(). 391 func (l *Int64) Clear() { 392 l.RemoveAll() 393 } 394 395 // RLockFunc 读操作调用f方法 396 func (l *Int64) 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 *Int64) 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 *Int64) Iterator(f func(e *ElementInt64) bool) { 416 l.IteratorAsc(f) 417 } 418 419 // IteratorAsc 正序遍历,如果f返回false则停止遍历 420 func (l *Int64) IteratorAsc(f func(e *ElementInt64) 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 *Int64) IteratorDesc(f func(e *ElementInt64) 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 localRWMutexVTypeInt64 struct { 454 *sync.RWMutex 455 } 456 457 func newLocalRWMutexVTypeInt64(safe bool) *localRWMutexVTypeInt64 { 458 mu := localRWMutexVTypeInt64{} 459 if safe { 460 mu.RWMutex = new(sync.RWMutex) 461 } 462 return &mu 463 } 464 465 func (mu *localRWMutexVTypeInt64) IsSafe() bool { 466 return mu.RWMutex != nil 467 } 468 469 func (mu *localRWMutexVTypeInt64) Lock() { 470 if mu.RWMutex != nil { 471 mu.RWMutex.Lock() 472 } 473 } 474 475 func (mu *localRWMutexVTypeInt64) Unlock() { 476 if mu.RWMutex != nil { 477 mu.RWMutex.Unlock() 478 } 479 } 480 481 func (mu *localRWMutexVTypeInt64) RLock() { 482 if mu.RWMutex != nil { 483 mu.RWMutex.RLock() 484 } 485 } 486 487 func (mu *localRWMutexVTypeInt64) RUnlock() { 488 if mu.RWMutex != nil { 489 mu.RWMutex.RUnlock() 490 } 491 } 492 493 //template format 494 var __formatToInt64 = func(i interface{}) int64 { 495 switch ii := i.(type) { 496 case int: 497 return int64(ii) 498 case int8: 499 return int64(ii) 500 case int16: 501 return int64(ii) 502 case int32: 503 return int64(ii) 504 case int64: 505 return int64(ii) 506 case uint: 507 return int64(ii) 508 case uint8: 509 return int64(ii) 510 case uint16: 511 return int64(ii) 512 case uint32: 513 return int64(ii) 514 case uint64: 515 return int64(ii) 516 case float32: 517 return int64(ii) 518 case float64: 519 return int64(ii) 520 case string: 521 iv, err := strconv.ParseInt(ii, 10, 64) 522 if err != nil { 523 panic(err) 524 } 525 return int64(iv) 526 default: 527 panic("unknown type") 528 } 529 }