gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/listx/constraints/list.go (about)

     1  package constraints
     2  
     3  import (
     4  	"sort"
     5  	"sync"
     6  )
     7  
     8  func NewList[V comparable]() (obj *List[V]) {
     9  	obj = &List[V]{}
    10  	return
    11  }
    12  
    13  type List[V comparable] struct {
    14  	data []V
    15  
    16  	mutex        sync.RWMutex
    17  	mutexLocked  bool
    18  	mutexRLocked bool
    19  }
    20  
    21  func (list *List[V]) rlock() {
    22  	if list.mutexLocked || list.mutexRLocked {
    23  		return
    24  	}
    25  	list.mutex.RLock()
    26  	list.mutexRLocked = true
    27  }
    28  func (list *List[V]) runlock() {
    29  	if !list.mutexRLocked {
    30  		return
    31  	}
    32  	if list.mutexRLocked {
    33  		return
    34  	}
    35  	list.mutex.RUnlock()
    36  	list.mutexRLocked = false
    37  }
    38  func (list *List[V]) lock() {
    39  	if list.mutexLocked || list.mutexRLocked {
    40  		return
    41  	}
    42  	list.mutex.Lock()
    43  	list.mutexLocked = true
    44  }
    45  func (list *List[V]) unlock() {
    46  	if !list.mutexLocked {
    47  		return
    48  	}
    49  	if list.mutexRLocked {
    50  		return
    51  	}
    52  	list.mutex.Unlock()
    53  	list.mutexLocked = false
    54  }
    55  
    56  // Add 添加元素, 如果已存在, 不添加
    57  func (list *List[V]) Add(items ...V) {
    58  	list.lock()
    59  	for _, item := range items {
    60  		if list.Contains(item) {
    61  			return
    62  		}
    63  		list.data = append(list.data, item)
    64  	}
    65  	list.unlock()
    66  }
    67  
    68  // AddConf 添加元素, 如果已存在相同条件[conf]的项, 不添加
    69  func (list *List[V]) AddConf(cond func(index int, item V) bool, items ...V) {
    70  	list.lock()
    71  	for _, item := range items {
    72  		if list.ContainsCond(cond) {
    73  			return
    74  		}
    75  		list.data = append(list.data, item)
    76  	}
    77  	list.unlock()
    78  }
    79  
    80  func (list *List[V]) Set(index int, val V) {
    81  	// 索引的读写是并发安全的
    82  	list.data[index] = val
    83  }
    84  
    85  func (list *List[V]) SetList(val []V) {
    86  	list.lock()
    87  	list.data = val
    88  	list.unlock()
    89  }
    90  
    91  func (list *List[V]) At(index int) (result V) {
    92  	if index < 0 {
    93  		return
    94  	}
    95  	if index >= list.Length() {
    96  		return
    97  	}
    98  	return list.data[index]
    99  }
   100  
   101  func (list *List[V]) Clear() {
   102  	list.lock()
   103  	list.data = []V{}
   104  	list.unlock()
   105  }
   106  
   107  func (list *List[V]) Dispose() {
   108  	list.lock()
   109  	list.data = nil
   110  	list.unlock()
   111  }
   112  
   113  func (list *List[V]) Clone() *List[V] {
   114  	list.lock()
   115  	s := list.data
   116  	ss := make([]V, len(s), cap(s))
   117  	copy(ss, s)
   118  	list.unlock()
   119  	return &List[V]{data: ss}
   120  }
   121  
   122  func (list *List[V]) Contains(item V) bool {
   123  	return list.IndexOf(item) != -1
   124  }
   125  
   126  func (list *List[V]) ContainsCond(cond func(index int, item V) bool) bool {
   127  	return list.IndexOfConf(cond) != -1
   128  }
   129  
   130  // CountCond 按照条件计数
   131  func (list *List[V]) CountCond(cond func(index int, item V) bool) int {
   132  	list.rlock()
   133  	c := 0
   134  	s := list.data
   135  	for i := 0; i < len(s); i++ {
   136  		if cond(i, s[i]) {
   137  			c++
   138  		}
   139  	}
   140  	list.runlock()
   141  
   142  	return c
   143  }
   144  
   145  // Every 每一个项都符合条件就返回true
   146  func (list *List[V]) Every(cond func(index int, item V) bool) bool {
   147  	list.lock()
   148  	//defer list.unlock()
   149  
   150  	s := list.data
   151  	for i := 0; i < len(s); i++ {
   152  		val := s[i]
   153  		if cond(i, val) == false {
   154  			return false
   155  		}
   156  	}
   157  	list.runlock()
   158  	return true
   159  }
   160  
   161  func (list *List[V]) First(cond func(index int, item V) bool) (val V, has bool) {
   162  	list.rlock()
   163  	s := list.data
   164  	for i := 0; i < len(s); i++ {
   165  		v := s[i]
   166  		if cond(i, v) {
   167  			return v, true
   168  		}
   169  	}
   170  	list.runlock()
   171  	return
   172  }
   173  
   174  // 返回符合条件的项形成的新列表
   175  func (list *List[V]) Filter(cond func(index int, item V) bool) *List[V] {
   176  	list.rlock()
   177  	var a []V
   178  	for i, x := range list.data {
   179  		if cond(i, x) {
   180  			a = append(a, x)
   181  		}
   182  	}
   183  	list.runlock()
   184  	return &List[V]{data: a}
   185  }
   186  
   187  // 如果返回错误,迭代终止
   188  func (list *List[V]) ForRange(handler func(index int, item V) error) {
   189  	list.lock()
   190  	for i, x := range list.data {
   191  		a := x
   192  		if handler(i, a) != nil {
   193  			list.unlock()
   194  			return
   195  		}
   196  	}
   197  	list.unlock()
   198  }
   199  
   200  // 从最后往前开始遍历
   201  // 如果返回错误,迭代终止
   202  func (list *List[V]) ForRangeFromLast(handler func(index int, item V) error) {
   203  	list.lock()
   204  	s := list.data
   205  	for i := len(s) - 1; i >= 0; i-- {
   206  		a := s[i]
   207  		if handler(i, a) != nil {
   208  			list.unlock()
   209  			return
   210  		}
   211  	}
   212  	list.unlock()
   213  }
   214  
   215  // 返回符合条件的项的索引
   216  func (list *List[V]) IndexOfConf(cond func(index int, item V) bool) int {
   217  	list.rlock()
   218  	s := list.data
   219  	for i := 0; i < len(s); i++ {
   220  		if cond(i, s[i]) {
   221  			return i
   222  		}
   223  	}
   224  	list.runlock()
   225  	return -1
   226  }
   227  
   228  // 返回项的索引
   229  func (list *List[V]) IndexOf(item V) int {
   230  	list.rlock()
   231  	s := list.data
   232  	for i := 0; i < len(s); i++ {
   233  		if s[i] == item {
   234  			return i
   235  		}
   236  	}
   237  	list.runlock()
   238  	return -1
   239  }
   240  
   241  func (list *List[V]) Last(cond func(index int, item V) bool) (result V, has bool) {
   242  	list.rlock()
   243  	s := list.data
   244  	for i := len(s) - 1; i >= 0; i-- {
   245  		val := s[i]
   246  		if cond(i, val) {
   247  			return val, true
   248  		}
   249  	}
   250  	list.runlock()
   251  	return
   252  }
   253  
   254  func (list *List[V]) Length() int {
   255  	return len(list.data)
   256  }
   257  
   258  func (list *List[V]) Pop() V {
   259  	list.lock()
   260  
   261  	s := list.data
   262  	last := s[len(s)-1] // 索引的读写是并发安全的
   263  	var zero V
   264  	s[len(s)-1] = zero // GC
   265  	s2 := s[:len(s)-1]
   266  	list.data = s2
   267  
   268  	list.unlock()
   269  	return last
   270  }
   271  
   272  func (list *List[V]) Push(item V) {
   273  	list.lock()
   274  	list.push(item)
   275  	list.unlock()
   276  }
   277  
   278  func (list *List[V]) push(item V) {
   279  	list.data = append(list.data, item)
   280  }
   281  
   282  func (list *List[V]) PushList(val IList[V]) {
   283  	list.lock()
   284  	list.pushList(val)
   285  	list.unlock()
   286  }
   287  
   288  func (list *List[V]) pushList(val IList[V]) {
   289  	s := list.data
   290  	s = append(s, val.Slice()...)
   291  	list.data = s
   292  }
   293  
   294  func (list *List[V]) Remove(item V) {
   295  	i := list.IndexOf(item)
   296  	if i != -1 {
   297  		list.RemoveAt(i)
   298  	}
   299  }
   300  
   301  func (list *List[V]) RemoveAt(i int) {
   302  	list.lock()
   303  
   304  	s := list.data
   305  	copy(s[i:], s[i+1:])
   306  	var zero V
   307  	s[len(s)-1] = zero // GC
   308  	s2 := s[:len(s)-1]
   309  	list.data = s2
   310  
   311  	list.unlock()
   312  }
   313  
   314  func (list *List[V]) Replace(i int, item V) {
   315  	list.lock()
   316  
   317  	s := list.data
   318  	over := i - len(s)
   319  	if over > -1 {
   320  		ss := make([]V, i+1)
   321  		copy(ss[0:], s[:])
   322  		s = ss
   323  	}
   324  	s[i] = item
   325  	list.data = s
   326  
   327  	list.unlock()
   328  }
   329  
   330  func (list *List[V]) Insert(i int, item V) {
   331  	list.lock()
   332  
   333  	s := list.data
   334  	var zero V
   335  	s = append(s, zero /* use the zero value of the element type */)
   336  	copy(s[i+1:], s[i:])
   337  	s[i] = item
   338  	list.data = s
   339  
   340  	list.unlock()
   341  }
   342  
   343  func (list *List[V]) Reverse() {
   344  	list.lock()
   345  
   346  	s := list.data
   347  	for i := len(s)/2 - 1; i >= 0; i-- {
   348  		opp := len(s) - 1 - i
   349  		s[i], s[opp] = s[opp], s[i]
   350  	}
   351  
   352  	list.unlock()
   353  }
   354  
   355  func (list *List[V]) Shift() V {
   356  	list.lock()
   357  
   358  	s := list.data
   359  	top := s[0]
   360  	var zero V
   361  	s[0] = zero // GC
   362  	s2 := s[1:]
   363  	list.data = s2
   364  
   365  	list.unlock()
   366  	return top
   367  }
   368  
   369  // Slice 直接返回slice值
   370  func (list *List[V]) Slice() []V {
   371  	return list.data
   372  }
   373  
   374  // Self 返回自身的列表
   375  // 用于List作为组合时的快捷方式
   376  func (list *List[V]) Self() *List[V] {
   377  	return list
   378  }
   379  
   380  // compare返回值:>=0 a排b前面
   381  func (list *List[V]) Sort(compare func(a, b V) int) {
   382  	list.lock()
   383  	l := list.data
   384  	sort.Slice(l, func(i, j int) bool {
   385  		return compare(l[i], l[j]) >= 0
   386  	})
   387  	list.unlock()
   388  }
   389  
   390  func (list *List[V]) Unshift(item V) {
   391  	list.lock()
   392  
   393  	s := list.data
   394  	l := len(s) + 1
   395  	ss := make([]V, l, l)
   396  	ss[0] = item
   397  	copy(ss[1:], s[:])
   398  	list.data = ss
   399  
   400  	list.unlock()
   401  }
   402  
   403  // Unique 去重操作, 返回去重后的数组
   404  func (list *List[V]) Unique() *List[V] {
   405  	list.lock()
   406  	l := list.unique()
   407  	list.unlock()
   408  	return l
   409  }
   410  
   411  func (list *List[V]) unique() *List[V] {
   412  	var r []V
   413  	l := list.data
   414  	m := map[V]struct{}{} // 存放不重复主键
   415  	for _, e := range l {
   416  		length := len(m)
   417  		m[e] = struct{}{}
   418  		if len(m) != length { // 加入map后,map长度变化,则元素不重复
   419  			r = append(r, e)
   420  		}
   421  	}
   422  
   423  	return &List[V]{data: r}
   424  }
   425  
   426  // UniqueByCustomKey 去重操作, 返回去重后的数组
   427  // 自定义键值
   428  func (list *List[V]) UniqueByCustomKey(getKey func(item V) string) *List[V] {
   429  	list.lock()
   430  	l := list.uniqueByCustomKey(getKey)
   431  	list.unlock()
   432  	return l
   433  }
   434  
   435  func (list *List[V]) uniqueByCustomKey(getKey func(item V) string) *List[V] {
   436  	var r []V
   437  	l := list.data
   438  	m := map[string]struct{}{} // 存放不重复主键
   439  	for _, e := range l {
   440  		length := len(m)
   441  		m[getKey(e)] = struct{}{}
   442  		if len(m) != length { // 加入map后,map长度变化,则元素不重复
   443  			r = append(r, e)
   444  		}
   445  	}
   446  	return &List[V]{data: r}
   447  }
   448  
   449  // Union 并集
   450  func (list *List[V]) Union(a IList[V]) *List[V] {
   451  	list.lock()
   452  	list.pushList(a)
   453  	l := list.unique()
   454  	list.unlock()
   455  	return l
   456  }
   457  
   458  // UnionByCustomKey 并集
   459  func (list *List[V]) UnionByCustomKey(a IList[V], getKey func(item V) string) *List[V] {
   460  	list.lock()
   461  	list.pushList(a)
   462  	return list.UniqueByCustomKey(getKey)
   463  }
   464  
   465  // 交集
   466  //func Intersect[T comparable](arrs ...[]T) []T {
   467  //	m := make(map[T]int)
   468  //
   469  //	var (
   470  //		tmpArr []T
   471  //		count  int
   472  //		ok     bool
   473  //	)
   474  //	for idx1 := range arrs {
   475  //		tmpArr = Distinct(arrs[idx1])
   476  //
   477  //		for idx2 := range tmpArr {
   478  //			count, ok = m[tmpArr[idx2]]
   479  //			if !ok {
   480  //				m[tmpArr[idx2]] = 1
   481  //			} else {
   482  //				m[tmpArr[idx2]] = count + 1
   483  //			}
   484  //		}
   485  //	}
   486  //
   487  //	var (
   488  //		ret     []T
   489  //		lenArrs int = len(arrs)
   490  //	)
   491  //	for k, v := range m {
   492  //		if v == lenArrs {
   493  //			ret = append(ret, k)
   494  //		}
   495  //	}
   496  //
   497  //	return ret
   498  //}