From f7faf074f4fba3a1ffc26f11246c8cdc856b58ba Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Thu, 24 Sep 2015 21:09:16 +0200 Subject: Try to use a worker-based approach (WIP) --- gwic.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/gwic.go b/gwic.go index 5133557..c806196 100644 --- a/gwic.go +++ b/gwic.go @@ -23,6 +23,11 @@ func (q *queue) insert(s string) { q.q = append(q.q[1:len(q.q)], s) } +type work struct { + c chan string + left int +} + func printWithContext(all []string, ind, ctxlen int) { var slice []string slice = append(slice, "\""+all[ind]+"\"") @@ -42,24 +47,56 @@ func printWithContext(all []string, ind, ctxlen int) { fmt.Fprintf(tw, "%s\n", strings.Join(slice, "\t")) } -func printqueue(q *queue, c chan string, left int) { - if left == 0 { - fmt.Fprintf(tw, "%s\n", strings.Join(q.q, "\t")) - fmt.Fprintf(os.Stdout, "called writer with %v\n", q.q) - return +func feedworkers(wlist []*work, w string) []*work { + var onefinished bool + var ret []*work + for _, wc := range wlist { + if wc.left == 0 { + close(wc.c) + onefinished = true + } + } + + if onefinished && len(wlist) > 1 { + ret = wlist[1:] + } + return ret +} + +func print_kwic(sl []string, c chan string) { + for w := range c { + sl = append(sl, w) } + fmt.Fprintf(tw, "%s\n", strings.Join(sl, "\t")) +} + +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) 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) - q.insert(fmt.Sprintf("%q", w)) - left-- - printqueue(q, c, left) - left = 5 + + wc := &work{c: make(chan string), left: 5} + + feedworkers(hungryhippos, w) + + sl := make([]string, MAX) + n := copy(sl, q.q[6:]) + fmt.Fprintf(os.Stdout, "copied %d : %v\n", n, sl) + hungryhippos = append(hungryhippos, wc) + go print_kwic(sl, wc.c) + } + for _, wc := range hungryhippos { + close(wc.c) } } -- cgit v1.2.3