git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: 0c0780cf35124713c92ed547b31057ee945069f8

Files: 0c0780cf35124713c92ed547b31057ee945069f8 / treeNode.js

1423 bytesRaw
1const Uint1Array = require('uint1array')
2const EMPTY_STATE_ROOT = Buffer.from('4cf812be9be2c6008325050f43d06676a08612c7', 'hex')
3
4function toTypedArray (array) {
5 return new Uint1Array(new Uint8Array(array).buffer)
6}
7
8// helper functions for nodes
9const EXTENSION = exports.EXTENSION = 0
10const LBRANCH = exports.LBRANCH = 1
11const RBRANCH = exports.RBRANCH = 2
12const VALUE = exports.VALUE = 3
13
14exports.setBranch = function (node, branch) {
15 node['/'][LBRANCH] = branch[0]
16 node['/'][RBRANCH] = branch[1]
17}
18
19exports.getBranch = function (node) {
20 return node['/'].slice(LBRANCH, LBRANCH + 2)
21}
22
23exports.getValue = function (node) {
24 return node['/'][VALUE]
25}
26
27exports.deleteValue = function (node) {
28 delete node['/'][VALUE]
29}
30
31exports.getExtension = function (node) {
32 if (node['/'][EXTENSION]) {
33 const len = node['/'][EXTENSION][0]
34 const extension = toTypedArray(node['/'][EXTENSION][1])
35 return extension.subarray(0, len)
36 } else {
37 return toTypedArray([])
38 }
39}
40
41exports.setExtension = function (node, ex) {
42 if (ex && ex.length) {
43 node['/'][EXTENSION] = [ex.length, Buffer.from(ex.buffer)]
44 } else {
45 delete node['/'][EXTENSION]
46 }
47}
48
49exports.setValue = function (node, val) {
50 node['/'][VALUE] = val
51}
52
53exports.isEmpty = function (node) {
54 if (Buffer.isBuffer(node['/'])) {
55 return !Buffer.compare(node['/'], EMPTY_STATE_ROOT)
56 } else {
57 return node['/'].every(el => !el)
58 }
59}
60

Built with git-ssb-web