lxr/Translate.hh

00001 /* -*- mode: c++; c-basic-offset: 4; -*- */
00002 #ifndef TRANSLATE_HH
00003 #define TRANSLATE_HH
00004 
00012 template <class T> 
00013 class Translate {
00014 public:
00017   Translate(void);
00018 
00022   Translate(T init);
00023 
00026   ~Translate(void);
00027 
00030   void char_type(const char*, T val);
00031 
00037   T operator[](char c) const;
00038 
00045   T& operator[](char c);
00046 
00051   void reset(void);
00052 
00058   void set_alpha(T alfval);
00059 
00065   void set_numer(T numval);
00066 
00075   void set_type(const char* x, T val);
00076 
00082   void unset(T val);
00083 
00084 private:
00085 #ifndef __CINT__
00086   static const int table_count=256;
00087   static const int table_mask=table_count-1;
00088 #else
00089   #define table_count 256
00090   #define table_mask  255
00091 #endif
00092   T _initval;
00093   T _table[table_count];
00094 };
00095 
00096 template <class T> 
00097 inline 
00098 Translate<T>::Translate(void) 
00099   : _initval(T(0))
00100 {
00101 }
00102 
00103 template <class T> 
00104 inline 
00105 Translate<T>::Translate(T init) 
00106   : _initval(init)
00107 {
00108     reset();
00109 }
00110 
00111 template <class T> 
00112 inline 
00113 Translate<T>::~Translate(void) {
00114 }
00115 
00116 template <class T> 
00117 inline T&
00118 Translate<T>::operator[](char c) {
00119     return _table[int(c) & table_mask];
00120 }
00121 
00122 template <class T> 
00123 inline T
00124 Translate<T>::operator[](char c) const {
00125     return _table[int(c) & table_mask];
00126 }
00127 
00128 template <class T> 
00129 inline void
00130 Translate<T>::reset(void) {
00131     for (int i=0; i<table_count; ++i) _table[i] = _initval;
00132 }
00133 
00134 template <class T> 
00135 inline void
00136 Translate<T>::unset(T val) {
00137     for (int i=0; i<table_count; ++i) if (_table[i] == val) _table[i] = _initval;
00138 }
00139 
00140 template <class T> 
00141 inline void
00142 Translate<T>::char_type(const char* ccc, T val) {
00143     const char* p=ccc;
00144     while (*p) (*this)[*p++] = val;
00145 }
00146 
00147 template <class T> 
00148 inline void
00149 Translate<T>::set_type(const char* ccc, T val) {
00150     unset(val);
00151     char_type(ccc, val);
00152 }
00153 
00154 template <class T> 
00155 inline void
00156 Translate<T>::set_alpha(T val) {
00157     char_type("abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ", val);
00158 }
00159 
00160 template <class T> 
00161 inline void
00162 Translate<T>::set_numer(T val) {
00163     char_type("0123456789", val);
00164 }
00165 
00166 #endif // !defined(TRANSLATE_HH)

Generated on Sun Mar 8 19:20:51 2009 for dmt by  doxygen 1.5.4