From 4e2193f4c6ef402adef7a3fed37efe724cdfd505 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 14 Feb 2025 19:24:58 -0600 Subject: [PATCH] Notes added by 'git notes append' --- e5a5454bc0e58d24a6146e4973518160b909582f | 297 +++++++++++++++++++++++ 1 file changed, 297 insertions(+) diff --git a/e5a5454bc0e58d24a6146e4973518160b909582f b/e5a5454bc0e58d24a6146e4973518160b909582f index 35d3050..fb4c99e 100644 --- a/e5a5454bc0e58d24a6146e4973518160b909582f +++ b/e5a5454bc0e58d24a6146e4973518160b909582f @@ -1315,3 +1315,300 @@ func file_goDep_proto_init() { } // `autogen:goDep.sort.pb.go` + +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// This file was autogenerated with autogenpb v0.0.49-2-g9a0e4a6 2025.02.01_0732 +// go install go.wit.com/apps/autogenpb@latest +// +// define which structs (messages) you want to use in the .proto file +// Then sort.pb.go and marshal.pb.go files are autogenerated +// +// autogenpb uses it and has an example .proto file with instructions +// + +package gitpb + +import ( + "fmt" + "sort" + "sync" +) + +// a simple global lock +var goDepMu sync.RWMutex + +func (x *GoDeps) fixUuid() { + if x == nil { + return + } + if x.Uuid == "7de62c09-b335-4d80-902d-08552c501b7c" { + return + } + x.Uuid = "7de62c09-b335-4d80-902d-08552c501b7c" + x.Version = "v0.0.51 go.wit.com/lib/protobuf/gitpb" +} + +func NewGoDeps() *GoDeps { + x := new(GoDeps) + x.Uuid = "7de62c09-b335-4d80-902d-08552c501b7c" + x.Version = "v0.0.51 go.wit.com/lib/protobuf/gitpb" + return x +} + +// START SORT + +// DEFINE THE GoDeps ITERATOR. +// itializes a new iterator. +func newGoDepsIterator(things []*GoDeps) *GoDepsIterator { + return &GoDepsIterator{things: things} +} + +type GoDepsIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*GoDeps + index int +} + +func (it *GoDepsIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *GoDepsIterator) Next() *GoDeps { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in GoDepsIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE GoDep ITERATOR. +// itializes a new iterator. +func newGoDepIterator(things []*GoDep) *GoDepIterator { + return &GoDepIterator{things: things} +} + +type GoDepIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*GoDep + index int +} + +func (it *GoDepIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *GoDepIterator) Next() *GoDep { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in GoDepIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// sort struct by Hash +type GoDepHash []*GoDep + +func (a GoDepHash) Len() int { return len(a) } +func (a GoDepHash) Less(i, j int) bool { return a[i].Hash < a[j].Hash } +func (a GoDepHash) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// sort struct by GoPath +type GoDepGoPath []*GoDep + +func (a GoDepGoPath) Len() int { return len(a) } +func (a GoDepGoPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath } +func (a GoDepGoPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// safely returns a slice of pointers to the FRUIT protobufs +func (x *GoDeps) allGoDeps() []*GoDep { + goDepMu.RLock() + defer goDepMu.RUnlock() + + // Create a new slice to hold pointers to each FRUIT + var tmp []*GoDep + tmp = make([]*GoDep, len(x.GoDeps)) + for i, p := range x.GoDeps { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the GoDep protobufs +func (x *GoDeps) selectAllGoDeps() []*GoDep { + goDepMu.RLock() + defer goDepMu.RUnlock() + + // Create a new slice to hold pointers to each GoDep + var tmp []*GoDep + tmp = make([]*GoDep, len(x.GoDeps)) + for i, p := range x.GoDeps { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} +func (x *GoDeps) SortByHash() *GoDepIterator { + // copy the pointers as fast as possible. + things := x.selectAllGoDeps() + + // todo: try slices.SortFunc() instead to see what happens + sort.Sort(GoDepHash(things)) + // slices.SortFunc(things, func(a, b *GoDeps) bool { + // return a.Hash < b.Hash // Sort by ??. let the compiler work it out?? + // }) + return newGoDepIterator(things) +} +func (x *GoDeps) SortByGoPath() *GoDepIterator { + // copy the pointers as fast as possible. + things := x.selectAllGoDeps() + + // todo: try slices.SortFunc() instead to see what happens + sort.Sort(GoDepGoPath(things)) + // slices.SortFunc(things, func(a, b *GoDeps) bool { + // return a.GoPath < b.GoPath // Sort by ??. let the compiler work it out?? + // }) + return newGoDepIterator(things) +} + +// END SORT + +func (x *GoDeps) Len() int { + goDepMu.RLock() + defer goDepMu.RUnlock() + + return len(x.GoDeps) +} + +// just a simple Append() shortcut (but still uses the mutex lock) +func (x *GoDeps) Append(y *GoDep) { + goDepMu.Lock() + defer goDepMu.Unlock() + + x.GoDeps = append(x.GoDeps, y) +} + +func (x *GoDeps) All() *GoDepIterator { + GoDepPointers := x.selectAllGoDeps() + + iterator := newGoDepIterator(GoDepPointers) + return iterator +} + +func (x *GoDeps) Delete(y *GoDep) bool { + goDepMu.Lock() + defer goDepMu.Unlock() + + for i, _ := range x.GoDeps { + if x.GoDeps[i] == y { + x.GoDeps[i] = x.GoDeps[len(x.GoDeps)-1] + x.GoDeps = x.GoDeps[:len(x.GoDeps)-1] + return true + } + } + return false +} + +// lookup a GoDeps by the Hash +func (x *GoDeps) FindByHash(s string) *GoDep { + if x == nil { + return nil + } + + goDepMu.RLock() + defer goDepMu.RUnlock() + + for i, _ := range x.GoDeps { + if x.GoDeps[i].Hash == s { + return x.GoDeps[i] + } + } + return nil +} + +// lookup a GoDeps by the GoPath +func (x *GoDeps) FindByGoPath(s string) *GoDep { + if x == nil { + return nil + } + + goDepMu.RLock() + defer goDepMu.RUnlock() + + for i, _ := range x.GoDeps { + if x.GoDeps[i].GoPath == s { + return x.GoDeps[i] + } + } + return nil +} + +func (x *GoDeps) DeleteByHash(s string) bool { + goDepMu.Lock() + defer goDepMu.Unlock() + + for i, _ := range x.GoDeps { + if x.GoDeps[i].Hash == s { + x.GoDeps[i] = x.GoDeps[len(x.GoDeps)-1] + x.GoDeps = x.GoDeps[:len(x.GoDeps)-1] + return true + } + } + return false +} + +func (x *GoDeps) DeleteByGoPath(s string) bool { + goDepMu.Lock() + defer goDepMu.Unlock() + + for i, _ := range x.GoDeps { + if x.GoDeps[i].GoPath == s { + x.GoDeps[i] = x.GoDeps[len(x.GoDeps)-1] + x.GoDeps = x.GoDeps[:len(x.GoDeps)-1] + return true + } + } + return false +} + +func (x *GoDeps) AppendByHash(y *GoDep) bool { + goDepMu.Lock() + defer goDepMu.Unlock() + + for _, p := range x.GoDeps { + if p.Hash == y.Hash { + return false + } + } + + x.GoDeps = append(x.GoDeps, y) + return true +} + +func (x *GoDeps) AppendByGoPath(y *GoDep) bool { + goDepMu.Lock() + defer goDepMu.Unlock() + + for _, p := range x.GoDeps { + if p.GoPath == y.GoPath { + return false + } + } + + x.GoDeps = append(x.GoDeps, y) + return true +}