-
Notifications
You must be signed in to change notification settings - Fork 2
/
int_tuple.go
61 lines (49 loc) · 2.03 KB
/
int_tuple.go
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
// -----------------------------------------------------------------------------
// ZR Library zr/[int_tuple.go]
// (c) balarabe@protonmail.com License: MIT
// -----------------------------------------------------------------------------
package zr
// # Types
// IntTuple struct{ A, B int }
// IntTuples []IntTuple
//
// # sort.Interface
// (ob IntTuples) Len() int
// (ob IntTuples) Less(i, j int) bool
// (ob IntTuples) Swap(i, j int)
//
// # fmt.Stringer Interface
// (ob IntTuple) String() string
import (
"fmt"
)
// IntTuple holds a tuple (pair) of integer values.
type IntTuple struct{ A, B int }
// IntTuples holds multiple tuples (pairs) of integer values.
// It implements the sort.Inteface to make sorting easy.
type IntTuples []IntTuple
// -----------------------------------------------------------------------------
// # sort.Interface
// Len is the number of elements in the collection. (sort.Interface)
func (ob IntTuples) Len() int {
return len(ob)
} // Len
// Less reports whether the element with index
// 'i' should sort before element[j].
// (sort.Interface)
func (ob IntTuples) Less(i, j int) bool {
ti, tj := ob[i], ob[j]
return ti.A < tj.A || (ti.A == tj.A && ti.B < tj.B)
} // Less
// Swap swaps the elements with indexes i and j. (sort.Interface)
func (ob IntTuples) Swap(i, j int) {
ob[i], ob[j] = ob[j], ob[i]
} // Swap
// -----------------------------------------------------------------------------
// # fmt.Stringer Interface
// String returns a string representation of the IntTuple structure
// and implements the fmt.Stringer interface.
func (ob IntTuple) String() string {
return fmt.Sprintf("IntTuple{A:%d, B:%d}", ob.A, ob.B)
} // String
// end