summaryrefslogtreecommitdiff
path: root/patchset.HAMDMADE
blob: 62741dc83d54e89bee476b38899f79157d862b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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