| |
| |
| |
|
|
| package a |
|
|
| import ( |
| "context" |
| "runtime" |
| ) |
|
|
| |
| |
| func SliceEqual[Elem comparable](s1, s2 []Elem) bool { |
| if len(s1) != len(s2) { |
| return false |
| } |
| for i, v1 := range s1 { |
| v2 := s2[i] |
| if v1 != v2 { |
| isNaN := func(f Elem) bool { return f != f } |
| if !isNaN(v1) || !isNaN(v2) { |
| return false |
| } |
| } |
| } |
| return true |
| } |
|
|
| |
| |
| func ReadAll[Elem any](ctx context.Context, c <-chan Elem) []Elem { |
| var r []Elem |
| for { |
| select { |
| case <-ctx.Done(): |
| return r |
| case v, ok := <-c: |
| if !ok { |
| return r |
| } |
| r = append(r, v) |
| } |
| } |
| } |
|
|
| |
| |
| |
| func Merge[Elem any](ctx context.Context, c1, c2 <-chan Elem) <-chan Elem { |
| r := make(chan Elem) |
| go func(ctx context.Context, c1, c2 <-chan Elem, r chan<- Elem) { |
| defer close(r) |
| for c1 != nil || c2 != nil { |
| select { |
| case <-ctx.Done(): |
| return |
| case v1, ok := <-c1: |
| if ok { |
| r <- v1 |
| } else { |
| c1 = nil |
| } |
| case v2, ok := <-c2: |
| if ok { |
| r <- v2 |
| } else { |
| c2 = nil |
| } |
| } |
| } |
| }(ctx, c1, c2, r) |
| return r |
| } |
|
|
| |
| |
| |
| |
| func Filter[Elem any](ctx context.Context, c <-chan Elem, f func(Elem) bool) <-chan Elem { |
| r := make(chan Elem) |
| go func(ctx context.Context, c <-chan Elem, f func(Elem) bool, r chan<- Elem) { |
| defer close(r) |
| for { |
| select { |
| case <-ctx.Done(): |
| return |
| case v, ok := <-c: |
| if !ok { |
| return |
| } |
| if f(v) { |
| r <- v |
| } |
| } |
| } |
| }(ctx, c, f, r) |
| return r |
| } |
|
|
| |
| |
| |
| func Sink[Elem any](ctx context.Context) chan<- Elem { |
| r := make(chan Elem) |
| go func(ctx context.Context, r <-chan Elem) { |
| for { |
| select { |
| case <-ctx.Done(): |
| return |
| case _, ok := <-r: |
| if !ok { |
| return |
| } |
| } |
| } |
| }(ctx, r) |
| return r |
| } |
|
|
| |
| |
| type Exclusive[Val any] struct { |
| c chan Val |
| } |
|
|
| |
| func MakeExclusive[Val any](initial Val) *Exclusive[Val] { |
| r := &Exclusive[Val]{ |
| c: make(chan Val, 1), |
| } |
| r.c <- initial |
| return r |
| } |
|
|
| |
| |
| func (e *Exclusive[Val]) Acquire() Val { |
| return <-e.c |
| } |
|
|
| |
| |
| |
| func (e *Exclusive[Val]) TryAcquire() (v Val, ok bool) { |
| select { |
| case r := <-e.c: |
| return r, true |
| default: |
| return v, false |
| } |
| } |
|
|
| |
| |
| func (e *Exclusive[Val]) Release(v Val) { |
| select { |
| case e.c <- v: |
| default: |
| panic("Exclusive Release without Acquire") |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func Ranger[Elem any]() (*Sender[Elem], *Receiver[Elem]) { |
| c := make(chan Elem) |
| d := make(chan struct{}) |
| s := &Sender[Elem]{ |
| values: c, |
| done: d, |
| } |
| r := &Receiver[Elem]{ |
| values: c, |
| done: d, |
| } |
| runtime.SetFinalizer(r, (*Receiver[Elem]).finalize) |
| return s, r |
| } |
|
|
| |
| type Sender[Elem any] struct { |
| values chan<- Elem |
| done <-chan struct{} |
| } |
|
|
| |
| |
| |
| func (s *Sender[Elem]) Send(ctx context.Context, v Elem) bool { |
| select { |
| case <-ctx.Done(): |
| return false |
| case s.values <- v: |
| return true |
| case <-s.done: |
| return false |
| } |
| } |
|
|
| |
| |
| func (s *Sender[Elem]) Close() { |
| close(s.values) |
| } |
|
|
| |
| type Receiver[Elem any] struct { |
| values <-chan Elem |
| done chan<- struct{} |
| } |
|
|
| |
| |
| func (r *Receiver[Elem]) Next(ctx context.Context) (v Elem, ok bool) { |
| select { |
| case <-ctx.Done(): |
| case v, ok = <-r.values: |
| } |
| return v, ok |
| } |
|
|
| |
| func (r *Receiver[Elem]) finalize() { |
| close(r.done) |
| } |
|
|