operator_array2.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 
33 #ifndef __INCLUDE_MIST_OPERATOR_ARRAY2__
34 #define __INCLUDE_MIST_OPERATOR_ARRAY2__
35 
36 
37 
47 template < class T, class Allocator >
48 inline const array2< T, Allocator >& operator +=( array2< T, Allocator > &a1, const array2< T, Allocator > &a2 )
49 {
50  typedef typename array2< T, Allocator >::size_type size_type;
51 #if _CHECK_ARRAY2_OPERATION_ != 0
52  if( a1.size( ) != a2.size( ) )
53  {
54  // 足し算できません例外
55  ::std::cerr << "can't add array2s." << ::std::endl;
56  return( a1 );
57  }
58 #endif
59  for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] += static_cast< T >( a2[i] );
60  return( a1 );
61 }
62 
63 
73 template < class T, class Allocator >
74 inline const array2< T, Allocator >& operator -=( array2< T, Allocator > &a1, const array2< T, Allocator > &a2 )
75 {
76  typedef typename array2< T, Allocator >::size_type size_type;
77 #if _CHECK_ARRAY2_OPERATION_ != 0
78  if( a1.size( ) != a2.size( ) )
79  {
80  // 引き算できません例外
81  ::std::cerr << "can't subtract array2s." << ::std::endl;
82  return( a1 );
83  }
84 #endif
85  for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] -= static_cast< T >( a2[i] );
86  return( a1 );
87 }
88 
89 
99 template < class T, class Allocator >
100 inline const array2< T, Allocator >& operator *=( array2< T, Allocator > &a1, const array2< T, Allocator > &a2 )
101 {
102  typedef typename array2< T, Allocator >::size_type size_type;
103 #if _CHECK_ARRAY2_OPERATION_ != 0
104  if( a1.size( ) != a2.size( ) )
105  {
106  // 掛け算できません例外
107  ::std::cerr << "can't multiply array2s." << ::std::endl;
108  return( a1 );
109  }
110 #endif
111  for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] *= static_cast< T >( a2[i] );
112  return( a1 );
113 }
114 
115 
125 template < class T, class Allocator >
126 inline const array2< T, Allocator >& operator /=( array2< T, Allocator > &a1, const array2< T, Allocator > &a2 )
127 {
128  typedef typename array2< T, Allocator >::size_type size_type;
129 #if _CHECK_ARRAY2_OPERATION_ != 0
130  if( a1.size( ) != a2.size( ) )
131  {
132  // 割り算できません例外
133  ::std::cerr << "can't divide array2s." << ::std::endl;
134  return( a1 );
135  }
136 #endif
137  for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] /= static_cast< T >( a2[i] );
138  return( a1 );
139 }
140 
141 
151 template < class T, class Allocator >
152 inline const array2< T, Allocator >& operator +=( array2< T, Allocator > &a1, typename array2< T, Allocator >::value_type val )
153 {
154  typedef typename array2< T, Allocator >::size_type size_type;
155  for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] += val;
156  return( a1 );
157 }
158 
159 
169 template < class T, class Allocator >
170 inline const array2< T, Allocator >& operator -=( array2< T, Allocator > &a1, typename array2< T, Allocator >::value_type val )
171 {
172  typedef typename array2< T, Allocator >::size_type size_type;
173  for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] -= val;
174  return( a1 );
175 }
176 
177 
187 template < class T, class Allocator >
188 inline const array2< T, Allocator >& operator *=( array2< T, Allocator > &a1, typename array2< T, Allocator >::value_type val )
189 {
190  typedef typename array2< T, Allocator >::size_type size_type;
191  for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] *= val;
192  return( a1 );
193 }
194 
195 
205 template < class T, class Allocator >
206 inline const array2< T, Allocator >& operator /=( array2< T, Allocator > &a1, typename array2< T, Allocator >::value_type val )
207 {
208  typedef typename array2< T, Allocator >::size_type size_type;
209  typedef typename array2< T, Allocator >::value_type value_type;
210 #if _CHECK_ARRAY2_OPERATION_ != 0
211  if( val == value_type( 0 ) )
212  {
213  // ゼロ除算発生
214  ::std::cerr << "zero division occured." << ::std::endl;
215  return( a1 );
216  }
217 #endif
218  for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] /= val;
219  return( a1 );
220 }
221 
222 
223 
232 template < class T, class Allocator >
233 inline array2< T, Allocator > operator -( const array2< T, Allocator > &a )
234 {
235  typedef typename array2< T, Allocator >::size_type size_type;
236  array2< T, Allocator > o( a.size1( ), a.size2( ), a.reso1( ), a.reso2( ) );
237  for( size_type i = 0 ; i < o.size( ) ; i++ ) o[i] = -a[i];
238  return( o );
239 }
240 
241 
242 
252 template < class T, class Allocator >
253 inline array2< T, Allocator > operator +( const array2< T, Allocator > &a1, const array2< T, Allocator > &a2 )
254 {
255  array2< T, Allocator > tmp( a1 );
256  return( tmp += a2 );
257 }
258 
259 
260 
270 template < class T, class Allocator >
271 inline array2< T, Allocator > operator -( const array2< T, Allocator > &a1, const array2< T, Allocator > &a2 )
272 {
273  array2< T, Allocator > tmp( a1 );
274  return( tmp -= a2 );
275 }
276 
277 
278 
288 template < class T, class Allocator >
289 inline array2< T, Allocator > operator *( const array2< T, Allocator > &a1, const array2< T, Allocator > &a2 )
290 {
291  array2< T, Allocator > tmp( a1 );
292  return( tmp *= a2 );
293 }
294 
295 
296 
306 template < class T, class Allocator >
307 inline array2< T, Allocator > operator /( const array2< T, Allocator > &a1, const array2< T, Allocator > &a2 )
308 {
309  array2< T, Allocator > tmp( a1 );
310  return( tmp /= a2 );
311 }
312 
313 
314 
324 template < class T, class Allocator >
325 inline array2< T, Allocator > operator +( const array2< T, Allocator > &a, typename array2< T, Allocator >::value_type val )
326 {
327  array2< T, Allocator > tmp( a );
328  return( tmp += val );
329 }
330 
331 
341 template < class T, class Allocator >
342 inline array2< T, Allocator > operator +( typename array2< T, Allocator >::value_type val, const array2< T, Allocator > &a )
343 {
344  array2< T, Allocator > tmp( a );
345  return( tmp += val );
346 }
347 
348 
358 template < class T, class Allocator >
359 inline array2< T, Allocator > operator -( const array2< T, Allocator > &a, typename array2< T, Allocator >::value_type val )
360 {
361  array2< T, Allocator > tmp( a );
362  return( tmp -= val );
363 }
364 
365 
375 template < class T, class Allocator >
376 inline array2< T, Allocator > operator -( typename array2< T, Allocator >::value_type val, const array2< T, Allocator > &a )
377 {
378  array2< T, Allocator > tmp( a.size1( ), a.size2( ), val );
379  return( tmp -= a );
380 }
381 
382 
392 template < class T, class Allocator >
393 inline array2< T, Allocator > operator *( const array2< T, Allocator > &a, typename array2< T, Allocator >::value_type val )
394 {
395  array2< T, Allocator > tmp( a );
396  return( tmp *= val );
397 }
398 
399 
409 template < class T, class Allocator >
410 inline array2< T, Allocator > operator *( typename array2< T, Allocator >::value_type val, const array2< T, Allocator > &a )
411 {
412  array2< T, Allocator > tmp( a );
413  return( tmp *= val );
414 }
415 
416 
426 template < class T, class Allocator >
427 inline array2< T, Allocator > operator /( const array2< T, Allocator > &a, typename array2< T, Allocator >::value_type val )
428 {
429  array2< T, Allocator > tmp( a );
430  return( tmp /= val );
431 }
432 
433 
434 #endif // __INCLUDE_MIST_OPERATOR_ARRAY2__
435 

Generated on Wed Nov 12 2014 19:44:21 for MIST by doxygen 1.8.1.2