diff options
| author | Jeff Carr <[email protected]> | 2025-01-11 05:53:52 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-01-11 05:53:52 -0600 |
| commit | 95afc5e0b47cd4788ade944f87e3452c5d4991c7 (patch) | |
| tree | ef051c7456ab02ed4ec50511b8162f4802c790e1 /patchset.HAMDMADE | |
| parent | fed674d04a07207d3bceecae43ddcf3c183d4c71 (diff) | |
add a new iterator generate file
Diffstat (limited to 'patchset.HAMDMADE')
| -rw-r--r-- | patchset.HAMDMADE | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/patchset.HAMDMADE b/patchset.HAMDMADE new file mode 100644 index 0000000..62741dc --- /dev/null +++ b/patchset.HAMDMADE @@ -0,0 +1,63 @@ +// DEFINE THE ITERATOR. Only one per Patch message + +// NewPatchsetIterator initializes a new iterator. +func NewPatchIterator(things []*Patch) *PatchIterator { + return &PatchIterator{things: things} +} + +// safely returns a slice of pointers to the Patchset protobufs +func (x *Patchset) all() []*Patch { + x.Lock.RLock() + defer x.Lock.RUnlock() + + // Create a new slice to hold pointers to each Patchset + var tmp []*Patch + tmp = make([]*Patch, len(x.Patches)) + for i, p := range x.Patches { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +type PatchIterator struct { + sync.RWMutex + + things []*Patch + index int +} + +func (it *PatchIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *PatchIterator) Next() *Patch { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in PatchIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// START sort by Filename (this is all you need once the Iterator is defined) +type PatchFilename []*Patch + +func (a PatchFilename) Len() int { return len(a) } +func (a PatchFilename) Less(i, j int) bool { return a[i].Filename < a[j].Filename } +func (a PatchFilename) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +func (x *Patchset) SortByFilename() *PatchIterator { + things := x.all() + + sort.Sort(PatchFilename(things)) + + iterator := NewPatchIterator(things) + return iterator +} +// END sort by Filename |
