19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
21 using System.Diagnostics;
23 using System.Runtime.CompilerServices;
24 using Spatial4n.Core.Context;
25 using Spatial4n.Core.Shapes;
27 namespace Lucene.Net.Spatial.Prefix.Tree
38 internal readonly SpatialContext ctx;
42 Debug.Assert(maxLevels > 0);
44 this.maxLevels = maxLevels;
47 public SpatialContext GetSpatialContext()
52 public int GetMaxLevels()
57 public override String ToString()
59 return GetType().Name +
"(maxLevels:" + maxLevels +
",ctx:" + ctx +
")";
71 public abstract int GetLevelForDistance(
double dist);
76 private Node worldNode;
86 if (worldNode == null)
88 worldNode = GetNode(
"");
97 public abstract Node GetNode(String token);
99 public abstract Node GetNode(byte[] bytes,
int offset,
int len);
116 return GetNode(token);
123 protected virtual Node GetNode(Point p,
int level)
125 return GetNodes(p, level,
false).ElementAt(0);
138 public virtual IList<Node> GetNodes(Shape shape,
int detailLevel,
bool inclParents)
140 if (detailLevel > maxLevels)
142 throw new ArgumentException(
"detailLevel > maxLevels",
"detailLevel");
149 int initialCapacity = inclParents ? 1 + detailLevel : 1;
150 cells =
new List<Node>(initialCapacity);
151 RecursiveGetNodes(GetWorldNode(), (Point)shape, detailLevel,
true, cells);
152 Debug.Assert(cells.Count == initialCapacity);
156 cells =
new List<Node>(inclParents ? 1024 : 512);
157 RecursiveGetNodes(GetWorldNode(), shape, detailLevel, inclParents, cells);
161 Debug.Assert(cells[0].GetLevel() == 0);
167 private void RecursiveGetNodes(
Node node, Shape shape,
int detailLevel,
bool inclParents, IList<Node> result)
176 if (node.
GetLevel() == detailLevel - 1)
182 foreach (var subCell
in subCells)
200 foreach (var subCell
in subCells)
202 RecursiveGetNodes(subCell, shape, detailLevel, inclParents, result);
207 private void RecursiveGetNodes(Node node, Point point,
int detailLevel,
bool inclParents, IList<Node> result)
213 Node pCell = node.GetSubCell(point);
214 if (node.GetLevel() == detailLevel - 1)
221 RecursiveGetNodes(pCell, point, detailLevel, inclParents, result);
231 protected virtual IList<Node> GetNodesAltPoint(Point p,
int detailLevel,
bool inclParents)
233 Node cell = GetNode(p, detailLevel);
237 return new ReadOnlyCollectionBuilder<Node>(
new[] { cell }).ToReadOnlyCollection();
239 return new List<Node>(
new[] { cell }).AsReadOnly();
244 Debug.Assert(endToken.Length == detailLevel);
245 var cells =
new List<Node>(detailLevel);
246 for (
int i = 1; i < detailLevel; i++)
248 cells.Add(GetNode(endToken.Substring(0, i)));
257 public static List<String> NodesToTokenStrings(Collection<Node> nodes)
259 var tokens =
new List<String>((nodes.Count));
260 foreach (
Node node
in nodes)