| package csync |
|
|
| import ( |
| "iter" |
| "slices" |
| "sync" |
| ) |
|
|
| |
| type LazySlice[K any] struct { |
| inner []K |
| wg sync.WaitGroup |
| } |
|
|
| |
| |
| func NewLazySlice[K any](load func() []K) *LazySlice[K] { |
| s := &LazySlice[K]{} |
| s.wg.Go(func() { |
| s.inner = load() |
| }) |
| return s |
| } |
|
|
| |
| func (s *LazySlice[K]) Seq() iter.Seq[K] { |
| s.wg.Wait() |
| return func(yield func(K) bool) { |
| for _, v := range s.inner { |
| if !yield(v) { |
| return |
| } |
| } |
| } |
| } |
|
|
| |
| type Slice[T any] struct { |
| inner []T |
| mu sync.RWMutex |
| } |
|
|
| |
| func NewSlice[T any]() *Slice[T] { |
| return &Slice[T]{ |
| inner: make([]T, 0), |
| } |
| } |
|
|
| |
| func NewSliceFrom[T any](s []T) *Slice[T] { |
| inner := make([]T, len(s)) |
| copy(inner, s) |
| return &Slice[T]{ |
| inner: inner, |
| } |
| } |
|
|
| |
| func (s *Slice[T]) Append(items ...T) { |
| s.mu.Lock() |
| defer s.mu.Unlock() |
| s.inner = append(s.inner, items...) |
| } |
|
|
| |
| func (s *Slice[T]) Prepend(item T) { |
| s.mu.Lock() |
| defer s.mu.Unlock() |
| s.inner = append([]T{item}, s.inner...) |
| } |
|
|
| |
| func (s *Slice[T]) Delete(index int) bool { |
| s.mu.Lock() |
| defer s.mu.Unlock() |
| if index < 0 || index >= len(s.inner) { |
| return false |
| } |
| s.inner = slices.Delete(s.inner, index, index+1) |
| return true |
| } |
|
|
| |
| func (s *Slice[T]) Get(index int) (T, bool) { |
| s.mu.RLock() |
| defer s.mu.RUnlock() |
| var zero T |
| if index < 0 || index >= len(s.inner) { |
| return zero, false |
| } |
| return s.inner[index], true |
| } |
|
|
| |
| func (s *Slice[T]) Set(index int, item T) bool { |
| s.mu.Lock() |
| defer s.mu.Unlock() |
| if index < 0 || index >= len(s.inner) { |
| return false |
| } |
| s.inner[index] = item |
| return true |
| } |
|
|
| |
| func (s *Slice[T]) Len() int { |
| s.mu.RLock() |
| defer s.mu.RUnlock() |
| return len(s.inner) |
| } |
|
|
| |
| func (s *Slice[T]) SetSlice(items []T) { |
| s.mu.Lock() |
| defer s.mu.Unlock() |
| s.inner = make([]T, len(items)) |
| copy(s.inner, items) |
| } |
|
|
| |
| func (s *Slice[T]) Seq() iter.Seq[T] { |
| return func(yield func(T) bool) { |
| for _, v := range s.Seq2() { |
| if !yield(v) { |
| return |
| } |
| } |
| } |
| } |
|
|
| |
| func (s *Slice[T]) Seq2() iter.Seq2[int, T] { |
| s.mu.RLock() |
| items := make([]T, len(s.inner)) |
| copy(items, s.inner) |
| s.mu.RUnlock() |
| return func(yield func(int, T) bool) { |
| for i, v := range items { |
| if !yield(i, v) { |
| return |
| } |
| } |
| } |
| } |
|
|