E. 2 канала

Верно решивших: 62
Рейтинг быстрых решений
Очень похожие решения учитываются в рейтинге один раз.
1 2 3 4 5
Время отправки: 2020-09-28 12:59:34 ID: 499 Время: 0.054s
package main import "sync" func Merge2Channels(fn func(int) int, in1 <-chan int, in2 <- chan int, out chan<- int, n int) { wg := new(sync.WaitGroup) f1 := make([]int, n) f2 := make([]int, n) calculate := func(in <-chan int, f []int, i int, wg *sync.WaitGroup) { defer wg.Done() f[i] = fn(<-in) } for i := 0; i < n; i++ { wg.Add(2) go calculate(in1, f1, i, wg) go calculate(in2, f2, i, wg) } go func() { wg.Wait() for i := 0; i < n; i++ { out <- f1[i] + f2[i] } }() }
Время отправки: 2020-06-16 19:17:41 ID: 452 Время: 0.054s
package main import "sync" type record struct { x [2]int } func (r *record) result() int { return r.x[0] + r.x[1] } type table struct { records []*record size int } func newTable(size int) *table { tbl := &table{ records: make([]*record, size), size: size, } for i := 0; i < size; i++ { tbl.records[i] = &record{} } return tbl } func (tbl *table) populate(ins []<-chan int) { var ok bool for _, rec := range tbl.records { for i := range ins { if rec.x[i], ok = <-ins[i]; !ok { return } } } } func (tbl *table) foreach(f func(int) int) { var wg sync.WaitGroup wg.Add(tbl.size << 1) defer wg.Wait() for i := range tbl.records { for j := 0; j < 2; j++ { go func(rec *record, idx int, wg *sync.WaitGroup) { defer wg.Done() rec.x[idx] = f(rec.x[idx]) }(tbl.records[i], j, &wg) } } } func (tbl *table) result(out chan<- int) { for _, rec := range tbl.records { out <- rec.result() } } // Merge2Channels ... func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { // defer close(out) tbl := newTable(n) tbl.populate([]<-chan int{in1, in2}) tbl.foreach(f) tbl.result(out) }(f, in1, in2, out, n) }
Время отправки: 2020-05-31 12:31:44 ID: 190 Время: 0.084s
package main import "time" type Result struct { done bool value int } func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { results := make([]Result, n) go func() { for i := 0; i < n; i++ { x1 := <-in1 x2 := <-in2 index := i go func() { x1res := make(chan int) x2res := make(chan int) go func() { x1res <- f(x1) }() go func() { x2res <- f(x2) }() results[index].value = (<-x1res) + (<-x2res) results[index].done = true }() } }() go func() { for i := 0; i < n; { time.Sleep(time.Millisecond) if results[i].done { out <- results[i].value i++ } } }() }
Время отправки: 2020-05-31 11:53:03 ID: 139 Время: 0.102s
package main var lock = make(chan struct{}, 1) func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func() { //mutex.Lock() lock <- struct{}{} ww1 := make(chan chan int, n) ww2 := make(chan chan int, n) go func() { for i := 0; i < n; i++ { f1 := <-<-ww1 f2 := <-<-ww2 out <- f1 + f2 } //mutex.Unlock() <-lock }() input := func(in <-chan int, ww chan chan int) { for i := 0; i < n; i++ { w := make(chan int) ww <- w x := <-in go func(w chan int, x int) { w <- f(x) }(w, x) } } go input(in1, ww1) go input(in2, ww2) }() }
Время отправки: 2020-05-31 11:59:56 ID: 157 Время: 0.102s
package main import "sync" var lock sync.Mutex func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func() { done := make(chan bool, n*2) r1 := make([]*int, n) // 1 r2 := make([]*int, n) go func() { nDone := 0 for <-done { for i := nDone; i < n; i++ { if r1[i] != nil && r2[i] != nil { out <- *r1[i] + *r2[i] if nDone++; nDone == n { lock.Unlock() return } } else { break } } } }() input := func(ch <-chan int, results []*int) { for i := 0; i < n; i++ { x := <-ch go func(i int, x int) { result := f(x) results[i] = &result done <- true }(i, x) } } lock.Lock() go input(in1, r1) go input(in2, r2) }() }
Время отправки: 2020-06-09 13:23:38 ID: 442 Время: 0.102s
package main import "sync" // AAAaaa, Let me in!! func Merge2Channels(f func(int) int, in1 <-chan int, in2 <- chan int, out chan<- int, n int) { go func() { var wg sync.WaitGroup r := make([]int, n) for i := 0; i < n; i++ { x1 := <- in1 x2 := <- in2 wg.Add(1) go func(idx, x1, x2 int, wg *sync.WaitGroup) { defer wg.Done() r[idx] = f(x1) + f(x2) }(i, x1, x2, &wg) } wg.Wait() for _, result := range r { out <- result } }() }
Время отправки: 2020-05-31 14:02:21 ID: 241 Время: 0.102s
package main var lim chan struct{} func init(){ lim= make(chan struct{},1) } func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int){ go func() { lim<-struct{}{} procIn := func(cin chan chan int, in <-chan int, n int) { defer close(cin) for i := 0; i < n; i++ { x, ok := <-in if !ok { return } c := make(chan int) cin <- c go func(c chan int) { c <- f(x) close(c) }(c) } } cin1 := make(chan chan int, n) cin2 := make(chan chan int, n) go procIn(cin1, in1, n) go procIn(cin2, in2, n) go func() { for cnt:=0; cnt<n;{ select { case x:=<-cin1: out<- <-x+<-<-cin2 cnt++ case x:=<-cin2: out<- <-x+<-<-cin1 cnt++ } } <-lim }() }() }
Время отправки: 2020-05-31 13:24:39 ID: 224 Время: 0.102s
package main import ( "sync" ) var lock sync.Mutex func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int) { done := make(chan bool, n*2) results := make([]*int, n*2) go func() { nDone := 0 for <-done { for i := nDone; i < n; i++ { if results[i] != nil && results[i+n] != nil { out <- (*results[i] + *results[i+n]) if nDone++; nDone == n { lock.Unlock() return } } else { break } } } }() input := func(ch <-chan int, results []*int) { for i := 0; i < n; i++ { x := <-ch go func(i int, x int) { result := f(x) results[i] = &result done <- true }(i, x) } } lock.Lock() go input(in1, results[:n]) go input(in2, results[n:]) }(f, in1, in2, out) }
Время отправки: 2020-05-31 13:37:36 ID: 234 Время: 0.102s
package main func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func() { sendChans := make([]chan int, n) for i := range sendChans { sendChans[i] = make(chan int) } for i := 0; i < n; i++ { c1, c2 := <-in1, <-in2 go func(k int) { sendChans[k] <- (f(c1) + f(c2)) }(i) } for i := 0; i < n; i++ { out <- (<-sendChans[i]) } }() }
Время отправки: 2020-06-03 13:12:20 ID: 391 Время: 0.102s
package main import "sync" func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func() { var results = make([]int, n) var wg sync.WaitGroup for i := 1; i <= n; i++ { wg.Add(1) x1 := <-in1 x2 := <-in2 go func(x1 int, x2 int, indx int) { if x1 == x2{ results[indx-1] = f(x1) * 2 } else { results[indx-1] = f(x1) + f(x2) } defer wg.Done() }(x1, x2, i) } wg.Wait() for _, res := range results { out <- res } }() }
1 2 3 4 5