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

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