Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
righ committed Aug 26, 2024
1 parent 5f4511b commit 1b45261
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
29 changes: 16 additions & 13 deletions typescript/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,46 +99,49 @@ export class Row extends Map<Scalar, number> implements RowType {
}

export class Controller {
private factors: FactorsType;
public factorLength: number;
public factorIsArray: Boolean;
private factors: FactorsType;

public options: OptionsType;
public mappings: MappingTypes;
public incomplete: IncompleteType;

public rejected: Set<Scalar> = new Set();
public row: Row;

constructor(factors: FactorsType, options: OptionsType) {
this.factors = factors;
this.options = options;
let { sorter = hash, seed = "" } = options;

let {
length = 2,
sorter = hash,
seed = "",
} = options;

this.mappings = serialize(factors);
this.incomplete = makeIncomplete(this.mappings, length, sorter, seed); // {"1,2": [1,2], "3,4": [3,4]}
this.mappings = serialize(factors);
this.incomplete = makeIncomplete(this.mappings, this.pairwiseCount, sorter, seed); // {"1,2": [1,2], "3,4": [3,4]}
this.row = new Row([]);
this.factorLength = len(factors);
this.factorIsArray = factors instanceof Array;

// Delete initial pairs that do not satisfy preFilter
for (let [pairKey, pair] of this.incomplete.entries()) {
const cand = getCandidate(pair, this.mappings.parents);
for (const [pairKey, pair] of this.incomplete.entries()) {
const cand = this.getCandidate(pair);
const storable = this.storable(cand);
if (storable == null) {
this.incomplete.delete(pairKey);
}
}
}

get pairwiseCount() {
return this.options.length || 2;
}

set(pair: PairType) {
for (let [key, value] of this.getCandidate(pair)) {
this.row.set(key, value);
}
this.consume(pair);
//this.consume(pair);
for (let p of combinations([...this.row.values()], this.pairwiseCount)) {
this.consume(p);
}
}

consume(pair: PairType) {
Expand Down
11 changes: 5 additions & 6 deletions typescript/src/criteria/greedy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ const getNumRemovablePairs = (indexes: Set<number>, incomplete: IncompleteType,
};

export default function* (ctrl: Controller): Generator<PairType> {
const { incomplete } = ctrl;

while (true) {
let maxNumPairs: number | null = null;
let efficientPair: PairType | null = null;

for (let [pairKey, pair] of incomplete.entries()) {
for (const [pairKey, pair] of ctrl.incomplete.entries()) {
const rowSize = ctrl.row.size;
if (rowSize === 0) {
yield pair;
Expand All @@ -41,11 +39,12 @@ export default function* (ctrl: Controller): Generator<PairType> {
continue;
}
const storableAbs = Math.abs(storable);

const { length = 2, tolerance = 0 } = ctrl.options!;
const { tolerance = 0 } = ctrl.options!;

const numPairs = getNumRemovablePairs(
new Set([... ctrl.row.values(), ...pair]), incomplete, length
new Set([...ctrl.row.values(), ...pair]),
ctrl.incomplete,
ctrl.pairwiseCount,
);

if (numPairs + tolerance > rowSize * storableAbs) {
Expand Down
4 changes: 0 additions & 4 deletions typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const makeAsync = function* <T extends FactorsType>(
options: OptionsType = {}
) {
const {
length = 2,
criterion = greedy,
postFilter,
} = options;
Expand All @@ -28,9 +27,6 @@ const makeAsync = function* <T extends FactorsType>(
break;
}
ctrl.set(pair);
for (let p of combinations([...ctrl.row.values()], length)) {
ctrl.consume(p);
}
}
try {
const complete = ctrl.close();
Expand Down

0 comments on commit 1b45261

Please sign in to comment.