/** * Moves the contents of matrix down by the number of purged rows and resizes the matrix accordingly. The * capacity of the matrix should be size * resize_factor. Caller must hold the lock. */ @SuppressWarnings("unchecked") @GuardedBy("lock") protected void _compact() { // This is the range we need to copy into the new matrix (including from and to) int from=computeRow(low), to=computeRow(hr); int range=to - from +1; // e.g. from=3, to=5, new_size has to be [3 .. 5] (=3) int new_size=(int)Math.max( (double)range * resize_factor, (double) range +1 ); new_size=Math.max(new_size, num_rows); // don't fall below the initial size defined if(new_size < matrix.length) { T[][] new_matrix=(T[][])new Object[new_size][]; System.arraycopy(matrix, from, new_matrix, 0, range); matrix=new_matrix; offset+=from * elements_per_row; num_compactions++; } }
/** * Returns an element at seqno * @param seqno * @return */ public T get(long seqno) { lock.lock(); try { if(seqno - low <= 0 || seqno - hr > 0) return null; int row_index=computeRow(seqno); if(row_index < 0 || row_index >= matrix.length) return null; T[] row=matrix[row_index]; if(row == null) return null; int index=computeIndex(seqno); return index >= 0? row[index] : null; } finally { lock.unlock(); } }
/** * To be used only for testing; doesn't do any index or sanity checks * @param seqno * @return */ public T _get(long seqno) { lock.lock(); try { int row_index=computeRow(seqno); if(row_index < 0 || row_index >= matrix.length) return null; T[] row=matrix[row_index]; if(row == null) return null; int index=computeIndex(seqno); return index >= 0? row[index] : null; } finally { lock.unlock(); } }
/** Moves rows down the matrix, by removing purged rows. If resizing to accommodate seqno is still needed, computes * a new size. Then either moves existing rows down, or copies them into a new array (if resizing took place). * The lock must be held by the caller of resize(). */ @SuppressWarnings("unchecked") @GuardedBy("lock") protected void resize(long seqno) { int num_rows_to_purge=computeRow(low); int row_index=computeRow(seqno) - num_rows_to_purge; if(row_index < 0) return; int new_size=Math.max(row_index +1, matrix.length); if(new_size > matrix.length) { T[][] new_matrix=(T[][])new Object[new_size][]; System.arraycopy(matrix, num_rows_to_purge, new_matrix, 0, matrix.length - num_rows_to_purge); matrix=new_matrix; num_resizes++; } else if(num_rows_to_purge > 0) { move(num_rows_to_purge); } offset+=(num_rows_to_purge * elements_per_row); }
/** Removes the next non-null element and nulls the index if nullify=true */ public T remove(boolean nullify) { lock.lock(); try { int row_index=computeRow(hd+1); if(row_index < 0 || row_index >= matrix.length) return null; T[] row=matrix[row_index]; if(row == null) return null; int index=computeIndex(hd+1); if(index < 0) return null; T existing_element=row[index]; if(existing_element != null) { hd++; size=Math.max(size-1, 0); // cannot be < 0 (well that would be a bug, but let's have this 2nd line of defense !) if(nullify) { row[index]=null; if(hd - low > 0) low=hd; } } return existing_element; } finally { lock.unlock(); } }
int start_row=computeRow(low), end_row=computeRow(seqno); if(start_row < 0) start_row=0; if(end_row < 0)
return false; int row_index=computeRow(seqno); if(check_if_resize_needed && row_index >= matrix.length) { resize(seqno); row_index=computeRow(seqno);
lock.lock(); try { if(highest_seqno != -1 && computeRow(highest_seqno) >= matrix.length) resize(highest_seqno);
/** * Iterates over the matrix with range [from .. to] (including from and to), and calls * {@link Visitor#visit(long,Object,int,int)}. If the visit() method returns false, the iteration is terminated. * <p/> * This method must be called with the lock held * @param from The starting seqno * @param to The ending seqno, the range is [from .. to] including from and to * @param visitor An instance of Visitor */ @GuardedBy("lock") public void forEach(long from, long to, Visitor<T> visitor) { if(from - to > 0) // same as if(from > to), but prevents long overflow return; int row=computeRow(from), column=computeIndex(from); int distance=(int)(to - from +1); T[] current_row=row+1 > matrix.length? null : matrix[row]; for(int i=0; i < distance; i++) { T element=current_row == null? null : current_row[column]; if(!visitor.visit(from, element, row, column)) break; from++; if(++column >= elements_per_row) { column=0; row++; current_row=row+1 > matrix.length? null : matrix[row]; } } }
/** * Moves the contents of matrix down by the number of purged rows and resizes the matrix accordingly. The * capacity of the matrix should be size * resize_factor. Caller must hold the lock. */ @SuppressWarnings("unchecked") @GuardedBy("lock") protected void _compact() { // This is the range we need to copy into the new matrix (including from and to) int from=computeRow(low), to=computeRow(hr); int range=to - from +1; // e.g. from=3, to=5, new_size has to be [3 .. 5] (=3) int new_size=(int)Math.max( (double)range * resize_factor, (double) range +1 ); new_size=Math.max(new_size, num_rows); // don't fall below the initial size defined if(new_size < matrix.length) { T[][] new_matrix=(T[][])new Object[new_size][]; System.arraycopy(matrix, from, new_matrix, 0, range); matrix=new_matrix; offset+=from * elements_per_row; num_compactions++; } }
/** * To be used only for testing; doesn't do any index or sanity checks * @param seqno * @return */ public T _get(long seqno) { lock.lock(); try { int row_index=computeRow(seqno); if(row_index < 0 || row_index >= matrix.length) return null; T[] row=matrix[row_index]; if(row == null) return null; int index=computeIndex(seqno); return index >= 0? row[index] : null; } finally { lock.unlock(); } }
/** Moves rows down the matrix, by removing purged rows. If resizing to accommodate seqno is still needed, computes * a new size. Then either moves existing rows down, or copies them into a new array (if resizing took place). * The lock must be held by the caller of resize(). */ @SuppressWarnings("unchecked") @GuardedBy("lock") protected void resize(long seqno) { int num_rows_to_purge=computeRow(low); int row_index=computeRow(seqno) - num_rows_to_purge; if(row_index < 0) return; int new_size=Math.max(row_index +1, matrix.length); if(new_size > matrix.length) { T[][] new_matrix=(T[][])new Object[new_size][]; System.arraycopy(matrix, num_rows_to_purge, new_matrix, 0, matrix.length - num_rows_to_purge); matrix=new_matrix; num_resizes++; } else if(num_rows_to_purge > 0) { move(num_rows_to_purge); } offset+=(num_rows_to_purge * elements_per_row); }
/** * Returns an element at seqno * @param seqno * @return */ public T get(long seqno) { lock.lock(); try { if(seqno - low <= 0 || seqno - hr > 0) return null; int row_index=computeRow(seqno); if(row_index < 0 || row_index >= matrix.length) return null; T[] row=matrix[row_index]; if(row == null) return null; int index=computeIndex(seqno); return index >= 0? row[index] : null; } finally { lock.unlock(); } }
/** Removes the next non-null element and nulls the index if nullify=true */ public T remove(boolean nullify) { lock.lock(); try { int row_index=computeRow(hd+1); if(row_index < 0 || row_index >= matrix.length) return null; T[] row=matrix[row_index]; if(row == null) return null; int index=computeIndex(hd+1); if(index < 0) return null; T existing_element=row[index]; if(existing_element != null) { hd++; size=Math.max(size-1, 0); // cannot be < 0 (well that would be a bug, but let's have this 2nd line of defense !) if(nullify) { row[index]=null; if(hd - low > 0) low=hd; } } return existing_element; } finally { lock.unlock(); } }
int start_row=computeRow(low), end_row=computeRow(seqno); if(start_row < 0) start_row=0; if(end_row < 0)
return false; int row_index=computeRow(seqno); if(check_if_resize_needed && row_index >= matrix.length) { resize(seqno); row_index=computeRow(seqno);
lock.lock(); try { if(highest_seqno != -1 && computeRow(highest_seqno) >= matrix.length) resize(highest_seqno);
/** * Iterates over the matrix with range [from .. to] (including from and to), and calls * {@link Visitor#visit(long,Object,int,int)}. If the visit() method returns false, the iteration is terminated. * <p/> * This method must be called with the lock held * @param from The starting seqno * @param to The ending seqno, the range is [from .. to] including from and to * @param visitor An instance of Visitor */ @GuardedBy("lock") public void forEach(long from, long to, Visitor<T> visitor) { if(from - to > 0) // same as if(from > to), but prevents long overflow return; int row=computeRow(from), column=computeIndex(from); int distance=(int)(to - from +1); T[] current_row=row+1 > matrix.length? null : matrix[row]; for(int i=0; i < distance; i++) { T element=current_row == null? null : current_row[column]; if(!visitor.visit(from, element, row, column)) break; from++; if(++column >= elements_per_row) { column=0; row++; current_row=row+1 > matrix.length? null : matrix[row]; } } }