Skip to content

Commit

Permalink
Sort synapses in Creature and CompactUtils (#299)
Browse files Browse the repository at this point in the history
Co-authored-by: Nigel Leck <[email protected]>
  • Loading branch information
nleck and nigelleck authored Mar 1, 2024
1 parent df5fe9c commit 36a72b4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,16 @@ export class Creature implements CreatureInternal {
});

this.synapses = connections;

/* Make sure the synapses are sorted */
this.synapses.sort((a, b) => {
if (a.from === b.from) {
return a.to - b.to;
} else {
return a.from - b.from;
}
});

this.clearCache();

let nodeRemoved = true;
Expand Down
8 changes: 8 additions & 0 deletions src/compact/CompactUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export function createConstantOne(creature: Creature, count: number) {
}
}
}

creature.synapses.sort((a, b) => {
if (a.from === b.from) {
return a.to - b.to;
} else {
return a.from - b.from;
}
});
}

creature.clearCache();
Expand Down
1 change: 1 addition & 0 deletions test/Compact/CompactKeepOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Deno.test("CompactKeepOrder", async () => {
if (!compacted) {
fail("Should have compacted");
}
compacted.validate();
Deno.writeTextFileSync(
`${traceDir}/2-compacted.json`,
JSON.stringify(compacted.exportJSON(), null, 2),
Expand Down

0 comments on commit 36a72b4

Please sign in to comment.