import type { CircularQueue } from './types' ; export * from './types' ; param MAX_SIZE returns export function createCircularQueue < T > ( ) { let _MAX_SIZE = 0 ; let _size = 0 ; const _queue = [ ] ; let _startIndex = 0 ; let _endIndex = 0 ; return { init , front , end , dequeue , enqueue , get , set , isValidIndex , size , isEmpty , } ; function init ( MAX_SIZE ) { _MAX_SIZE = Math . max ( 2 , MAX_SIZE ) ; if ( _queue . length < MAX_SIZE ) _queue . length = _MAX_SIZE ; _size = 0 ; _startIndex = 0 ; _endIndex = 0 ; } function front ( ) { return _size === 0 ? undefined : _queue [ _startIndex ] ; } function end ( ) { if ( _size === 0 ) return undefined ; const index = _endIndex === 0 ? _MAX_SIZE - 1 : _endIndex - 1 ; return _queue [ index ] ; } function dequeue ( ) { if ( _size === 0 ) return undefined ; const element = _queue [ _startIndex ] ; _size -= 1 ; _startIndex += 1 ; if ( _startIndex === _MAX_SIZE ) _startIndex = 0 ; return element ; } function enqueue ( element ) { const index = _endIndex ; _queue [ index ] = element ; _endIndex += 1 ; if ( _endIndex === _MAX_SIZE ) _endIndex = 0 ; if ( _size < _MAX_SIZE ) _size += 1 ; else _startIndex = _endIndex ; return index ; } function get ( idx , strickCheck = true ) { if ( strickCheck && ! isValidIndex ( idx ) ) return undefined ; return _queue [ idx ] ; } function set ( idx , element ) { if ( isValidIndex ( idx ) ) { _queue [ idx ] = element ; return true ; } return false ; } function isValidIndex ( idx ) { if ( _size === 0 ) return false ; if ( idx < 0 || idx >= _MAX_SIZE ) return false ; if ( _startIndex < _endIndex ) return idx >= _startIndex && idx < _endIndex ; return idx >= _startIndex || idx < _endIndex ; } function size ( ) { return _size ; } function isEmpty ( ) { return _size === 0 ; } }