OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [sieve.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// $G $F.go && $L $F.$A  # don't run it - goes forever
2
 
3
// Copyright 2009 The Go Authors. All rights reserved.
4
// Use of this source code is governed by a BSD-style
5
// license that can be found in the LICENSE file.
6
 
7
package main
8
 
9
// Send the sequence 2, 3, 4, ... to channel 'ch'.
10
func Generate(ch chan<- int) {
11
        for i := 2; ; i++ {
12
                ch <- i // Send 'i' to channel 'ch'.
13
        }
14
}
15
 
16
// Copy the values from channel 'in' to channel 'out',
17
// removing those divisible by 'prime'.
18
func Filter(in <-chan int, out chan<- int, prime int) {
19
        for {
20
                i := <-in // Receive value of new variable 'i' from 'in'.
21
                if i%prime != 0 {
22
                        out <- i // Send 'i' to channel 'out'.
23
                }
24
        }
25
}
26
 
27
// The prime sieve: Daisy-chain Filter processes together.
28
func Sieve() {
29
        ch := make(chan int) // Create a new channel.
30
        go Generate(ch)      // Start Generate() as a subprocess.
31
        for {
32
                prime := <-ch
33
                print(prime, "\n")
34
                ch1 := make(chan int)
35
                go Filter(ch, ch1, prime)
36
                ch = ch1
37
        }
38
}
39
 
40
func main() {
41
        Sieve()
42
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.