Skip to content

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy