using System.Collections;
namespace GisSharpBlog.NetTopologySuite.Index.Bintree
{
///
/// The base class for nodes in a Bintree.
///
public abstract class NodeBase
{
///
///
///
protected IList items = new ArrayList();
///
/// Subnodes are numbered as follows:
/// 0 | 1
/// .
///
protected Node[] subnode = new Node[2];
///
///
///
public IList Items
{
get
{
return items;
}
}
///
///
///
public int Depth
{
get
{
int maxSubDepth = 0;
for (int i = 0; i < 2; i++)
{
if (subnode[i] != null)
{
int sqd = subnode[i].Depth;
if (sqd > maxSubDepth)
{
maxSubDepth = sqd;
}
}
}
return maxSubDepth + 1;
}
}
///
///
///
public int Count
{
get
{
int subSize = 0;
for (int i = 0; i < 2; i++)
{
if (subnode[i] != null)
{
subSize += subnode[i].Count;
}
}
return subSize + items.Count;
}
}
///
///
///
public int NodeCount
{
get
{
int subCount = 0;
for (int i = 0; i < 2; i++)
{
if (subnode[i] != null)
{
subCount += subnode[i].NodeCount;
}
}
return subCount + 1;
}
}
///
/// Returns the index of the subnode that wholely contains the given interval.
/// If none does, returns -1.
///
///
///
public static int GetSubnodeIndex(Interval interval, double centre)
{
int subnodeIndex = -1;
if (interval.Min >= centre)
{
subnodeIndex = 1;
}
if (interval.Max <= centre)
{
subnodeIndex = 0;
}
return subnodeIndex;
}
///
///
///
///
public void Add(object item)
{
items.Add(item);
}
///
///
///
///
///
public IList AddAllItems(IList items)
{
// items.addAll(this.items);
foreach (object o in this.items)
{
items.Add(o);
}
for (int i = 0; i < 2; i++)
{
if (subnode[i] != null)
{
subnode[i].AddAllItems(items);
}
}
return items;
}
///
///
///
///
///
///
public IList AddAllItemsFromOverlapping(Interval interval, IList resultItems)
{
if (!IsSearchMatch(interval))
{
return items;
}
// resultItems.addAll(items);
foreach (object o in items)
{
resultItems.Add(o);
}
for (int i = 0; i < 2; i++)
{
if (subnode[i] != null)
{
subnode[i].AddAllItemsFromOverlapping(interval, resultItems);
}
}
return items;
}
///
///
///
///
///
protected abstract bool IsSearchMatch(Interval interval);
}
}