00001 // [License] 00002 // The Ariba-Underlay Copyright 00003 // 00004 // Copyright (c) 2008-2009, Institute of Telematics, Universität Karlsruhe (TH) 00005 // 00006 // Institute of Telematics 00007 // Universität Karlsruhe (TH) 00008 // Zirkel 2, 76128 Karlsruhe 00009 // Germany 00010 // 00011 // Redistribution and use in source and binary forms, with or without 00012 // modification, are permitted provided that the following conditions are 00013 // met: 00014 // 00015 // 1. Redistributions of source code must retain the above copyright 00016 // notice, this list of conditions and the following disclaimer. 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND 00022 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00024 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OF TELEMATICS OR 00025 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00027 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // The views and conclusions contained in the software and documentation 00034 // are those of the authors and should not be interpreted as representing 00035 // official policies, either expressed or implied, of the Institute of 00036 // Telematics. 00037 // [License] 00038 #ifndef DISTANCES_HPP_ 00039 #define DISTANCES_HPP_ 00040 00046 namespace distances { 00047 00049 template<class T> 00050 T max_value() { 00051 return ~(T)0; 00052 } 00053 00055 template<class T> 00056 T min_value() { 00057 return 0; 00058 } 00059 00061 struct default_distance { 00062 template<typename T> 00063 T operator ()(const T& x, const T& y) { 00064 return abs(x - y); 00065 } 00066 }; 00067 00069 struct xor_distance { 00070 template<typename T> 00071 T operator()(const T& x, const T& y) { 00072 return x ^ y; 00073 } 00074 }; 00075 00077 struct ring_distance { 00078 template<typename T> 00079 T operator()(const T& x, const T& y) { 00080 const T m = max_value<T>(); 00081 T d_nor = (x >= y) ? (x-y) : (y-x); 00082 T d_inv = m - d_nor; 00083 return d_nor < d_inv ? d_nor : d_inv; 00084 } 00085 }; 00086 00088 struct ring_pred_distance { 00089 template<typename T> 00090 T operator ()(const T& x, const T& y) { 00091 const T m = max_value<T>(); 00092 if (x >= y) return x - y; 00093 else return m - y + x; 00094 } 00095 }; 00096 00098 struct ring_succ_distance { 00099 template<typename T> 00100 T operator ()(const T& y, const T& x) { 00101 const T m = max_value<T>(); 00102 if (x >= y) return x - y; 00103 else return m - y + x; 00104 } 00105 }; 00106 00107 } 00108 00109 #endif /* DISTANCES_HPP_ */