Changeset: Use Maps to simplify attribute processing

This commit is contained in:
Richard Hansen 2021-10-12 02:00:56 -04:00
parent 9401ae876b
commit b62534a6b2

View file

@ -1129,32 +1129,23 @@ exports.composeAttributes = (att1, att2, resultIsMutation, pool) => {
return att2; return att2;
} }
if (!att2) return att1; if (!att2) return att1;
const atts = []; const atts = new Map();
att1.replace(/\*([0-9a-z]+)/g, (_, a) => { att1.replace(/\*([0-9a-z]+)/g, (_, a) => {
atts.push(pool.getAttrib(exports.parseNum(a))); const [key, val] = pool.getAttrib(exports.parseNum(a));
atts.set(key, val);
return ''; return '';
}); });
att2.replace(/\*([0-9a-z]+)/g, (_, a) => { att2.replace(/\*([0-9a-z]+)/g, (_, a) => {
const pair = pool.getAttrib(exports.parseNum(a)); const [key, val] = pool.getAttrib(exports.parseNum(a));
let found = false; if (val || resultIsMutation) {
for (let i = 0; i < atts.length; i++) { atts.set(key, val);
const oldPair = atts[i]; } else {
if (oldPair[0] !== pair[0]) continue; atts.delete(key);
if (pair[1] || resultIsMutation) {
oldPair[1] = pair[1];
} else {
atts.splice(i, 1);
}
found = true;
break;
}
if ((!found) && (pair[1] || resultIsMutation)) {
atts.push(pair);
} }
return ''; return '';
}); });
const buf = exports.stringAssembler(); const buf = exports.stringAssembler();
for (const att of sortAttribs(atts)) { for (const att of sortAttribs([...atts])) {
buf.append('*'); buf.append('*');
buf.append(exports.numToString(pool.putAttrib(att))); buf.append(exports.numToString(pool.putAttrib(att)));
} }
@ -2265,22 +2256,15 @@ const followAttributes = (att1, att2, pool) => {
// to produce the merged set. // to produce the merged set.
if ((!att2) || (!pool)) return ''; if ((!att2) || (!pool)) return '';
if (!att1) return att2; if (!att1) return att2;
const atts = []; const atts = new Map();
att2.replace(/\*([0-9a-z]+)/g, (_, a) => { att2.replace(/\*([0-9a-z]+)/g, (_, a) => {
atts.push(pool.getAttrib(exports.parseNum(a))); const [key, val] = pool.getAttrib(exports.parseNum(a));
atts.set(key, val);
return ''; return '';
}); });
att1.replace(/\*([0-9a-z]+)/g, (_, a) => { att1.replace(/\*([0-9a-z]+)/g, (_, a) => {
const pair1 = pool.getAttrib(exports.parseNum(a)); const [key, val] = pool.getAttrib(exports.parseNum(a));
for (let i = 0; i < atts.length; i++) { if (atts.has(key) && val <= atts.get(key)) atts.delete(key);
const pair2 = atts[i];
if (pair1[0] !== pair2[0]) continue;
if (pair1[1] <= pair2[1]) {
// winner of merge is pair1, delete this attribute
atts.splice(i, 1);
}
break;
}
return ''; return '';
}); });
// we've only removed attributes, so they're already sorted // we've only removed attributes, so they're already sorted