// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. // This file was autogenerated with autogenpb v0.0.75 2025-08-18_04:51:43_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 main import ( "fmt" "iter" "sort" "sync" ) // a simple global lock var blockMu sync.RWMutex func (x *Blocks) fixUuid() { if x == nil { return } if x.Uuid == "abe10848-cf2b-4440-b5a8-0aaa2ce50aad" { return } x.Uuid = "abe10848-cf2b-4440-b5a8-0aaa2ce50aad" x.Version = "v0.0.1 go.wit.com/apps/utils/fixup" } func NewBlocks() *Blocks { x := new(Blocks) x.Uuid = "abe10848-cf2b-4440-b5a8-0aaa2ce50aad" x.Version = "v0.0.1 go.wit.com/apps/utils/fixup" return x } // START SORT // DEFINE THE Blocks SCANNER. // itializes a new scanner. func newBlocksScanner(things []*Blocks) *BlocksScanner { return &BlocksScanner{things: things} } type BlocksScanner struct { sync.Mutex things []*Blocks index int } func (it *BlocksScanner) Scan() bool { if it.index >= len(it.things) { return false } it.Lock() it.index++ it.Unlock() return true } // Next() returns the next thing in the array func (it *BlocksScanner) Next() *Blocks { if it.things[it.index-1] == nil { fmt.Println("Next() error in BlocksScanner", it.index) } return it.things[it.index-1] } // END DEFINE THE SCANNER // DEFINE THE Block SCANNER. // itializes a new scanner. func newBlockScanner(things []*Block) *BlockScanner { return &BlockScanner{things: things} } type BlockScanner struct { sync.Mutex things []*Block index int } func (it *BlockScanner) Scan() bool { if it.index >= len(it.things) { return false } it.Lock() it.index++ it.Unlock() return true } // Next() returns the next thing in the array func (it *BlockScanner) Next() *Block { if it.things[it.index-1] == nil { fmt.Println("Next() error in BlockScanner", it.index) } return it.things[it.index-1] } // END DEFINE THE SCANNER // sort struct by Name type sortBlockName []*Block func (a sortBlockName) Len() int { return len(a) } func (a sortBlockName) Less(i, j int) bool { return a[i].Name < a[j].Name } func (a sortBlockName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } // safely returns a slice of pointers to the FRUIT protobufs func (x *Blocks) allBlocks() []*Block { x.RLock() defer x.RUnlock() // Create a new slice to hold pointers to each FRUIT var tmp []*Block tmp = make([]*Block, len(x.Blocks)) for i, p := range x.Blocks { tmp[i] = p // Copy pointers for safe iteration } return tmp } // safely returns a slice of pointers to the Block protobufs func (x *Blocks) selectAllBlocks() []*Block { x.RLock() defer x.RUnlock() // Create a new slice to hold pointers to each Block var tmp []*Block tmp = make([]*Block, len(x.Blocks)) for i, p := range x.Blocks { tmp[i] = p // Copy pointers for safe iteration } return tmp } func (x *Blocks) SortByName() *BlockScanner { // copy the pointers as fast as possible. things := x.selectAllBlocks() // todo: try slices.SortFunc() instead to see what happens sort.Sort(sortBlockName(things)) // slices.SortFunc(things, func(a, b *Blocks) bool { // return a.Name < b.Name // Sort by ??. let the compiler work it out?? // }) return newBlockScanner(things) } // 'for x := range' syntax using the awesome golang 1.24 'iter' func (x *Blocks) IterByName() iter.Seq[*Block] { items := x.selectAllBlocks() sort.Sort(sortBlockName(items)) // log.Println("Made Iter.Seq[] with length", len(items)) return func(yield func(*Block) bool) { for _, v := range items { if !yield(v) { return } } } } // END SORT func (x *Blocks) Len() int { x.RLock() defer x.RUnlock() return len(x.Blocks) } // just a simple Append() shortcut (but still uses the mutex lock) func (x *Blocks) Append(y *Block) { x.Lock() defer x.Unlock() x.Blocks = append(x.Blocks, y) } func (x *Blocks) All() *BlockScanner { BlockPointers := x.selectAllBlocks() scanner := newBlockScanner(BlockPointers) return scanner } // Iterate 'for x := range' syntax using the awesome golang 1.24 'iter' func (x *Blocks) IterAll() iter.Seq[*Block] { items := x.selectAllBlocks() // log.Println("Made All() Iter.Seq[] with length", len(items)) return func(yield func(*Block) bool) { for _, v := range items { if !yield(v) { return } } } } func (x *Blocks) Delete(y *Block) bool { x.Lock() defer x.Unlock() for i, _ := range x.Blocks { if x.Blocks[i] == y { x.Blocks[i] = x.Blocks[len(x.Blocks)-1] x.Blocks = x.Blocks[:len(x.Blocks)-1] return true } } return false } // lookup a Blocks by the Name func (x *Blocks) FindByName(s string) *Block { if x == nil { return nil } x.RLock() defer x.RUnlock() for i, _ := range x.Blocks { if x.Blocks[i].Name == s { return x.Blocks[i] } } return nil } // returns a Block if Name matches, otherwise create func (x *Blocks) InsertByName(y string) *Block { x.Lock() defer x.Unlock() for _, z := range x.Blocks { if z.Name == y { return z } } z := new(Block) z.Name = y x.Blocks = append(x.Blocks, z) return z } func (x *Blocks) DeleteByName(s string) bool { x.Lock() defer x.Unlock() for i, _ := range x.Blocks { if x.Blocks[i].Name == s { x.Blocks[i] = x.Blocks[len(x.Blocks)-1] x.Blocks = x.Blocks[:len(x.Blocks)-1] return true } } return false } func (x *Blocks) AppendByName(y *Block) bool { x.Lock() defer x.Unlock() for _, p := range x.Blocks { if p.Name == y.Name { return false } } x.Blocks = append(x.Blocks, y) return true }