1 /**
2  * @file methods/neighbor_search/neighbor_search_stat.hpp
3  * @author Ryan Curtin
4  *
5  * Defines the NeighborSearch class, which performs an abstract
6  * nearest-neighbor-like query on two datasets.
7  *
8  * mlpack is free software; you may redistribute it and/or modify it under the
9  * terms of the 3-clause BSD license.  You should have received a copy of the
10  * 3-clause BSD license along with mlpack.  If not, see
11  * http://www.opensource.org/licenses/BSD-3-Clause for more information.
12  */
13 #ifndef MLPACK_METHODS_NEIGHBOR_SEARCH_NEIGHBOR_SEARCH_STAT_HPP
14 #define MLPACK_METHODS_NEIGHBOR_SEARCH_NEIGHBOR_SEARCH_STAT_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace neighbor {
20 
21 /**
22  * Extra data for each node in the tree.  For neighbor searches, each node only
23  * needs to store a bound on neighbor distances.
24  */
25 template<typename SortPolicy>
26 class NeighborSearchStat
27 {
28  private:
29   //! The first bound on the node's neighbor distances (B_1).  This represents
30   //! the worst candidate distance of any descendants of this node.
31   double firstBound;
32   //! The second bound on the node's neighbor distances (B_2).  This represents
33   //! a bound on the worst distance of any descendants of this node assembled
34   //! using the best descendant candidate distance modified by the furthest
35   //! descendant distance.
36   double secondBound;
37   //! The aux bound on the node's neighbor distances (B_aux). This represents
38   //! the best descendant candidate distance (used to calculate secondBound).
39   double auxBound;
40   //! The last distance evaluation.
41   double lastDistance;
42 
43  public:
44   /**
45    * Initialize the statistic with the worst possible distance according to
46    * our sorting policy.
47    */
NeighborSearchStat()48   NeighborSearchStat() :
49       firstBound(SortPolicy::WorstDistance()),
50       secondBound(SortPolicy::WorstDistance()),
51       auxBound(SortPolicy::WorstDistance()),
52       lastDistance(0.0) { }
53 
54   /**
55    * Initialization for a fully initialized node.  In this case, we don't need
56    * to worry about the node.
57    */
58   template<typename TreeType>
NeighborSearchStat(TreeType &)59   NeighborSearchStat(TreeType& /* node */) :
60       firstBound(SortPolicy::WorstDistance()),
61       secondBound(SortPolicy::WorstDistance()),
62       auxBound(SortPolicy::WorstDistance()),
63       lastDistance(0.0) { }
64 
65   /**
66    * Reset statistic parameters to initial values.
67    */
Reset()68   void Reset()
69   {
70     firstBound = SortPolicy::WorstDistance();
71     secondBound = SortPolicy::WorstDistance();
72     auxBound = SortPolicy::WorstDistance();
73     lastDistance = 0.0;
74   }
75 
76   //! Get the first bound.
FirstBound() const77   double FirstBound() const { return firstBound; }
78   //! Modify the first bound.
FirstBound()79   double& FirstBound() { return firstBound; }
80   //! Get the second bound.
SecondBound() const81   double SecondBound() const { return secondBound; }
82   //! Modify the second bound.
SecondBound()83   double& SecondBound() { return secondBound; }
84   //! Get the aux bound.
AuxBound() const85   double AuxBound() const { return auxBound; }
86   //! Modify the aux bound.
AuxBound()87   double& AuxBound() { return auxBound; }
88   //! Get the last distance calculation.
LastDistance() const89   double LastDistance() const { return lastDistance; }
90   //! Modify the last distance calculation.
LastDistance()91   double& LastDistance() { return lastDistance; }
92 
93   //! Serialize the statistic to/from an archive.
94   template<typename Archive>
serialize(Archive & ar,const unsigned int)95   void serialize(Archive& ar, const unsigned int /* version */)
96   {
97     ar & BOOST_SERIALIZATION_NVP(firstBound);
98     ar & BOOST_SERIALIZATION_NVP(secondBound);
99     ar & BOOST_SERIALIZATION_NVP(auxBound);
100     ar & BOOST_SERIALIZATION_NVP(lastDistance);
101   }
102 };
103 
104 } // namespace neighbor
105 } // namespace mlpack
106 
107 #endif
108