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.

71 lines
1.2 KiB

  1. package bptree
  2. import (
  3. "bytes"
  4. "hash/fnv"
  5. )
  6. type String string
  7. type ByteSlice []byte
  8. func (self *String) MarshalBinary() ([]byte, error) {
  9. return []byte(*self), nil
  10. }
  11. func (self *String) UnmarshalBinary(data []byte) error {
  12. *self = String(data)
  13. return nil
  14. }
  15. func (self String) Equals(other Equatable) bool {
  16. if o, ok := other.(String); ok {
  17. return self == o
  18. } else {
  19. return false
  20. }
  21. }
  22. func (self String) Less(other Sortable) bool {
  23. if o, ok := other.(String); ok {
  24. return self < o
  25. } else {
  26. return false
  27. }
  28. }
  29. func (self String) Hash() int {
  30. h := fnv.New32a()
  31. h.Write([]byte(string(self)))
  32. return int(h.Sum32())
  33. }
  34. func (self *ByteSlice) MarshalBinary() ([]byte, error) {
  35. return []byte(*self), nil
  36. }
  37. func (self *ByteSlice) UnmarshalBinary(data []byte) error {
  38. *self = ByteSlice(data)
  39. return nil
  40. }
  41. func (self ByteSlice) Equals(other Equatable) bool {
  42. if o, ok := other.(ByteSlice); ok {
  43. return bytes.Equal(self, o)
  44. } else {
  45. return false
  46. }
  47. }
  48. func (self ByteSlice) Less(other Sortable) bool {
  49. if o, ok := other.(ByteSlice); ok {
  50. return bytes.Compare(self, o) < 0 // -1 if a < b
  51. } else {
  52. return false
  53. }
  54. }
  55. func (self ByteSlice) Hash() int {
  56. h := fnv.New32a()
  57. h.Write([]byte(self))
  58. return int(h.Sum32())
  59. }