Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
floops.hh
Go to the documentation of this file.
1 #ifndef INCLUDED_ObjexxFCL_floops_hh
2 #define INCLUDED_ObjexxFCL_floops_hh
3 
4 
5 // Fortran Compatible DO Loop Support
6 //
7 // Project: Objexx Fortran Compatibility Library (ObjexxFCL)
8 //
9 // Version: 3.0.0
10 //
11 // Language: C++
12 //
13 // Copyright (c) 2000-2009 Objexx Engineering, Inc. All Rights Reserved.
14 // Use of this source code or any derivative of it is restricted by license.
15 // Licensing is available from Objexx Engineering, Inc.: http://objexx.com Objexx@objexx.com
16 
17 // Semantics:
18 // Fortran: DO i = b, e, s
19 // C++ equiv: for ( std::size_t j = 1, n = floops( i, b, e, s ); j <= n; ++j, i += s )
20 
21 
22 // C++ Headers
23 #include <cassert>
24 #include <cstddef>
25 
26 
27 namespace ObjexxFCL {
28 
29 
30 // Fortran DO Loop Control
31 template< typename I, typename B, typename E, typename S >
32 inline
33 std::size_t
34 floops( I & i, B const & b, E const & e, S const & s ) // DO i = b, e, s
35 {
36  // Initializes the DO variable i and returns the number of iterations count
37 
38  // Convert control expressions to type of loop control variable
39  I const n1( b );
40  I const n2( e );
41  I const n3( s );
42  I const ZERO( 0 );
43 
44  // Initialize loop variable (Fortran does this even if loop never executes)
45  i = n1;
46 
47  // Get loop count being careful to avoid negative values in case I is an unsigned type
48  if ( n1 < n2 ) {
49  if ( n3 < ZERO ) {
50  return 0;
51  } else { // n3 > 0
52  assert( n3 > ZERO );
53  return ( ( n2 - n1 ) + n3 ) / n3;
54  }
55  } else if ( n1 > n2 ) {
56  if ( n3 > ZERO ) {
57  return 0;
58  } else { // n3 < 0
59  assert( n3 < ZERO );
60  return ( ( n1 - n2 ) - n3 ) / -n3;
61  }
62  } else { // n1 == n2
63  // Allow n3 == 0 in this case but Fortran doesn't
64  return 1;
65  }
66 }
67 
68 
69 // Fortran DO Loop Control with Implicit Step=1
70 template< typename I, typename B, typename E >
71 inline
72 std::size_t
73 floops( I & i, B const & b, E const & e ) // DO i = b, e
74 {
75  return floops( i, b, e, 1 );
76 }
77 
78 
79 } // namespace ObjexxFCL
80 
81 
82 #endif // INCLUDED_ObjexxFCL_floops_HH
std::size_t floops(I &i, B const &b, E const &e, S const &s)
Definition: floops.hh:34
std::string E(int const w, int const d, float const &t)
Exponential Format: float.
Definition: format.cc:312
std::string I(int const w, T const &t)
Integer Format.
Definition: format.hh:758