port module entitas . utils { "use strict" ; /**
     * Collection type a bit like ArrayList but does not preserve the order of its
     * entities, speedwise it is very good, especially suited for games.
     */ export class Bag < E > extends Array implements ImmutableBag < E > { public size_ : number = 0 ; constructor param capacity constructor ( capacity = 64 ) { super ( ) ; this . length = capacity ; } param index return { Object } removeAt ( index ) { const e = this [ index ] ; this [ index ] = this [ -- this . size_ ] ; this [ this . size_ ] = null ; return e ; } param e return { boolean } remove ( e ) { let i ; let e2 ; const size = this . size_ ; for ( i = 0 ; i < size ; i ++ ) { e2 = this [ i ] ; if ( e == e2 ) { this [ i ] = this [ -- this . size_ ] ; this [ this . size_ ] = null ; return true ; } } return false ; } return { Object } removeLast ( ) { if ( this . size_ > 0 ) { const e = this [ -- this . size_ ] ; this [ this . size_ ] = null ; return e ; } return null ; } param e return { boolean } contains ( e ) { let i ; let size ; for ( i = 0 , size = this . size_ ; size > i ; i ++ ) { if ( e === this [ i ] ) { return true ; } } return false ; } param bag return { boolean } removeAll ( bag ) { let modified = false ; let i ; let j ; let l ; let e1 ; let e2 ; for ( i = 0 , l = bag . size ( ) ; i < l ; i ++ ) { e1 = bag . get ( i ) ; for ( j = 0 ; j < this . size_ ; j ++ ) { e2 = this [ j ] ; if ( e1 === e2 ) { this . removeAt ( j ) ; j -- ; modified = true ; break ; } } } return modified ; } param index return { Object } get ( index ) { if ( index >= this . length ) { throw new Error ( 'ArrayIndexOutOfBoundsException' ) ; } return this [ index ] ; } param index return { Object } safeGet ( index ) { if ( index >= this . length ) { this . grow ( ( index * 7 ) / 4 + 1 ) ; } return this [ index ] ; } return { number } size ( ) { return this . size_ ; } return { number } getCapacity ( ) { return this . length ; } param index return { boolean } isIndexWithinBounds ( index ) { return index < this . getCapacity ( ) ; } return { boolean } isEmpty ( ) { return this . size_ == 0 ; } param e add ( e ) { if ( this . size_ === this . length ) { this . grow ( ) ; } this [ this . size_ ++ ] = e ; } param index param e set ( index , e ) { if ( index >= this . length ) { this . grow ( index * 2 ) ; } this . size_ = index + 1 ; this [ index ] = e ; } grow ( newCapacity = ~ ~ ( ( this . length * 3 ) / 2 ) + 1 ) { this . length = ~ ~ newCapacity ; } ensureCapacity ( index ) { if ( index >= this . length ) { this . grow ( index * 2 ) ; } } /**
         * Removes all of the elements from this bag. The bag will be empty after
         * this call returns.
         */ clear ( ) { let i ; let size ; for ( i = 0 , size = this . size_ ; i < size ; i ++ ) { this [ i ] = null ; } this . size_ = 0 ; } param items addAll ( items ) { let i ; for ( i = 0 ; items . size ( ) > i ; i ++ ) { this . add ( items . get ( i ) ) ; } } } }