github.com/sandwich-go/boost@v1.3.29/xcontainer/sarray/gen_any.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 Any struct { 17 mu *localRWMutexVTypeAny 18 array []interface{} 19 } 20 21 // New 创建非协程安全版本 22 func NewAny() *Any { return newWithSafeAny(false) } 23 24 // NewSync 创建协程安全版本 25 func NewSyncAny() *Any { return newWithSafeAny(true) } 26 27 func newWithSafeAny(safe bool) *Any { 28 return &Any{ 29 mu: newLocalRWMutexVTypeAny(safe), 30 array: make([]interface{}, 0), 31 } 32 } 33 34 // At 返回指定位置元素,如果越界则返回默认空值 35 func (a *Any) At(index int) (value interface{}) { 36 value, _ = a.Get(index) 37 return 38 } 39 40 // Get 返回指定位置元素,found标识元素是否存在 41 func (a *Any) Get(index int) (value interface{}, 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 *Any) 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 *Any) Set(index int, value interface{}) 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 *Any) SetArray(array []interface{}) *Any { 67 a.mu.Lock() 68 defer a.mu.Unlock() 69 a.array = array 70 return a 71 } 72 73 // Replace 替换指定位置元素 74 func (a *Any) Replace(given []interface{}) *Any { 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 *Any) SortFunc(less func(v1, v2 interface{}) bool) *Any { 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 *Any) InsertBefore(index int, value interface{}) 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([]interface{}{}, 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 *Any) InsertAfter(index int, value interface{}) 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([]interface{}{}, 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 *Any) Contains(value interface{}) bool { 125 return a.Search(value) != -1 126 } 127 128 // Search 查找元素,不存在返回-1 129 func (a *Any) Search(value interface{}) 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 *Any) DeleteValue(value interface{}) (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 *Any) LoadAndDelete(index int) (value interface{}, found bool) { 155 a.mu.Lock() 156 defer a.mu.Unlock() 157 return a.doDeleteWithoutLock(index) 158 } 159 160 // doRemoveWithoutLock 不加锁移除元素 161 func (a *Any) doDeleteWithoutLock(index int) (value interface{}, 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 *Any) PushLeft(value ...interface{}) *Any { 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 *Any) PushRight(value ...interface{}) *Any { 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 *Any) PopLeft() (value interface{}, 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 *Any) PopRight() (value interface{}, 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 *Any) PopRand() (value interface{}, 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 *Any) PopRands(size int) []interface{} { 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([]interface{}, 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 *Any) Append(value ...interface{}) *Any { return a.PushRight(value...) } 246 247 // Len 获取长度 248 func (a *Any) 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 *Any) Slice() []interface{} { 257 array := ([]interface{})(nil) 258 if a.mu.IsSafe() { 259 a.mu.RLock() 260 defer a.mu.RUnlock() 261 array = make([]interface{}, 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 *Any) Clear() *Any { 271 a.mu.Lock() 272 if len(a.array) > 0 { 273 a.array = make([]interface{}, 0) 274 } 275 a.mu.Unlock() 276 return a 277 } 278 279 // LockFunc 写锁操作array 280 func (a *Any) LockFunc(f func(array []interface{})) *Any { 281 a.mu.Lock() 282 defer a.mu.Unlock() 283 f(a.array) 284 return a 285 } 286 287 // RLockFunc 读锁操作array 288 func (a *Any) RLockFunc(f func(array []interface{})) *Any { 289 a.mu.RLock() 290 defer a.mu.RUnlock() 291 f(a.array) 292 return a 293 } 294 295 // Rand 随机一个元素 296 func (a *Any) Rand() (value interface{}, 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 *Any) WalkAsc(f func(k int, v interface{}) 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 *Any) WalkDesc(f func(k int, v interface{}) 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 Any) 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 *Any) UnmarshalJSON(b []byte) error { 336 if a.array == nil { 337 a.array = make([]interface{}, 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 *Any) Empty() bool { return a.Len() == 0 } 349 350 type localRWMutexVTypeAny struct { 351 *sync.RWMutex 352 } 353 354 func newLocalRWMutexVTypeAny(safe bool) *localRWMutexVTypeAny { 355 mu := localRWMutexVTypeAny{} 356 if safe { 357 mu.RWMutex = new(sync.RWMutex) 358 } 359 return &mu 360 } 361 362 func (mu *localRWMutexVTypeAny) IsSafe() bool { 363 return mu.RWMutex != nil 364 } 365 366 func (mu *localRWMutexVTypeAny) Lock() { 367 if mu.RWMutex != nil { 368 mu.RWMutex.Lock() 369 } 370 } 371 372 func (mu *localRWMutexVTypeAny) Unlock() { 373 if mu.RWMutex != nil { 374 mu.RWMutex.Unlock() 375 } 376 } 377 378 func (mu *localRWMutexVTypeAny) RLock() { 379 if mu.RWMutex != nil { 380 mu.RWMutex.RLock() 381 } 382 } 383 384 func (mu *localRWMutexVTypeAny) RUnlock() { 385 if mu.RWMutex != nil { 386 mu.RWMutex.RUnlock() 387 } 388 } 389 390 //template format 391 var __formatToAny = func(i interface{}) interface{} { 392 return i 393 }