github.com/sandwich-go/boost@v1.3.29/xcontainer/sarray/gen_uint.go (about)

     1  // Code generated by gotemplate. DO NOT EDIT.
     2  
     3  package sarray
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"strconv"
     9  
    10  	"math/rand"
    11  	"sort"
    12  	"sync"
    13  )
    14  
    15  //template type SyncArray(VType)
    16  
    17  type Uint struct {
    18  	mu    *localRWMutexVTypeUint
    19  	array []uint
    20  }
    21  
    22  // New 创建非协程安全版本
    23  func NewUint() *Uint { return newWithSafeUint(false) }
    24  
    25  // NewSync 创建协程安全版本
    26  func NewSyncUint() *Uint { return newWithSafeUint(true) }
    27  
    28  func newWithSafeUint(safe bool) *Uint {
    29  	return &Uint{
    30  		mu:    newLocalRWMutexVTypeUint(safe),
    31  		array: make([]uint, 0),
    32  	}
    33  }
    34  
    35  // At 返回指定位置元素,如果越界则返回默认空值
    36  func (a *Uint) At(index int) (value uint) {
    37  	value, _ = a.Get(index)
    38  	return
    39  }
    40  
    41  // Get 返回指定位置元素,found标识元素是否存在
    42  func (a *Uint) Get(index int) (value uint, found bool) {
    43  	a.mu.RLock()
    44  	defer a.mu.RUnlock()
    45  	if index < 0 || index >= len(a.array) {
    46  		return
    47  	}
    48  	return a.array[index], true
    49  }
    50  
    51  func (a *Uint) errorIndexOutRangeUnLock(index int) error {
    52  	return fmt.Errorf("index %d out of array range %d", index, len(a.array))
    53  }
    54  
    55  // Set 设定指定位置数据
    56  func (a *Uint) Set(index int, value uint) error {
    57  	a.mu.Lock()
    58  	defer a.mu.Unlock()
    59  	if index < 0 || index >= len(a.array) {
    60  		return a.errorIndexOutRangeUnLock(index)
    61  	}
    62  	a.array[index] = value
    63  	return nil
    64  }
    65  
    66  // SetArray 替换底层存储
    67  func (a *Uint) SetArray(array []uint) *Uint {
    68  	a.mu.Lock()
    69  	defer a.mu.Unlock()
    70  	a.array = array
    71  	return a
    72  }
    73  
    74  // Replace 替换指定位置元素
    75  func (a *Uint) Replace(given []uint) *Uint {
    76  	a.mu.Lock()
    77  	defer a.mu.Unlock()
    78  	max := len(given)
    79  	if max > len(a.array) {
    80  		max = len(a.array)
    81  	}
    82  	for i := 0; i < max; i++ {
    83  		a.array[i] = given[i]
    84  	}
    85  	return a
    86  }
    87  
    88  // SortFunc  根据指定的方法进行排序
    89  func (a *Uint) SortFunc(less func(v1, v2 uint) bool) *Uint {
    90  	a.mu.Lock()
    91  	defer a.mu.Unlock()
    92  	sort.Slice(a.array, func(i, j int) bool {
    93  		return less(a.array[i], a.array[j])
    94  	})
    95  	return a
    96  }
    97  
    98  // InsertBefore 在index位置前插入数据
    99  func (a *Uint) InsertBefore(index int, value uint) error {
   100  	a.mu.Lock()
   101  	defer a.mu.Unlock()
   102  	if index < 0 || index >= len(a.array) {
   103  		return a.errorIndexOutRangeUnLock(index)
   104  	}
   105  	rear := append([]uint{}, a.array[index:]...)
   106  	a.array = append(a.array[0:index], value)
   107  	a.array = append(a.array, rear...)
   108  	return nil
   109  }
   110  
   111  // InsertAfter 在index位置后插入数据
   112  func (a *Uint) InsertAfter(index int, value uint) error {
   113  	a.mu.Lock()
   114  	defer a.mu.Unlock()
   115  	if index < 0 || index >= len(a.array) {
   116  		return a.errorIndexOutRangeUnLock(index)
   117  	}
   118  	rear := append([]uint{}, a.array[index+1:]...)
   119  	a.array = append(a.array[0:index+1], value)
   120  	a.array = append(a.array, rear...)
   121  	return nil
   122  }
   123  
   124  // Contains  是否存在value
   125  func (a *Uint) Contains(value uint) bool {
   126  	return a.Search(value) != -1
   127  }
   128  
   129  // Search 查找元素,不存在返回-1
   130  func (a *Uint) Search(value uint) int {
   131  	a.mu.RLock()
   132  	defer a.mu.RUnlock()
   133  	if len(a.array) == 0 {
   134  		return -1
   135  	}
   136  	result := -1
   137  	for index, v := range a.array {
   138  		if v == value {
   139  			result = index
   140  			break
   141  		}
   142  	}
   143  	return result
   144  }
   145  
   146  func (a *Uint) DeleteValue(value uint) (found bool) {
   147  	if i := a.Search(value); i != -1 {
   148  		_, found = a.LoadAndDelete(i)
   149  		return found
   150  	}
   151  	return false
   152  }
   153  
   154  // LoadAndDelete 删除元素,如果删除成功返回被删除的元素
   155  func (a *Uint) LoadAndDelete(index int) (value uint, found bool) {
   156  	a.mu.Lock()
   157  	defer a.mu.Unlock()
   158  	return a.doDeleteWithoutLock(index)
   159  }
   160  
   161  // doRemoveWithoutLock 不加锁移除元素
   162  func (a *Uint) doDeleteWithoutLock(index int) (value uint, found bool) {
   163  	if index < 0 || index >= len(a.array) {
   164  		return
   165  	}
   166  	if index == 0 {
   167  		value = a.array[0]
   168  		a.array = a.array[1:]
   169  		return value, true
   170  	} else if index == len(a.array)-1 {
   171  		value = a.array[index]
   172  		a.array = a.array[:index]
   173  		return value, true
   174  	}
   175  	value = a.array[index]
   176  	a.array = append(a.array[:index], a.array[index+1:]...)
   177  	return value, true
   178  }
   179  
   180  // PushLeft 头插入
   181  func (a *Uint) PushLeft(value ...uint) *Uint {
   182  	a.mu.Lock()
   183  	a.array = append(value, a.array...)
   184  	a.mu.Unlock()
   185  	return a
   186  }
   187  
   188  // PushRight 尾插入
   189  func (a *Uint) PushRight(value ...uint) *Uint {
   190  	a.mu.Lock()
   191  	a.array = append(a.array, value...)
   192  	a.mu.Unlock()
   193  	return a
   194  }
   195  
   196  // PopLeft 头弹出
   197  func (a *Uint) PopLeft() (value uint, found bool) {
   198  	a.mu.Lock()
   199  	defer a.mu.Unlock()
   200  	if len(a.array) == 0 {
   201  		return
   202  	}
   203  	value = a.array[0]
   204  	a.array = a.array[1:]
   205  	return value, true
   206  }
   207  
   208  // PopRight 尾弹出
   209  func (a *Uint) PopRight() (value uint, found bool) {
   210  	a.mu.Lock()
   211  	defer a.mu.Unlock()
   212  	index := len(a.array) - 1
   213  	if index < 0 {
   214  		return
   215  	}
   216  	value = a.array[index]
   217  	a.array = a.array[:index]
   218  	return value, true
   219  }
   220  
   221  // PopRand 随机弹出
   222  func (a *Uint) PopRand() (value uint, found bool) {
   223  	a.mu.Lock()
   224  	defer a.mu.Unlock()
   225  	return a.doDeleteWithoutLock(rand.Intn(len(a.array)))
   226  }
   227  
   228  // PopRands 随机n个元素并弹出,如果size大于数组尺寸则全部弹出
   229  func (a *Uint) PopRands(size int) []uint {
   230  	a.mu.Lock()
   231  	defer a.mu.Unlock()
   232  	if size <= 0 || len(a.array) == 0 {
   233  		return nil
   234  	}
   235  	if size >= len(a.array) {
   236  		size = len(a.array)
   237  	}
   238  	array := make([]uint, size)
   239  	for i := 0; i < size; i++ {
   240  		array[i], _ = a.doDeleteWithoutLock(rand.Intn(len(a.array)))
   241  	}
   242  	return array
   243  }
   244  
   245  // Append 尾添加元素 alias of PushRight
   246  func (a *Uint) Append(value ...uint) *Uint { return a.PushRight(value...) }
   247  
   248  // Len 获取长度
   249  func (a *Uint) Len() int {
   250  	a.mu.RLock()
   251  	length := len(a.array)
   252  	a.mu.RUnlock()
   253  	return length
   254  }
   255  
   256  // Slice 获取底层数据存储,如果为sync安全模式则返回一份拷贝,否则直接返回底层数据指针
   257  func (a *Uint) Slice() []uint {
   258  	array := ([]uint)(nil)
   259  	if a.mu.IsSafe() {
   260  		a.mu.RLock()
   261  		defer a.mu.RUnlock()
   262  		array = make([]uint, len(a.array))
   263  		copy(array, a.array)
   264  	} else {
   265  		array = a.array
   266  	}
   267  	return array
   268  }
   269  
   270  // Clear 清空存储
   271  func (a *Uint) Clear() *Uint {
   272  	a.mu.Lock()
   273  	if len(a.array) > 0 {
   274  		a.array = make([]uint, 0)
   275  	}
   276  	a.mu.Unlock()
   277  	return a
   278  }
   279  
   280  // LockFunc 写锁操作array
   281  func (a *Uint) LockFunc(f func(array []uint)) *Uint {
   282  	a.mu.Lock()
   283  	defer a.mu.Unlock()
   284  	f(a.array)
   285  	return a
   286  }
   287  
   288  // RLockFunc 读锁操作array
   289  func (a *Uint) RLockFunc(f func(array []uint)) *Uint {
   290  	a.mu.RLock()
   291  	defer a.mu.RUnlock()
   292  	f(a.array)
   293  	return a
   294  }
   295  
   296  // Rand 随机一个元素
   297  func (a *Uint) Rand() (value uint, found bool) {
   298  	a.mu.RLock()
   299  	defer a.mu.RUnlock()
   300  	if len(a.array) == 0 {
   301  		return
   302  	}
   303  	return a.array[rand.Intn(len(a.array))], true
   304  }
   305  
   306  // WalkAsc 按照index从小到大的顺序进行遍历,并将k,v作为参数执行f。如果f执行返回false则中止
   307  func (a *Uint) WalkAsc(f func(k int, v uint) bool) {
   308  	a.mu.RLock()
   309  	defer a.mu.RUnlock()
   310  	for k, v := range a.array {
   311  		if !f(k, v) {
   312  			break
   313  		}
   314  	}
   315  }
   316  
   317  // WalkDesc 按照index从大到小的顺序进行遍历,并将k,v作为参数执行f。如果f执行返回false则中止
   318  func (a *Uint) WalkDesc(f func(k int, v uint) bool) {
   319  	a.mu.RLock()
   320  	defer a.mu.RUnlock()
   321  	for i := len(a.array) - 1; i >= 0; i-- {
   322  		if !f(i, a.array[i]) {
   323  			break
   324  		}
   325  	}
   326  }
   327  
   328  // MarshalJSON 序列化到json
   329  func (a Uint) MarshalJSON() ([]byte, error) {
   330  	a.mu.RLock()
   331  	defer a.mu.RUnlock()
   332  	return json.Marshal(a.array)
   333  }
   334  
   335  // UnmarshalJSON 由json反序列化
   336  func (a *Uint) UnmarshalJSON(b []byte) error {
   337  	if a.array == nil {
   338  		a.array = make([]uint, 0)
   339  	}
   340  	a.mu.Lock()
   341  	defer a.mu.Unlock()
   342  	if err := json.Unmarshal(b, &a.array); err != nil {
   343  		return err
   344  	}
   345  	return nil
   346  }
   347  
   348  // Empty 是否为空
   349  func (a *Uint) Empty() bool { return a.Len() == 0 }
   350  
   351  type localRWMutexVTypeUint struct {
   352  	*sync.RWMutex
   353  }
   354  
   355  func newLocalRWMutexVTypeUint(safe bool) *localRWMutexVTypeUint {
   356  	mu := localRWMutexVTypeUint{}
   357  	if safe {
   358  		mu.RWMutex = new(sync.RWMutex)
   359  	}
   360  	return &mu
   361  }
   362  
   363  func (mu *localRWMutexVTypeUint) IsSafe() bool {
   364  	return mu.RWMutex != nil
   365  }
   366  
   367  func (mu *localRWMutexVTypeUint) Lock() {
   368  	if mu.RWMutex != nil {
   369  		mu.RWMutex.Lock()
   370  	}
   371  }
   372  
   373  func (mu *localRWMutexVTypeUint) Unlock() {
   374  	if mu.RWMutex != nil {
   375  		mu.RWMutex.Unlock()
   376  	}
   377  }
   378  
   379  func (mu *localRWMutexVTypeUint) RLock() {
   380  	if mu.RWMutex != nil {
   381  		mu.RWMutex.RLock()
   382  	}
   383  }
   384  
   385  func (mu *localRWMutexVTypeUint) RUnlock() {
   386  	if mu.RWMutex != nil {
   387  		mu.RWMutex.RUnlock()
   388  	}
   389  }
   390  
   391  //template format
   392  var __formatToUint = func(i interface{}) uint {
   393  	switch ii := i.(type) {
   394  	case int:
   395  		return uint(ii)
   396  	case int8:
   397  		return uint(ii)
   398  	case int16:
   399  		return uint(ii)
   400  	case int32:
   401  		return uint(ii)
   402  	case int64:
   403  		return uint(ii)
   404  	case uint:
   405  		return uint(ii)
   406  	case uint8:
   407  		return uint(ii)
   408  	case uint16:
   409  		return uint(ii)
   410  	case uint32:
   411  		return uint(ii)
   412  	case uint64:
   413  		return uint(ii)
   414  	case float32:
   415  		return uint(ii)
   416  	case float64:
   417  		return uint(ii)
   418  	case string:
   419  		iv, err := strconv.ParseUint(ii, 10, 64)
   420  		if err != nil {
   421  			panic(err)
   422  		}
   423  		return uint(iv)
   424  	default:
   425  		panic("unknown type")
   426  	}
   427  }