JLCoverTreePoint.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2012 Fernando José Iglesias García
00008  * Written (W) John Langford and Dinoj Surendran, v_array and its templatization
00009  * Copyright (C) 2012 Fernando José Iglesias García
00010  */
00011 
00012 #ifndef _JLCTPOINT_H__
00013 #define _JLCTPOINT_H__
00014 
00015 #include <shogun/lib/config.h>
00016 #include <shogun/distance/Distance.h>
00017 #include <shogun/features/Features.h>
00018 
00019 namespace shogun
00020 {
00021 
00023 template<class T> 
00024 class v_array{
00025 
00026     public:
00029         T last() { return elements[index-1];}
00030 
00032         void decr() { index--;}
00033 
00035         v_array() { index = 0; length=0; elements = NULL;}
00036 
00040         T& operator[](unsigned int i) { return elements[i]; }
00041 
00042     public:
00044         int index;
00045 
00047         int length;
00048 
00050         T* elements;
00051 
00052 };
00053 
00060 template<class T> 
00061 void push(v_array<T>& v, const T &new_ele)
00062 {
00063     while(v.index >= v.length)
00064     {
00065         v.length = 2*v.length + 3;
00066         v.elements = (T *)realloc(v.elements,sizeof(T) * v.length);
00067     }
00068     v[v.index++] = new_ele;
00069 }
00070 
00077 template<class T> 
00078 void alloc(v_array<T>& v, int length)
00079 {
00080     v.elements = (T *)realloc(v.elements, sizeof(T) * length);
00081     v.length = length;
00082 }
00083 
00093 template<class T> 
00094 v_array<T> pop(v_array<v_array<T> > &stack)
00095 {
00096     if (stack.index > 0)
00097         return stack[--stack.index];
00098     else
00099         return v_array<T>();
00100 }
00101 
00107 enum EFeaturesContainer
00108 {
00109     FC_LHS = 0,
00110     FC_RHS = 1,
00111 };
00112 
00118 class CJLCoverTreePoint
00119 {
00120 
00121     public:
00122 
00125         CDistance* m_distance;
00126 
00128         int32_t m_index;
00129 
00131         EFeaturesContainer m_features_container;
00132 
00133 }; /* class JLCoverTreePoint */
00134 
00138 float distance(CJLCoverTreePoint p1, CJLCoverTreePoint p2, float64_t upper_bound)
00139 {
00143     if ( p1.m_features_container == p2.m_features_container )
00144     {
00145         if ( ! p1.m_distance->lhs_equals_rhs() )
00146         {
00147             SG_SERROR("lhs != rhs but the distance of two points "
00148                    "from the same container has been requested\n");
00149         }
00150         else
00151         {
00152             return p1.m_distance->distance_upper_bounded(p1.m_index, 
00153                     p2.m_index, upper_bound);
00154         }
00155     }
00156     else
00157     {
00158         if ( p1.m_distance->lhs_equals_rhs() )
00159         {
00160             SG_SERROR("lhs == rhs but the distance of two points "
00161                   "from different containers has been requested\n");
00162         }
00163         else
00164         {
00165             if ( p1.m_features_container == FC_LHS )
00166             {
00167                 return p1.m_distance->distance_upper_bounded(p1.m_index, 
00168                         p2.m_index, upper_bound);
00169             }
00170             else
00171             {
00172                 return p1.m_distance->distance_upper_bounded(p2.m_index, 
00173                         p1.m_index, upper_bound);
00174             }
00175         }
00176     }
00177 
00178     SG_SERROR("Something has gone wrong, case not handled\n");
00179     return -1;
00180 }
00181 
00183 v_array< CJLCoverTreePoint > parse_points(CDistance* distance, EFeaturesContainer fc)
00184 {
00185     CFeatures* features;
00186     if ( fc == FC_LHS )
00187         features = distance->get_lhs();
00188     else
00189         features = distance->get_rhs();
00190 
00191     v_array< CJLCoverTreePoint > parsed;
00192     for ( int32_t i = 0 ; i < features->get_num_vectors() ; ++i )
00193     {
00194         CJLCoverTreePoint new_point;
00195 
00196         new_point.m_distance = distance;
00197         new_point.m_index = i;
00198         new_point.m_features_container = fc;
00199 
00200         push(parsed, new_point);
00201     }
00202     
00203     return parsed;
00204 }
00205 
00207 void print(CJLCoverTreePoint &p)
00208 {
00209     SG_SERROR("Print JLCoverTreePoint not implemented\n");
00210 }
00211 
00212 } /* namespace shogun */
00213 
00214 #endif /* _JLCTPOINT_H__*/
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation