File tree 1 file changed +21
-14
lines changed
1 file changed +21
-14
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- const ids = function * ( ) {
4
- const free = [ '0' ] ;
5
- const prepared = { has : false , value : '' } ;
3
+ const digits = [ '00' , '10' , '1' ] ;
4
+ const LEADING_ONE = 2 ;
5
+
6
+ const inc = num => {
7
+ let i = num . length - 1 ;
8
+ let add = 1 ;
9
+ while ( i > 0 && add === 1 ) {
10
+ const newAdd = num [ i ] & add ;
11
+ num [ i ] ^= add ;
12
+ add = newAdd ;
13
+ i -- ;
14
+ }
6
15
7
- while ( true ) {
8
- if ( prepared . has ) {
9
- prepared . has = false ;
10
- yield prepared . value ;
11
- }
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 ;
16
+ if ( i === 0 && add === 1 ) {
17
+ num . push ( 0 ) ;
18
+ }
19
+ } ;
20
+
21
+ const ids = function * ( ) {
22
+ const num = [ LEADING_ONE , 0 ] ;
23
+ for ( ; ; inc ( num ) ) {
24
+ yield num . map ( d => digits [ d ] ) . join ( '' ) ;
18
25
}
19
26
} ;
20
27
You can’t perform that action at this time.
0 commit comments