github.com/sandwich-go/boost@v1.3.29/xcontainer/sarray/gen_uint32.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 Uint32 struct {
    18  	mu    *localRWMutexVTypeUint32
    19  	array []uint32
    20  }
    21  
    22  // New 创建非协程安全版本
    23  func NewUint32() *Uint32 { return newWithSafeUint32(false) }
    24  
    25  // NewSync 创建协程安全版本
    26  func NewSyncUint32() *Uint32 { return newWithSafeUint32(true) }
    27  
    28  func newWithSafeUint32(safe bool) *Uint32 {
    29  	return &Uint32{
    30  		mu:    newLocalRWMutexVTypeUint32(safe),
    31  		array: make([]uint32, 0),
    32  	}
    33  }
    34  
    35  // At 返回指定位置元素,如果越界则返回默认空值
    36  func (a *Uint32) At(index int) (value uint32) {
    37  	value, _ = a.Get(index)
    38  	return
    39  }
    40  
    41  // Get 返回指定位置元素,found标识元素是否存在
    42  func (a *Uint32) Get(index int) (value uint32, 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 *Uint32) 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 *Uint32) Set(index int, value uint32) 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 *Uint32) SetArray(array []uint32) *Uint32 {
    68  	a.mu.Lock()
    69  	defer a.mu.Unlock()
    70  	a.array = array
    71  	return a
    72  }
    73  
    74  // Replace 替换指定位置元素
    75  func (a *Uint32) Replace(given []uint32) *Uint32 {
    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 *Uint32) SortFunc(less func(v1, v2 uint32) bool) *Uint32 {
    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 *Uint32) InsertBefore(index int, value uint32) 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([]uint32{}, 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 *Uint32) InsertAfter(index int, value uint32) 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([]uint32{}, 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 *Uint32) Contains(value uint32) bool {
   126  	return a.Search(value) != -1
   127  }
   128  
   129  // Search 查找元素,不存在返回-1
   130  func (a *Uint32) Search(value uint32) 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 *Uint32) DeleteValue(value uint32) (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 *Uint32) LoadAndDelete(index int) (value uint32, found bool) {
   156  	a.mu.Lock()
   157  	defer a.mu.Unlock()
   158  	return a.doDeleteWithoutLock(index)
   159  }
   160  
   161  // doRemoveWithoutLock 不加锁移除元素
   162  func (a *Uint32) doDeleteWithoutLock(index int) (value uint32, 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 *Uint32) PushLeft(value ...uint32) *Uint32 {
   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 *Uint32) PushRight(value ...uint32) *Uint32 {
   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 *Uint32) PopLeft() (value uint32, 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 *Uint32) PopRight() (value uint32, 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 *Uint32) PopRand() (value uint32, 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 *Uint32) PopRands(size int) []uint32 {
   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([]uint32, 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 *Uint32) Append(value ...uint32) *Uint32 { return a.PushRight(value...) }
   247  
   248  // Len 获取长度
   249  func (a *Uint32) 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 *Uint32) Slice() []uint32 {
   258  	array := ([]uint32)(nil)
   259  	if a.mu.IsSafe() {
   260  		a.mu.RLock()
   261  		defer a.mu.RUnlock()
   262  		array = make([]uint32, 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 *Uint32) Clear() *Uint32 {
   272  	a.mu.Lock()
   273  	if len(a.array) > 0 {
   274  		a.array = make([]uint32, 0)
   275  	}
   276  	a.mu.Unlock()
   277  	return a
   278  }
   279  
   280  // LockFunc 写锁操作array
   281  func (a *Uint32) LockFunc(f func(array []uint32)) *Uint32 {
   282  	a.mu.Lock()
   283  	defer a.mu.Unlock()
   284  	f(a.array)
   285  	return a
   286  }
   287  
   288  // RLockFunc 读锁操作array
   289  func (a *Uint32) RLockFunc(f func(array []uint32)) *Uint32 {
   290  	a.mu.RLock()
   291  	defer a.mu.RUnlock()
   292  	f(a.array)
   293  	return a
   294  }
   295  
   296  // Rand 随机一个元素
   297  func (a *Uint32) Rand() (value uint32, 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 *Uint32) WalkAsc(f func(k int, v uint32) 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 *Uint32) WalkDesc(f func(k int, v uint32) 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 Uint32) 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 *Uint32) UnmarshalJSON(b []byte) error {
   337  	if a.array == nil {
   338  		a.array = make([]uint32, 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 *Uint32) Empty() bool { return a.Len() == 0 }
   350  
   351  type localRWMutexVTypeUint32 struct {
   352  	*sync.RWMutex
   353  }
   354  
   355  func newLocalRWMutexVTypeUint32(safe bool) *localRWMutexVTypeUint32 {
   356  	mu := localRWMutexVTypeUint32{}
   357  	if safe {
   358  		mu.RWMutex = new(sync.RWMutex)
   359  	}
   360  	return &mu
   361  }
   362  
   363  func (mu *localRWMutexVTypeUint32) IsSafe() bool {
   364  	return mu.RWMutex != nil
   365  }
   366  
   367  func (mu *localRWMutexVTypeUint32) Lock() {
   368  	if mu.RWMutex != nil {
   369  		mu.RWMutex.Lock()
   370  	}
   371  }
   372  
   373  func (mu *localRWMutexVTypeUint32) Unlock() {
   374  	if mu.RWMutex != nil {
   375  		mu.RWMutex.Unlock()
   376  	}
   377  }
   378  
   379  func (mu *localRWMutexVTypeUint32) RLock() {
   380  	if mu.RWMutex != nil {
   381  		mu.RWMutex.RLock()
   382  	}
   383  }
   384  
   385  func (mu *localRWMutexVTypeUint32) RUnlock() {
   386  	if mu.RWMutex != nil {
   387  		mu.RWMutex.RUnlock()
   388  	}
   389  }
   390  
   391  //template format
   392  var __formatToUint32 = func(i interface{}) uint32 {
   393  	switch ii := i.(type) {
   394  	case int:
   395  		return uint32(ii)
   396  	case int8:
   397  		return uint32(ii)
   398  	case int16:
   399  		return uint32(ii)
   400  	case int32:
   401  		return uint32(ii)
   402  	case int64:
   403  		return uint32(ii)
   404  	case uint:
   405  		return uint32(ii)
   406  	case uint8:
   407  		return uint32(ii)
   408  	case uint16:
   409  		return uint32(ii)
   410  	case uint32:
   411  		return uint32(ii)
   412  	case uint64:
   413  		return uint32(ii)
   414  	case float32:
   415  		return uint32(ii)
   416  	case float64:
   417  		return uint32(ii)
   418  	case string:
   419  		iv, err := strconv.ParseUint(ii, 10, 64)
   420  		if err != nil {
   421  			panic(err)
   422  		}
   423  		return uint32(iv)
   424  	default:
   425  		panic("unknown type")
   426  	}
   427  }