operator_array1.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_ARRAY1__
34 #define __INCLUDE_MIST_OPERATOR_ARRAY1__
35 
36 
37 
47 template < class T, class Allocator >
48 inline const array1< T, Allocator >& operator +=( array1< T, Allocator > &a1, const array1< T, Allocator > &a2 )
49 {
50  typedef typename array1< T, Allocator >::size_type size_type;
51 #if _CHECK_ARRAY1_OPERATION_ != 0
52  if( a1.size( ) != a2.size( ) )
53  {
54  // 足し算できません例外
55  ::std::cerr << "can't add array1s." << ::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 array1< T, Allocator >& operator -=( array1< T, Allocator > &a1, const array1< T, Allocator > &a2 )
75 {
76  typedef typename array1< T, Allocator >::size_type size_type;
77 #if _CHECK_ARRAY1_OPERATION_ != 0
78  if( a1.size( ) != a2.size( ) )
79  {
80  // 引き算できません例外
81  ::std::cerr << "can't subtract array1s." << ::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 array1< T, Allocator >& operator *=( array1< T, Allocator > &a1, const array1< T, Allocator > &a2 )
101 {
102  typedef typename array1< T, Allocator >::size_type size_type;
103 #if _CHECK_ARRAY1_OPERATION_ != 0
104  if( a1.size( ) != a2.size( ) )
105  {
106  // 掛け算できません例外
107  ::std::cerr << "can't multiply array1s." << ::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 array1< T, Allocator >& operator /=( array1< T, Allocator > &a1, const array1< T, Allocator > &a2 )
127 {
128  typedef typename array1< T, Allocator >::size_type size_type;
129 #if _CHECK_ARRAY1_OPERATION_ != 0
130  if( a1.size( ) != a2.size( ) )
131  {
132  // 割り算できません例外
133  ::std::cerr << "can't divide array1s." << ::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 array1< T, Allocator >& operator +=( array1< T, Allocator > &a1, typename array1< T, Allocator >::value_type val )
153 {
154  typedef typename array1< 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 array1< T, Allocator >& operator -=( array1< T, Allocator > &a1, typename array1< T, Allocator >::value_type val )
171 {
172  typedef typename array1< 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 array1< T, Allocator >& operator *=( array1< T, Allocator > &a1, typename array1< T, Allocator >::value_type val )
189 {
190  typedef typename array1< 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 array1< T, Allocator >& operator /=( array1< T, Allocator > &a1, typename array1< T, Allocator >::value_type val )
207 {
208  typedef typename array1< T, Allocator >::size_type size_type;
209 #ifndef _CHECK_ARRAY_OPERATION_
210  if( val == 0 )
211  {
212  // ゼロ除算発生
213  ::std::cerr << "zero division occured." << ::std::endl;
214  return;
215  }
216 #endif
217  for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] /= val;
218  return( a1 );
219 }
220 
221 
230 template < class T, class Allocator >
231 inline array1< T, Allocator > operator -( const array1< T, Allocator > &a )
232 {
233  typedef typename array1< T, Allocator >::size_type size_type;
234  array1< T, Allocator > o( a.size1( ), a.reso1( ) );
235  for( size_type i = 0 ; i < o.size( ) ; i++ ) o[i] = -a[i];
236  return( o );
237 }
238 
239 
249 template < class T, class Allocator >
250 inline array1< T, Allocator > operator +( const array1< T, Allocator > &a1, const array1< T, Allocator > &a2 )
251 {
252  array1< T, Allocator > tmp( a1 );
253  return( tmp += a2 );
254 }
255 
256 
266 template < class T, class Allocator >
267 inline array1< T, Allocator > operator -( const array1< T, Allocator > &a1, const array1< T, Allocator > &a2 )
268 {
269  array1< T, Allocator > tmp( a1 );
270  return( tmp -= a2 );
271 }
272 
273 
283 template < class T, class Allocator >
284 inline array1< T, Allocator > operator *( const array1< T, Allocator > &a1, const array1< T, Allocator > &a2 )
285 {
286  array1< T, Allocator > tmp( a1 );
287  return( tmp *= a2 );
288 }
289 
290 
300 template < class T, class Allocator >
301 inline array1< T, Allocator > operator /( const array1< T, Allocator > &a1, const array1< T, Allocator > &a2 )
302 {
303  array1< T, Allocator > tmp( a1 );
304  return( tmp /= a2 );
305 }
306 
307 
317 template < class T, class Allocator >
318 inline array1< T, Allocator > operator +( const array1< T, Allocator > &a, typename array1< T, Allocator >::value_type val )
319 {
320  array1< T, Allocator > tmp( a );
321  return( tmp += val );
322 }
323 
324 
334 template < class T, class Allocator >
335 inline array1< T, Allocator > operator +( typename array1< T, Allocator >::value_type val, const array1< T, Allocator > &a )
336 {
337  array1< T, Allocator > tmp( a );
338  return( tmp += val );
339 }
340 
341 
351 template < class T, class Allocator >
352 inline array1< T, Allocator > operator -( const array1< T, Allocator > &a, typename array1< T, Allocator >::value_type val )
353 {
354  array1< T, Allocator > tmp( a );
355  return( tmp -= val );
356 }
357 
358 
368 template < class T, class Allocator >
369 inline array1< T, Allocator > operator -( typename array1< T, Allocator >::value_type val, const array1< T, Allocator > &a )
370 {
371  array1< T, Allocator > tmp( a.size( ), val );
372  return( tmp -= a );
373 }
374 
375 
385 template < class T, class Allocator >
386 inline array1< T, Allocator > operator *( const array1< T, Allocator > &a, typename array1< T, Allocator >::value_type val )
387 {
388  array1< T, Allocator > tmp( a );
389  return( tmp *= val );
390 }
391 
392 
402 template < class T, class Allocator >
403 inline array1< T, Allocator > operator *( typename array1< T, Allocator >::value_type val, const array1< T, Allocator > &a )
404 {
405  array1< T, Allocator > tmp( a );
406  return( tmp *= val );
407 }
408 
409 
419 template < class T, class Allocator >
420 inline array1< T, Allocator > operator /( const array1< T, Allocator > &a, typename array1< T, Allocator >::value_type val )
421 {
422  array1< T, Allocator > tmp( a );
423  return( tmp /= val );
424 }
425 
426 
427 #endif // __INCLUDE_MIST_OPERATOR_ARRAY1__
428 

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