Source code: com/port80/graph/dot/impl/DotBoxList.java
1 //
2 // Copyright(c) 2002, Chris Leung
3 //
4
5 package com.port80.graph.dot.impl;
6
7 /**
8 * An allocated list of DotBox.
9 *
10 * To avoid allocating lots of small DotBox objects, the list have pre-allocated list of DotBox
11 * and would never replace them with other boxes.
12 *
13 * @author chrisl
14 */
15 public class DotBoxList {
16
17 protected DotBox[] fBoxes;
18 protected int fSize;
19 protected int fCapacity;
20
21 public DotBoxList(int n) {
22 fBoxes = new DotBox[n];
23 for(int i=0; i<n; ++i) fBoxes[i]=new DotBox();
24 fSize = 0;
25 fCapacity=n;
26 }
27 public int size() {
28 return fSize;
29 }
30 public DotBox get(int i) {
31 return fBoxes[i];
32 }
33 public DotBox getLast() {
34 if(fSize<=0) return null;
35 return fBoxes[fSize - 1];
36 }
37 public DotBox add(int llx, int lly, int urx, int ury) {
38 if (fSize >= fCapacity)
39 grow(fCapacity / 2 + 1);
40 DotBox b=fBoxes[fSize++];
41 b.set(llx,lly,urx,ury);
42 return b;
43 }
44 public DotBox add(DotBox box) {
45 if (fSize >= fCapacity)
46 grow(fCapacity / 2 + 1);
47 DotBox b=fBoxes[fSize++];
48 b.set(box);
49 return b;
50 }
51 public DotBox removeLast() {
52 if(fSize<=0) return null;
53 --fSize;
54 return new DotBox(fBoxes[fSize]);
55 }
56 /**
57 * Grow box list by given size.
58 */
59 public void grow(int n) {
60 DotBox[] a = new DotBox[fCapacity + n];
61 System.arraycopy(fBoxes, 0, a, 0, fCapacity);
62 for(int i=fCapacity; i<a.length; ++i) a[i]=new DotBox();
63 fBoxes = a;
64 fCapacity+=n;
65 }
66 public void reset() {
67 fSize = 0;
68 }
69 }