summaryrefslogtreecommitdiff
path: root/agreement/sort.go
blob: e9455336ccbd0249d67676eccbdaa3478a6aef78 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package agreement

import (
	"bytes"

	"github.com/algorand/go-algorand/data/basics"
)

// These types are defined to satisfy SortInterface used by

// SortAddress is re-exported from basics.Address since the interface is already defined there
//
//msgp:sort basics.Address SortAddress
type SortAddress = basics.SortAddress

// SortUint64 is re-exported from basics since the interface is already defined there
// canonical encoding of maps in msgpack format.
type SortUint64 = basics.SortUint64

// SortStep defines SortInterface used by msgp to consistently sort maps with this type as key.
//
//msgp:ignore SortStep
//msgp:sort step SortStep
type SortStep []step

func (a SortStep) Len() int           { return len(a) }
func (a SortStep) Less(i, j int) bool { return a[i] < a[j] }
func (a SortStep) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// SortPeriod defines SortInterface used by msgp to consistently sort maps with this type as key.
//
//msgp:ignore SortPeriod
//msgp:sort period SortPeriod
type SortPeriod []period

func (a SortPeriod) Len() int           { return len(a) }
func (a SortPeriod) Less(i, j int) bool { return a[i] < a[j] }
func (a SortPeriod) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// SortRound defines SortInterface used by msgp to consistently sort maps with this type as key.
// note, for type aliases the base type is used for the interface
//
//msgp:ignore SortRound
//msgp:sort basics.Round SortRound
type SortRound []basics.Round

func (a SortRound) Len() int           { return len(a) }
func (a SortRound) Less(i, j int) bool { return a[i] < a[j] }
func (a SortRound) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// SortProposalValue defines SortInterface used by msgp to consistently sort maps with this type as key.
//
//msgp:ignore SortProposalValue
//msgp:sort proposalValue SortProposalValue
type SortProposalValue []proposalValue

func (a SortProposalValue) Len() int { return len(a) }
func (a SortProposalValue) Less(i, j int) bool {
	if a[i].OriginalPeriod != a[j].OriginalPeriod {
		return a[i].OriginalPeriod < a[j].OriginalPeriod
	}
	cmp := bytes.Compare(a[i].OriginalProposer[:], a[j].OriginalProposer[:])
	if cmp != 0 {
		return cmp < 0
	}
	cmp = bytes.Compare(a[i].BlockDigest[:], a[j].BlockDigest[:])
	if cmp != 0 {
		return cmp < 0
	}
	cmp = bytes.Compare(a[i].EncodingDigest[:], a[j].EncodingDigest[:])
	return cmp < 0
}

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