operators.h 6.87 KB
Newer Older
1
2
3
4
5
/*! \file operators.h
 * \brief provide operators for iteratable objects (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
6
 * $Id$
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
 * \author Thomas Forbriger
 * \date 10/02/2004
 * 
 * provide operators for iteratable objects (prototypes)
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 * 
 * Copyright (c) 2004 by Thomas Forbriger (BFO Schiltach) 
 * 
 * REVISIONS and CHANGES 
 *  - 10/02/2004   V1.0   Thomas Forbriger
 *  - 05/07/2005   V1.1   support modification of data in containers that are
 *                        declared const
 *  - 15/05/2011   V1.2   added group name (thof)
 * 
 * ============================================================================
 */

// include guard
#ifndef AFF_OPERATORS_CLASS
#error "never ever inlcude this from your code!"
#endif

#ifndef AFF_OPERATORS_CONSTCLASS
#error "definition of container of const is missing!"
#endif

#include<aff/iterator.h>

namespace aff {

/*! \brief Container operators
 * \defgroup group_operators Container operators
 *
 * Operators for containers are defined through a macro expansion mechanism,
 * since the basic algorithm is the same for all containers.
 * We make use of iterators to traverse the elements of the containers.
 */
  
/*! \brief Basic macro type definitions for operators (see details).
 * \ingroup group_operators
 *
 * The macros
 * - #AFF_OPERATORS_CLASS
 * - #AFF_OPERATORS_CONSTCLASS
 * 
 * are defined by the header file including this header file.
 * \sa arrayoperators.h, seriesoperators.h
 */
//@{
#define AFF_OPERATORS_TEMPAR T
#define AFF_OPERATORS_CONT AFF_OPERATORS_CLASS< AFF_OPERATORS_TEMPAR >
#define AFF_OPERATORS_CONSTCONT AFF_OPERATORS_CONSTCLASS< AFF_OPERATORS_TEMPAR >
//@}

/*----------------------------------------------------------------------*/

/*! \brief unary operator macro for containers with common value type
 * \ingroup group_operators
 */
#define AFF_OPERATORS_UNOP( CONT, OP ) \
  template<class AFF_OPERATORS_TEMPAR > \
  const CONT& operator OP ## =(const CONT& container, \
                         typename CONT::Tconst_reference value) \
  { \
    for (aff::Iterator< CONT > i(container); i.valid(); ++i) \
    { (*i) OP ## = value; } \
    return(container); \
  }

/*----------------------------------------------------------------------*/

/*! \brief binary operator macro for containers with common value type
 * \ingroup group_operators
 */
#define AFF_OPERATORS_BINOP( RETCONT, INCONT, OP ) \
  template<class AFF_OPERATORS_TEMPAR > \
  RETCONT operator OP(const INCONT& container, \
                       typename INCONT::Tconst_reference value) \
  { \
    RETCONT retval(container.shape()); \
    aff::Browser< typename INCONT::Tcontainer > i(container); \
    aff::Iterator< typename RETCONT::Tcontainer > o(retval); \
    while ( i.valid() && o. valid() ) \
    {  (*o) = (*i) OP value;  ++o; ++i;  } \
    return(retval); \
  } \
  template<class AFF_OPERATORS_TEMPAR > \
  RETCONT operator OP(typename INCONT::Tconst_reference value, \
                      const INCONT& container) \
  { return( container OP value ); }

/*----------------------------------------------------------------------*/

/*! \brief unary operator macro for containers with different value type
 * \ingroup group_operators
 */
#define AFF_OPERATORS_UNOPB( RETCONT, INCONT, OP ) \
  template<class A , class B > \
  RETCONT< A >& operator OP ## =(RETCONT< A >& container1, \
                           const INCONT< B >& container2) \
  { \
    aff::Iterator< RETCONT< A > > i1(container1); \
    aff::Browser< INCONT< B > > i2(container2); \
    while ( i1.valid() && i2.valid() ) \
    {  (*i1) OP ## = (*i2); ++i1; ++i2; } \
    return(container1); \
  }

/*----------------------------------------------------------------------*/

/*! \brief binary operator macro for containers with different value type
 * \ingroup group_operators
 */
#define AFF_OPERATORS_BINOPB( RETCONT, INCONT, OP ) \
  template<class A , class B > \
  RETCONT< A > operator OP(const INCONT< A >& container1, \
                           const INCONT< B >& container2) \
  { \
    RETCONT< A > retval(container1.shape()); \
    retval=A(0); \
    aff::Browser< INCONT< A > > i1(container1); \
    aff::Browser< INCONT< B > > i2(container2); \
    aff::Iterator< RETCONT< A > > o(retval); \
    while ( i1.valid() && o.valid() && i2.valid() ) \
    {  (*o) = (*i1) OP (*i2);  ++o; ++i1; ++i2; } \
    return(retval); \
  }

/*======================================================================*/

/*! \brief unary operators
 * \ingroup group_operators
 * This makes use of #AFF_OPERATORS_THEUNOP and #AFF_OPERATORS_UNOP
 */
//@{
#define AFF_OPERATORS_THEUNOP( OP ) \
  AFF_OPERATORS_UNOP( AFF_OPERATORS_CONT , OP )
  AFF_OPERATORS_THEUNOP( + )
  AFF_OPERATORS_THEUNOP( * )
  AFF_OPERATORS_THEUNOP( / )
  AFF_OPERATORS_THEUNOP( - )
//@}
#undef AFF_OPERATORS_THEUNOP

/*! \brief binary operators
 * \ingroup group_operators
 * This makes use of #AFF_OPERATORS_THEBINOP and #AFF_OPERATORS_BINOP
 */
//@{
#define AFF_OPERATORS_THEBINOP( OP ) \
  AFF_OPERATORS_BINOP( AFF_OPERATORS_CONT , AFF_OPERATORS_CONSTCONT, OP )
  AFF_OPERATORS_THEBINOP( + )
  AFF_OPERATORS_THEBINOP( * )
  AFF_OPERATORS_THEBINOP( / )
  AFF_OPERATORS_THEBINOP( - )
//@}
#undef AFF_OPERATORS_THEBINOP

/*! \brief binary operators
 * \ingroup group_operators
 * This makes use of #AFF_OPERATORS_THEBINOP and #AFF_OPERATORS_BINOPB
 */
//@{
#define AFF_OPERATORS_THEBINOP( OP ) \
  AFF_OPERATORS_BINOPB( AFF_OPERATORS_CLASS , AFF_OPERATORS_CONSTCLASS , OP )
  AFF_OPERATORS_THEBINOP( + )
  AFF_OPERATORS_THEBINOP( * )
  AFF_OPERATORS_THEBINOP( / )
  AFF_OPERATORS_THEBINOP( - )
//@}
#undef AFF_OPERATORS_THEBINOP

/*! \brief unary operators
 * \ingroup group_operators
 * This makes use of #AFF_OPERATORS_THEUNOP and #AFF_OPERATORS_UNOPB
 */
//@{
#define AFF_OPERATORS_THEUNOP( OP ) \
  AFF_OPERATORS_UNOPB( AFF_OPERATORS_CLASS , AFF_OPERATORS_CONSTCLASS , OP )
  AFF_OPERATORS_THEUNOP( + )
  AFF_OPERATORS_THEUNOP( * )
  AFF_OPERATORS_THEUNOP( / )
  AFF_OPERATORS_THEUNOP( - )
//@}
#undef AFF_OPERATORS_THEUNOP

#undef AFF_OPERATORS_UNOP
#undef AFF_OPERATORS_BINOP
#undef AFF_OPERATORS_BINOPB
#undef AFF_OPERATORS_CONSTCONT
#undef AFF_OPERATORS_CONT
#undef AFF_OPERATORS_TEMPAR
}

/* ----- END OF operators.h ----- */