steghide 0.5.1
AUtils.h
Go to the documentation of this file.
1/*
2 * steghide 0.5.1 - a steganography program
3 * Copyright (C) 1999-2003 Stefan Hetzl <shetzl@chello.at>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21#ifndef SH_AUTILS_H
22#define SH_AUTILS_H
23
24#include <cmath>
25#ifndef log2
26 // this is in an #ifndef because some cmath implementations #define log2 and some not
27# define log2(x) (log(x) / log(2.0))
28#endif
29
34class AUtils {
35 public:
39 template<class T> static T max (T a, T b) ;
40
44 template<class T> static T min (T a, T b) ;
45
49 template<class T> static T div_roundup (T a, T b) ;
50
54 template<class T> static T bminus (T a, T b) ;
55
59 template<class T, T top> static T bplus (T a, T b) ;
60 template<class T> static T bplus (T a, T b, T top) ;
61
65 template<class T, class CTYPE> static T modsum (T* s, CTYPE n, T m) ;
66
70 template<class IT, class FT> static IT roundup (FT x) ;
71
75 template<class T> static T log2_ceil (T n) ;
76} ;
77
78template<class T>
79T AUtils::max (T a, T b)
80{
81 if (a > b) {
82 return a ;
83 }
84 else {
85 return b ;
86 }
87}
88
89template<class T>
90T AUtils::min (T a, T b)
91{
92 if (a < b) {
93 return a ;
94 }
95 else {
96 return b ;
97 }
98}
99
100template<class T>
102{
103 T c = b-- ;
104 return ((a + b) / c) ;
105}
106
107template<class T>
108T AUtils::bminus (T a, T b)
109{
110 if (a > b) {
111 return (a - b) ;
112 }
113 else {
114 return T() ;
115 }
116}
117
118template<class T, T top>
119T AUtils::bplus (T a, T b)
120{
121 a += b ;
122 if (a > top) {
123 return top ;
124 }
125 else {
126 return a ;
127 }
128}
129
130template<class T>
131T AUtils::bplus (T a, T b, T top)
132{
133 a += b ;
134 if (a > top) {
135 return top ;
136 }
137 else {
138 return a ;
139 }
140}
141
142template<class T, class CTYPE>
143T AUtils::modsum (T* s, CTYPE n, T m)
144{
145 T retval = 0 ;
146 for (CTYPE i = CTYPE() ; i < n ; ++i) {
147 retval = (retval + s[i]) % m ;
148 }
149 return retval ;
150}
151
152template<class IT, class FT>
154{
155 IT retval = 0 ;
156 FT intpart = (FT) ((IT) x) ;
157 if (x - intpart == (FT) 0.0) {
158 retval = (IT) x ;
159 }
160 else {
161 retval = ((IT) x) + 1 ;
162 }
163 return retval ;
164}
165
166template<class T>
168{
169 T retval = 0 ;
170 while (n > 1) {
171 n = div_roundup<T> (n, 2) ;
172 ++retval ;
173 }
174 return retval ;
175}
176
177#endif // ndef SH_AUTILS_H
provides some generic functions for non-standard arithmetic operations
Definition: AUtils.h:34
static T log2_ceil(T n)
Definition: AUtils.h:167
static T bplus(T a, T b)
Definition: AUtils.h:119
static T div_roundup(T a, T b)
Definition: AUtils.h:101
static T min(T a, T b)
Definition: AUtils.h:90
static T modsum(T *s, CTYPE n, T m)
Definition: AUtils.h:143
static T max(T a, T b)
Definition: AUtils.h:79
static T bminus(T a, T b)
Definition: AUtils.h:108
static IT roundup(FT x)
Definition: AUtils.h:153