1- /// <reference path="./functions.ts" />
2- namespace core {
1+ namespace collections {
32 export interface SortOptions < T > {
43 comparer : ( a : T , b : T ) => number ;
54 sort : "insertion" | "comparison" ;
@@ -43,16 +42,16 @@ namespace core {
4342 }
4443
4544 public has ( key : K ) {
46- return binarySearch ( this . _keys , key , identity , this . _comparer ) >= 0 ;
45+ return ts . binarySearch ( this . _keys , key , ts . identity , this . _comparer ) >= 0 ;
4746 }
4847
4948 public get ( key : K ) {
50- const index = binarySearch ( this . _keys , key , identity , this . _comparer ) ;
49+ const index = ts . binarySearch ( this . _keys , key , ts . identity , this . _comparer ) ;
5150 return index >= 0 ? this . _values [ index ] : undefined ;
5251 }
5352
5453 public set ( key : K , value : V ) {
55- const index = binarySearch ( this . _keys , key , identity , this . _comparer ) ;
54+ const index = ts . binarySearch ( this . _keys , key , ts . identity , this . _comparer ) ;
5655 if ( index >= 0 ) {
5756 this . _values [ index ] = value ;
5857 }
@@ -67,12 +66,12 @@ namespace core {
6766 }
6867
6968 public delete ( key : K ) {
70- const index = binarySearch ( this . _keys , key , identity , this . _comparer ) ;
69+ const index = ts . binarySearch ( this . _keys , key , ts . identity , this . _comparer ) ;
7170 if ( index >= 0 ) {
7271 this . writePreamble ( ) ;
73- removeAt ( this . _keys , index ) ;
74- removeAt ( this . _values , index ) ;
75- if ( this . _order ) removeAt ( this . _order , index ) ;
72+ ts . orderedRemoveItemAt ( this . _keys , index ) ;
73+ ts . orderedRemoveItemAt ( this . _values , index ) ;
74+ if ( this . _order ) ts . orderedRemoveItemAt ( this . _order , index ) ;
7675 this . writePostScript ( ) ;
7776 return true ;
7877 }
@@ -211,226 +210,6 @@ namespace core {
211210 }
212211 }
213212
214- export class SortedSet < T > {
215- private _comparer : ( a : T , b : T ) => number ;
216- private _values : T [ ] = [ ] ;
217- private _order : number [ ] | undefined ;
218- private _version = 0 ;
219- private _copyOnWrite = false ;
220-
221- constructor ( comparer : ( ( a : T , b : T ) => number ) | SortOptions < T > , iterable ?: Iterable < T > ) {
222- this . _comparer = typeof comparer === "object" ? comparer . comparer : comparer ;
223- this . _order = typeof comparer === "object" && comparer . sort === "insertion" ? [ ] : undefined ;
224-
225- if ( iterable ) {
226- const iterator = getIterator ( iterable ) ;
227- try {
228- for ( let i = nextResult ( iterator ) ; i ; i = nextResult ( iterator ) ) {
229- const value = i . value ;
230- this . add ( value ) ;
231- }
232- }
233- finally {
234- closeIterator ( iterator ) ;
235- }
236- }
237- }
238-
239- public get size ( ) {
240- return this . _values . length ;
241- }
242-
243- public get comparer ( ) {
244- return this . _comparer ;
245- }
246-
247- public get [ Symbol . toStringTag ] ( ) {
248- return "SortedSet" ;
249- }
250-
251- public has ( value : T ) {
252- return binarySearch ( this . _values , value , identity , this . _comparer ) >= 0 ;
253- }
254-
255- public add ( value : T ) {
256- const index = binarySearch ( this . _values , value , identity , this . _comparer ) ;
257- if ( index < 0 ) {
258- this . writePreamble ( ) ;
259- insertAt ( this . _values , ~ index , value ) ;
260- if ( this . _order ) insertAt ( this . _order , ~ index , this . _version ) ;
261- this . writePostScript ( ) ;
262- }
263- return this ;
264- }
265-
266- public delete ( value : T ) {
267- const index = binarySearch ( this . _values , value , identity , this . _comparer ) ;
268- if ( index >= 0 ) {
269- this . writePreamble ( ) ;
270- removeAt ( this . _values , index ) ;
271- if ( this . _order ) removeAt ( this . _order , index ) ;
272- this . writePostScript ( ) ;
273- return true ;
274- }
275- return false ;
276- }
277-
278- public clear ( ) {
279- if ( this . size > 0 ) {
280- this . writePreamble ( ) ;
281- this . _values . length = 0 ;
282- if ( this . _order ) this . _order . length = 0 ;
283- this . writePostScript ( ) ;
284- }
285- }
286-
287- public forEach ( callback : ( value : T , key : T , collection : this) => void , thisArg ?: any ) {
288- const values = this . _values ;
289- const indices = this . getIterationOrder ( ) ;
290- const version = this . _version ;
291- this . _copyOnWrite = true ;
292- try {
293- if ( indices ) {
294- for ( const i of indices ) {
295- callback . call ( thisArg , values [ i ] , values [ i ] , this ) ;
296- }
297- }
298- else {
299- for ( const value of values ) {
300- callback . call ( thisArg , value , value , this ) ;
301- }
302- }
303- }
304- finally {
305- if ( version === this . _version ) {
306- this . _copyOnWrite = false ;
307- }
308- }
309- }
310-
311- public keys ( ) {
312- return this . values ( ) ;
313- }
314-
315- public * values ( ) {
316- const values = this . _values ;
317- const indices = this . getIterationOrder ( ) ;
318- const version = this . _version ;
319- this . _copyOnWrite = true ;
320- try {
321- if ( indices ) {
322- for ( const i of indices ) {
323- yield values [ i ] ;
324- }
325- }
326- else {
327- for ( const value of values ) {
328- yield value ;
329- }
330- }
331- }
332- finally {
333- if ( version === this . _version ) {
334- this . _copyOnWrite = false ;
335- }
336- }
337- }
338-
339- public * entries ( ) {
340- const values = this . _values ;
341- const indices = this . getIterationOrder ( ) ;
342- const version = this . _version ;
343- this . _copyOnWrite = true ;
344- try {
345- if ( indices ) {
346- for ( const i of indices ) {
347- yield [ values [ i ] , values [ i ] ] as [ T , T ] ;
348- }
349- }
350- else {
351- for ( const value of values ) {
352- yield [ value , value ] as [ T , T ] ;
353- }
354- }
355- }
356- finally {
357- if ( version === this . _version ) {
358- this . _copyOnWrite = false ;
359- }
360- }
361- }
362-
363- public [ Symbol . iterator ] ( ) {
364- return this . values ( ) ;
365- }
366-
367- private writePreamble ( ) {
368- if ( this . _copyOnWrite ) {
369- this . _values = this . _values . slice ( ) ;
370- if ( this . _order ) this . _order = this . _order . slice ( ) ;
371- this . _copyOnWrite = false ;
372- }
373- }
374-
375- private writePostScript ( ) {
376- this . _version ++ ;
377- }
378-
379- private getIterationOrder ( ) {
380- if ( this . _order ) {
381- const order = this . _order ;
382- return this . _order
383- . map ( ( _ , i ) => i )
384- . sort ( ( x , y ) => order [ x ] - order [ y ] ) ;
385- }
386- return undefined ;
387- }
388- }
389-
390- export function binarySearch < T , U > ( array : ReadonlyArray < T > , value : T , keySelector : ( v : T ) => U , keyComparer : ( a : U , b : U ) => number , offset ?: number ) : number {
391- if ( ! array || array . length === 0 ) {
392- return - 1 ;
393- }
394-
395- let low = offset || 0 ;
396- let high = array . length - 1 ;
397- const key = keySelector ( value ) ;
398- while ( low <= high ) {
399- const middle = low + ( ( high - low ) >> 1 ) ;
400- const midKey = keySelector ( array [ middle ] ) ;
401- const result = keyComparer ( midKey , key ) ;
402- if ( result < 0 ) {
403- low = middle + 1 ;
404- }
405- else if ( result > 0 ) {
406- high = middle - 1 ;
407- }
408- else {
409- return middle ;
410- }
411- }
412-
413- return ~ low ;
414- }
415-
416- export function removeAt < T > ( array : T [ ] , index : number ) : void {
417- if ( index < 0 || index >= array . length ) {
418- return ;
419- }
420- else if ( index === 0 ) {
421- array . shift ( ) ;
422- }
423- else if ( index === array . length - 1 ) {
424- array . pop ( ) ;
425- }
426- else {
427- for ( let i = index ; i < array . length - 1 ; i ++ ) {
428- array [ i ] = array [ i + 1 ] ;
429- }
430- array . length -- ;
431- }
432- }
433-
434213 export function insertAt < T > ( array : T [ ] , index : number , value : T ) : void {
435214 if ( index === 0 ) {
436215 array . unshift ( value ) ;
0 commit comments