operators.h 4.25 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file operators.h
 * \brief provide operators for iteratable objects (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
6
 * $Id: operators.h,v 1.7 2005-07-05 13:12:00 tforb Exp $
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
10
11
12
13
14
15
 * \author Thomas Forbriger
 * \date 10/02/2004
 * 
 * provide operators for iteratable objects (prototypes)
 * 
 * Copyright (c) 2004 by Thomas Forbriger (BFO Schiltach) 
 * 
 * REVISIONS and CHANGES 
 *  - 10/02/2004   V1.0   Thomas Forbriger
16
17
 *  - 05/07/2005   V1.1   support modification of data in containers that are
 *                        declared const
thomas.forbriger's avatar
thomas.forbriger committed
18
19
20
21
22
 * 
 * ============================================================================
 */

// include guard
thomas.forbriger's avatar
thomas.forbriger committed
23
#ifndef AFF_OPERATORS_CLASS
thomas.forbriger's avatar
thomas.forbriger committed
24
25
26
27
#error "never ever inlcude this from your code!"
#endif
#ifndef AFF_OPERATORS_CONSTCLASS
#error "definition of container of const is missing!"
thomas.forbriger's avatar
thomas.forbriger committed
28
#endif
thomas.forbriger's avatar
thomas.forbriger committed
29

thomas.forbriger's avatar
thomas.forbriger committed
30
#include<aff/iterator.h>
thomas.forbriger's avatar
thomas.forbriger committed
31

thomas.forbriger's avatar
thomas.forbriger committed
32
namespace aff {
thomas.forbriger's avatar
thomas.forbriger committed
33
34
35
36
  
#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 >
thomas.forbriger's avatar
thomas.forbriger committed
37

thomas.forbriger's avatar
thomas.forbriger committed
38
39
#define AFF_OPERATORS_UNOP( CONT, OP ) \
  template<class AFF_OPERATORS_TEMPAR > \
40
  const CONT& operator OP ## =(const CONT& container, \
thomas.forbriger's avatar
thomas.forbriger committed
41
42
43
44
45
                         typename CONT::Tconst_reference value) \
  { \
    for (aff::Iterator< CONT > i(container); i.valid(); ++i) \
    { (*i) OP ## = value; } \
    return(container); \
thomas.forbriger's avatar
thomas.forbriger committed
46
  }
thomas.forbriger's avatar
thomas.forbriger committed
47

thomas.forbriger's avatar
thomas.forbriger committed
48
49
50
51
52
#define AFF_OPERATORS_BINOP( RETCONT, INCONT, OP ) \
  template<class AFF_OPERATORS_TEMPAR > \
  RETCONT operator OP(const INCONT& container, \
                       typename INCONT::Tconst_reference value) \
  { \
thomas.forbriger's avatar
thomas.forbriger committed
53
54
55
56
57
    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;  } \
thomas.forbriger's avatar
thomas.forbriger committed
58
    return(retval); \
59
60
61
62
63
64
  } \
  template<class AFF_OPERATORS_TEMPAR > \
  RETCONT operator OP(typename INCONT::Tconst_reference value, \
                      const INCONT& container) \
  { return( container OP value ); }

thomas.forbriger's avatar
thomas.forbriger committed
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
#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); \
  }

#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); \
  }

thomas.forbriger's avatar
thomas.forbriger committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#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

#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

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#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

#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

thomas.forbriger's avatar
thomas.forbriger committed
125
126
#undef AFF_OPERATORS_UNOP
#undef AFF_OPERATORS_BINOP
127
#undef AFF_OPERATORS_BINOPB
thomas.forbriger's avatar
thomas.forbriger committed
128
129
130
#undef AFF_OPERATORS_CONSTCONT
#undef AFF_OPERATORS_CONT
#undef AFF_OPERATORS_TEMPAR
thomas.forbriger's avatar
thomas.forbriger committed
131
}
thomas.forbriger's avatar
thomas.forbriger committed
132
133

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