Skip to content

Instantly share code, notes, and snippets.

@pomle
Last active October 11, 2016 15:34
Show Gist options
  • Save pomle/f931036d3a4e31f4c61d13d99851f849 to your computer and use it in GitHub Desktop.
Save pomle/f931036d3a4e31f4c61d13d99851f849 to your computer and use it in GitHub Desktop.
let map;
function setup(phone1, phone2) {
map[phone1] = phone2;
map[phone2] = phone1;
}
function teardown(phone1, phone2) {
delete map[phone1];
delete map[phone2];
}
function print(map) {
println('======');
Object.keys(map).forEach(k => {
const v = map[k];
println(`${k} >> ${v}`);
});
}
function println(str) {
process.stdout.write(str + '\n');
}
map = Object.create(null);
setup('A', 'B');
print(map);
/* A and B can reach each other */
setup('A', 'C');
print(map);
/* A can reach C, (ok)
B can reach A, (bad)
C can reach A, (ok)
but A can not reach B (orphan mapping) */
teardown('A', 'B');
print(map);
/* C can reach A only, and is now only orphan mapping. */
@pomle
Copy link
Author

pomle commented Oct 11, 2016

A >> B
B >> A

A >> C
B >> A
C >> A

C >> A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment