Skip to content

Commit 234a264

Browse files
committed
Store excluded arrangements in state
This optimization was removed a long time ago to simply the node data model. Now that nodes can have mutable state again (43b0ab6), it can be restored.
1 parent e6d6a33 commit 234a264

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

β€Žlib/parse/grammar.jsβ€Ž

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,17 @@ function* children(parent, alternative) {
322322
* @param {object} node
323323
* @yields {object}
324324
*/
325-
function* alternatives({ children, definition: { type, value: set }, value }) {
325+
function* alternatives({ children, definition: { type, value: set }, state, value }) {
326326
if (type === '&&' || type === '||') {
327-
const index = 0 < children.length
328-
? getPermutationIndex(set, children.map(node => node.definition))
329-
: 0
330-
yield* createPermutationIterator(set, type === '&&', index)
327+
if (children.length === 0) {
328+
const excluded = []
329+
state.extended.set('excluded', excluded)
330+
yield* createPermutationIterator(set, type === '&&', excluded)
331+
} else {
332+
const index = getPermutationIndex(set, children.map(node => node.definition))
333+
const excluded = state.extended.get('excluded')
334+
yield* createPermutationIterator(set, type === '&&', excluded, index)
335+
}
331336
} else if (type === '|') {
332337
if (0 < children.length) {
333338
set = set.slice(set.indexOf(children[0].definition))

β€Žlib/parse/permutation.jsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ function findExcludedPermutationPattern(excluded, permutation) {
113113
/**
114114
* @param {object[]} sequence
115115
* @param {boolean} [full]
116+
* @param {object[][]} [excluded]
116117
* @param {number} [index]
117118
* @yields {object}
118119
*/
119-
function* createPermutationIterator(sequence, full, index = 0) {
120+
function* createPermutationIterator(sequence, full, excluded = [], index = 0) {
120121
const n = sequence.length
121122
const lastIndex = full ? factorial(n) : getAllPermutationsLength(n)
122-
const excluded = []
123123
let permutation = 0 < index ? getNthPermutation(sequence, index) : sequence
124124
let valid = yield permutation
125125
while (true) {

0 commit comments

Comments
 (0)