inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/lut
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_lut
00005 #define inlib_lut
00006 
00007 namespace inlib {
00008 
00009 // TB = Borne
00010 template <class TB,class TO>
00011 class lut {
00012 public:  
00013   lut()
00014   :m_min(0),m_max(0)
00015   ,m_bornen(0)
00016   ,m_bornes(0)
00017   {}
00018   lut(const TB& a_min,const TB& a_max,unsigned int a_coln)
00019   :m_min(a_min)
00020   ,m_max(a_max)
00021   {
00022     //NOTE : we should have a_max>a_min and a_coln >=3.
00023 
00024     // a_coln = 5 :
00025     // <>  |  <>  |  <>  |  <>  |  <>  // a_coln <>
00026     //    min                  max
00027     // there are :
00028     //   5 <> 
00029     // and :
00030     //   4 |
00031     if(m_max<=m_min) m_max = m_min+1; //throw ?
00032     if(a_coln<3) { //throw ?
00033       m_bornen = 2;
00034       m_bornes = new TB[m_bornen];
00035       m_bornes[0] = m_min;
00036       m_bornes[1] = m_max;
00037     } else {
00038       m_bornen = a_coln-1;
00039       m_bornes = new TB[m_bornen];
00040       TB dx = (m_max-m_min)/(m_bornen-1);
00041       for(unsigned int i=0;i<m_bornen;i++) {
00042         m_bornes[i] = m_min+dx*i;
00043       }
00044     }
00045   }
00046   virtual ~lut(){delete [] m_bornes;}
00047 public:
00048   lut(const lut& a_from)
00049   :m_min(a_from.m_min)
00050   ,m_max(a_from.m_max)
00051   ,m_bornen(a_from.m_bornen)
00052   ,m_bornes(0)
00053   {
00054     if(m_bornen) {
00055       m_bornes = new TB[m_bornen];
00056       for(unsigned int i=0;i<m_bornen;i++) {
00057         m_bornes[i] = a_from.m_bornes[i];
00058       }
00059     }
00060   }
00061   lut& operator=(const lut& a_from){
00062     delete [] m_bornes;
00063     m_bornes = 0;
00064 
00065     m_min = a_from.m_min;
00066     m_max = a_from.m_max;
00067     m_bornen = a_from.m_bornen;
00068     if(m_bornen) {
00069       m_bornes = new TB[m_bornen];
00070       for(unsigned int i=0;i<m_bornen;i++) {
00071         m_bornes[i] = a_from.m_bornes[i];
00072       }
00073     }
00074     return *this;
00075   }
00076 public:
00077   TO apply(const TB& a_x) const {
00078     // return something in [0,a_coln-1]
00079     TB* pos = m_bornes;
00080     for(TO i=0;i<m_bornen;i++,pos++) {
00081       if(a_x<*pos) return i;
00082     }
00083     return m_bornen;
00084   }
00085 private:
00086   TB m_min,m_max;
00087   unsigned int m_bornen;
00088   TB* m_bornes;
00089 private:
00090   static void check_instantiation() {
00091     lut<double,unsigned char> dummy(0,1,256);
00092     unsigned char o = dummy.apply(0.5);
00093   }
00094 };
00095 
00096 }
00097 
00098 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines