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.

357 lines
6.4 KiB

  1. package bptree
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. )
  6. type Int8 int8
  7. type UInt8 uint8
  8. type Int16 int16
  9. type UInt16 uint16
  10. type Int32 int32
  11. type UInt32 uint32
  12. type Int64 int64
  13. type UInt64 uint64
  14. type Int int
  15. type UInt uint
  16. func (self *Int8) MarshalBinary() ([]byte, error) {
  17. bytes := make([]byte, 0)
  18. bytes[0] = uint8(*self)
  19. return bytes, nil
  20. }
  21. func (self *Int8) UnmarshalBinary(data []byte) error {
  22. if len(data) != 1 {
  23. return fmt.Errorf("data wrong size")
  24. }
  25. *self = Int8(data[0])
  26. return nil
  27. }
  28. func (self Int8) Equals(other Equatable) bool {
  29. if o, ok := other.(Int8); ok {
  30. return self == o
  31. } else {
  32. return false
  33. }
  34. }
  35. func (self Int8) Less(other Sortable) bool {
  36. if o, ok := other.(Int8); ok {
  37. return self < o
  38. } else {
  39. return false
  40. }
  41. }
  42. func (self Int8) Hash() int {
  43. return int(self)
  44. }
  45. func (self *UInt8) MarshalBinary() ([]byte, error) {
  46. bytes := make([]byte, 0)
  47. bytes[0] = uint8(*self)
  48. return bytes, nil
  49. }
  50. func (self *UInt8) UnmarshalBinary(data []byte) error {
  51. if len(data) != 1 {
  52. return fmt.Errorf("data wrong size")
  53. }
  54. *self = UInt8(data[0])
  55. return nil
  56. }
  57. func (self UInt8) Equals(other Equatable) bool {
  58. if o, ok := other.(UInt8); ok {
  59. return self == o
  60. } else {
  61. return false
  62. }
  63. }
  64. func (self UInt8) Less(other Sortable) bool {
  65. if o, ok := other.(UInt8); ok {
  66. return self < o
  67. } else {
  68. return false
  69. }
  70. }
  71. func (self UInt8) Hash() int {
  72. return int(self)
  73. }
  74. func (self *Int16) MarshalBinary() ([]byte, error) {
  75. bytes := make([]byte, 2)
  76. binary.BigEndian.PutUint16(bytes, uint16(*self))
  77. return bytes, nil
  78. }
  79. func (self *Int16) UnmarshalBinary(data []byte) error {
  80. if len(data) != 2 {
  81. return fmt.Errorf("data wrong size")
  82. }
  83. *self = Int16(binary.BigEndian.Uint16(data))
  84. return nil
  85. }
  86. func (self Int16) Equals(other Equatable) bool {
  87. if o, ok := other.(Int16); ok {
  88. return self == o
  89. } else {
  90. return false
  91. }
  92. }
  93. func (self Int16) Less(other Sortable) bool {
  94. if o, ok := other.(Int16); ok {
  95. return self < o
  96. } else {
  97. return false
  98. }
  99. }
  100. func (self Int16) Hash() int {
  101. return int(self)
  102. }
  103. func (self *UInt16) MarshalBinary() ([]byte, error) {
  104. bytes := make([]byte, 2)
  105. binary.BigEndian.PutUint16(bytes, uint16(*self))
  106. return bytes, nil
  107. }
  108. func (self *UInt16) UnmarshalBinary(data []byte) error {
  109. if len(data) != 2 {
  110. return fmt.Errorf("data wrong size")
  111. }
  112. *self = UInt16(binary.BigEndian.Uint16(data))
  113. return nil
  114. }
  115. func (self UInt16) Equals(other Equatable) bool {
  116. if o, ok := other.(UInt16); ok {
  117. return self == o
  118. } else {
  119. return false
  120. }
  121. }
  122. func (self UInt16) Less(other Sortable) bool {
  123. if o, ok := other.(UInt16); ok {
  124. return self < o
  125. } else {
  126. return false
  127. }
  128. }
  129. func (self UInt16) Hash() int {
  130. return int(self)
  131. }
  132. func (self *Int32) MarshalBinary() ([]byte, error) {
  133. bytes := make([]byte, 4)
  134. binary.BigEndian.PutUint32(bytes, uint32(*self))
  135. return bytes, nil
  136. }
  137. func (self *Int32) UnmarshalBinary(data []byte) error {
  138. if len(data) != 4 {
  139. return fmt.Errorf("data wrong size")
  140. }
  141. *self = Int32(binary.BigEndian.Uint32(data))
  142. return nil
  143. }
  144. func (self Int32) Equals(other Equatable) bool {
  145. if o, ok := other.(Int32); ok {
  146. return self == o
  147. } else {
  148. return false
  149. }
  150. }
  151. func (self Int32) Less(other Sortable) bool {
  152. if o, ok := other.(Int32); ok {
  153. return self < o
  154. } else {
  155. return false
  156. }
  157. }
  158. func (self *UInt32) MarshalBinary() ([]byte, error) {
  159. bytes := make([]byte, 4)
  160. binary.BigEndian.PutUint32(bytes, uint32(*self))
  161. return bytes, nil
  162. }
  163. func (self *UInt32) UnmarshalBinary(data []byte) error {
  164. if len(data) != 4 {
  165. return fmt.Errorf("data wrong size")
  166. }
  167. *self = UInt32(binary.BigEndian.Uint32(data))
  168. return nil
  169. }
  170. func (self Int32) Hash() int {
  171. return int(self)
  172. }
  173. func (self UInt32) Equals(other Equatable) bool {
  174. if o, ok := other.(UInt32); ok {
  175. return self == o
  176. } else {
  177. return false
  178. }
  179. }
  180. func (self UInt32) Less(other Sortable) bool {
  181. if o, ok := other.(UInt32); ok {
  182. return self < o
  183. } else {
  184. return false
  185. }
  186. }
  187. func (self UInt32) Hash() int {
  188. return int(self)
  189. }
  190. func (self *Int64) MarshalBinary() ([]byte, error) {
  191. bytes := make([]byte, 8)
  192. binary.BigEndian.PutUint64(bytes, uint64(*self))
  193. return bytes, nil
  194. }
  195. func (self *Int64) UnmarshalBinary(data []byte) error {
  196. if len(data) != 8 {
  197. return fmt.Errorf("data wrong size")
  198. }
  199. *self = Int64(binary.BigEndian.Uint64(data))
  200. return nil
  201. }
  202. func (self Int64) Equals(other Equatable) bool {
  203. if o, ok := other.(Int64); ok {
  204. return self == o
  205. } else {
  206. return false
  207. }
  208. }
  209. func (self Int64) Less(other Sortable) bool {
  210. if o, ok := other.(Int64); ok {
  211. return self < o
  212. } else {
  213. return false
  214. }
  215. }
  216. func (self Int64) Hash() int {
  217. return int(self>>32) ^ int(self)
  218. }
  219. func (self *UInt64) MarshalBinary() ([]byte, error) {
  220. bytes := make([]byte, 8)
  221. binary.BigEndian.PutUint64(bytes, uint64(*self))
  222. return bytes, nil
  223. }
  224. func (self *UInt64) UnmarshalBinary(data []byte) error {
  225. if len(data) != 8 {
  226. return fmt.Errorf("data wrong size")
  227. }
  228. *self = UInt64(binary.BigEndian.Uint64(data))
  229. return nil
  230. }
  231. func (self UInt64) Equals(other Equatable) bool {
  232. if o, ok := other.(UInt64); ok {
  233. return self == o
  234. } else {
  235. return false
  236. }
  237. }
  238. func (self UInt64) Less(other Sortable) bool {
  239. if o, ok := other.(UInt64); ok {
  240. return self < o
  241. } else {
  242. return false
  243. }
  244. }
  245. func (self UInt64) Hash() int {
  246. return int(self>>32) ^ int(self)
  247. }
  248. func (self *Int) MarshalBinary() ([]byte, error) {
  249. bytes := make([]byte, 4)
  250. binary.BigEndian.PutUint32(bytes, uint32(*self))
  251. return bytes, nil
  252. }
  253. func (self *Int) UnmarshalBinary(data []byte) error {
  254. if len(data) != 4 {
  255. return fmt.Errorf("data wrong size")
  256. }
  257. *self = Int(binary.BigEndian.Uint32(data))
  258. return nil
  259. }
  260. func (self Int) Equals(other Equatable) bool {
  261. if o, ok := other.(Int); ok {
  262. return self == o
  263. } else {
  264. return false
  265. }
  266. }
  267. func (self Int) Less(other Sortable) bool {
  268. if o, ok := other.(Int); ok {
  269. return self < o
  270. } else {
  271. return false
  272. }
  273. }
  274. func (self Int) Hash() int {
  275. return int(self)
  276. }
  277. func (self *UInt) MarshalBinary() ([]byte, error) {
  278. bytes := make([]byte, 4)
  279. binary.BigEndian.PutUint32(bytes, uint32(*self))
  280. return bytes, nil
  281. }
  282. func (self *UInt) UnmarshalBinary(data []byte) error {
  283. if len(data) != 4 {
  284. return fmt.Errorf("data wrong size")
  285. }
  286. *self = UInt(binary.BigEndian.Uint32(data))
  287. return nil
  288. }
  289. func (self UInt) Equals(other Equatable) bool {
  290. if o, ok := other.(UInt); ok {
  291. return self == o
  292. } else {
  293. return false
  294. }
  295. }
  296. func (self UInt) Less(other Sortable) bool {
  297. if o, ok := other.(UInt); ok {
  298. return self < o
  299. } else {
  300. return false
  301. }
  302. }
  303. func (self UInt) Hash() int {
  304. return int(self)
  305. }