登录
首页 >  Golang >  Go问答

使用 goroutine 进行组合

来源:stackoverflow

时间:2024-04-08 21:48:32 460浏览 收藏

从现在开始,努力学习吧!本文《使用 goroutine 进行组合》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

我正在尝试让代码工作,它几乎涉及 goroutine 中的通道(在 c# 中类似于产量的行为)

代码涉及从切片中获取可迭代矩阵,如下所示:

elements := []float64{1, 2, 3, 4}

expected := [][]float64{
    {1},
    {2},
    {3},
    {4},
    {1, 2},
    {1, 3},
    {2, 3},
    {1, 4},
    {2, 4},
    {3, 4},
    {1, 2, 3},
    {1, 2, 4},
    {1, 3, 4},
    {2, 3, 4},
    {1, 2, 3, 4},
}

我尝试通过以下方式应用 knuth 的方法:

func Combinadic(values []float64) <-chan []float64 {
    ch := make(chan []float64)
    go func() {
        for i := 0; i < len(values); i++ {

            for value := range CombinadicK(values, i+1) {
                ch <- value
            }

        }
        close(ch)
    }()

    return ch

}

func CombinadicK(values []float64, k int) <-chan []float64 {
    chnl := make(chan []float64)
    go func() {
        n := len(values)

        t := k

        c := make([]int, t+3)
        current := make([]float64, t)

        x := 0
        j := 0

        for j = 1; j <= t; j++ {
            c[j] = j - 1
        }

        c[t+1] = n
        c[t+2] = 0

        j = t

        for {
            for i := 0; i < len(current); i++ {
                current[i] = values[c[i+1]]
            }

            chnl <- current

            if j > 0 {
                x = j
            } else {
                if c[1]+1 < c[2] {
                    c[1]++
                    continue
                } else {
                    j = 2
                }
            }

            for {
                c[j-1] = j - 2
                x = c[j] + 1
                if x == c[j+1] {
                    j++
                } else {
                    break
                }
            }

            c[j] = x
            j--

            if j >= t {
                break
            }
        }

        close(chnl)
    }()

    return chnl
}

它似乎给出了每行的随机数,但预期的结构(每行的项目数)似乎没问题。

go演示中的代码


解决方案


您正在进行数据竞赛。您的结果未定义。

$ go run -race racer.go
==================
warning: data race
read at 0x00c00009c010 by main goroutine:
  reflect.typedmemmove()
      /home/peter/go/src/runtime/mbarrier.go:177 +0x0
  reflect.packeface()
      /home/peter/go/src/reflect/value.go:119 +0x103
  reflect.valueinterface()
      /home/peter/go/src/reflect/value.go:1027 +0x16f
  fmt.(*pp).printvalue()
      /home/peter/go/src/reflect/value.go:997 +0x38f7
  fmt.(*pp).printvalue()
      /home/peter/go/src/fmt/print.go:868 +0xec7
  fmt.(*pp).printarg()
      /home/peter/go/src/fmt/print.go:715 +0x2ee
  fmt.(*pp).doprintln()
      /home/peter/go/src/fmt/print.go:1172 +0xad
  fmt.fprintln()
      /home/peter/go/src/fmt/print.go:263 +0x65
  main.main()
      /home/peter/go/src/fmt/print.go:273 +0x14b

previous write at 0x00c00009c010 by goroutine 8:
  main.combinadick.func1()
      /home/peter/racer.go:48 +0x1e6

goroutine 8 (running) created at:
  main.combinadick()
      /home/peter/racer.go:26 +0x96
  main.combinadic.func1()
      /home/peter/racer.go:12 +0xda
==================
[3]
[3]
[4]
[4]
[2 3]
[2 4]
[1 4]
[3 4]
[3 4]
[3 4]
[1 3 4]
[1 3 4]
[1 3 4]
[2 3 4]
[1 2 3 4]
found 1 data race(s)
exit status 66
$

racer.go

package main

import (
    "fmt"
)

func Combinadic(values []float64) <-chan []float64 {
    ch := make(chan []float64)
    go func() {
        for i := 0; i < len(values); i++ {

            for value := range CombinadicK(values, i+1) {
                ch <- value
            }

        }
        close(ch)
    }()

    return ch

}

func CombinadicK(values []float64, k int) <-chan []float64 {
    chnl := make(chan []float64)
    go func() {
        n := len(values)

        t := k

        c := make([]int, t+3)
        current := make([]float64, t)

        x := 0
        j := 0

        for j = 1; j <= t; j++ {
            c[j] = j - 1
        }

        c[t+1] = n
        c[t+2] = 0

        j = t

        for {
            for i := 0; i < len(current); i++ {
                current[i] = values[c[i+1]]
            }

            chnl <- current

            if j > 0 {
                x = j
            } else {
                if c[1]+1 < c[2] {
                    c[1]++
                    continue
                } else {
                    j = 2
                }
            }

            for {
                c[j-1] = j - 2
                x = c[j] + 1
                if x == c[j+1] {
                    j++
                } else {
                    break
                }
            }

            c[j] = x
            j--

            if j >= t {
                break
            }
        }

        close(chnl)
    }()

    return chnl
}

func main() {
    elements := []float64{1, 2, 3, 4}
    for v := range Combinadic(elements) {
        fmt.Println(v)
    }
}

演示:https://play.golang.org/p/hhQgVdqe6l1

Go: Data Race Detector

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>