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
CachingDoubleValueSource.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.Collections.Generic;
19 using Lucene.Net.Index;
20 using Lucene.Net.Search.Function;
21 
22 namespace Lucene.Net.Spatial.Util
23 {
25  {
26  protected readonly ValueSource source;
27  protected readonly Dictionary<int, double> cache;
28 
30  {
31  this.source = source;
32  cache = new Dictionary<int, double>();
33  }
34 
36  {
37  private readonly int docBase;
38  private readonly DocValues values;
39  private readonly Dictionary<int, double> cache;
40 
41  public CachingDoubleDocValue(int docBase, DocValues vals, Dictionary<int, double> cache)
42  {
43  this.docBase = docBase;
44  this.values = vals;
45  this.cache = cache;
46  }
47 
48  public override double DoubleVal(int doc)
49  {
50  var key = docBase + doc;
51  double v;
52  if (!cache.TryGetValue(key, out v))
53  {
54  v = values.DoubleVal(doc);
55  cache[key] = v;
56  }
57  return v;
58  }
59 
60  public override float FloatVal(int doc)
61  {
62  return (float)DoubleVal(doc);
63  }
64 
65  public override string ToString(int doc)
66  {
67  return DoubleVal(doc) + string.Empty;
68  }
69  }
70 
71  public override DocValues GetValues(IndexReader reader)
72  {
73  var @base = 0; //reader.DocBase;
74  var vals = source.GetValues(reader);
75  return new CachingDoubleDocValue(@base, vals, cache);
76 
77  }
78 
79  public override string Description()
80  {
81  return "Cached[" + source.Description() + "]";
82  }
83 
84  public override bool Equals(object o)
85  {
86  if (this == o) return true;
87 
88  var that = o as CachingDoubleValueSource;
89 
90  if (that == null) return false;
91  if (source != null ? !source.Equals(that.source) : that.source != null) return false;
92 
93  return true;
94  }
95 
96  public override int GetHashCode()
97  {
98  return source != null ? source.GetHashCode() : 0;
99  }
100  }
101 }