- メインページ
- モジュール
- ネームスペース
- データ構造
- ファイル

- ファイル一覧
- グローバル

operator_array.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_ARRAY__
34 #define __INCLUDE_MIST_OPERATOR_ARRAY__
35
36
46 template < class T, class Allocator >
47 inline const array< T, Allocator >& operator +=( array< T, Allocator > &a1, const array< T, Allocator > &a2 )
48 {
49 typedef typename array< T, Allocator >::size_type size_type;
50 #if _CHECK_ARRAY_OPERATION_ != 0
51 if( a1.size( ) != a2.size( ) )
52 {
53 // 足し算できません例外
54 ::std::cerr << "can't add arrays." << ::std::endl;
55 return( a1 );
56 }
57 #endif
58 for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] += static_cast< T >( a2[i] );
59 return( a1 );
60 }
61
62
72 template < class T, class Allocator >
73 inline const array< T, Allocator >& operator -=( array< T, Allocator > &a1, const array< T, Allocator > &a2 )
74 {
75 typedef typename array< T, Allocator >::size_type size_type;
76 #if _CHECK_ARRAY_OPERATION_ != 0
77 if( a1.size( ) != a2.size( ) )
78 {
79 // 引き算できません例外
80 ::std::cerr << "can't subtract arrays." << ::std::endl;
81 return( a1 );
82 }
83 #endif
84 for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] -= static_cast< T >( a2[i] );
85 return( a1 );
86 }
87
88
98 template < class T, class Allocator >
99 inline const array< T, Allocator >& operator *=( array< T, Allocator > &a1, const array< T, Allocator > &a2 )
100 {
101 typedef typename array< T, Allocator >::size_type size_type;
102 #if _CHECK_ARRAY_OPERATION_ != 0
103 if( a1.size( ) != a2.size( ) )
104 {
105 // 掛け算できません例外
106 ::std::cerr << "can't multiply arrays." << ::std::endl;
107 return( a1 );
108 }
109 #endif
110 for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] *= static_cast< T >( a2[i] );
111 return( a1 );
112 }
113
114
124 template < class T, class Allocator >
125 inline const array< T, Allocator >& operator /=( array< T, Allocator > &a1, const array< T, Allocator > &a2 )
126 {
127 typedef typename array< T, Allocator >::size_type size_type;
128 #if _CHECK_ARRAY_OPERATION_ != 0
129 if( a1.size( ) != a2.size( ) )
130 {
131 // 割り算できません例外
132 ::std::cerr << "can't divide arrays." << ::std::endl;
133 return( a1 );
134 }
135 #endif
136 for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] /= static_cast< T >( a2[i] );
137 return( a1 );
138 }
139
140
150 template < class T, class Allocator >
151 inline const array< T, Allocator >& operator +=( array< T, Allocator > &a1, typename array< T, Allocator >::value_type val )
152 {
153 typedef typename array< T, Allocator >::size_type size_type;
154 for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] += val;
155 return( a1 );
156 }
157
158
168 template < class T, class Allocator >
169 inline const array< T, Allocator >& operator -=( array< T, Allocator > &a1, typename array< T, Allocator >::value_type val )
170 {
171 typedef typename array< T, Allocator >::size_type size_type;
172 for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] -= val;
173 return( a1 );
174 }
175
176
186 template < class T, class Allocator >
187 inline const array< T, Allocator >& operator *=( array< T, Allocator > &a1, typename array< T, Allocator >::value_type val )
188 {
189 typedef typename array< T, Allocator >::size_type size_type;
190 for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] *= val;
191 return( a1 );
192 }
193
194
204 template < class T, class Allocator >
205 inline const array< T, Allocator >& operator /=( array< T, Allocator > &a1, typename array< T, Allocator >::value_type val )
206 {
207 typedef typename array< T, Allocator >::size_type size_type;
208 #if _CHECK_ARRAY_OPERATION_ != 0
209 if( val == 0 )
210 {
211 // ゼロ除算発生
212 ::std::cerr << "zero division occured." << ::std::endl;
213 return( a1 );
214 }
215 #endif
216 for( size_type i = 0 ; i < a1.size( ) ; i++ ) a1[i] /= val;
217 return( a1 );
218 }
219
220
221
230 template < class T, class Allocator >
231 inline array< T, Allocator > operator -( const array< T, Allocator > &a )
232 {
233 typedef typename array< T, Allocator >::size_type size_type;
234 array< T, Allocator > o( a.size( ) );
235 for( size_type i = 0 ; i < o.size( ) ; i++ ) o[i] = -a[i];
236 return( o );
237 }
238
239
240
241
251 template < class T, class Allocator >
252 inline array< T, Allocator > operator +( const array< T, Allocator > &a1, const array< T, Allocator > &a2 )
253 {
254 array< T, Allocator > tmp( a1 );
255 return( tmp += a2 );
256 }
257
258
259
260
270 template < class T, class Allocator >
271 inline array< T, Allocator > operator -( const array< T, Allocator > &a1, const array< T, Allocator > &a2 )
272 {
273 array< T, Allocator > tmp( a1 );
274 return( tmp -= a2 );
275 }
276
277
278
279
289 template < class T, class Allocator >
290 inline array< T, Allocator > operator *( const array< T, Allocator > &a1, const array< T, Allocator > &a2 )
291 {
292 array< T, Allocator > tmp( a1 );
293 return( tmp *= a2 );
294 }
295
296
297
307 template < class T, class Allocator >
308 inline array< T, Allocator > operator /( const array< T, Allocator > &a1, const array< T, Allocator > &a2 )
309 {
310 array< T, Allocator > tmp( a1 );
311 return( tmp /= a2 );
312 }
313
314
324 template < class T, class Allocator >
325 inline array< T, Allocator > operator +( const array< T, Allocator > &a, typename array< T, Allocator >::value_type val )
326 {
327 array< T, Allocator > tmp( a );
328 return( tmp += val );
329 }
330
331
341 template < class T, class Allocator >
342 inline array< T, Allocator > operator +( typename array< T, Allocator >::value_type val, const array< T, Allocator > &a )
343 {
344 array< T, Allocator > tmp( a );
345 return( tmp += val );
346 }
347
348
358 template < class T, class Allocator >
359 inline array< T, Allocator > operator -( const array< T, Allocator > &a, typename array< T, Allocator >::value_type val )
360 {
361 array< T, Allocator > tmp( a );
362 return( tmp -= val );
363 }
364
365
375 template < class T, class Allocator >
376 inline array< T, Allocator > operator -( typename array< T, Allocator >::value_type val, const array< T, Allocator > &a )
377 {
378 array< T, Allocator > tmp( a.size( ), val );
379 return( tmp -= a );
380 }
381
382
392 template < class T, class Allocator >
393 inline array< T, Allocator > operator *( const array< T, Allocator > &a, typename array< T, Allocator >::value_type val )
394 {
395 array< T, Allocator > tmp( a );
396 return( tmp *= val );
397 }
398
399
409 template < class T, class Allocator >
410 inline array< T, Allocator > operator *( typename array< T, Allocator >::value_type val, const array< T, Allocator > &a )
411 {
412 array< T, Allocator > tmp( a );
413 return( tmp *= val );
414 }
415
416
426 template < class T, class Allocator >
427 inline array< T, Allocator > operator /( const array< T, Allocator > &a, typename array< T, Allocator >::value_type val )
428 {
429 array< T, Allocator > tmp( a );
430 return( tmp /= val );
431 }
432
433 #endif // __INCLUDE_MIST_OPERATOR_ARRAY__
434