summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2015-09-24 21:09:16 +0200
committerSilvan Jegen <s.jegen@gmail.com>2015-10-27 21:40:56 +0100
commitf7faf074f4fba3a1ffc26f11246c8cdc856b58ba (patch)
tree3607abdde215e59d3957c1403cea74a62ff75132
parentbdf6bcac460593b1be2eb103c0ea5ad2ba142f61 (diff)
Try to use a worker-based approach (WIP)
-rw-r--r--gwic.go55
1 files 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)
}
}