private int findFreeSlot ( ) { if ( tc . isEntryEnabled ( ) ) SibTr . entry ( this , tc , "findFreeSlot" ) ; boolean foundFreeSlot = false ; int index = lowestPossibleFree ; int largestIndex = Math . min ( highWaterMark , table . length - 1 ) ; while ( ( ! foundFreeSlot ) && ( index <= largestIndex ) ) { foundFreeSlot = ( table [ index ] == null ) ; ++ index ; } int freeSlot = 0 ; if ( foundFreeSlot ) { freeSlot = index - 1 ; // If we have already scanned more than half the table then // we might as well spend a little more time and see if we can // move the high water mark any lower... if ( ( index * 2 ) > largestIndex ) { boolean quit = false ; int lowest = index ; index = largestIndex ; while ( ! quit && ( index >= lowest ) ) { if ( table [ index ] == null ) highWaterMark = index ; else quit = true ; -- index ; } } } if ( tc . isEntryEnabled ( ) ) SibTr . exit ( this , tc , "findFreeSlot" , "" + freeSlot ) ; return freeSlot ; }
Helper method which attempts to locate a free slot in the table . The algorithm used is to scan from the lowest possible free value looking for an entry with a null value . If more than half the table is scanned before a free slot is found then the code also attempts to move the high watermark back towards the beginning of the table by scanning backwards looking for the last empty slot .