001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/rtree/NoneLeafNode.java $
002: //
003: // RTree implementation.
004: // Copyright (C) 2002-2004 Wolfgang Baer - WBaer@gmx.de
005: //
006: // This library is free software; you can redistribute it and/or
007: // modify it under the terms of the GNU Lesser General Public
008: // License as published by the Free Software Foundation; either
009: // version 2.1 of the License, or (at your option) any later version.
010: //
011: // This library is distributed in the hope that it will be useful,
012: // but WITHOUT ANY WARRANTY; without even the implied warranty of
013: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: // Lesser General Public License for more details.
015: //
016: // You should have received a copy of the GNU Lesser General Public
017: // License along with this library; if not, write to the Free Software
018: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: package org.deegree.io.rtree;
021:
022: /**
023: * <p>
024: * Implementation of a NoneLeafNode.
025: * Inherits methods from the abstract class Node filling
026: * the defined abstract methods with life.
027: * </p>
028: * @author Wolfgang Baer - WBaer@gmx.de
029: */
030: class NoneLeafNode extends Node {
031:
032: protected int[] childNodes;
033:
034: /**
035: * Constructor.
036: * @param pageNumber - number of this node in page file
037: * @param file - the PageFile of this node
038: */
039: protected NoneLeafNode(int pageNumber, PageFile file) {
040: super (pageNumber, file);
041: childNodes = new int[file.getCapacity()];
042:
043: for (int i = 0; i < file.getCapacity(); i++)
044: childNodes[i] = -1;
045: }
046:
047: /**
048: * @see Node#getData(int)
049: */
050: protected Object getData(int index) {
051: Object obj = null;
052:
053: try {
054: obj = file.readNode(childNodes[index]);
055: } catch (PageFileException e) {
056: // PageFileException NoneLeafNode.getData
057: e.printStackTrace();
058: }
059:
060: return obj;
061: }
062:
063: /**
064: * @see Node#insertData(java.lang.Object, HyperBoundingBox)
065: */
066: protected void insertData(Object node, HyperBoundingBox box) {
067: childNodes[counter] = ((Node) node).getPageNumber();
068: hyperBBs[counter] = box;
069: unionMinBB = unionMinBB.unionBoundingBox(box);
070: ((Node) node).parentNode = this .pageNumber;
071: ((Node) node).place = this .counter;
072: counter++;
073:
074: try {
075: file.writeNode((Node) node);
076: } catch (PageFileException e) {
077: //PageFileException NoneLeafNode.insertData - at writeNode(Node)
078: e.printStackTrace();
079: }
080: }
081:
082: /**
083: * @see Node#insertData(java.lang.Object, HyperBoundingBox)
084: */
085: protected void deleteData(int index) {
086:
087: if (this .getUsedSpace() == 1) {
088: // only one element is a special case.
089: hyperBBs[0] = HyperBoundingBox.getNullHyperBoundingBox(file
090: .getDimension());
091: childNodes[0] = -1;
092: counter--;
093: } else {
094: System.arraycopy(hyperBBs, index + 1, hyperBBs, index,
095: counter - index - 1);
096: System.arraycopy(childNodes, index + 1, childNodes, index,
097: counter - index - 1);
098: hyperBBs[counter - 1] = HyperBoundingBox
099: .getNullHyperBoundingBox(file.getDimension());
100: childNodes[counter - 1] = -1;
101: counter--;
102:
103: for (int i = 0; i < counter; i++) {
104: Node help = (Node) this .getData(i);
105: help.place = i;
106:
107: try {
108: file.writeNode(help);
109: } catch (PageFileException e) {
110: // "PageFileException NoneLeafNode.deleteData - at writeNode(Node)
111: e.printStackTrace();
112: }
113: }
114: }
115:
116: updateNodeBoundingBox();
117: }
118:
119: /**
120: * Computes the index of the entry with least enlargement if the given
121: * HyperBoundingBox would be added.
122: * @param box - HyperBoundingBox to be added
123: * @return int - index of entry with least enlargement
124: */
125: protected int getLeastEnlargement(HyperBoundingBox box) {
126:
127: double[] area = new double[counter];
128:
129: for (int i = 0; i < counter; i++)
130: area[i] = (hyperBBs[i].unionBoundingBox(box)).getArea()
131: - hyperBBs[i].getArea();
132:
133: double min = area[0];
134: int minnr = 0;
135:
136: for (int i = 1; i < counter; i++) {
137: if (area[i] < min) {
138: min = area[i];
139: minnr = i;
140: }
141: }
142:
143: return minnr;
144: }
145:
146: /**
147: * @see Node#clone()
148: */
149: protected Object clone() {
150: NoneLeafNode clone = new NoneLeafNode(this .pageNumber,
151: this .file);
152: clone.counter = this .counter;
153: clone.place = this .place;
154: clone.unionMinBB = (HyperBoundingBox) this .unionMinBB.clone();
155: clone.parentNode = this .parentNode;
156:
157: for (int i = 0; i < file.getCapacity(); i++)
158: clone.hyperBBs[i] = (HyperBoundingBox) this.hyperBBs[i]
159: .clone();
160:
161: return clone;
162: }
163: }
|