git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: 3228beb1ebc4a9921b555fd514ede84f91ce5bf9

Files: 3228beb1ebc4a9921b555fd514ede84f91ce5bf9 / treeNode.js

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

Built with git-ssb-web