1 package prefuse.data.util; 2 3 import prefuse.data.Table; 4 import prefuse.util.collections.IntIntSortedMap; 5 import prefuse.util.collections.IntIntTreeMap; 6 import prefuse.util.collections.IntIterator; 7 8 9 16 public class RowManager { 17 18 protected Table m_table; 19 private IntIntSortedMap m_openrows; 20 private int m_firstid = 0; 21 private int m_curid = -1; 22 23 26 30 public RowManager(Table table) { 31 m_table = table; 32 } 33 34 38 public Table getTable() { 39 return m_table; 40 } 41 42 45 49 public int getMinimumRow() { 50 return m_firstid; 51 } 52 53 57 public int getMaximumRow() { 58 return m_curid; 59 } 60 61 65 public int getRowCount() { 66 return 1 + m_curid - m_firstid 67 - (m_openrows==null ? 0 : m_openrows.size()); 68 } 69 70 76 public boolean isValidRow(int row) { 77 return ( row >= m_firstid && row <=m_curid && 78 (m_openrows == null || !m_openrows.containsKey(row)) ); 79 } 80 81 84 87 public void clear() { 88 m_openrows = null; 89 m_firstid = 0; 90 m_curid = -1; 91 } 92 93 98 public int addRow() { 99 int r; 100 if ( m_openrows == null || m_openrows.isEmpty() ) { 101 r = ( m_firstid == 0 ? ++m_curid : --m_firstid ); 102 } else { 103 int key = m_openrows.firstKey(); 104 r = m_openrows.remove(key); 105 } 106 return r; 107 } 108 109 115 public boolean releaseRow(int row) { 116 if ( row < 0 ) { 117 return false; 118 } else if ( m_openrows != null && m_openrows.containsKey(row) ) { 119 return false; 120 } else if ( row == m_curid ) { 121 --m_curid; 122 } else if ( row == m_firstid ) { 123 ++m_firstid; 124 } else { 125 if ( m_openrows == null ) 126 m_openrows = new IntIntTreeMap(false); 127 m_openrows.put(row, row); 128 } 129 return true; 130 } 131 132 135 146 public int getColumnRow(int row, int col) { 147 return this.isValidRow(row) ? row : -1; 148 } 149 150 161 public int getTableRow(int columnRow, int col) { 162 return this.isValidRow(columnRow) ? columnRow : -1; 163 } 164 165 171 public IntIterator columnRows(int col) { 172 return new ColumnRowIterator(rows(), col); 173 } 174 175 183 public IntIterator columnRows(int col, boolean reverse) { 184 return new ColumnRowIterator(rows(reverse), col); 185 } 186 187 194 public IntIterator columnRows(IntIterator rows, int col) { 195 return new ColumnRowIterator(rows, col); 196 } 197 198 201 205 public IntIterator rows() { 206 return new RowIterator(false); 207 } 208 209 215 public IntIterator rows(boolean reverse) { 216 return new RowIterator(reverse); 217 } 218 219 222 public class RowIterator extends IntIterator { 223 boolean reverse; 224 int last = -1, next; 225 226 public RowIterator(boolean reverse) { 227 this.reverse = reverse; 228 next = advance(reverse ? m_curid : m_firstid); 229 } 230 public boolean hasNext() { 231 return ( reverse ? next >= 0 : next <= m_curid ); 232 } 233 public int nextInt() { 234 last = next; 236 next = advance(reverse ? --next : ++next); 237 return last; 238 } 239 public void remove() { 240 m_table.removeRow(last); 241 } 242 private final int advance(int idx) { 243 if ( m_openrows == null ) 244 return idx; 245 else if ( reverse ) 246 for (; idx >= 0 && m_openrows.containsKey(idx); --idx); 247 else 248 for (; idx <= m_curid && m_openrows.containsKey(idx); ++idx); 249 return idx; 250 } 251 } 253 257 public class ColumnRowIterator extends IntIterator { 258 private IntIterator rows; 259 private int row; 260 private int col; 261 public ColumnRowIterator(IntIterator rows, int col) { 262 this.rows = rows; 263 this.col = col; 264 } 265 public boolean hasNext() { 266 return rows.hasNext(); 267 } 268 public int nextInt() { 269 row = rows.nextInt(); 270 return getColumnRow(row, col); 271 } 272 public void remove() { 273 m_table.removeRow(row); 274 } 275 } 277 } | Popular Tags |