Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
DistanceValueSource.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System.Diagnostics;
19 using Lucene.Net.Index;
20 using Lucene.Net.Search;
21 using Lucene.Net.Search.Function;
22 using Lucene.Net.Spatial.Util;
23 using Spatial4n.Core.Distance;
24 using Spatial4n.Core.Shapes;
25 using Spatial4n.Core.Shapes.Impl;
26 
27 namespace Lucene.Net.Spatial.Vector
28 {
29  /// <summary>
30  /// An implementation of the Lucene ValueSource model that returns the distance.
31  /// </summary>
33  {
34  private readonly PointVectorStrategy strategy;
35  private readonly Point from;
36 
37  public DistanceValueSource(PointVectorStrategy strategy, Point from)
38  {
39  this.strategy = strategy;
40  this.from = from;
41  }
42 
44  {
45  private readonly DistanceValueSource enclosingInstance;
46 
47  private readonly double[] ptX, ptY;
48  private readonly IBits validX, validY;
49 
50  private readonly Point from;
51  private readonly DistanceCalculator calculator;
52  private readonly double nullValue;
53 
54  public DistanceDocValues(DistanceValueSource enclosingInstance, IndexReader reader)
55  {
56  this.enclosingInstance = enclosingInstance;
57 
58  ptX = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.GetFieldNameX()/*, true*/);
59  ptY = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.GetFieldNameY()/*, true*/);
60  validX = FieldCache_Fields.DEFAULT.GetDocsWithField(reader, enclosingInstance.strategy.GetFieldNameX());
61  validY = FieldCache_Fields.DEFAULT.GetDocsWithField(reader, enclosingInstance.strategy.GetFieldNameY());
62 
63  from = enclosingInstance.from;
64  calculator = enclosingInstance.strategy.GetSpatialContext().GetDistCalc();
65  nullValue = (enclosingInstance.strategy.GetSpatialContext().IsGeo() ? 180 : double.MaxValue);
66  }
67 
68  public override float FloatVal(int doc)
69  {
70  return (float)DoubleVal(doc);
71  }
72 
73  public override double DoubleVal(int doc)
74  {
75  // make sure it has minX and area
76  if (validX.Get(doc))
77  {
78  Debug.Assert(validY.Get(doc));
79  return calculator.Distance(from, ptX[doc], ptY[doc]);
80  }
81  return nullValue;
82  }
83 
84  public override string ToString(int doc)
85  {
86  return enclosingInstance.Description() + "=" + FloatVal(doc);
87  }
88  }
89 
90  public override DocValues GetValues(IndexReader reader)
91  {
92  return new DistanceDocValues(this, reader);
93  }
94 
95  public override string Description()
96  {
97  return "DistanceValueSource(" + strategy + ", " + from + ")";
98  }
99 
100  public override bool Equals(object o)
101  {
102  if (this == o) return true;
103 
104  var that = o as DistanceValueSource;
105  if (that == null) return false;
106 
107  if (!from.Equals(that.from)) return false;
108  if (!strategy.Equals(that.strategy)) return false;
109 
110  return true;
111  }
112 
113  public override int GetHashCode()
114  {
115  return from.GetHashCode();
116  }
117  }
118 }