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.

335 lines
6.0 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package merkle
  2. import (
  3. "encoding/binary"
  4. )
  5. type Byte byte
  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. // Byte
  17. func (self Byte) Equals(other Binary) bool {
  18. return self == other
  19. }
  20. func (self Byte) Less(other Key) bool {
  21. if o, ok := other.(Byte); ok {
  22. return self < o
  23. } else {
  24. panic("Cannot compare unequal types")
  25. }
  26. }
  27. func (self Byte) ByteSize() int {
  28. return 1
  29. }
  30. func (self Byte) SaveTo(b []byte) int {
  31. if cap(b) < 1 { panic("buf too small") }
  32. b[0] = byte(self)
  33. return 1
  34. }
  35. func LoadByte(bytes []byte) Byte {
  36. return Byte(bytes[0])
  37. }
  38. // Int8
  39. func (self Int8) Equals(other Binary) bool {
  40. return self == other
  41. }
  42. func (self Int8) Less(other Key) bool {
  43. if o, ok := other.(Int8); ok {
  44. return self < o
  45. } else {
  46. panic("Cannot compare unequal types")
  47. }
  48. }
  49. func (self Int8) ByteSize() int {
  50. return 1
  51. }
  52. func (self Int8) SaveTo(b []byte) int {
  53. if cap(b) < 1 { panic("buf too small") }
  54. b[0] = byte(self)
  55. return 1
  56. }
  57. func LoadInt8(bytes []byte) Int8 {
  58. return Int8(bytes[0])
  59. }
  60. // UInt8
  61. func (self UInt8) Equals(other Binary) bool {
  62. return self == other
  63. }
  64. func (self UInt8) Less(other Key) bool {
  65. if o, ok := other.(UInt8); ok {
  66. return self < o
  67. } else {
  68. panic("Cannot compare unequal types")
  69. }
  70. }
  71. func (self UInt8) ByteSize() int {
  72. return 1
  73. }
  74. func (self UInt8) SaveTo(b []byte) int {
  75. if cap(b) < 1 { panic("buf too small") }
  76. b[0] = byte(self)
  77. return 1
  78. }
  79. func LoadUInt8(bytes []byte) UInt8 {
  80. return UInt8(bytes[0])
  81. }
  82. // Int16
  83. func (self Int16) Equals(other Binary) bool {
  84. return self == other
  85. }
  86. func (self Int16) Less(other Key) bool {
  87. if o, ok := other.(Int16); ok {
  88. return self < o
  89. } else {
  90. panic("Cannot compare unequal types")
  91. }
  92. }
  93. func (self Int16) ByteSize() int {
  94. return 2
  95. }
  96. func (self Int16) SaveTo(b []byte) int {
  97. if cap(b) < 2 { panic("buf too small") }
  98. binary.LittleEndian.PutUint16(b, uint16(self))
  99. return 2
  100. }
  101. func LoadInt16(bytes []byte) Int16 {
  102. return Int16(binary.LittleEndian.Uint16(bytes))
  103. }
  104. // UInt16
  105. func (self UInt16) Equals(other Binary) bool {
  106. return self == other
  107. }
  108. func (self UInt16) Less(other Key) bool {
  109. if o, ok := other.(UInt16); ok {
  110. return self < o
  111. } else {
  112. panic("Cannot compare unequal types")
  113. }
  114. }
  115. func (self UInt16) ByteSize() int {
  116. return 2
  117. }
  118. func (self UInt16) SaveTo(b []byte) int {
  119. if cap(b) < 2 { panic("buf too small") }
  120. binary.LittleEndian.PutUint16(b, uint16(self))
  121. return 2
  122. }
  123. func LoadUInt16(bytes []byte) UInt16 {
  124. return UInt16(binary.LittleEndian.Uint16(bytes))
  125. }
  126. // Int32
  127. func (self Int32) Equals(other Binary) bool {
  128. return self == other
  129. }
  130. func (self Int32) Less(other Key) bool {
  131. if o, ok := other.(Int32); ok {
  132. return self < o
  133. } else {
  134. panic("Cannot compare unequal types")
  135. }
  136. }
  137. func (self Int32) ByteSize() int {
  138. return 4
  139. }
  140. func (self Int32) SaveTo(b []byte) int {
  141. if cap(b) < 4 { panic("buf too small") }
  142. binary.LittleEndian.PutUint32(b, uint32(self))
  143. return 4
  144. }
  145. func LoadInt32(bytes []byte) Int32 {
  146. return Int32(binary.LittleEndian.Uint32(bytes))
  147. }
  148. // UInt32
  149. func (self UInt32) Equals(other Binary) bool {
  150. return self == other
  151. }
  152. func (self UInt32) Less(other Key) bool {
  153. if o, ok := other.(UInt32); ok {
  154. return self < o
  155. } else {
  156. panic("Cannot compare unequal types")
  157. }
  158. }
  159. func (self UInt32) ByteSize() int {
  160. return 4
  161. }
  162. func (self UInt32) SaveTo(b []byte) int {
  163. if cap(b) < 4 { panic("buf too small") }
  164. binary.LittleEndian.PutUint32(b, uint32(self))
  165. return 4
  166. }
  167. func LoadUInt32(bytes []byte) UInt32 {
  168. return UInt32(binary.LittleEndian.Uint32(bytes))
  169. }
  170. // Int64
  171. func (self Int64) Equals(other Binary) bool {
  172. return self == other
  173. }
  174. func (self Int64) Less(other Key) bool {
  175. if o, ok := other.(Int64); ok {
  176. return self < o
  177. } else {
  178. panic("Cannot compare unequal types")
  179. }
  180. }
  181. func (self Int64) ByteSize() int {
  182. return 8
  183. }
  184. func (self Int64) SaveTo(b []byte) int {
  185. if cap(b) < 8 { panic("buf too small") }
  186. binary.LittleEndian.PutUint64(b, uint64(self))
  187. return 8
  188. }
  189. func LoadInt64(bytes []byte) Int64 {
  190. return Int64(binary.LittleEndian.Uint64(bytes))
  191. }
  192. // UInt64
  193. func (self UInt64) Equals(other Binary) bool {
  194. return self == other
  195. }
  196. func (self UInt64) Less(other Key) bool {
  197. if o, ok := other.(UInt64); ok {
  198. return self < o
  199. } else {
  200. panic("Cannot compare unequal types")
  201. }
  202. }
  203. func (self UInt64) ByteSize() int {
  204. return 8
  205. }
  206. func (self UInt64) SaveTo(b []byte) int {
  207. if cap(b) < 8 { panic("buf too small") }
  208. binary.LittleEndian.PutUint64(b, uint64(self))
  209. return 8
  210. }
  211. func LoadUInt64(bytes []byte) UInt64 {
  212. return UInt64(binary.LittleEndian.Uint64(bytes))
  213. }
  214. // Int
  215. func (self Int) Equals(other Binary) bool {
  216. return self == other
  217. }
  218. func (self Int) Less(other Key) bool {
  219. if o, ok := other.(Int); ok {
  220. return self < o
  221. } else {
  222. panic("Cannot compare unequal types")
  223. }
  224. }
  225. func (self Int) ByteSize() int {
  226. return 8
  227. }
  228. func (self Int) SaveTo(b []byte) int {
  229. if cap(b) < 8 { panic("buf too small") }
  230. binary.LittleEndian.PutUint64(b, uint64(self))
  231. return 8
  232. }
  233. func LoadInt(bytes []byte) Int {
  234. return Int(binary.LittleEndian.Uint64(bytes))
  235. }
  236. // UInt
  237. func (self UInt) Equals(other Binary) bool {
  238. return self == other
  239. }
  240. func (self UInt) Less(other Key) bool {
  241. if o, ok := other.(UInt); ok {
  242. return self < o
  243. } else {
  244. panic("Cannot compare unequal types")
  245. }
  246. }
  247. func (self UInt) ByteSize() int {
  248. return 8
  249. }
  250. func (self UInt) SaveTo(b []byte) int {
  251. if cap(b) < 8 { panic("buf too small") }
  252. binary.LittleEndian.PutUint64(b, uint64(self))
  253. return 8
  254. }
  255. func LoadUInt(bytes []byte) UInt {
  256. return UInt(binary.LittleEndian.Uint64(bytes))
  257. }