Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

AUtils.h

Go to the documentation of this file.
00001 /*
00002  * steghide 0.5.1 - a steganography program
00003  * Copyright (C) 1999-2003 Stefan Hetzl <shetzl@chello.at>
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  *
00019  */
00020 
00021 #ifndef SH_AUTILS_H
00022 #define SH_AUTILS_H
00023 
00024 #include <cmath>
00025 #ifndef log2
00026         // this is in an #ifndef because some cmath implementations #define log2 and some not
00027 #       define log2(x) (log(x) / log(2.0))
00028 #endif
00029 
00034 class AUtils {
00035         public:
00039         template<class T> static T max (T a, T b) ;
00040 
00044         template<class T> static T min (T a, T b) ;
00045 
00049         template<class T> static T div_roundup (T a, T b) ;
00050 
00054         template<class T> static T bminus (T a, T b) ;
00055 
00059         template<class T, T top> static T bplus (T a, T b) ;
00060         template<class T> static T bplus (T a, T b, T top) ;
00061 
00065         template<class T, class CTYPE> static T modsum (T* s, CTYPE n, T m) ;
00066 
00070         template<class IT, class FT> static IT roundup (FT x) ;
00071 
00075         template<class T> static T log2_ceil (T n) ;
00076 } ;
00077 
00078 template<class T>
00079 T AUtils::max (T a, T b)
00080 {
00081         if (a > b) {
00082                 return a ;
00083         }
00084         else {
00085                 return b ;
00086         }
00087 }
00088 
00089 template<class T>
00090 T AUtils::min (T a, T b)
00091 {
00092         if (a < b) {
00093                 return a ;
00094         }
00095         else {
00096                 return b ;
00097         }
00098 }
00099 
00100 template<class T>
00101 T AUtils::div_roundup (T a, T b)
00102 {
00103         T c = b-- ;
00104         return ((a + b) / c) ;
00105 }
00106 
00107 template<class T>
00108 T AUtils::bminus (T a, T b)
00109 {
00110         if (a > b) {
00111                 return (a - b) ;
00112         }
00113         else {
00114                 return T() ;
00115         }
00116 }
00117 
00118 template<class T, T top>
00119 T AUtils::bplus (T a, T b)
00120 {
00121         a += b ;
00122         if (a > top) {
00123                 return top ;
00124         }
00125         else {
00126                 return a ;
00127         }
00128 }
00129 
00130 template<class T>
00131 T AUtils::bplus (T a, T b, T top)
00132 {
00133         a += b ;
00134         if (a > top) {
00135                 return top ;
00136         }
00137         else {
00138                 return a ;
00139         }
00140 }
00141 
00142 template<class T, class CTYPE>
00143 T AUtils::modsum (T* s, CTYPE n, T m)
00144 {
00145         T retval = 0 ;
00146         for (CTYPE i = CTYPE() ; i < n ; ++i) {
00147                 retval = (retval + s[i]) % m ;
00148         }
00149         return retval ;
00150 }
00151 
00152 template<class IT, class FT>
00153 IT AUtils::roundup (FT x)
00154 {
00155         IT retval = 0 ;
00156         FT intpart = (FT) ((IT) x) ;
00157         if (x - intpart == (FT) 0.0) {
00158                 retval = (IT) x ;
00159         }
00160         else {
00161                 retval = ((IT) x) + 1 ;
00162         }
00163         return retval ;
00164 }
00165 
00166 template<class T>
00167 T AUtils::log2_ceil (T n)
00168 {
00169         T retval = 0 ;
00170         while (n > 1) {
00171                 n = div_roundup<T> (n, 2) ;
00172                 ++retval ;
00173         }
00174         return retval ;
00175 }
00176 
00177 #endif // ndef SH_AUTILS_H

Generated on Thu Nov 13 23:44:21 2003 for steghide by doxygen 1.3.3