finitedomain
Version:
A fast feature rich finite domain solver
439 lines (335 loc) • 35 kB
JavaScript
import expect from '../fixtures/mocha_proxy.fixt';
import {
trie_add,
trie_addNum,
trie_create,
_trie_debug,
trie_get,
trie_getNum,
trie_has,
trie_hasNum,
} from '../../src/trie';
describe('src/tries.spec', function() {
it('should create a new empty tree', function() {
let trie = trie_create();
expect(trie).to.be.an('object');
expect(trie.buffer instanceof Uint16Array).to.eql(true);
});
it('should create a pre-filled tree', function() {
let trie = trie_create(['a', 'b', 'foo']);
expect(trie).to.be.an('object');
expect(trie.buffer instanceof Uint16Array).to.eql(true);
});
describe('size hint', function() {
it('should work with 8bits', function() {
let trie = trie_create(undefined, 50, 8);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
});
it('should work with 16bits', function() {
let trie = trie_create(undefined, 50, 16);
expect(trie.buffer).to.be.an.instanceof(Uint16Array);
});
it('should work with 32bits', function() {
let trie = trie_create(undefined, 50, 32);
expect(trie.buffer).to.be.an.instanceof(Uint32Array);
});
it('should work with 64bits', function() {
let trie = trie_create(undefined, 50, 64);
expect(trie.buffer).to.be.an.instanceof(Float64Array);
});
});
describe('string keys', function() {
it('should be able to add a key', function() {
let trie = trie_create();
let before = trie_add(trie, 'foo', 15);
expect(before).to.eql(-1);
});
it('should be able to add value=0', function() {
let trie = trie_create();
let before = trie_add(trie, 'foo', 0);
expect(before).to.eql(-1);
});
it('should be able to return before-value if it was set', function() {
let trie = trie_create();
trie_add(trie, 'bar', 100);
let before = trie_add(trie, 'bar', 15);
//console.log(_trie_debug(trie));
expect(before).to.eql(100);
});
it('should be able to return before-value if it was zero', function() {
let trie = trie_create();
trie_add(trie, 'bar', 0);
let before = trie_add(trie, 'bar', 10);
expect(before).to.eql(0);
});
it('should be able to read a key', function() {
let trie = trie_create();
trie_add(trie, 'foo', 15);
let n = trie_get(trie, 'foo');
expect(n).to.eql(15);
});
it('should be able to read a value=0', function() {
let trie = trie_create();
trie_add(trie, 'hi', 0);
let n = trie_get(trie, 'hi');
//console.log(_trie_debug(trie));
expect(n).to.eql(0);
});
it('should be able to set value>128 when trie is small', function() {
// buffer should be a clamped array (=unsigned byte) or results can be weird.
// we assume all input values are zero or positive
let trie = trie_create();
trie_add(trie, 'unsigned', 200);
let n = trie_get(trie, 'unsigned');
expect(n).to.eql(200);
});
it('should fit some small strings', function() {
// from perf test case. a bit biased but whatever
let strs = [
'1', '_ROOT_BRANCH_', 'SECTION', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'ITEM', 'COMPONENT', 'inlets', '11', '12', 'inlets&n=1', '13', '14', 'inlets&n=2', '15', '16', 'inlets&n=3', '17', '18', 'inlets&n=4', '19', '20', 'COMPONENT_START', 'COMPONENT_END', 'SECTION&n=1', 'ITEM&n=1', 'COMPONENT&n=1', 'inlets&n=5', '21', '22', 'inlets&n=6', '23', '24', 'inlets&n=7', '25', '26', 'inlets&n=8', '27', '28', 'inlets&n=9', '29', '30', 'COMPONENT_START&n=1', 'COMPONENT_END&n=1', 'SECTION&n=2', 'ITEM&n=2', 'COMPONENT&n=2', 'inlets&n=10', '31', '32', 'inlets&n=11', '33', '34', 'inlets&n=12', '35', '36', 'inlets&n=13', '37', '38', 'inlets&n=14', '39', '40', 'COMPONENT_START&n=2', 'COMPONENT_END&n=2', 'SECTION&n=3', 'ITEM&n=3', 'COMPONENT&n=3', 'inlets&n=15', '41', '42', 'inlets&n=16', '43', '44', 'inlets&n=17', '45', '46', 'inlets&n=18', '47', '48', 'inlets&n=19', '49', '50', 'COMPONENT_START&n=3', 'COMPONENT_END&n=3', 'SECTION&n=4', 'ITEM&n=4', 'COMPONENT&n=4', 'inlets&n=20', '51', '52', 'inlets&n=21', '53', '54', 'inlets&n=22', '55', '56', 'inlets&n=23', '57', '58', 'inlets&n=24', '59', '60', 'COMPONENT_START&n=4', 'COMPONENT_END&n=4', 'SECTION&n=5', 'ITEM&n=5', 'COMPONENT&n=5', 'inlets&n=25', '61', '62', 'inlets&n=26', '63', '64', 'inlets&n=27', '65', '66', 'inlets&n=28', '67', '68', 'inlets&n=29', '69', '70', 'COMPONENT_START&n=5', 'COMPONENT_END&n=5', 'SECTION&n=6', 'ITEM&n=6', 'COMPONENT&n=6', 'inlets&n=30', '71', '72', 'inlets&n=31', '73', '74', 'inlets&n=32', '75', '76', 'inlets&n=33', '77', '78', 'inlets&n=34', '79', '80', 'COMPONENT_START&n=6', 'COMPONENT_END&n=6', 'SECTION&n=7', 'ITEM&n=7', 'COMPONENT&n=7', 'inlets&n=35', '81', '82', 'inlets&n=36', '83', '84', 'inlets&n=37', '85', '86', 'inlets&n=38', '87', '88', 'inlets&n=39', '89', '90', 'COMPONENT_START&n=7', 'COMPONENT_END&n=7', 'SECTION&n=8', 'ITEM&n=8', 'COMPONENT&n=8', 'inlets&n=40', '91', '92', 'inlets&n=41', '93', '94', 'inlets&n=42', '95', '96', 'inlets&n=43', '97', '98', 'inlets&n=44', '99', '100', 'COMPONENT_START&n=8', 'COMPONENT_END&n=8',
];
let trie = trie_create();
for (let i = 0; i < strs.length; ++i) {
expect(trie_get(trie, strs[i])).to.eql(-1);
expect(trie_has(trie, strs[i]), 'should not yet have it').to.eql(false);
expect(trie_add(trie, strs[i], i), 'should be unset (-1): i=' + i + ' -> ' + strs[i]).to.eql(-1);
expect(trie_has(trie, strs[i]), 'should be set now').to.eql(true);
expect(trie_get(trie, strs[i])).to.eql(i);
}
expect(trie.count).to.eql(strs.length);
// also check after all keys are added to ensure nothing is clobbered or whatever
for (let i = 0; i < strs.length; ++i) {
expect(trie_get(trie, strs[i]), 'i=' + i + ' -> ' + strs[i]).to.eql(i);
}
//console.log(_trie_debug(trie));
});
describe('value bitsize', function() {
it('should work incrementally', function() {
let trie = trie_create(undefined, 50, 8);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
trie_add(trie, '1', 1);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
expect(trie_get(trie, '1')).to.eql(1);
trie_add(trie, '1 << 10', 1 << 10);
expect(trie.buffer).to.be.an.instanceof(Uint16Array);
expect(trie_get(trie, '1 << 10')).to.eql(1 << 10);
trie_add(trie, '1 << 20', 1 << 20);
expect(trie.buffer).to.be.an.instanceof(Uint32Array);
expect(trie_get(trie, '1 << 20')).to.eql(1 << 20);
trie_add(trie, '0xA000000000', 0xA000000000);
expect(trie.buffer).to.be.an.instanceof(Float64Array);
expect(trie_get(trie, '0xA000000000')).to.eql(0xA000000000);
});
it('should grow from min to max bitsize', function() {
let trie = trie_create(undefined, 50, 8);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
trie_add(trie, '1', 1);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
expect(trie_get(trie, '1')).to.eql(1);
trie_add(trie, '0xA000000000', 0xA000000000);
expect(trie.buffer).to.be.an.instanceof(Float64Array);
expect(trie_get(trie, '0xA000000000')).to.eql(0xA000000000);
});
it('should grow from 16 to 32bit', function() {
let trie = trie_create(undefined, 50, 8);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
trie_add(trie, '1 << 10', 1 << 10);
expect(trie.buffer).to.be.an.instanceof(Uint16Array);
expect(trie_get(trie, '1 << 10')).to.eql(1 << 10);
trie_add(trie, '1 << 20', 1 << 20);
expect(trie.buffer).to.be.an.instanceof(Uint32Array);
expect(trie_get(trie, '1 << 20')).to.eql(1 << 20);
});
});
});
describe('number keys', function() {
it('should be able to add a key=213', function() {
let trie = trie_create();
let before = trie_addNum(trie, 213, 15);
expect(before).to.eql(-1);
});
it('should be able to add a key=0', function() {
let trie = trie_create();
let before = trie_addNum(trie, 213, 15);
expect(before).to.eql(-1);
});
it('should be able to add value=0', function() {
let trie = trie_create();
let before = trie_addNum(trie, 544, 0);
expect(before).to.eql(-1);
});
it('should be able to return before-value if it was set', function() {
let trie = trie_create();
trie_addNum(trie, 7448, 100);
let before = trie_addNum(trie, 7448, 15);
expect(before).to.eql(100);
});
it('should be able to return before-value if it was zero', function() {
let trie = trie_create();
trie_addNum(trie, 855747, 0);
let before = trie_addNum(trie, 855747, 10);
expect(before).to.eql(0);
});
it('should be able to read a key', function() {
let trie = trie_create();
trie_addNum(trie, 4258, 15);
let n = trie_getNum(trie, 4258);
expect(n).to.eql(15);
});
it('should be able to read a value=0', function() {
let trie = trie_create();
trie_addNum(trie, 484, 0);
let n = trie_getNum(trie, 484);
//console.log(_trie_debug(trie));
expect(n).to.eql(0);
});
it('should be able to set value>128 when trie is small', function() {
// buffer should be a clamped array (=unsigned byte) or results can be weird.
// we assume all input values are zero or positive
let trie = trie_create();
trie_addNum(trie, 1337, 200);
let n = trie_getNum(trie, 1337);
expect(n).to.eql(200);
});
it('should fit some numbers', function() {
// from perf test case. a bit biased but whatever
//let strs = new Array(2000).fill(0xffffffff).map(n => Math.floor(Math.random() * n) >>> Math.floor(Math.random() * 15));
//
//console.log('['+strs.join(',')+']');
let strs = [
1498157, 4344892, 479260828, 1091635, 1275488184, 239689, 1700686334, 978974, 14662476, 67430468, 1409102898, 8855387, 5985276, 8136386, 392241, 503224339, 33234430, 756246, 23743700, 467881, 5612192, 6703487, 4515968, 196348, 640230570, 859890, 2473373419, 2315894, 434218, 118632652, 3902272, 21256061, 131337432, 7231959, 247655, 480755, 133456, 251798, 6300965, 7440260, 4146905571, 130403781, 4648765, 311987, 1501471207, 54570673, 119586665, 839576, 165312529, 60239312, 1891920, 430014, 521037, 84558935, 4047, 182390, 235047449, 1222244, 51682109, 5992301, 1008197, 2037729, 3272057216, 114452, 1237032, 814372899, 139874, 528856399, 35868489, 245158, 931489136, 2396223, 1962391375, 118788260, 1644995, 7944, 1564593, 26265, 660373072, 40168, 93788, 62140791, 508769667, 74221446, 59883288, 395075, 46959077, 20798493, 57815, 37868918, 110165089, 155309, 12754204, 193450, 3491155229, 229301285, 1463765395, 224357161, 259581621, 3072736154, 3055848579, 52222, 16955099, 432663, 10667471, 519422285, 1497362, 8320744, 1492838, 76616680, 6561080, 2617944, 1636563, 2566, 33012433, 7264164, 228700824, 382954744, 15281610, 1146724, 514283184, 126388, 2091087721, 1043571, 1326835770, 2814898, 1466580, 7443641, 25326898, 2888856, 1075096632, 643602, 1910094406, 163513, 1477485843, 356901, 422727746, 856969, 6022524, 626996164, 454537329, 4940800, 8224045, 509, 63442100, 3053975328, 54139671, 9084301, 100809, 224527, 40098569, 131002786, 2061593247, 88184, 9835991, 217306935, 31722479, 33603547, 75163, 379532, 1666701690, 273267115, 484146614, 1111160, 231513, 1124700, 188877, 2879678, 621243197, 180087, 8074, 1046313660, 12756730, 128353233, 192579751, 24587097, 1136931, 16416855, 133042671, 3089923840, 33286451, 175001, 204937, 207526, 552942, 1900391, 6811909, 193455, 870730, 3262768, 5838227, 1789613587, 5567289, 16193620, 344179, 2188797, 68914794, 56740901, 130223055, 75778561, 1331043, 652278, 12426613, 65457882, 516109, 34587, 1630776, 33267193, 189450165, 1230294, 190250, 5971005, 13782218, 61493, 30092, 178462390, 175436, 21094851, 429200988, 1744918, 4338500, 93606, 48392098, 55604, 1208613171, 263559, 2319128, 50381, 237530675, 5482603, 2888941392, 140808500, 12425787, 194564995, 510944094, 371268512, 393620370, 405934, 138786, 25699507, 95423381, 187040997, 126456386, 225461, 981901026, 231, 223031643, 592208057, 279117, 1636857170, 219437, 20452, 4239, 54431823, 338821289, 3421423, 21574142, 1960207, 10840928, 233566, 504320199, 1135079675, 107899, 617425, 53760, 3981668, 481316555, 109336229, 26115218, 1628294, 179195, 2365717, 457777, 1847150, 222991, 433582, 137735, 15019283, 16537122, 56398920, 979622, 22495452, 472955, 6321845, 116010879, 934839411, 97487448, 1655544257, 105471, 10412180, 12502121, 3980516237, 73471725, 902868, 143964023, 30703, 1972256755, 126854256, 99757443, 5305501, 159059, 3715143, 397513, 14551243, 41076417, 2980731, 995760, 6202342, 137319, 1177921, 7546011, 3923621902, 564515, 1000531394, 501711, 856550, 7354586, 4167342420, 453564, 170442, 174654110, 71800, 2053774656, 922939136, 9996334, 32721434, 22481931, 2722090732, 2617045017, 318970187, 11566396, 55963786, 96577412, 157485498, 181181039, 292187, 673178307, 483401, 227733517, 24128, 2479478110, 160370231, 25360270, 459876646, 974573, 723649, 342759716, 185151, 1490768860, 8310492, 50979924, 6978847, 493985516, 3223400203, 107227395, 95139, 760944, 377018554, 549162, 8007486, 1071755235, 693683584, 604534, 133502, 456050, 251527829, 770883, 1745222997, 502132482, 339761, 22622, 1170130, 410003, 26535198, 109421090, 195101, 1777046635, 1955356169, 215764759, 3887879489, 132202013, 14430067, 22425841, 6922165, 104147, 1762443, 13451455, 2504179, 253618, 70841666, 35405842, 1604661268, 12554488, 62354391, 19241, 8360459, 26891295, 209914678, 7261014, 292067, 14328992, 6824351, 1916312243, 170804, 946455, 1015088, 234233, 1068550, 198652, 275540, 3447052, 60564, 26858289, 2870806, 42788374, 73228809, 248255505, 47515637, 713964025, 9619014, 550758, 1259840748, 1692907, 12808782, 10992863, 2205188114, 914982, 25326714, 1903265, 126111630, 49323, 57594134, 489394, 686291, 73478, 163921102, 1179985, 646325, 970484928, 427524674, 180320438, 91712, 342293279, 1208141, 40194458, 3992461, 532504066, 135125, 2947005, 227182, 214585, 99430, 22065885, 316208, 1114797366, 79400, 3705828, 102419, 111610, 3817009, 13768053, 24665679, 339071390, 342181, 40847996, 5253129, 90090978, 30199480, 2325917, 52681026, 1484449, 584949, 179872359, 341322743, 732669, 146876359, 199580, 3391548, 431724, 1655889699, 35695, 4632209, 2789494523, 9040689, 4077399, 458488, 366315960, 1403653, 20451384, 10179901, 30884, 750922, 60411891, 5713453, 230775, 940896522, 1865582832, 2866629868, 1828700946, 3812468683, 10696039, 15690691, 4390922, 23032296, 86706640, 170984324, 1940890, 1789206, 89289614, 31041062, 48748206, 51356, 371343, 72707496, 659551080, 728652685, 439967, 32658927, 524993, 68701577, 1623237797, 42228845, 596720, 5390628, 837788, 12193325, 293630, 903948133, 26506955, 20965073, 4608497, 3388264, 1441452, 91668818, 849167, 89527561, 493716834, 5886226, 87006495, 27044700, 35833548, 36044251, 727376, 568808759, 70164722, 36151264, 63595874, 37917266, 2078031761, 3299976, 696850990, 360426, 1417185586, 230828, 1341876116, 158484503, 204618299, 1438489, 12248831, 512111020, 799061, 248621372, 437945, 1037254129, 2138458856, 134881, 138470823, 15128074, 685105086, 1303361505, 41146893, 536366, 635036921, 418768345, 116281061, 16153495, 6689224, 2031761, 25241340, 1086252, 318005, 4249579663, 103532705, 286657, 170740, 46059293, 236387139, 15347354, 4058736563, 75004317, 22423846, 1241053, 269333, 204622, 1772479557, 13147473, 213921, 626695, 1344630, 136299064, 204431, 712205, 19658071, 11972333, 654088, 220553, 70736, 63580614, 198849, 1165320750, 1289, 179900582, 40966670, 6672489, 1403684, 7752272, 3462235, 140960, 10312045, 33308057, 9394023, 272674209, 772140666, 2300136, 26245505, 74224809, 7691, 1296387853, 72286563, 37321800, 247608722, 212786735, 93081466, 517171535, 7610827, 489775310, 222407, 673678, 173830862, 100331335, 1767059, 3666974, 161348, 4555305, 87296187, 122414233, 1000368291, 90039305, 768438064, 53954127, 1379611, 132183, 511685922, 4433616, 95049248, 62833448, 1895587, 2040295, 132583, 182386, 1265963, 939013473, 32874646, 4351730, 909400, 253176936, 14847955, 48386041, 279424, 428169388, 434940, 4597159, 87784936, 30757, 942940384, 3694589, 25953216, 2470097, 2213798, 366184, 27960070, 348257, 162622, 2283521, 509590298, 408164004, 468310689, 113225149, 125340124, 120994, 1903552, 395813022, 24690168, 1386268, 267333, 366962, 18389, 143453, 9226, 27655427, 751465643, 1988324, 284474, 252586546, 785345, 32648, 97735962, 834471383, 26425, 15434414, 67584786, 8654870, 11615658, 1747618, 4411757, 764210, 37399418, 7149921, 195060574, 1224705414, 4018113293, 5001030, 12868150, 931021738, 725662779, 1053965, 230782, 6104538, 137429518, 10389301, 3710412645, 1171611129, 372527769, 720722, 188479478, 3666230032, 222389221, 41403986, 465890, 8877918, 1431768610, 1214253526, 80310, 167745, 460248, 124855428, 3403011, 3968935914, 65379593, 3200866, 923511473, 349112031, 5265878, 37111578, 239495, 10027774, 1037974918, 318450, 13882763, 3136166, 14407140, 11303222, 31442597, 1140816297, 1605620724, 9811278, 27991662, 67519672, 1135034160, 27833074, 47574171, 17708044, 115448, 6328344, 77308, 187056108, 317996, 62788361, 12157307, 408284, 1322839, 5176791, 189906, 49362, 132875235, 253266636, 311311, 9809979, 87272, 216532, 4238331931, 2528819, 3076547, 2066716, 37336603, 339885112, 1685800, 1592944, 92573385, 240986939, 41312, 24730, 6488173, 292552268, 139600, 797042397, 237785063, 386653, 465983, 26529652, 259863, 267017721, 6377, 4702875, 753217339, 1125178, 236234, 1091636, 562587946, 534577, 3250396348, 22670085, 577397249, 3658997, 3913482, 422485, 1181551, 980553058, 3507479, 273102749, 56776435, 10115, 202786, 27612844, 300105694, 252338437, 464422, 70898, 7418118, 232860, 3519826, 1409575440, 220799090, 520070611, 341382453, 355248842, 373564, 538572, 12957993, 56524697, 33494845, 4019052, 102687353, 99149680, 99671539, 957952, 136399, 99847, 881420284, 662186, 360185411, 2539705260, 6455410, 2463550, 40784171, 1991667, 1670052, 3866, 43364, 250964, 65428, 252629125, 728057808, 287324478, 233236, 24694919, 163358, 1427790, 3059170, 53503203, 527175, 268233, 64557230, 28942391, 1156499, 182967, 1769055468, 391215502, 8038162, 3492434, 56655135, 13861633, 3844560, 29833795, 3335278710, 7100386, 14294235, 3150558414, 28935348, 139902, 352983855, 633572952, 587327, 2136802, 916858075, 933847076, 53382973, 39464155, 7019996, 3945194, 786906626, 1160008, 229462, 67652, 811808927, 206968875, 1021853, 112170, 569977, 19895323, 397028341, 117133262, 222406, 7199817, 217524425, 15061532, 14057894, 2925616, 68914417, 217097, 2150984, 318402716, 1318891, 101192238, 510230, 559568, 2955902335, 15834067, 94160764, 228217379, 1360201417, 3460917, 1607108468, 82482965, 32154453, 5453425, 119616391, 11708938, 291308, 27521083, 44869155, 351398602, 2134586, 1395679, 7523512, 36963963, 14434682, 170930, 1879049764, 3026839, 917043, 12302166, 301011251, 1033761409, 261580954, 663254344, 1594062, 1403330, 419354, 2213412, 6119954, 3726421041, 2989654, 220060, 14903899, 818264521, 25475936, 558469, 39056287, 90121, 273714571, 3402498576, 334507970, 187747, 732230746, 1560964975, 10466208, 81372229, 1712641, 146831, 719671, 187450473, 9187204, 2996721, 61830, 935658, 167503, 3374429, 1272307862, 2093805, 621453214, 2459412, 97928169, 1232125625, 28835387, 68675, 1633594, 548121, 424467333, 35407699, 2660252, 1872166, 402573, 351388, 102326852, 4160764, 13323572, 37880290, 56289, 31738899, 3921234, 2722245, 3041505966, 65128343, 113496, 3162719, 98886, 999091, 2773853, 652711377, 229221, 1735808, 3769422189, 1418293, 10107886, 335944585, 2353522, 191329739, 458959, 212535858, 206320, 1563211, 49428460, 95960302, 273549, 120290612, 71629, 43279553, 877091174, 2060972, 61474567, 10506862, 1285048, 41104727, 9230922, 5837331, 93419704, 512778845, 492067, 24963822, 5308037, 383951941, 11461487, 127181616, 159215, 8210476, 371914989, 452643, 16101014, 265969731, 7717243, 3690386, 250077, 572787839, 1709297315, 2064086043, 876732860, 2485578, 1610942, 817762, 2201024, 1665433549, 127596, 362737446, 1289837017, 14561381, 1832556, 5807607, 1883178597, 17425814, 29581774, 2380423, 278306, 8327, 2499114, 24957686, 828363, 1964019153, 1344993746, 1880109, 443353863, 122872131, 11549237, 161650, 10713, 2016176376, 245525878, 306170, 757188, 99433477, 222579, 11519021, 386854, 198099182, 929094, 3111754498, 3142329490, 2558833, 26516872, 9462778, 1682122, 500998162, 415169294, 1366876, 1699490093, 17944479, 461239, 3294130960, 131386426, 66963794, 706022876, 6265277, 280951, 122129304, 16465002, 2336898, 54936535, 1285670, 510813942, 15707305, 142599, 16701877, 101211423, 3181543, 22480513, 3632468, 6820376, 609461, 1105103, 3904304, 38431326, 610729, 1454201541, 8311355, 7971078, 4153062, 849946, 1071436, 1027264840, 123966059, 164418547, 2072689, 790106, 47914, 232738859, 171261, 1694332340, 385454298, 867865, 3079165, 240976100, 44323721, 637135, 2296587944, 8834493, 349089, 2495310, 7195317, 7994810, 38289163, 66220, 438564, 3969396440, 13826949, 249767994, 28863769, 506409041, 29420120, 899757384, 5722509, 1890227199, 2125279, 38519, 705868, 31331585, 359975984, 48907, 43367415, 1235269, 3639197, 1300746505, 58101, 2180490532, 52861833, 52541700, 1856321, 136829963, 835947, 12908242, 31518910, 52191, 231820, 513811986, 64202401, 1381791, 132173083, 1480372, 1942582, 15541958, 2581482090, 16067244, 278474352, 32479317, 436663, 275580, 5287412, 514987582, 2631725, 650967259, 139041709, 1007589717, 1891187735, 113891024, 7194328, 199570382, 2093440, 3335067, 49163882, 2816658, 1055361, 1640626928, 501325974, 308597, 816598, 180872765, 3521682, 519635, 37807, 1352684541, 4084318, 559, 571924812, 7645078, 14459978, 2025182260, 501973250, 69493, 80523, 972582082, 521751018, 475530101, 2214045, 36991909, 799744, 14136385, 379420, 8813679, 130218, 292869809, 81127708, 18272960, 581356, 1778631, 1732982463, 49104548, 39130787, 242600535, 1925762873, 304341546, 420030, 63485534, 548981, 229640, 640373, 1735809, 7260092, 167530, 912994, 32687, 2670826697, 2995836, 5355327, 30814846, 36237, 4013762, 677840608, 1328429, 446092097, 52387142, 96376063, 16981467, 1557404395, 1709033079, 2297756, 4872934, 3797864091, 261835, 989275, 79785684, 181707, 296962, 1135535, 616216, 938047, 21748391, 534086950, 2852289384, 2497179556, 18234469, 2076835909, 5558286, 275915629, 5650698, 15620118, 1644887554, 891802, 749556930, 1808494, 1901031654, 25864869, 132688532, 635221389, 16630829, 233893303, 233978914, 3330620, 169560183, 24533632, 464345, 129601747, 2029657, 186619972, 62781974, 1749918, 48838836, 38060770, 173172, 29691452, 759514, 7019496, 342701096, 176685, 313395, 261027083, 779101, 23290, 165122583, 207755694, 805011, 63413297, 890591842, 3597497, 2192675, 734280128, 252047321, 3053377, 88686, 5492591, 141164443, 1166862, 2524127, 1012576, 10814261, 115419207, 65693216, 154189, 1133526, 3137465, 3012330961, 518109408, 12864187, 132971080, 55575362, 5061558, 27993, 154977, 63456927, 7178752, 13288288, 190376010, 462273, 4283898, 8196514, 458318, 2558694, 1168878, 22664547, 830410973, 191985526, 2001866, 9206741, 169212212, 238817, 531531671, 186887, 3819725, 102874, 86690899, 1876967803, 849709, 1028569077, 32084883, 8365760, 191857, 751674233, 743596246, 49463, 3032765336, 620235800, 188781550, 24912140, 5442141, 182248, 224088426, 27674, 14967994, 421399, 156646572, 40742399, 32363836, 393588357, 2018814489, 6317347, 58191614, 2710189, 2870862508, 1714560, 21096954, 151198202, 499829597, 272718, 41243742, 428164, 195014882, 35654791, 30594045, 2494821163, 204459, 910570408, 161767, 2885901, 2476356, 130347, 123139223, 714, 287166072, 1558978, 5742717, 417799419, 6589835, 45101701, 50380, 812666734, 24256, 1418047, 1178864, 413186, 47851192, 23227962, 380796, 91896705, 181153, 21583863, 283931, 11102782, 2733348658, 2198885, 118244343, 609360683, 61387295, 3921568, 454294395, 510711, 163568375, 14312703, 8999580, 122844345, 30848523, 7955422, 3089278, 541330, 54610, 4209414, 2882126922, 264105989, 185286324, 18281168, 929254292, 212007, 6558556, 66008766, 381285, 170220591, 7446, 821598165, 244273869, 880658, 91036, 518743292, 72188, 1379696, 254383724, 406903, 98859250, 388319, 114586, 844211, 23626, 126658, 5571531, 511282647, 941373723, 450634, 2101036509, 130454239, 23069091, 87644639, 3449520327, 98800, 281451228, 404945138, 466866400, 111789891, 1461881, 594177295, 1315336, 1022438856, 778845920, 111445151, 479411, 10449762, 601022, 2138258, 12171338, 581627, 26997978, 969318, 483253, 4837356, 24779996, 1525013, 586162, 424738, 59408838, 437136, 249214, 1266901258, 7643596, 206081582, 706032214, 24246, 224733085, 479234509, 7792082, 111597640, 2338377, 293236799, 52725785, 2433688, 881041, 28420061, 418983027, 258143, 21714315, 390183790, 118955, 5264934, 514790, 19612531, 678147, 16073604, 68870727, 1773768, 23053509, 132670045, 300560, 216546390, 59662908, 25247789, 114122, 1035989, 868780, 361429, 214726, 106286738, 269355, 465937, 92631, 818614, 62347167, 3817122, 15398243, 968932232, 52574850, 5539031, 851926169, 46694876, 777476580, 2886385082, 741625, 3668829740, 3194489744, 10891, 2235822, 848556, 343382, 4036882, 313886, 3930145419, 839844, 132421550, 779615, 1442468, 901536, 139136212, 2314954, 75011112, 147261243, 34491173, 24004983, 173497878, 540915935, 847311, 24678806, 299296, 772037, 61234446, 9966859, 1777410934, 2140455357, 1012491833, 3137659302, 259201, 5551424, 9859913, 88233, 151134879, 78674, 1985332, 201832, 1345382, 202405334, 77077245, 485906476, 1164011, 1423102, 1811110, 7504453, 38567411, 1977202432, 920516229, 6286149, 496536056, 28905400, 6574991, 279475873, 124655, 28214025, 11610486, 283587, 1889030, 3906127, 809553052, 2461950, 9966175, 594888, 1184210, 2277372845, 43037198, 3362556054, 6708235, 6734387, 28181181, 1460128920, 384055, 94807272, 1415914516, 207115, 27819, 467203, 2545254, 10525990, 1471423, 9714842, 974883, 208496, 8060210, 268176, 731397, 160188040, 838776, 115743, 4870916, 324835999, 2491502, 203016, 1500995, 149327, 443916, 387447932, 13939728, 24246276, 259702692, 935517920, 907079460, 2629376, 6491128, 1172994, 21942360, 407166426, 1046168, 23767904, 169183, 2633826, 3822718, 1505111256, 1095209, 18878652, 414863, 202894, 472708560, 67132, 38317619, 7029, 13700071, 502161, 2203678, 10292756, 49508192, 1865304947, 2143272, 60143786, 427651, 785517544, 148265, 239984105, 939476, 36422527, 50315736, 451547, 601369, 187360937, 18353451, 238915, 285218, 159638, 1744025868, 771095, 41634160, 20670049, 3952064828, 424565349, 57768400, 776904, 4410862, 324278, 427896, 88347, 715888, 849097366, 37185, 198377670, 214643, 86703, 5818804, 158087644, 60736, 22800854, 747987, 1117418668, 59107, 54894737, 73895927, 1993832, 361120, 4660263, 224712032, 55285880, 2007549649, 39586, 1808345, 201401, 5805515, 12906978, 3853067200, 2094369, 6011280, 4300059, 403516, 99719592, 37220513, 334119, 3411494, 7766852, 52294886, 18933187, 1460815875, 1935257167, 224828, 210045138, 230172355, 33196, 5790469, 22223877, 159471, 1041725183, 64071, 31468877, 50802604, 76981762, 63503741, 1908823606, 262465, 2176150, 403697774, 1497836610, 1525526, 3437534355, 32648532, 414104, 17381215, 258387, 1134682, 6679087, 244656547, 5606689, 257819, 38739753, 921848213, 483403, 10477011, 16620063, 207782233, 455347456, 462086, 12656479, 625448, 126915455, 5267275, 3740867, 149357194, 921356, 133553803, 605630, 28499993, 1430606, 1046186, 26245836, 4624026, 328850, 1431905, 120147, 10875755, 3933256345, 61884663, 467913, 341104, 13879645, 22480594, 10278151, 347693, 318846803, 69032, 4804385, 88636579, 34746, 110905109, 1267961207, 966987261, 22851677, 6033901, 257567, 49263, 516838, 17361110, 3092693, 16714771, 20527153, 157807655, 145689875, 460461793, 45993, 11075882, 2501881, 17398526, 627537, 2469001, 56250666, 685982912, 134049, 167802, 29021685, 37939, 18055463, 310878, 38783449, 623925, 3951910, 770140, 23670344, 18306641, 17614378, 121170592, 55072, 462800329, 4817873, 322140426, 3565772, 331390, 56405682, 24198407, 30426337, 5410948, 271604544, 122999132, 763638, 68151932, 1088855821, 37833075, 83663743, 2389425, 68342, 7602003, 34630643, 21658, 557743, 1914680357, 874182, 1476904, 364611, 95357941, 592507197, 261558357, 876465, 33386131, 121163, 64871, 7552190, 2084015751, 342025692, 546030, 4924777, 362497011, 522779837, 986436414, 1574106, 122527032, 79029319, 521875355, 379414, 133575, 263223415, 495424762, 3524109, 1027853, 64594729, 890593366, 313512, 333309, 5576371, 77921, 11391039, 164828364, 1912214248, 159858, 9191065, 7644687, 709366889, 151613543, 136370, 55090119, 341116, 54678563, 6563647, 412406, 2071392, 4304935, 23955136, 29574108, 1550360362, 17982840, 245581301, 261224, 271951658, 174115350, 683529, 125781, 332630, 623692252, 1983838800,
];
let trie = trie_create();
for (let i = 0; i < strs.length; ++i) {
trie_addNum(trie, strs[i], i);
expect(trie_hasNum(trie, strs[i]), 'should be set now').to.eql(true);
expect(trie_getNum(trie, strs[i])).to.eql(i);
}
expect(trie.count).to.eql(strs.length);
// also check after all keys are added to ensure nothing is clobbered or whatever
for (let i = 0; i < strs.length; ++i) {
expect(trie_getNum(trie, strs[i]), 'i=' + i + ' -> ' + strs[i]).to.eql(i);
}
if (false) console.log(_trie_debug(trie));
});
describe('value bitsize', function() {
it('should work incrementally', function() {
let trie = trie_create(undefined, 50, 8);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
trie_addNum(trie, 1, 1);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
expect(trie_getNum(trie, 1)).to.eql(1);
trie_addNum(trie, 1 << 10, 1 << 10);
expect(trie.buffer).to.be.an.instanceof(Uint16Array);
expect(trie_getNum(trie, 1 << 10)).to.eql(1 << 10);
trie_addNum(trie, 1 << 20, 1 << 20);
expect(trie.buffer).to.be.an.instanceof(Uint32Array);
expect(trie_getNum(trie, 1 << 20)).to.eql(1 << 20);
trie_addNum(trie, 0xA000000000, 0xA000000000);
expect(trie.buffer).to.be.an.instanceof(Float64Array);
expect(trie_getNum(trie, 0xA000000000)).to.eql(0xA000000000);
});
it('should grow from min to max bitsize', function() {
let trie = trie_create(undefined, 50, 8);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
trie_addNum(trie, 1, 1);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
expect(trie_getNum(trie, 1)).to.eql(1);
trie_addNum(trie, 0xA000000000, 0xA000000000);
expect(trie.buffer).to.be.an.instanceof(Float64Array);
expect(trie_getNum(trie, 0xA000000000)).to.eql(0xA000000000);
});
it('should grow from 16 to 32bit', function() {
let trie = trie_create(undefined, 50, 8);
expect(trie.buffer).to.be.an.instanceof(Uint8Array);
trie_addNum(trie, 1 << 10, 1 << 10);
expect(trie.buffer).to.be.an.instanceof(Uint16Array);
expect(trie_getNum(trie, 1 << 10)).to.eql(1 << 10);
trie_addNum(trie, 1 << 20, 1 << 20);
expect(trie.buffer).to.be.an.instanceof(Uint32Array);
expect(trie_getNum(trie, 1 << 20)).to.eql(1 << 20);
});
});
});
describe('key bitsize', function() {
it('should ignore bitsize if it is too small to address the whole space', function() {
let trie = trie_create(undefined, 300, 8); // note: the 0 was an important value for the regression
expect(trie.bits).to.eql(16);
});
it('should adjust bitsize even if there is enough "space"', function() {
// regression
let trie = trie_create(undefined, 0, 8); // note: the 0 was an important value for the regression
// add 300 keys to the table, so the key size overflows the 8 bit cell size
for (let i = 0; i < 300; ++i) {
let prevValue = trie_addNum(trie, i, 0);
expect(prevValue, 'key = ' + i + ' should not yet have a value').to.eql(-1);
}
expect(trie.count).to.eql(300);
});
});
it.skip('benchmark size', function() {
let trie = trie_create(undefined, 1, 8);
let arr1k = new Array(1000).fill(0);
arr1k.forEach((_, i) => trie_addNum(trie, i, 0));
arr1k.forEach((_, i) => trie_getNum(trie, i));
console.log(_trie_debug(trie, true));
/*
Key count: 1000
Node count: 1013 (1.013 nodes per key)
Buffer cell length: 12289
Buffer byte length: 24578
Bit size: 16
Node len: 11
Node size: 11
Last Node: 11132
Used space: 11143 cells, 21.76 kb
Unused space: 1146 cells, 2.23 kb
Mallocs: 1 4097 8193 12289
trie_adds: 1000
Avg key distance: 3.893
trie_hass: 0
trie_gets: 1000
Avg get distance: 3893 -> 3.893
*/
new Array(10000).fill(0).forEach((_, i) => trie_addNum(trie, i, 0));
console.log(_trie_debug(trie, true));
/*
Key count: 10000
Node count: 10113 (1.0113 nodes per key)
Buffer cell length: 116857
Buffer byte length: 467428
Bit size: 32
Node len: 11
Node size: 11
Last Node: 111232
Used space: 111243 cells, 434.54 kb
Unused space: 5614 cells, 21.92 kb
Mallocs: 1 4097 8193 12289 16385 20481 24577 28673 32769 36865 40961 45057 49562 54518 59969 65965 72561 79817 87798 96577 106234 116857
trie_adds: 11000
Avg key distance: 4.798818181818182
trie_hass: 0
trie_gets: 1000
Avg get distance: 3893 -> 3.893
*/
new Array(25000).fill(0).forEach((_, i) => trie_addNum(trie, i, 0));
console.log(_trie_debug(trie, true));
/*
Key count: 25000
Node count: 26112 (1.04448 nodes per key)
Buffer cell length: 303088
Buffer byte length: 1212352
Bit size: 32
Node len: 11
Node size: 11
Last Node: 287221
Used space: 287232 cells, 1.09 mb
Unused space: 15856 cells, 61.93 kb
Mallocs: 1 4097 8193 12289 16385 20481 24577 28673 32769 36865 40961 45057 49562 54518 59969 65965 72561 79817 87798 96577 106234 116857 128542 141396 155535 171088 188196 207015 227716 250487 275535 303088
trie_adds: 36000
Avg key distance: 5.324472222222222
trie_hass: 0
trie_gets: 1000
Avg get distance: 3893 -> 3.893
*/
});
});