SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
KNNHeap.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Parijat Mazumdar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
33 #include <shogun/lib/SGVector.h>
34 
35 using namespace shogun;
36 
38 {
39  m_capacity=k;
40  m_dists=SGVector<float64_t>(m_capacity);
41  m_inds=SGVector<index_t>(m_capacity);
42  m_sorted=false;
43 
44  for (int32_t i=0;i<m_capacity;i++)
45  {
46  m_dists[i]=CMath::MAX_REAL_NUMBER;
47  m_inds[i]=0;
48  }
49 }
50 
51 void CKNNHeap::push(index_t index, float64_t dist)
52 {
53  if (dist>m_dists[0])
54  return;
55 
56  m_dists[0]=dist;
57  m_inds[0]=index;
58 
59  index_t i_swap;
60  index_t i=0;
61  while (true)
62  {
63  index_t l=2*i+1;
64  index_t r=l+1;
65  if (l>=m_capacity)
66  {
67  break;
68  }
69  else if (r>=m_capacity)
70  {
71  if (m_dists[l]>dist)
72  i_swap=l;
73  else
74  break;
75  }
76  else if (m_dists[l]>=m_dists[r])
77  {
78  if (m_dists[l]>dist)
79  i_swap=l;
80  else
81  break;
82  }
83  else
84  {
85  if (m_dists[r]>dist)
86  i_swap=r;
87  else
88  break;
89  }
90 
91  m_dists[i]=m_dists[i_swap];
92  m_inds[i]=m_inds[i_swap];
93 
94  m_dists[i_swap]=dist;
95  m_inds[i_swap]=index;
96  i=i_swap;
97  }
98 }
99 
101 {
102  if (m_sorted)
103  return m_dists;
104 
105  m_sorted=true;
106  SGVector<float64_t> new_dists(m_capacity);
107  SGVector<index_t> new_inds(m_capacity);
108 
109  // O(nlogn) heap-sort
110  for (int32_t i=m_capacity-1;i>-1;i--)
111  {
112  new_dists[i]=m_dists[0];
113  new_inds[i]=m_inds[0];
114  push(0,-1);
115  }
116 
117  m_dists=new_dists;
118  m_inds=new_inds;
119 
120  return m_dists;
121 }
122 
124 {
125  if (m_sorted)
126  return m_inds;
127 
128  m_sorted=true;
129  SGVector<float64_t> new_dists(m_capacity);
130  SGVector<index_t> new_inds(m_capacity);
131 
132  // O(nlogn) heap-sort
133  for (int32_t i=m_capacity-1;i>-1;i--)
134  {
135  new_dists[i]=m_dists[0];
136  new_inds[i]=m_inds[0];
137  push(0,-1);
138  }
139 
140  m_dists=new_dists;
141  m_inds=new_inds;
142 
143  return m_inds;
144 }
void push(index_t index, float64_t dist)
Definition: KNNHeap.cpp:51
SGVector< index_t > get_indices()
Definition: KNNHeap.cpp:123
int32_t index_t
Definition: common.h:62
double float64_t
Definition: common.h:50
CKNNHeap(int32_t k=1)
Definition: KNNHeap.cpp:37
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGVector< float64_t > get_dists()
Definition: KNNHeap.cpp:100
static const float64_t MAX_REAL_NUMBER
Definition: Math.h:2061

SHOGUN Machine Learning Toolbox - Documentation