gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/listx/list.go (about) 1 package listx 2 3 import ( 4 "sort" 5 "sync" 6 ) 7 8 func NewList() (obj *List) { 9 obj = &List{} 10 return 11 } 12 13 type List struct { 14 data []interface{} 15 16 mutex sync.RWMutex 17 mutexLocked bool 18 mutexRLocked bool 19 } 20 21 func (list *List) rlock() { 22 if list.mutexLocked || list.mutexRLocked { 23 return 24 } 25 list.mutex.RLock() 26 list.mutexRLocked = true 27 } 28 func (list *List) 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) lock() { 39 if list.mutexLocked || list.mutexRLocked { 40 return 41 } 42 list.mutex.Lock() 43 list.mutexLocked = true 44 } 45 func (list *List) 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) Add(items ...interface{}) { 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) AddConf(cond func(index int, item interface{}) bool, items ...interface{}) { 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) Set(index int, val interface{}) { 81 // 索引的读写是并发安全的 82 list.data[index] = val 83 } 84 85 func (list *List) SetList(val []interface{}) { 86 list.lock() 87 list.data = val 88 list.unlock() 89 } 90 91 func (list *List) At(index int) (result interface{}) { 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) Clear() { 102 list.lock() 103 list.data = []interface{}{} 104 list.unlock() 105 } 106 107 func (list *List) Dispose() { 108 list.lock() 109 list.data = nil 110 list.unlock() 111 } 112 113 func (list *List) Clone() *List { 114 list.lock() 115 s := list.data 116 ss := make([]interface{}, len(s), cap(s)) 117 copy(ss, s) 118 list.unlock() 119 return &List{data: ss} 120 } 121 122 func (list *List) Contains(item interface{}) bool { 123 return list.IndexOf(item) != -1 124 } 125 126 func (list *List) ContainsCond(cond func(index int, item interface{}) bool) bool { 127 return list.IndexOfConf(cond) != -1 128 } 129 130 // CountCond 按照条件计数 131 func (list *List) CountCond(cond func(index int, item interface{}) 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) Every(cond func(index int, item interface{}) 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) First(cond func(index int, item interface{}) bool) (val interface{}, 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) Filter(cond func(index int, item interface{}) bool) *List { 176 list.rlock() 177 var a []interface{} 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{data: a} 185 } 186 187 // 如果返回错误,迭代终止 188 func (list *List) ForRange(handler func(index int, item interface{}) 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) ForRangeFromLast(handler func(index int, item interface{}) 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) IndexOfConf(cond func(index int, item interface{}) 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) IndexOf(item interface{}) 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) Last(cond func(index int, item interface{}) bool) (result interface{}, 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) Length() int { 255 return len(list.data) 256 } 257 258 func (list *List) Pop() interface{} { 259 list.lock() 260 261 s := list.data 262 last := s[len(s)-1] // 索引的读写是并发安全的 263 var zero interface{} 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) Push(item interface{}) { 273 list.lock() 274 list.push(item) 275 list.unlock() 276 } 277 278 func (list *List) push(item interface{}) { 279 list.data = append(list.data, item) 280 } 281 282 func (list *List) PushList(val IList) { 283 list.lock() 284 list.pushList(val) 285 list.unlock() 286 } 287 288 func (list *List) pushList(val IList) { 289 s := list.data 290 s = append(s, val.Slice()...) 291 list.data = s 292 } 293 294 func (list *List) Remove(item interface{}) { 295 i := list.IndexOf(item) 296 if i != -1 { 297 list.RemoveAt(i) 298 } 299 } 300 301 func (list *List) RemoveAt(i int) { 302 list.lock() 303 304 s := list.data 305 copy(s[i:], s[i+1:]) 306 var zero interface{} 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) Replace(i int, item interface{}) { 315 list.lock() 316 317 s := list.data 318 over := i - len(s) 319 if over > -1 { 320 ss := make([]interface{}, 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) Insert(i int, item interface{}) { 331 list.lock() 332 333 s := list.data 334 var zero interface{} 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) 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) Shift() interface{} { 356 list.lock() 357 358 s := list.data 359 top := s[0] 360 var zero interface{} 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) Slice() []interface{} { 371 return list.data 372 } 373 374 // Self 返回自身的列表 375 // 用于List作为组合时的快捷方式 376 func (list *List) Self() *List { 377 return list 378 } 379 380 // compare返回值:>0 a排b前面 381 func (list *List) Sort(compare func(a, b interface{}) 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) Unshift(item interface{}) { 391 list.lock() 392 393 s := list.data 394 l := len(s) + 1 395 ss := make([]interface{}, 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) Unique() *List { 405 list.lock() 406 l := list.unique() 407 list.unlock() 408 return l 409 } 410 411 func (list *List) unique() *List { 412 var r []interface{} 413 l := list.data 414 m := map[interface{}]struct{}{} // 存放不重复主键 415 { 416 } // 存放不重复主键 417 for _, e := range l { 418 length := len(m) 419 m[e] = struct{}{} 420 if len(m) != length { // 加入map后,map长度变化,则元素不重复 421 r = append(r, e) 422 } 423 } 424 425 return &List{data: r} 426 } 427 428 // UniqueByCustomKey 去重操作, 返回去重后的数组 429 // 自定义键值 430 func (list *List) UniqueByCustomKey(getKey func(item interface{}) string) *List { 431 list.lock() 432 l := list.uniqueByCustomKey(getKey) 433 list.unlock() 434 return l 435 } 436 437 func (list *List) uniqueByCustomKey(getKey func(item interface{}) string) *List { 438 var r []interface{} 439 l := list.data 440 m := map[string]struct{}{} // 存放不重复主键 441 for _, e := range l { 442 length := len(m) 443 m[getKey(e)] = struct{}{} 444 if len(m) != length { // 加入map后,map长度变化,则元素不重复 445 r = append(r, e) 446 } 447 } 448 return &List{data: r} 449 } 450 451 // Union 并集 452 func (list *List) Union(a IList) *List { 453 list.lock() 454 list.pushList(a) 455 l := list.unique() 456 list.unlock() 457 return l 458 } 459 460 // UnionByCustomKey 并集 461 func (list *List) UnionByCustomKey(a IList, getKey func(item interface{}) string) *List { 462 list.lock() 463 list.pushList(a) 464 return list.UniqueByCustomKey(getKey) 465 }