From 6993463ee17b2c6b2de65649d535e878e3310827 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Fri, 23 Oct 2015 18:34:23 +0200 Subject: Use the workers (WIP) --- gwic.go | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'gwic.go') 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() } -- cgit v1.2.1-18-gbd029