aboutsummaryrefslogtreecommitdiff
path: root/commitquery/node_generation.go
blob: 03283cf68bcb276b63b4df0363c4082f018ae3e6 (about) (plain) (blame)
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
package commitquery

import (
	"math"

	objectid "codeberg.org/lindenii/furgit/object/id"
)

// effectiveGeneration returns one node's generation value.
func (query *query) effectiveGeneration(idx nodeIndex) uint64 {
	if !query.nodes[idx].hasGeneration {
		return generationInfinity
	}

	return query.nodes[idx].generation
}

// generationInfinity sorts nodes without a known generation last.
const (
	generationInfinity = uint64(math.MaxUint64)
)

// compareByGeneration builds one comparator ordered by generation first.
func (query *query) compareByGeneration() func(nodeIndex, nodeIndex) int {
	return func(left, right nodeIndex) int {
		leftGeneration := query.effectiveGeneration(left)
		rightGeneration := query.effectiveGeneration(right)

		switch {
		case leftGeneration < rightGeneration:
			return -1
		case leftGeneration > rightGeneration:
			return 1
		}

		switch {
		case query.nodes[left].commitTime < query.nodes[right].commitTime:
			return -1
		case query.nodes[left].commitTime > query.nodes[right].commitTime:
			return 1
		}

		return objectid.Compare(query.nodes[left].id, query.nodes[right].id)
	}
}