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