From 6c2b6bfb156e98c54dbba7def180cd09d51c5eae Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 9 Feb 2025 15:19:55 -0600 Subject: [PATCH] Notes added by 'git notes append' --- bc55a8b33b15a6a905835790ce818c28d10ebdb5 | 293 +++++++++++++++++++++++ 1 file changed, 293 insertions(+) diff --git a/bc55a8b33b15a6a905835790ce818c28d10ebdb5 b/bc55a8b33b15a6a905835790ce818c28d10ebdb5 index dfdf122..4e349ff 100644 --- a/bc55a8b33b15a6a905835790ce818c28d10ebdb5 +++ b/bc55a8b33b15a6a905835790ce818c28d10ebdb5 @@ -944,3 +944,296 @@ func file_widget_proto_init() { } // `autogen:widget.sort.pb.go` + +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// This file was autogenerated with autogenpb v0.0.50 2025-02-07_18:46:58_UTC +// 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 tree + +import ( + "fmt" + "sync" +) + +// a simple global lock +var widgetMu sync.RWMutex + +func (x *Widgets) fixUuid() { + if x == nil { + return + } + if x.Uuid == "0331fcd7-3c8c-43e4-be1b-77db6a6bc58c" { + return + } + x.Uuid = "0331fcd7-3c8c-43e4-be1b-77db6a6bc58c" + x.Version = "v1 " +} + +func NewWidgets() *Widgets { + x := new(Widgets) + x.Uuid = "0331fcd7-3c8c-43e4-be1b-77db6a6bc58c" + x.Version = "v1 " + return x +} + +// START SORT + +// DEFINE THE Widgets ITERATOR. +// itializes a new iterator. +func newWidgetsIterator(things []*Widgets) *WidgetsIterator { + return &WidgetsIterator{things: things} +} + +type WidgetsIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Widgets + index int +} + +func (it *WidgetsIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *WidgetsIterator) Next() *Widgets { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in WidgetsIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Widget ITERATOR. +// itializes a new iterator. +func newWidgetIterator(things []*Widget) *WidgetIterator { + return &WidgetIterator{things: things} +} + +type WidgetIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Widget + index int +} + +func (it *WidgetIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *WidgetIterator) Next() *Widget { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in WidgetIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Size ITERATOR. +// itializes a new iterator. +func newSizeIterator(things []*Size) *SizeIterator { + return &SizeIterator{things: things} +} + +type SizeIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Size + index int +} + +func (it *SizeIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *SizeIterator) Next() *Size { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in SizeIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Location ITERATOR. +// itializes a new iterator. +func newLocationIterator(things []*Location) *LocationIterator { + return &LocationIterator{things: things} +} + +type LocationIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Location + index int +} + +func (it *LocationIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *LocationIterator) Next() *Location { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in LocationIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Tree ITERATOR. +// itializes a new iterator. +func newTreeIterator(things []*Tree) *TreeIterator { + return &TreeIterator{things: things} +} + +type TreeIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Tree + index int +} + +func (it *TreeIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *TreeIterator) Next() *Tree { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in TreeIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// safely returns a slice of pointers to the FRUIT protobufs +func (x *Widgets) allWidgets() []*Widget { + widgetMu.RLock() + defer widgetMu.RUnlock() + + // Create a new slice to hold pointers to each FRUIT + var tmp []*Widget + tmp = make([]*Widget, len(x.Widgets)) + for i, p := range x.Widgets { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the FRUIT protobufs +func (x *Tree) allChildren() []*Widget { + widgetMu.RLock() + defer widgetMu.RUnlock() + + // Create a new slice to hold pointers to each FRUIT + var tmp []*Widget + tmp = make([]*Widget, len(x.Children)) + for i, p := range x.Children { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the Widget protobufs +func (x *Widgets) selectAllWidgets() []*Widget { + widgetMu.RLock() + defer widgetMu.RUnlock() + + // Create a new slice to hold pointers to each Widget + var tmp []*Widget + tmp = make([]*Widget, len(x.Widgets)) + for i, p := range x.Widgets { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the Widget protobufs +func (x *Tree) selectAllChildren() []*Widget { + widgetMu.RLock() + defer widgetMu.RUnlock() + + // Create a new slice to hold pointers to each Widget + var tmp []*Widget + tmp = make([]*Widget, len(x.Children)) + for i, p := range x.Children { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// END SORT + +func (x *Widgets) Len() int { + widgetMu.RLock() + defer widgetMu.RUnlock() + + return len(x.Widgets) +} + +// just a simple Append() shortcut (but still uses the mutex lock) +func (x *Widgets) Append(y *Widget) { + widgetMu.Lock() + defer widgetMu.Unlock() + + x.Widgets = append(x.Widgets, y) +} + +func (x *Widgets) All() *WidgetIterator { + WidgetPointers := x.selectAllWidgets() + + iterator := newWidgetIterator(WidgetPointers) + return iterator +} + +func (x *Widgets) Delete(y *Widget) bool { + widgetMu.Lock() + defer widgetMu.Unlock() + + for i, _ := range x.Widgets { + if x.Widgets[i] == y { + x.Widgets[i] = x.Widgets[len(x.Widgets)-1] + x.Widgets = x.Widgets[:len(x.Widgets)-1] + return true + } + } + return false +}