aboutsummaryrefslogtreecommitdiffstats
path: root/difftree/internal/radixmerkle/node.go
blob: 99be5b87ee442b19bd5ea2e4ee7340d877789846 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package merkletrie

import (
	"sort"
	"strings"
)

// A node is a Noder implementation for testing purposes:  It is easier
// to create test trees using nodes than using real git tree objects.
type node struct {
	hash     []byte
	key      string
	children []*node
}

// newNode returns a new Node with the given hash, key and children
// (children can be specified in any order).
func newNode(hash []byte, key string, children []*node) *node {
	sort.Sort(reverseAlphabeticallyByKey(children))

	return &node{
		hash:     hash,
		key:      key,
		children: children,
	}
}

// Hash returns the hash of the node.
func (n *node) Hash() []byte {
	return n.hash
}

// Key returns the key of the node.
func (n *node) Key() string {
	return n.key
}

// NumChildren returns the number of children.
func (n *node) NumChildren() int {
	return len(n.children)
}

// Children returns the node's children in reverse key alphabetical
// order.
func (n *node) Children() []Noder {
	ret := make([]Noder, n.NumChildren())
	for i := range n.children {
		ret[i] = n.children[i]
	}
	return ret
}

type reverseAlphabeticallyByKey []*node

func (a reverseAlphabeticallyByKey) Len() int {
	return len(a)
}

func (a reverseAlphabeticallyByKey) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func (a reverseAlphabeticallyByKey) Less(i, j int) bool {
	return strings.Compare(a[i].key, a[j].key) > 0
}