1 /*********************************************************************** 2 * Software License Agreement (BSD License) 3 * 4 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved. 5 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved. 6 * 7 * THE BSD LICENSE 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 *************************************************************************/ 30 31 #ifndef FLANN_GENERAL_H_ 32 #define FLANN_GENERAL_H_ 33 34 #include "defines.h" 35 #include <stdexcept> 36 #include <cassert> 37 #include <limits.h> 38 39 namespace flann 40 { 41 42 class FLANNException : public std::runtime_error 43 { 44 public: FLANNException(const char * message)45 FLANNException(const char* message) : std::runtime_error(message) { } 46 FLANNException(const std::string & message)47 FLANNException(const std::string& message) : std::runtime_error(message) { } 48 }; 49 50 51 template <typename T> 52 struct flann_datatype_value 53 { 54 static const flann_datatype_t value = FLANN_NONE; 55 }; 56 57 template<> 58 struct flann_datatype_value<char> 59 { 60 static const flann_datatype_t value = FLANN_INT8; 61 }; 62 63 template<> 64 struct flann_datatype_value<short> 65 { 66 static const flann_datatype_t value = FLANN_INT16; 67 }; 68 69 template<> 70 struct flann_datatype_value<int> 71 { 72 static const flann_datatype_t value = FLANN_INT32; 73 }; 74 75 #ifdef LLONG_MAX 76 template<> 77 struct flann_datatype_value<long long> 78 { 79 static const flann_datatype_t value = FLANN_INT64; 80 }; 81 #endif 82 83 template<> 84 struct flann_datatype_value<unsigned char> 85 { 86 static const flann_datatype_t value = FLANN_UINT8; 87 }; 88 89 template<> 90 struct flann_datatype_value<unsigned short> 91 { 92 static const flann_datatype_t value = FLANN_UINT16; 93 }; 94 95 template<> 96 struct flann_datatype_value<unsigned int> 97 { 98 static const flann_datatype_t value = FLANN_UINT32; 99 }; 100 101 #ifdef ULLONG_MAX 102 template<> 103 struct flann_datatype_value<unsigned long long> 104 { 105 static const flann_datatype_t value = FLANN_UINT64; 106 }; 107 #endif 108 109 110 template<> 111 struct flann_datatype_value<float> 112 { 113 static const flann_datatype_t value = FLANN_FLOAT32; 114 }; 115 116 template<> 117 struct flann_datatype_value<double> 118 { 119 static const flann_datatype_t value = FLANN_FLOAT64; 120 }; 121 122 123 124 template <flann_datatype_t datatype> 125 struct flann_datatype_type 126 { 127 typedef void type; 128 }; 129 130 template<> 131 struct flann_datatype_type<FLANN_INT8> 132 { 133 typedef char type; 134 }; 135 136 template<> 137 struct flann_datatype_type<FLANN_INT16> 138 { 139 typedef short type; 140 }; 141 142 template<> 143 struct flann_datatype_type<FLANN_INT32> 144 { 145 typedef int type; 146 }; 147 148 #ifdef LLONG_MAX 149 template<> 150 struct flann_datatype_type<FLANN_INT64> 151 { 152 typedef long long type; 153 }; 154 #endif 155 156 template<> 157 struct flann_datatype_type<FLANN_UINT8> 158 { 159 typedef unsigned char type; 160 }; 161 162 163 template<> 164 struct flann_datatype_type<FLANN_UINT16> 165 { 166 typedef unsigned short type; 167 }; 168 169 template<> 170 struct flann_datatype_type<FLANN_UINT32> 171 { 172 typedef unsigned int type; 173 }; 174 175 #ifdef ULLONG_MAX 176 template<> 177 struct flann_datatype_type<FLANN_UINT64> 178 { 179 typedef unsigned long long type; 180 }; 181 #endif 182 183 template<> 184 struct flann_datatype_type<FLANN_FLOAT32> 185 { 186 typedef float type; 187 }; 188 189 template<> 190 struct flann_datatype_type<FLANN_FLOAT64> 191 { 192 typedef double type; 193 }; 194 195 196 inline size_t flann_datatype_size(flann_datatype_t type) 197 { 198 switch (type) { 199 case FLANN_INT8: 200 return sizeof(flann_datatype_type<FLANN_INT8>::type); 201 case FLANN_INT16: 202 return sizeof(flann_datatype_type<FLANN_INT16>::type); 203 case FLANN_INT32: 204 return sizeof(flann_datatype_type<FLANN_INT32>::type); 205 case FLANN_INT64: 206 return sizeof(flann_datatype_type<FLANN_INT64>::type); 207 case FLANN_UINT8: 208 return sizeof(flann_datatype_type<FLANN_UINT8>::type); 209 case FLANN_UINT16: 210 return sizeof(flann_datatype_type<FLANN_UINT16>::type); 211 case FLANN_UINT32: 212 return sizeof(flann_datatype_type<FLANN_UINT32>::type); 213 case FLANN_UINT64: 214 return sizeof(flann_datatype_type<FLANN_UINT64>::type); 215 case FLANN_FLOAT32: 216 return sizeof(flann_datatype_type<FLANN_FLOAT32>::type); 217 case FLANN_FLOAT64: 218 return sizeof(flann_datatype_type<FLANN_FLOAT64>::type); 219 default: 220 return 0; 221 } 222 } 223 224 } 225 226 227 #endif /* FLANN_GENERAL_H_ */ 228