slices.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package csync
  2. import (
  3. "iter"
  4. "slices"
  5. "sync"
  6. )
  7. // LazySlice is a thread-safe lazy-loaded slice.
  8. type LazySlice[K any] struct {
  9. inner []K
  10. wg sync.WaitGroup
  11. }
  12. // NewLazySlice creates a new slice and runs the [load] function in a goroutine
  13. // to populate it.
  14. func NewLazySlice[K any](load func() []K) *LazySlice[K] {
  15. s := &LazySlice[K]{}
  16. s.wg.Go(func() {
  17. s.inner = load()
  18. })
  19. return s
  20. }
  21. // Seq returns an iterator that yields elements from the slice.
  22. func (s *LazySlice[K]) Seq() iter.Seq[K] {
  23. s.wg.Wait()
  24. return func(yield func(K) bool) {
  25. for _, v := range s.inner {
  26. if !yield(v) {
  27. return
  28. }
  29. }
  30. }
  31. }
  32. // Slice is a thread-safe slice implementation that provides concurrent access.
  33. type Slice[T any] struct {
  34. inner []T
  35. mu sync.RWMutex
  36. }
  37. // NewSlice creates a new thread-safe slice.
  38. func NewSlice[T any]() *Slice[T] {
  39. return &Slice[T]{
  40. inner: make([]T, 0),
  41. }
  42. }
  43. // NewSliceFrom creates a new thread-safe slice from an existing slice.
  44. func NewSliceFrom[T any](s []T) *Slice[T] {
  45. inner := make([]T, len(s))
  46. copy(inner, s)
  47. return &Slice[T]{
  48. inner: inner,
  49. }
  50. }
  51. // Append adds an element to the end of the slice.
  52. func (s *Slice[T]) Append(items ...T) {
  53. s.mu.Lock()
  54. defer s.mu.Unlock()
  55. s.inner = append(s.inner, items...)
  56. }
  57. // Prepend adds an element to the beginning of the slice.
  58. func (s *Slice[T]) Prepend(item T) {
  59. s.mu.Lock()
  60. defer s.mu.Unlock()
  61. s.inner = append([]T{item}, s.inner...)
  62. }
  63. // Delete removes the element at the specified index.
  64. func (s *Slice[T]) Delete(index int) bool {
  65. s.mu.Lock()
  66. defer s.mu.Unlock()
  67. if index < 0 || index >= len(s.inner) {
  68. return false
  69. }
  70. s.inner = slices.Delete(s.inner, index, index+1)
  71. return true
  72. }
  73. // Get returns the element at the specified index.
  74. func (s *Slice[T]) Get(index int) (T, bool) {
  75. s.mu.RLock()
  76. defer s.mu.RUnlock()
  77. var zero T
  78. if index < 0 || index >= len(s.inner) {
  79. return zero, false
  80. }
  81. return s.inner[index], true
  82. }
  83. // Set updates the element at the specified index.
  84. func (s *Slice[T]) Set(index int, item T) bool {
  85. s.mu.Lock()
  86. defer s.mu.Unlock()
  87. if index < 0 || index >= len(s.inner) {
  88. return false
  89. }
  90. s.inner[index] = item
  91. return true
  92. }
  93. // Len returns the number of elements in the slice.
  94. func (s *Slice[T]) Len() int {
  95. s.mu.RLock()
  96. defer s.mu.RUnlock()
  97. return len(s.inner)
  98. }
  99. // SetSlice replaces the entire slice with a new one.
  100. func (s *Slice[T]) SetSlice(items []T) {
  101. s.mu.Lock()
  102. defer s.mu.Unlock()
  103. s.inner = make([]T, len(items))
  104. copy(s.inner, items)
  105. }
  106. // Seq returns an iterator that yields elements from the slice.
  107. func (s *Slice[T]) Seq() iter.Seq[T] {
  108. return func(yield func(T) bool) {
  109. for _, v := range s.Seq2() {
  110. if !yield(v) {
  111. return
  112. }
  113. }
  114. }
  115. }
  116. // Seq2 returns an iterator that yields index-value pairs from the slice.
  117. func (s *Slice[T]) Seq2() iter.Seq2[int, T] {
  118. s.mu.RLock()
  119. items := make([]T, len(s.inner))
  120. copy(items, s.inner)
  121. s.mu.RUnlock()
  122. return func(yield func(int, T) bool) {
  123. for i, v := range items {
  124. if !yield(i, v) {
  125. return
  126. }
  127. }
  128. }
  129. }