1-
1+ import greedy from "./criteria/greedy" ;
22import hash from "./sorters/hash" ;
33import {
44 range ,
@@ -58,13 +58,15 @@ export class Controller<T extends FactorsType> {
5858 private parents : ParentsType = new Map ( ) ;
5959 private indices : IndicesType = new Map ( ) ;
6060 public incomplete : PairByKeyType = new Map ( ) ;
61+ private numAllChunks : number = 0 ;
6162
6263 private rejected : Set < ScalarType > = new Set ( ) ;
6364 public row : Row ;
6465
65- constructor ( public factors : FactorsType , public options : OptionsType < T > ) {
66+ constructor ( public factors : FactorsType , public options : OptionsType < T > = { } ) {
6667 this . serialize ( factors ) ;
6768 this . setIncomplete ( ) ;
69+ this . numAllChunks = this . incomplete . size ;
6870 this . row = new Row ( [ ] ) ;
6971 this . factorLength = len ( factors ) ;
7072 this . factorIsArray = factors instanceof Array ;
@@ -112,7 +114,7 @@ export class Controller<T extends FactorsType> {
112114 }
113115 }
114116
115- setPair ( pair : PairType ) {
117+ private setPair ( pair : PairType ) {
116118 for ( let [ key , value ] of this . getCandidate ( pair ) ) {
117119 this . row . set ( key , value ) ;
118120 }
@@ -122,20 +124,20 @@ export class Controller<T extends FactorsType> {
122124 }
123125 }
124126
125- consume ( pair : PairType ) {
127+ public consume ( pair : PairType ) {
126128 const pairKey = unique ( pair ) ;
127129 const deleted = this . incomplete . delete ( pairKey ) ;
128130 if ( deleted ) {
129131 this . row . consumed . set ( pairKey , pair ) ;
130132 }
131133 }
132134
133- getCandidate ( pair : PairType ) {
135+ public getCandidate ( pair : PairType ) {
134136 return getCandidate ( pair , this . parents ) ;
135137 }
136138
137139 // Returns a negative value if it is unknown if it can be stored.
138- storable ( candidate : CandidateType ) {
140+ public storable ( candidate : CandidateType ) {
139141 let num = 0 ;
140142 for ( let [ key , el ] of candidate ) {
141143 let existing : number | undefined = this . row . get ( key ) ;
@@ -165,11 +167,11 @@ export class Controller<T extends FactorsType> {
165167 return num ;
166168 }
167169
168- isFilled ( row : Row ) : boolean {
170+ public isFilled ( row : Row ) : boolean {
169171 return row . size === this . factorLength ;
170172 }
171173
172- toMap ( row : Row ) : Map < ScalarType , number [ ] > {
174+ private toMap ( row : Row ) : Map < ScalarType , number [ ] > {
173175 const result : Map < ScalarType , number [ ] > = new Map ( ) ;
174176 for ( let [ key , serial ] of row . entries ( ) ) {
175177 const index = this . indices . get ( serial ) as number ;
@@ -180,35 +182,35 @@ export class Controller<T extends FactorsType> {
180182 return result ;
181183 }
182184
183- toProxy ( row : Row ) {
185+ private toProxy ( row : Row ) {
184186 const obj : DictType = { } ;
185187 for ( let [ key , value ] of this . toMap ( row ) . entries ( ) ) {
186188 obj [ key ] = value ;
187189 }
188190 return new Proxy ( obj , proxyHandler ) as SuggestRowType < T > ;
189191 }
190192
191- toObject ( row : Row ) {
193+ private toObject ( row : Row ) {
192194 const obj : DictType = { } ;
193195 for ( let [ key , value ] of this . toMap ( row ) . entries ( ) ) {
194196 obj [ key ] = value ;
195197 }
196198 return obj as SuggestRowType < T > ;
197199 }
198200
199- reset ( ) {
201+ private reset ( ) {
200202 this . row . consumed . forEach ( ( pair , pairKey ) => {
201203 this . incomplete . set ( pairKey , pair ) ;
202204 } ) ;
203205 this . row = new Row ( [ ] ) ;
204206 }
205207
206- discard ( ) {
208+ private discard ( ) {
207209 this . rejected . add ( this . row . getPairKey ( ) ) ;
208210 this . row = new Row ( [ ] ) ;
209211 }
210212
211- restore ( ) {
213+ private restore ( ) {
212214 const row = this . row ;
213215 this . row = new Row ( [ ] ) ;
214216 if ( this . factorIsArray ) {
@@ -220,7 +222,7 @@ export class Controller<T extends FactorsType> {
220222 return this . toObject ( row ) ;
221223 }
222224
223- close ( ) {
225+ private close ( ) {
224226 const trier = new Row ( [ ...this . row . entries ( ) ] ) ;
225227 const kvs = getItems ( this . serials ) ;
226228 for ( let [ k , vs ] of kvs ) {
@@ -274,4 +276,35 @@ export class Controller<T extends FactorsType> {
274276 throw e ;
275277 }
276278 }
279+
280+ get progress ( ) {
281+ return 1 - this . incomplete . size / this . numAllChunks ;
282+ }
283+
284+ public * makeAsync < T extends FactorsType > ( ) : Generator < SuggestRowType < T > , void , unknown > {
285+ const { criterion = greedy , postFilter} = this . options ;
286+ do {
287+ for ( let pair of criterion ( this ) ) {
288+ if ( this . isFilled ( this . row ) ) {
289+ break ;
290+ }
291+ this . setPair ( pair ) ;
292+ }
293+ try {
294+ const complete = this . close ( ) ;
295+ if ( complete ) {
296+ if ( ! postFilter || postFilter ( this . toObject ( this . row ) ) ) {
297+ yield this . restore ( ) as SuggestRowType < T > ;
298+ } else {
299+ this . discard ( ) ;
300+ }
301+ }
302+ } catch ( e ) {
303+ if ( e instanceof NeverMatch ) {
304+ break ;
305+ }
306+ throw e ;
307+ }
308+ } while ( this . incomplete . size ) ;
309+ }
277310}
0 commit comments