8000 Rewrite ids generation without array · HowProgrammingWorks/Generator@3dae8c9 · GitHub
[go: up one dir, main page]

Skip to content < 8000 div data-target="react-partial.reactRoot">

Commit 3dae8c9

Browse files
Rewrite ids generation without array
1 parent 0222be2 commit 3dae8c9

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Exercises/1-ids.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
/* eslint-disable no-undef */
12
'use strict';
23

3-
const ids = function* () {
4-
const free = ['0'];
5-
const prepared = { has: false, value: '' };
4+
const BIG_ONE = BigInt(1);
5+
const BIG_ZERO = BigInt(0);
66

7-
while (true) {
8-
if (prepared.has) {
9-
prepared.has = false;
10-
yield prepared.value;
7+
const ids = function* () {
8+
for (let i = BIG_ONE; ; i++) {
9+
const base = BIG_ONE << (i << BIG_ONE);
10+
for (let j = BIG_ZERO; j < BIG_ONE << i; j++) {
11+
let value = base;
12+
for (let k = BIG_ZERO; k <= Math.log2(Number(j)); k++) {
13+
if (j / (BIG_ONE << k) & BIG_ONE) {
14+
value |= BIG_ONE << (k << BIG_ONE) + BIG_ONE;
15+
}
16+
}
17+
yield value.toString(2);
1118
}
12-
const nextFree = free.shift();
13-
free.push('01' + nextFree);
14-
free.push('00' + nextFree);
15-
prepared.value = '11' + nextFree;
16-
prepared.has = true;
17-
yield '10' + nextFree;
1819
}
1920
};
2021

0 commit comments

Comments
 (0)
0