1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2006 Refractions Research Inc.
7 * Copyright (C) 2001-2002 Vivid Solutions Inc.
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
14 **********************************************************************/
17 #include <geos/index/bintree/Bintree.h>
18 #include <geos/index/bintree/Root.h>
19 #include <geos/index/bintree/Interval.h>
23 namespace index
{ // geos.index
24 namespace bintree
{ // geos.index.bintree
29 Bintree::ensureExtent(const Interval
* itemInterval
, double minExtent
)
31 double min
= itemInterval
->getMin();
32 double max
= itemInterval
->getMax();
33 // has a non-zero extent
36 // GEOS forces a copy here to be predictable wrt
37 // memory management. May change in the future.
38 return new Interval(*itemInterval
);
44 min
= min
-minExtent
/2.0;
45 max
= min
+minExtent
/2.0;
48 return new Interval(min
, max
);
61 for (unsigned int i
=0; i
<newIntervals
.size(); i
++)
62 delete newIntervals
[i
];
69 if (root
!=NULL
) return root
->depth();
76 if (root
!=NULL
) return root
->size();
81 * Compute the total number of nodes in the tree
83 * @return the number of nodes in the tree
88 if (root
!=NULL
) return root
->nodeSize();
93 Bintree::insert(Interval
*itemInterval
,void* item
)
95 collectStats(itemInterval
);
96 Interval
*insertInterval
=ensureExtent(itemInterval
,minExtent
);
97 if ( insertInterval
!= itemInterval
)
98 newIntervals
.push_back(insertInterval
);
100 root
->insert(insertInterval
,item
);
104 System.out.println("BinTree: size="+newSize+" node size="+nodeSize());
105 if (newSize <= oldSize) {
106 System.out.println("Lost item!");
107 root.insert(insertInterval, item);
108 System.out.println("reinsertion size="+size());
113 vector
<void*>* Bintree::iterator() {
114 vector
<void*>* foundItems
=new vector
<void*>();
115 root
->addAllItems(foundItems
);
119 vector
<void*>* Bintree::query(double x
) {
120 return query(new Interval(x
, x
));
124 * min and max may be the same value
126 vector
<void*>* Bintree::query(Interval
*interval
) {
128 * the items that are matched are all items in intervals
129 * which overlap the query interval
131 vector
<void*>* foundItems
=new vector
<void*>();
132 query(interval
,foundItems
);
137 Bintree::query(Interval
*interval
,vector
<void*> *foundItems
)
139 root
->addAllItemsFromOverlapping(interval
,foundItems
);
143 Bintree::collectStats(Interval
*interval
)
145 double del
=interval
->getWidth();
146 if (del
<minExtent
&& del
>0.0)
150 } // namespace geos.index.bintree
151 } // namespace geos.index
154 /**********************************************************************
156 * Revision 1.13 2006/03/22 16:01:33 strk
157 * indexBintree.h header split, classes renamed to match JTS
159 **********************************************************************/