diff options
Diffstat (limited to 'sort.go')
| -rw-r--r-- | sort.go | 60 |
1 files changed, 28 insertions, 32 deletions
@@ -11,10 +11,10 @@ import ( func (pb *Files) makeSortfile(pf *File) { f, _ := os.OpenFile(pf.Filebase+".sort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - header(f, pf) + // header(f, pf) - pf.iterTop(f, sortmap["base"]) - iterNext(f, sortmap) + // pf.iterTop(f, sortmap["base"]) + // iterNext(f, sortmap) iterAppend(f, sortmap) // Append() enforce no unique keys iterSortAll(f, sortmap) @@ -55,8 +55,8 @@ func (pb *Files) makeSortfile(pf *File) { iterEnd(f, sortmap) } -func (pf *File) syncLock(w io.Writer, lock string) { - var LOCK string = lock +func (msg *MsgName) syncLock(w io.Writer, s string) { + var LOCK string = msg.Name fmt.Fprintln(w, "// bad global lock until modifying the .pb.go file is tested") fmt.Fprintln(w, "// sync.RWMutex or sync.Mutex?") @@ -64,9 +64,7 @@ func (pf *File) syncLock(w io.Writer, lock string) { fmt.Fprintln(w, "") } -func (pf *File) iterTop(w io.Writer, base string) { - var BASE string = base - +func (pf *File) iterTop(w io.Writer, BASE string) { fmt.Fprintln(w, "type "+BASE+"Iterator struct {") fmt.Fprintln(w, " sync.RWMutex") fmt.Fprintln(w, "") @@ -96,9 +94,9 @@ func (pf *File) iterTop(w io.Writer, base string) { fmt.Fprintln(w, "") } -func iterNext(w io.Writer, names map[string]string) { +func (pf *File) iterNext(w io.Writer, BASE string) { fmt.Fprintln(w, "// Next() returns the next thing in the array") - fmt.Fprintln(w, "func (it *"+names["Base"]+"Iterator) Next() *"+names["Base"]+" {") + fmt.Fprintln(w, "func (it *"+BASE+"Iterator) Next() *"+BASE+" {") fmt.Fprintln(w, " if it.things[it.index-1] == nil {") fmt.Fprintln(w, " for i, d := range it.things {") fmt.Fprintln(w, " fmt.Println(\"i =\", i, d)") @@ -175,34 +173,32 @@ func iterEnd(w io.Writer, names map[string]string) { } func iterAppend(w io.Writer, names map[string]string) { - if names["append"] == "" { - fmt.Fprintln(w, "// does not enforce any unique fields") - } else { - fmt.Fprintln(w, "// enforces "+names["append"]+" is unique") - } - if names["append"] == "" { - fmt.Fprintln(w, "func (all *"+names["Bases"]+") Append(newP *"+names["Base"]+") bool {") - } else { - // fmt.Fprintln(w, "func (all *"+names["Bases"]+") Append(newP *"+names["Base"]+") bool { // todo: make unique name here") - fmt.Fprintln(w, "func (all *"+names["Bases"]+") AppendUnique"+names["append"]+"(newP *"+names["Base"]+") bool {") - } - if sortmap["lock"] == "all" { - fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") - fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") +} + +func (pf *File) appendUnique(w io.Writer, msg *MsgName, names map[string]string) { + var MSG string = msg.Name // msg.Name + var BASE string = names["Base"] + var LOCK string = names["lock"] + if argv.Mutex { + LOCK = "Lock" } else { - fmt.Fprintln(w, " "+names["lock"]+".RLock()") - fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + LOCK = names["lock"] + ".Lock" } + + fmt.Fprintln(w, "// enforces "+BASE+" is unique") + fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique(newP *"+BASE+") bool {") + fmt.Fprintln(w, " "+LOCK+".RLock()") + fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") fmt.Fprintln(w, "") - if names["append"] != "" { - fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {") - fmt.Fprintln(w, " if p."+names["append"]+" == newP."+names["append"]+" {") + fmt.Fprintln(w, " for _, p := range all."+MSG+" {") + for _, KEY := range msg.Unique { + fmt.Fprintln(w, " if p."+KEY+" == newP."+KEY+" {") fmt.Fprintln(w, " return false") fmt.Fprintln(w, " }") - fmt.Fprintln(w, " }") - fmt.Fprintln(w, "") } - fmt.Fprintln(w, " all."+names["Bases"]+" = append(all."+names["Bases"]+", newP)") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " all."+MSG+" = append(all."+MSG+", newP)") fmt.Fprintln(w, " return true") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") |
