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.

67 lines
1.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package common
  2. import (
  3. "bytes"
  4. "sort"
  5. )
  6. //----------------------------------------
  7. // KVPair
  8. /*
  9. Defined in types.proto
  10. type KVPair struct {
  11. Key []byte
  12. Value []byte
  13. }
  14. */
  15. type KVPairs []KVPair
  16. // Sorting
  17. func (kvs KVPairs) Len() int { return len(kvs) }
  18. func (kvs KVPairs) Less(i, j int) bool {
  19. switch bytes.Compare(kvs[i].Key, kvs[j].Key) {
  20. case -1:
  21. return true
  22. case 0:
  23. return bytes.Compare(kvs[i].Value, kvs[j].Value) < 0
  24. case 1:
  25. return false
  26. default:
  27. panic("invalid comparison result")
  28. }
  29. }
  30. func (kvs KVPairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
  31. func (kvs KVPairs) Sort() { sort.Sort(kvs) }
  32. //----------------------------------------
  33. // KI64Pair
  34. /*
  35. Defined in types.proto
  36. type KI64Pair struct {
  37. Key []byte
  38. Value int64
  39. }
  40. */
  41. type KI64Pairs []KI64Pair
  42. // Sorting
  43. func (kvs KI64Pairs) Len() int { return len(kvs) }
  44. func (kvs KI64Pairs) Less(i, j int) bool {
  45. switch bytes.Compare(kvs[i].Key, kvs[j].Key) {
  46. case -1:
  47. return true
  48. case 0:
  49. return kvs[i].Value < kvs[j].Value
  50. case 1:
  51. return false
  52. default:
  53. panic("invalid comparison result")
  54. }
  55. }
  56. func (kvs KI64Pairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
  57. func (kvs KI64Pairs) Sort() { sort.Sort(kvs) }