summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2015-10-23 18:34:23 +0200
committerSilvan Jegen <s.jegen@gmail.com>2015-10-27 21:40:56 +0100
commit6993463ee17b2c6b2de65649d535e878e3310827 (patch)
tree91edfde011b6cdf965873b1e6c6cc80a9a507c18
parentf7faf074f4fba3a1ffc26f11246c8cdc856b58ba (diff)
Use the workers (WIP)
-rw-r--r--gwic.go39
1 files changed, 27 insertions, 12 deletions
diff --git a/gwic.go b/gwic.go
index c806196..b71a307 100644
--- a/gwic.go
+++ b/gwic.go
@@ -48,18 +48,30 @@ func printWithContext(all []string, ind, ctxlen int) {
}
func feedworkers(wlist []*work, w string) []*work {
- var onefinished bool
var ret []*work
+ if len(wlist) == 0 {
+ return ret
+ }
+ ret = make([]*work, len(wlist))
+
+ copy(ret, wlist)
+ //fmt.Fprintf(os.Stderr, "wlist not empty: %v ret: %v\n", wlist, ret)
+
for _, wc := range wlist {
if wc.left == 0 {
close(wc.c)
- onefinished = true
+ //fmt.Fprintf(os.Stderr, "Closed wc: %s\n", wc)
+ // TODO: remove the right one?
+ if len(wlist) >= 1 {
+ ret = wlist[1:]
+ }
+ } else {
+ wc.c <- w
+ wc.left -= 1
+ //fmt.Fprintf(os.Stderr, "diminished wc: %v\n", wc)
}
}
- if onefinished && len(wlist) > 1 {
- ret = wlist[1:]
- }
return ret
}
@@ -72,27 +84,30 @@ func print_kwic(sl []string, c chan string) {
func printqueue(q *queue, c chan string, left int) {
var hungryhippos []*work
+
for w := range c {
index := strings.Index(w, kw)
if index < 0 {
q.insert(w)
- feedworkers(hungryhippos, w)
+ hungryhippos = feedworkers(hungryhippos, w)
continue
}
q.insert(fmt.Sprintf("%q", w))
- fmt.Fprintf(os.Stdout, "%q totally matched %q\n", w, kw)
- fmt.Fprintf(os.Stdout, "queue was %v\n", q.q)
+ //fmt.Fprintf(os.Stdout, "%q totally matched %q\n", w, kw)
+ //fmt.Fprintf(os.Stdout, "queue was %v\n", q.q)
wc := &work{c: make(chan string), left: 5}
- feedworkers(hungryhippos, w)
+ hungryhippos = feedworkers(hungryhippos, w)
sl := make([]string, MAX)
- n := copy(sl, q.q[6:])
- fmt.Fprintf(os.Stdout, "copied %d : %v\n", n, sl)
+ //n := copy(sl, q.q[6:])
+ copy(sl, q.q[6:])
+ //fmt.Fprintf(os.Stdout, "copied %d : %v\n", n, sl)
hungryhippos = append(hungryhippos, wc)
+ //fmt.Fprintf(os.Stderr, "appended hungryhippos: %v %d\n", hungryhippos, len(hungryhippos))
go print_kwic(sl, wc.c)
}
for _, wc := range hungryhippos {
@@ -137,6 +152,6 @@ func main() {
}(sc)
printqueue(&q, sc, 5)
- //printWithContext(splline, i, 5)
+ fmt.Printf("Reached end\n")
tw.Flush()
}