needed DeleteBy()

This commit is contained in:
Jeff Carr 2025-01-09 21:15:01 -06:00
parent 805d3cdcc3
commit 02e868647f
2 changed files with 20 additions and 19 deletions

View File

@ -21,5 +21,6 @@ func (pb *Files) makeNewSortfile(pf *File) error {
pf.appendUnique(f) // Append() enforce no unique keys
pf.iterSortBy(f)
pf.iterAll(f)
pf.iterDelete(f)
return nil
}

26
sort.go
View File

@ -223,26 +223,26 @@ func iterReplace(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "")
}
func iterDelete(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") DeleteBy"+names["append"]+"(s string) bool {")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
func (pf *File) iterDelete(w io.Writer) {
var MSG string = pf.Bases.Name
var LOCK string = pf.Bases.Lockname
for _, KEY := range pf.Base.Sort {
fmt.Fprintln(w, "func (all *"+MSG+") DeleteBy"+KEY+"(s string) bool {")
fmt.Fprintln(w, " "+LOCK+".RLock()")
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {")
fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")
fmt.Fprintln(w, " all."+names["Bases"]+"[i] = all."+names["Bases"]+"[len(all."+names["Bases"]+")-1]")
fmt.Fprintln(w, " all."+names["Bases"]+" = all."+names["Bases"]+"[:len(all."+names["Bases"]+")-1]")
fmt.Fprintln(w, " for i, _ := range all."+MSG+" {")
fmt.Fprintln(w, " if all."+MSG+"[i]."+KEY+" == s {")
fmt.Fprintln(w, " all."+MSG+"[i] = all."+MSG+"[len(all."+MSG+")-1]")
fmt.Fprintln(w, " all."+MSG+" = all."+MSG+"[:len(all."+MSG+")-1]")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
}
// this tries to return the deleted one but is wrong/gives warning if mutex lock is in struct