OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / tmlibs / common / int.go
1 package common
2
3 import (
4         "encoding/binary"
5         "sort"
6 )
7
8 // Sort for []uint64
9
10 type Uint64Slice []uint64
11
12 func (p Uint64Slice) Len() int           { return len(p) }
13 func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] }
14 func (p Uint64Slice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
15 func (p Uint64Slice) Sort()              { sort.Sort(p) }
16
17 func SearchUint64s(a []uint64, x uint64) int {
18         return sort.Search(len(a), func(i int) bool { return a[i] >= x })
19 }
20
21 func (p Uint64Slice) Search(x uint64) int { return SearchUint64s(p, x) }
22
23 //--------------------------------------------------------------------------------
24
25 func PutUint64LE(dest []byte, i uint64) {
26         binary.LittleEndian.PutUint64(dest, i)
27 }
28
29 func GetUint64LE(src []byte) uint64 {
30         return binary.LittleEndian.Uint64(src)
31 }
32
33 func PutUint64BE(dest []byte, i uint64) {
34         binary.BigEndian.PutUint64(dest, i)
35 }
36
37 func GetUint64BE(src []byte) uint64 {
38         return binary.BigEndian.Uint64(src)
39 }
40
41 func PutInt64LE(dest []byte, i int64) {
42         binary.LittleEndian.PutUint64(dest, uint64(i))
43 }
44
45 func GetInt64LE(src []byte) int64 {
46         return int64(binary.LittleEndian.Uint64(src))
47 }
48
49 func PutInt64BE(dest []byte, i int64) {
50         binary.BigEndian.PutUint64(dest, uint64(i))
51 }
52
53 func GetInt64BE(src []byte) int64 {
54         return int64(binary.BigEndian.Uint64(src))
55 }