astronomical 3.0.5 → 3.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.mjs CHANGED
@@ -634,6 +634,42 @@ function createQuerier() {
634
634
  result
635
635
  };
636
636
  }
637
+ function activateDescendant(fnode, state) {
638
+ state.descendant[state.depth + 1].push(fnode);
639
+ fnode.seq = state.seqCounter++;
640
+ const value = fnode.node.value;
641
+ if (fnode.node.attribute) {
642
+ state.descendantOther.push(fnode);
643
+ state.descendantAttr.push(fnode);
644
+ } else if (value == "*") {
645
+ state.descendantOther.push(fnode);
646
+ } else if (value != void 0) {
647
+ let bucket = state.descendantByType.get(value);
648
+ if (!bucket) {
649
+ bucket = [];
650
+ state.descendantByType.set(value, bucket);
651
+ }
652
+ bucket.push(fnode);
653
+ }
654
+ state.descendantActiveCount++;
655
+ }
656
+ function removeFromBucket(arr, fnode) {
657
+ const i = arr.lastIndexOf(fnode);
658
+ if (i >= 0) arr.splice(i, 1);
659
+ }
660
+ function deactivateDescendant(fnode, state) {
661
+ const value = fnode.node.value;
662
+ if (fnode.node.attribute) {
663
+ removeFromBucket(state.descendantOther, fnode);
664
+ removeFromBucket(state.descendantAttr, fnode);
665
+ } else if (value == "*") {
666
+ removeFromBucket(state.descendantOther, fnode);
667
+ } else if (value != void 0) {
668
+ const bucket = state.descendantByType.get(value);
669
+ if (bucket) removeFromBucket(bucket, fnode);
670
+ }
671
+ state.descendantActiveCount--;
672
+ }
637
673
  function addFilterChildrenToState(filter, state) {
638
674
  if ("type" in filter && (filter.type == NodeType.AND || filter.type == NodeType.OR || filter.type == NodeType.EQUALS)) {
639
675
  addFilterChildrenToState(filter.left, state);
@@ -645,7 +681,7 @@ function createQuerier() {
645
681
  }
646
682
  if (filter.node.type == NodeType.DESCENDANT) {
647
683
  log2?.debug("ADDING FILTER DESCENDANT", filter.node);
648
- state.descendant[state.depth + 1].push(filter);
684
+ activateDescendant(filter, state);
649
685
  }
650
686
  }
651
687
  }
@@ -655,7 +691,7 @@ function createQuerier() {
655
691
  if (token.type == NodeType.CHILD) {
656
692
  state.child[state.depth + 1].push(fnode);
657
693
  } else if (token.type == NodeType.DESCENDANT) {
658
- state.descendant[state.depth + 1].push(fnode);
694
+ activateDescendant(fnode, state);
659
695
  }
660
696
  return fnode;
661
697
  }
@@ -674,16 +710,24 @@ function createQuerier() {
674
710
  }
675
711
  function addIfTokenMatch(fnode, path, state) {
676
712
  if (!isMatch(fnode, path)) return;
713
+ addMatch(fnode, path, state);
714
+ }
715
+ function addMatch(fnode, path, state) {
677
716
  state.matches[state.depth].push([fnode, path]);
678
717
  if (fnode.node.filter) {
679
718
  const filter = createFilter(fnode.node.filter, []);
680
719
  const filteredResult = [];
681
720
  const f = { filter, qNode: fnode.node, node: path.node, result: filteredResult };
682
721
  state.filters[state.depth].push(f);
683
- let fmap = state.filtersMap[state.depth].get(fnode.node);
722
+ let fmapContainer = state.filtersMap[state.depth];
723
+ if (!fmapContainer) {
724
+ fmapContainer = /* @__PURE__ */ new Map();
725
+ state.filtersMap[state.depth] = fmapContainer;
726
+ }
727
+ let fmap = fmapContainer.get(fnode.node);
684
728
  if (!fmap) {
685
729
  fmap = [];
686
- state.filtersMap[state.depth].set(fnode.node, fmap);
730
+ fmapContainer.set(fnode.node, fmap);
687
731
  }
688
732
  fmap.push(f);
689
733
  addFilterChildrenToState(filter, state);
@@ -889,7 +933,8 @@ function createQuerier() {
889
933
  function addResultIfTokenMatch(fnode, path, state) {
890
934
  const matchingFilters = [];
891
935
  const filters = [];
892
- const nodeFilters = state.filtersMap[state.depth].get(fnode.node);
936
+ const fmapContainer = state.filtersMap[state.depth];
937
+ const nodeFilters = fmapContainer ? fmapContainer.get(fnode.node) : void 0;
893
938
  if (nodeFilters) {
894
939
  for (let i = 0; i < nodeFilters.length; i++) {
895
940
  const f = nodeFilters[i];
@@ -987,9 +1032,14 @@ function createQuerier() {
987
1032
  child: [[], []],
988
1033
  descendant: [[], []],
989
1034
  filters: [[], []],
990
- filtersMap: [/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map()],
1035
+ filtersMap: [void 0, void 0],
991
1036
  matches: [[]],
992
- functionCalls: [[]]
1037
+ functionCalls: [[]],
1038
+ descendantByType: /* @__PURE__ */ new Map(),
1039
+ descendantOther: [],
1040
+ descendantAttr: [],
1041
+ descendantActiveCount: 0,
1042
+ seqCounter: 0
993
1043
  };
994
1044
  for (const [name, node] of Object.entries(queries)) {
995
1045
  createFNodeAndAddToState(node, results[name], state);
@@ -998,12 +1048,8 @@ function createQuerier() {
998
1048
  for (let i = 0; i < childAtDepth.length; i++) {
999
1049
  addPrimitiveAttributeIfMatch(childAtDepth[i], root);
1000
1050
  }
1001
- const descendantSlice = state.descendant.slice(0, state.depth + 1);
1002
- for (let i = 0; i < descendantSlice.length; i++) {
1003
- const fnodes = descendantSlice[i];
1004
- for (let j = 0; j < fnodes.length; j++) {
1005
- addPrimitiveAttributeIfMatch(fnodes[j], root);
1006
- }
1051
+ for (let i = 0; i < state.descendantAttr.length; i++) {
1052
+ addPrimitiveAttributeIfMatch(state.descendantAttr[i], root);
1007
1053
  }
1008
1054
  traverse(root.node, {
1009
1055
  enter(path, state2) {
@@ -1011,15 +1057,31 @@ function createQuerier() {
1011
1057
  state2.child.push([]);
1012
1058
  state2.descendant.push([]);
1013
1059
  state2.filters.push([]);
1014
- state2.filtersMap.push(/* @__PURE__ */ new Map());
1060
+ state2.filtersMap.push(void 0);
1015
1061
  state2.matches.push([]);
1016
1062
  state2.functionCalls.push([]);
1017
1063
  for (const fnode of state2.child[state2.depth]) {
1018
1064
  addIfTokenMatch(fnode, path, state2);
1019
1065
  }
1020
- for (const fnodes of state2.descendant.slice(0, state2.depth + 1)) {
1021
- for (const fnode of fnodes) {
1022
- addIfTokenMatch(fnode, path, state2);
1066
+ const bucket = state2.descendantByType.get(path.node.type);
1067
+ const other = state2.descendantOther;
1068
+ const bucketLen = bucket ? bucket.length : 0;
1069
+ const otherLen = other.length;
1070
+ if (otherLen == 0) {
1071
+ for (let i = 0; i < bucketLen; i++) {
1072
+ addMatch(bucket[i], path, state2);
1073
+ }
1074
+ } else if (bucketLen == 0) {
1075
+ for (let i = 0; i < otherLen; i++) {
1076
+ addIfTokenMatch(other[i], path, state2);
1077
+ }
1078
+ } else {
1079
+ const cands = [];
1080
+ for (let i = 0; i < bucketLen; i++) cands.push(bucket[i]);
1081
+ for (let i = 0; i < otherLen; i++) cands.push(other[i]);
1082
+ cands.sort((a, b) => a.seq - b.seq);
1083
+ for (let i = 0; i < cands.length; i++) {
1084
+ addIfTokenMatch(cands[i], path, state2);
1023
1085
  }
1024
1086
  }
1025
1087
  },
@@ -1029,16 +1091,17 @@ function createQuerier() {
1029
1091
  for (let i = 0; i < childAtDepthPlusOne.length; i++) {
1030
1092
  addPrimitiveAttributeIfMatch(childAtDepthPlusOne[i], path);
1031
1093
  }
1032
- for (let i = 0; i < state2.descendant.length; i++) {
1033
- const fnodes = state2.descendant[i];
1034
- for (let j = 0; j < fnodes.length; j++) {
1035
- addPrimitiveAttributeIfMatch(fnodes[j], path);
1036
- }
1094
+ for (let i = 0; i < state2.descendantAttr.length; i++) {
1095
+ addPrimitiveAttributeIfMatch(state2.descendantAttr[i], path);
1037
1096
  }
1038
1097
  const matchesAtDepth = state2.matches[state2.depth];
1039
1098
  for (let i = 0; i < matchesAtDepth.length; i++) {
1040
1099
  addResultIfTokenMatch(matchesAtDepth[i][0], matchesAtDepth[i][1], state2);
1041
1100
  }
1101
+ const leavingDescendants = state2.descendant[state2.descendant.length - 1];
1102
+ for (let i = 0; i < leavingDescendants.length; i++) {
1103
+ deactivateDescendant(leavingDescendants[i], state2);
1104
+ }
1042
1105
  state2.depth--;
1043
1106
  state2.child.pop();
1044
1107
  state2.descendant.pop();
@@ -1296,7 +1359,7 @@ function createTraverser() {
1296
1359
  registerBindings(stack, nodePath.scopeId, nodePath.functionScopeId);
1297
1360
  }
1298
1361
  const stateTyped = state;
1299
- const hasDescendantQueries = stateTyped.descendant && stateTyped.descendant.some((arr) => arr.length > 0);
1362
+ const hasDescendantQueries = stateTyped.descendantActiveCount > 0;
1300
1363
  const hasChildQueriesAtNextDepth = stateTyped.child && stateTyped.child[stateTyped.depth + 1] && stateTyped.child[stateTyped.depth + 1].length > 0;
1301
1364
  if (!hasDescendantQueries && !hasChildQueriesAtNextDepth) {
1302
1365
  return;
@@ -1304,17 +1367,18 @@ function createTraverser() {
1304
1367
  for (let keyIdx = 0; keyIdx < keys.length; keyIdx++) {
1305
1368
  const key = keys[keyIdx];
1306
1369
  const childNodes = node[key];
1307
- const children = Array.isArray(childNodes) ? childNodes : childNodes ? [childNodes] : [];
1308
- const nodePaths = [];
1309
- for (let i = 0; i < children.length; i++) {
1310
- const child = children[i];
1311
- if (isNode(child)) {
1312
- const childPath = createNodePath(child, Array.isArray(childNodes) ? i : key, key, nodePath.scopeId, nodePath.functionScopeId, nodePath);
1313
- nodePaths.push(childPath);
1370
+ if (childNodes == void 0) continue;
1371
+ if (Array.isArray(childNodes)) {
1372
+ for (let i = 0; i < childNodes.length; i++) {
1373
+ const child = childNodes[i];
1374
+ if (!isNode(child)) continue;
1375
+ const childPath = createNodePath(child, i, key, nodePath.scopeId, nodePath.functionScopeId, nodePath);
1376
+ visitor.enter(childPath, state);
1377
+ traverseInner(childPath.node, visitor, nodePath.scopeId, nodePath.functionScopeId, state, childPath);
1378
+ visitor.exit(childPath, state);
1314
1379
  }
1315
- }
1316
- for (let i = 0; i < nodePaths.length; i++) {
1317
- const childPath = nodePaths[i];
1380
+ } else if (isNode(childNodes)) {
1381
+ const childPath = createNodePath(childNodes, key, key, nodePath.scopeId, nodePath.functionScopeId, nodePath);
1318
1382
  visitor.enter(childPath, state);
1319
1383
  traverseInner(childPath.node, visitor, nodePath.scopeId, nodePath.functionScopeId, state, childPath);
1320
1384
  visitor.exit(childPath, state);