01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.poi.util;
18:
19: import java.util.List;
20: import java.util.ArrayList;
21:
22: /**
23: * Provides an interface for interacting with 2d arrays of integers. This
24: * implementation will return 0 for items not yet allocated and automatically
25: * increase the array size for set operations. You never get an index out of
26: * bounds.
27: *
28: * @author Glen Stampoultzis (glens at apache.org)
29: * @version $Id: IntList2d.java 496526 2007-01-15 22:46:35Z markt $
30: */
31: public class IntList2d {
32: // Implemented using a List of IntList's.
33: List rows = new ArrayList();
34:
35: public int get(int col, int row) {
36: if (row >= rows.size()) {
37: return 0;
38: } else {
39: IntList cols = (IntList) rows.get(row);
40: if (col >= cols.size())
41: return 0;
42: else
43: return cols.get(col);
44: }
45: }
46:
47: public void set(int col, int row, int value) {
48: resizeRows(row);
49: resizeCols(row, col);
50: IntList cols = (IntList) rows.get(row);
51: cols.set(col, value);
52: }
53:
54: private void resizeRows(int row) {
55: while (rows.size() <= row)
56: rows.add(new IntList());
57: }
58:
59: private void resizeCols(int row, int col) {
60: IntList cols = (IntList) rows.get(row);
61: while (cols.size() <= col)
62: cols.add(0);
63: }
64:
65: public boolean isAllocated(int col, int row) {
66: if (row < rows.size()) {
67: IntList cols = (IntList) rows.get(row);
68: return (col < cols.size());
69: } else {
70: return false;
71: }
72: }
73:
74: }
|