You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.4 KiB

9 years ago
9 years ago
  1. package common
  2. import (
  3. "encoding/binary"
  4. "sort"
  5. )
  6. // Sort for []uint64
  7. type Uint64Slice []uint64
  8. func (p Uint64Slice) Len() int { return len(p) }
  9. func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] }
  10. func (p Uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  11. func (p Uint64Slice) Sort() { sort.Sort(p) }
  12. func SearchUint64s(a []uint64, x uint64) int {
  13. return sort.Search(len(a), func(i int) bool { return a[i] >= x })
  14. }
  15. func (p Uint64Slice) Search(x uint64) int { return SearchUint64s(p, x) }
  16. //--------------------------------------------------------------------------------
  17. func PutUint64LE(dest []byte, i uint64) {
  18. binary.LittleEndian.PutUint64(dest, i)
  19. }
  20. func GetUint64LE(src []byte) uint64 {
  21. return binary.LittleEndian.Uint64(src)
  22. }
  23. func PutUint64BE(dest []byte, i uint64) {
  24. binary.BigEndian.PutUint64(dest, i)
  25. }
  26. func GetUint64BE(src []byte) uint64 {
  27. return binary.BigEndian.Uint64(src)
  28. }
  29. func PutInt64LE(dest []byte, i int64) {
  30. binary.LittleEndian.PutUint64(dest, uint64(i))
  31. }
  32. func GetInt64LE(src []byte) int64 {
  33. return int64(binary.LittleEndian.Uint64(src))
  34. }
  35. func PutInt64BE(dest []byte, i int64) {
  36. binary.BigEndian.PutUint64(dest, uint64(i))
  37. }
  38. func GetInt64BE(src []byte) int64 {
  39. return int64(binary.BigEndian.Uint64(src))
  40. }
  41. // IntInSlice returns true if a is found in the list.
  42. func IntInSlice(a int, list []int) bool {
  43. for _, b := range list {
  44. if b == a {
  45. return true
  46. }
  47. }
  48. return false
  49. }