github.com/sandwich-go/boost@v1.3.29/xcontainer/templates/slist/slist.go (about)

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