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