aboutsummaryrefslogtreecommitdiff
path: root/src/jtutil
diff options
context:
space:
mode:
authorjthorn <jthorn@f88db872-0e4f-0410-b76b-b9085cfa78c5>2002-03-27 20:37:34 +0000
committerjthorn <jthorn@f88db872-0e4f-0410-b76b-b9085cfa78c5>2002-03-27 20:37:34 +0000
commit08555ac2f37b48d03dee307a8356a5c709b31efb (patch)
tree8654b81418c6a377387061be8ea6ad8e7c272c9c /src/jtutil
parent9aa4aeea088defb0049d20d49cb25838c9ee5a8a (diff)
put *everything* into namespace jtutil::
start changing arrays to allow user-specified strides (only array2d so far) git-svn-id: http://svn.einsteintoolkit.org/cactus/EinsteinAnalysis/AHFinderDirect/trunk@378 f88db872-0e4f-0410-b76b-b9085cfa78c5
Diffstat (limited to 'src/jtutil')
-rw-r--r--src/jtutil/README3
-rw-r--r--src/jtutil/array.cc102
-rw-r--r--src/jtutil/array.hh38
-rw-r--r--src/jtutil/cpm_map.cc17
-rw-r--r--src/jtutil/cpm_map.hh3
-rw-r--r--src/jtutil/fuzzy.cc30
-rw-r--r--src/jtutil/linear_map.cc52
-rw-r--r--src/jtutil/linear_map.hh5
-rw-r--r--src/jtutil/miscfp.cc16
-rw-r--r--src/jtutil/norm.cc4
-rw-r--r--src/jtutil/round.cc19
-rw-r--r--src/jtutil/stdc.h2
-rw-r--r--src/jtutil/test_array.cc10
-rw-r--r--src/jtutil/test_cpm_map.cc1
-rw-r--r--src/jtutil/test_fuzzy.cc1
-rw-r--r--src/jtutil/test_linear_map.cc2
-rw-r--r--src/jtutil/test_modulo.cc7
-rw-r--r--src/jtutil/test_norm.cc9
-rw-r--r--src/jtutil/test_round.cc1
-rw-r--r--src/jtutil/util.hh17
20 files changed, 235 insertions, 104 deletions
diff --git a/src/jtutil/README b/src/jtutil/README
index 2273735..d16004e 100644
--- a/src/jtutil/README
+++ b/src/jtutil/README
@@ -1,6 +1,7 @@
This directory holds low-level utility code that we use, but that's
not really specific to this project -- things like min/max templates,
-multidimensional array classes, fuzzy arithmetic routines, etc:
+multidimensional array classes, fuzzy arithmetic routines, etc. All
+of this is in namespace jtutil::.
array<fp>
is a template class (templated on the integer or floating-point
diff --git a/src/jtutil/array.cc b/src/jtutil/array.cc
index 4d308ff..8ca4246 100644
--- a/src/jtutil/array.cc
+++ b/src/jtutil/array.cc
@@ -1,23 +1,24 @@
// array.cc -- array template classes
// $Id$
//
-// array1d::array1d - 1D array template constructor
-// array1d::~array1d - 1D array template destructor
+// jtutil::array1d::array1d - 1D array template constructor
+// jtutil::array1d::~array1d - 1D array template destructor
//
-// array2d::array2d - 2D array template constructor
-// array2d::~array2d - 2D array template destructor
+// jtutil::array2d::array2d - 2D array template constructor
+// jtutil::array2d::~array2d - 2D array template destructor
//
-// array3d::array3d - 3D array template constructor
-// array3d::~array3d - 3D array template destructor
+// jtutil::array3d::array3d - 3D array template constructor
+// jtutil::array3d::~array3d - 3D array template destructor
//
-// array4d::array4d - 4D array template constructor
-// array4d::~array4d - 4D array template destructor
+// jtutil::array4d::array4d - 4D array template constructor
+// jtutil::array4d::~array4d - 4D array template destructor
//
// ***** template instantiations *****
//
-#include <assert.h>
-#include <stdio.h>
+#include <cassert>
+#include <cstddef> // for NULL
+
#include "jt/stdc.h"
#include "jt/util.hh"
#include "jt/array.hh"
@@ -29,6 +30,8 @@
//
// This function constructs an array1d object.
//
+namespace jtutil
+ {
template <typename fp>
array1d<fp>::array1d(int min_i_in, int max_i_in,
fp *array_in = NULL)
@@ -43,18 +46,22 @@ array_ = we_own_array_ ? new fp[N_i()] : array_in;
array_[i] = fp(0);
}
}
+ } // namespace jtutil::
//******************************************************************************
//
// This function destroys an array1d object.
//
+namespace jtutil
+ {
template <typename fp>
array1d<fp>::~array1d()
{
if (we_own_array_)
then delete[] array_;
}
+ } // namespace jtutil::
//******************************************************************************
//******************************************************************************
@@ -63,40 +70,61 @@ if (we_own_array_)
//
// This function constructs an array2d object.
//
+namespace jtutil
+ {
template <typename fp>
array2d<fp>::array2d(int min_i_in, int max_i_in,
int min_j_in, int max_j_in,
- fp *array_in = NULL)
- : min_i_(min_i_in), max_i_(max_i_in),
+ fp *array_in = NULL,
+ int stride_i_in = 0, int stride_j_in = 0)
+ : array_(array_in),
+ min_i_(min_i_in), max_i_(max_i_in),
min_j_(min_j_in), max_j_(max_j_in),
we_own_array_(array_in == NULL)
{
-stride_i_ = N_j();
-N_array_ = N_i() * stride_i_;
-array_ = we_own_array_ ? new fp[N_array_] : array_in;
+if (stride_j_ == 0)
+ then stride_j_ = 1;
+if (stride_i_ == 0)
+ then stride_i_ = N_j();
-// need to explicitly initialize -- new[] *doesn't* do this automagically
- for (int i = 0 ; i < N_array() ; ++i)
- {
- array_[i] = fp(0);
+if (we_own_array_)
+ then {
+ // allocate it
+ const int N_allocate = N_i() * N_j();
+ array_ = new fp[N_allocate];
}
-offset_ = 0; // temp value, needed for following subscript() call
+// must use unchecked subscripting here since setup isn't done yet
+offset_ = 0; // temp value, needed for following subscript_unchecked()
offset_ = - subscript_unchecked(min_i_, min_j_);
- // must use unchecked form here since setup isn't done yet
+assert( subscript_unchecked(min_i(), min_j()) == 0 );
+max_subscript_ = subscript_unchecked(max_i(), max_j());
+
+// explicitly initialize array (new[] *doesn't* do this automagically)
+ for (int i = min_i() ; i < max_i() ; ++i)
+ {
+ for (int j = min_j() ; j < max_j() ; ++j)
+ {
+ operator()(i,j) = fp(0);
+ }
+ }
}
+ } // namespace jtutil::
//******************************************************************************
//
// This function destroys an array2d object.
//
+namespace jtutil
+ {
template <typename fp>
array2d<fp>::~array2d()
{
if (we_own_array_)
then delete[] array_;
}
+ } // namespace jtutil::
//******************************************************************************
//******************************************************************************
@@ -105,6 +133,8 @@ if (we_own_array_)
//
// This function constructs an array3d object.
//
+namespace jtutil
+ {
template <typename fp>
array3d<fp>::array3d(int min_i_in, int max_i_in,
int min_j_in, int max_j_in,
@@ -130,18 +160,22 @@ offset_ = 0; // temp value, needed for following subscript() call
offset_ = - subscript_unchecked(min_i_, min_j_, min_k_);
// must use unchecked form here since setup isn't done yet
}
+ } // namespace jtutil::
//******************************************************************************
//
// This function destroys an array3d object.
//
+namespace jtutil
+ {
template <typename fp>
array3d<fp>::~array3d()
{
if (we_own_array_)
then delete[] array_;
}
+ } // namespace jtutil::
//******************************************************************************
//******************************************************************************
@@ -150,6 +184,8 @@ if (we_own_array_)
//
// This function constructs an array4d object.
//
+namespace jtutil
+ {
template <typename fp>
array4d<fp>::array4d(int min_i_in, int max_i_in,
int min_j_in, int max_j_in,
@@ -178,18 +214,22 @@ offset_ = 0; // temp value, needed for following subscript() call
offset_ = - subscript_unchecked(min_i_, min_j_, min_k_, min_l_);
// must use unchecked form here since setup isn't done yet
}
+ } // namespace jtutil::
//******************************************************************************
//
// This function destroys an array4d object.
//
+namespace jtutil
+ {
template <typename fp>
array4d<fp>::~array4d()
{
if (we_own_array_)
then delete[] array_;
}
+ } // namespace jtutil::
//******************************************************************************
//******************************************************************************
@@ -204,17 +244,17 @@ if (we_own_array_)
// kludge to conditionally disable the template instantations here
#else
-template class array1d<int>;
-template class array2d<int>;
+template class jtutil::array1d<int>;
+template class jtutil::array2d<int>;
-template class array1d<float>;
-template class array2d<float>;
-template class array3d<float>;
-template class array4d<float>;
+template class jtutil::array1d<float>;
+template class jtutil::array2d<float>;
+template class jtutil::array3d<float>;
+template class jtutil::array4d<float>;
-template class array1d<double>;
-template class array2d<double>;
-template class array3d<double>;
-template class array4d<double>;
+template class jtutil::array1d<double>;
+template class jtutil::array2d<double>;
+template class jtutil::array3d<double>;
+template class jtutil::array4d<double>;
#endif
diff --git a/src/jtutil/array.hh b/src/jtutil/array.hh
index 0d674bd..fc2060d 100644
--- a/src/jtutil/array.hh
+++ b/src/jtutil/array.hh
@@ -9,8 +9,8 @@
//
// prerequisites:
-// <assert.h>
-// <stdio.h> // only used for NULL
+// <cassert>
+// <cstddef> // for NULL
// "jt/stdc.h"
// "jt/util++.h" // for jtutil::how_many_in_range()
//
@@ -20,15 +20,13 @@
// contiguous arrays, parameterized by the data type (most commonly float
// or double, but could also be bool, int, long double, ...). The
// underlying storage can be either supplied by the caller, or allocated
-// by new[].
+// by new[]. In the latter case, arbitrary strides are also possible.
//
// These arrays cannot be copied or passed to functions by value; use
// pass by reference instead. (This is a feature, not a bug: passing
// large arrays by value would be ++slow, so we don't want to run the
// risk of this happing accidentally.)
//
-// FIXME: Maybe these should be in namespace jtutil::?
-//
//
// Stroustrup ("The C++ Programming Language", 3rd edition, appendix C.7)
@@ -58,6 +56,8 @@
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
class array1d
{
@@ -114,9 +114,12 @@ private:
bool we_own_array_; // true ==> array_ --> new[] array which we own
// false ==> array --> caller-owns array_
};
+ }; // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
class array2d
{
@@ -138,7 +141,7 @@ public:
// FIXME: should we also provide the reverse mapping, i.e.
// subscript --> (i,j) ?
int subscript_unchecked(int i, int j) const
- { return offset_ + stride_i_*i + j; }
+ { return offset_ + stride_i_*i + stride_j_*j; }
int subscript(int i, int j) const
{
assert( is_valid_subscript(i,j) );
@@ -147,7 +150,7 @@ public:
// source line, so an assert() failure message can
// pinpoint *which* index is bad
assert(posn >= 0);
- assert(posn <= N_array_);
+ assert(posn <= max_subscript_);
return posn;
}
@@ -161,15 +164,19 @@ public:
// get access to internal 0-origin 1D storage array
// (low-level, dangerous, use with caution!)
- int N_array() const { return N_array_; }
+ // ... semantics of N_array() may not be what you want
+ // if strides specify noncontiguous storage
+ int N_array() const { return max_subscript_+1; }
fp* get_array() const { return array_; }
// constructor, destructor
- // constructor initializes all array elements to fp(0.0)
+ // ... constructor initializes all array elements to fp(0.0)
+ // ... omitted strides default to C storage order
array2d(int min_i_in, int max_i_in,
int min_j_in, int max_j_in,
- fp *array_in = NULL); // caller-provided storage array
+ fp *array_in = NULL, // caller-provided storage array
// if non-NULL
+ int stride_i_in = 0, int stride_j_in = 0);
~array2d();
private:
@@ -188,20 +195,23 @@ private:
// subscripting info
// n.b. put this next in class so it should be in the same
// cpu cache line as array_ ==> faster array access
- int offset_, stride_i_;
+ int offset_, stride_i_, stride_j_;
// min/max array bounds
- int N_array_;
const int min_i_, max_i_;
const int min_j_, max_j_;
+ int max_subscript_;
// n.b. put this at end of class since performance doesn't matter
bool we_own_array_; // true ==> array_ --> new[] array which we own
// false ==> array --> caller-owns array_
};
+ }; // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
class array3d
{
@@ -290,9 +300,12 @@ private:
bool we_own_array_; // true ==> array_ --> new[] array which we own
// false ==> array --> caller-owns array_
};
+ }; // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
class array4d
{
@@ -393,3 +406,4 @@ private:
bool we_own_array_; // true ==> array_ --> new[] array which we own
// false ==> array --> caller-owns array_
};
+ }; // namespace jtutil::
diff --git a/src/jtutil/cpm_map.cc b/src/jtutil/cpm_map.cc
index dbbe5b6..bb96bf1 100644
--- a/src/jtutil/cpm_map.cc
+++ b/src/jtutil/cpm_map.cc
@@ -1,9 +1,9 @@
// cpm_map.cc -- "integer +/-" mapping i --> j = const +/- i
// $Id$
//
-// cpm_map::cpm_map // mirror map, specified by fixed point
-// cpm_map::cpm_map // generic map specified by sample point & sign
-// cpm_map::cpm_map // generic map specified by *fp* sample point & sign
+// jtutil::cpm_map::cpm_map // mirror map, specified by fixed point
+// jtutil::cpm_map::cpm_map // generic map specified by sample point & sign
+// jtutil::cpm_map::cpm_map // generic map specified by *fp* sample point & sign
//
// ***** template instantiations *****
//
@@ -24,6 +24,8 @@ using jtutil::error_exit;
// specified fixed point (must be integer or half-integer) and domain.
// The sample point need not be in the map's domain/range.
//
+namespace jtutil
+ {
template <typename fp>
cpm_map<fp>::cpm_map(int min_i_in, int max_i_in,
fp fixed_point)
@@ -47,6 +49,7 @@ assert(
fuzzy<fp>::ceiling(fixed_point)
);
}
+ } // namespace jtutil::
//******************************************************************************
@@ -55,6 +58,8 @@ assert(
// specified by a sample point sample_i --> sample_j and by sign.
// The sample point need not be in the map's domain/range.
//
+namespace jtutil
+ {
template <typename fp>
cpm_map<fp>::cpm_map(int min_i_in, int max_i_in,
int sample_i, int sample_j,
@@ -67,6 +72,7 @@ cpm_map<fp>::cpm_map(int min_i_in, int max_i_in,
// verify that we have setup correct
assert( map_unchecked(sample_i) == sample_j );
}
+ } // namespace jtutil::
//******************************************************************************
@@ -78,6 +84,8 @@ assert( map_unchecked(sample_i) == sample_j );
// is never ok) and by sign. The sample point need not be in the map's
// domain/range.
//
+namespace jtutil
+ {
template <typename fp>
cpm_map<fp>::cpm_map(int min_i_in, int max_i_in,
fp sample_i, fp sample_j,
@@ -106,6 +114,7 @@ assert(
(map_is_plus_in ? fuzzy<fp>::floor (sample_j)
: fuzzy<fp>::ceiling(sample_j)) );
}
+ } // namespace jtutil::
//******************************************************************************
//******************************************************************************
@@ -116,4 +125,4 @@ assert(
//
// instantiation for <double>
-template class cpm_map<double>;
+template class jtutil::cpm_map<double>;
diff --git a/src/jtutil/cpm_map.hh b/src/jtutil/cpm_map.hh
index 33a7a72..5031111 100644
--- a/src/jtutil/cpm_map.hh
+++ b/src/jtutil/cpm_map.hh
@@ -23,6 +23,8 @@
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
class cpm_map
{
@@ -126,3 +128,4 @@ private:
int offset_;
bool map_is_plus_;
};
+ }; // namespace jtutil::
diff --git a/src/jtutil/fuzzy.cc b/src/jtutil/fuzzy.cc
index 1ebfe54..80f9adb 100644
--- a/src/jtutil/fuzzy.cc
+++ b/src/jtutil/fuzzy.cc
@@ -1,11 +1,11 @@
// fuzzy.cc -- template for fuzzy comparisons et al on floating point values
// $Id$
//
-// fuzzy::tolerance - comparison tolerance
-// fuzzy::EQ
-// fuzzy::is_integer
-// fuzzy::floor
-// fuzzy::ceiling
+// jtutil::fuzzy::tolerance - comparison tolerance
+// jtutil::fuzzy::EQ
+// jtutil::fuzzy::is_integer
+// jtutil::fuzzy::floor
+// jtutil::fuzzy::ceiling
//
// ***** template instantiations *****
//
@@ -15,6 +15,8 @@
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
bool fuzzy<fp>::EQ(fp x, fp y)
{
@@ -23,18 +25,24 @@ fp epsilon = jtutil::max(tolerance, tolerance*max_abs);
return jtutil::abs<fp>(x-y) <= epsilon;
}
+ } // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
bool fuzzy<fp>::is_integer(fp x)
{
int i = round<fp>::to_integer(x);
return EQ(x, fp(i));
}
+ } // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
int fuzzy<fp>::floor(fp x)
{
@@ -42,9 +50,12 @@ return fuzzy<fp>::is_integer(x)
? round<fp>::to_integer(x)
: round<fp>::floor(x);
}
+ } // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
int fuzzy<fp>::ceiling(fp x)
{
@@ -52,6 +63,7 @@ return fuzzy<fp>::is_integer(x)
? round<fp>::to_integer(x)
: round<fp>::ceiling(x);
}
+ } // namespace jtutil::
//******************************************************************************
//******************************************************************************
@@ -62,9 +74,9 @@ return fuzzy<fp>::is_integer(x)
//
// instantiation for <float>
-template class fuzzy<float>;
-float fuzzy<float>::tolerance = 1.0e-5; // about 100 * FLT_EPSILON
+template class jtutil::fuzzy<float>;
+float jtutil::fuzzy<float>::tolerance = 1.0e-5; // about 100 * FLT_EPSILON
// instantiations for <double>
-template class fuzzy<double>;
-double fuzzy<double>::tolerance = 1.0e-12; // about 1e4 * DBL_EPSILON
+template class jtutil::fuzzy<double>;
+double jtutil::fuzzy<double>::tolerance = 1.0e-12; // about 1e4 * DBL_EPSILON
diff --git a/src/jtutil/linear_map.cc b/src/jtutil/linear_map.cc
index 09928ce..5106949 100644
--- a/src/jtutil/linear_map.cc
+++ b/src/jtutil/linear_map.cc
@@ -1,11 +1,11 @@
// linear_map.cc -- linear mapping from integers <--> floating point values
// $Id$
//
-// linear_map::linear_map - basic constructor
-// linear_map::linear_map - constructor for subrange of existing linear_map
-// linear_map::fp_int_of_fp - convert fp --> int coord, return as fp
-// linear_map::int_of_fp - convert fp --> int, check for being fuzzily integral
-// linear_map::delta_int_of_delta_fp - ... same for spacings (delta_* values)
+// jtutil::linear_map::linear_map - basic ctor
+// jtutil::linear_map::linear_map - ctor for subrange of existing linear_map
+// jtutil::linear_map::fp_int_of_fp - convert fp --> int coord, return as fp
+// jtutil::linear_map::int_of_fp - convert fp --> int, check for ==fuzzy int
+// jtutil::linear_map::delta_int_of_delta_fp - ... same for spacings
//
// ***** template instantiation *****
//
@@ -26,6 +26,8 @@ using jtutil::error_exit;
//
// This function constructs a linear_map<fp> object.
//
+namespace jtutil
+ {
template <typename fp>
linear_map<fp>::linear_map(int min_int_in, int max_int_in,
fp min_fp_in, fp delta_fp_in, fp max_fp_in)
@@ -34,6 +36,7 @@ linear_map<fp>::linear_map(int min_int_in, int max_int_in,
{
constructor_common(min_fp_in, max_fp_in);
}
+ } // namespace jtutil::
//******************************************************************************
@@ -41,6 +44,8 @@ constructor_common(min_fp_in, max_fp_in);
// This function constructs a linear_map<fp> object with a subrange
// of an existing one.
//
+namespace jtutil
+ {
template <typename fp>
linear_map<fp>::linear_map(const linear_map<fp> &lm_in,
int min_int_in, int max_int_in) // subrange
@@ -59,6 +64,7 @@ if (! (is_in_range(min_int_in) && is_in_range(max_int_in)) )
constructor_common(lm_in.fp_of_int_unchecked(min_int_in),
lm_in.fp_of_int_unchecked(max_int_in));
}
+ } // namespace jtutil::
//******************************************************************************
@@ -66,6 +72,8 @@ constructor_common(lm_in.fp_of_int_unchecked(min_int_in),
// This function does the common argument validation and setup for
// all the constructors of class linear_map<fp>:: .
//
+namespace jtutil
+ {
template <typename fp>
void linear_map<fp>::constructor_common(fp min_fp_in, fp max_fp_in)
// assumes
@@ -92,6 +100,7 @@ if (fuzzy<fp>::NE(fp_of_int_unchecked(max_int()), max_fp_in))
double(min_fp_in), double(delta_fp()), double(max_fp_in));
/*NOTREACHED*/
}
+ } // namespace jtutil::
//******************************************************************************
@@ -99,6 +108,8 @@ if (fuzzy<fp>::NE(fp_of_int_unchecked(max_int()), max_fp_in))
// This function converts fp --> int coordinate, returning the result
// as an fp (which need not be fuzzily integral).
//
+namespace jtutil
+ {
template <typename fp>
fp linear_map<fp>::fp_int_of_fp(fp x)
const
@@ -115,6 +126,7 @@ if (! is_in_range(x))
return inverse_delta_ * (x - offset_);
}
+ } // namespace jtutil::
//******************************************************************************
@@ -124,9 +136,11 @@ return inverse_delta_ * (x - offset_);
// result *isn't* fuzzily integral.)
//
// FIXME:
-// Having to explicitly specify the global namespace for ::round<fp>::
+// Having to explicitly specify the namespace for jtutil::round<fp>::
// is ++ugly. :(
//
+namespace jtutil
+ {
template <typename fp>
int linear_map<fp>::int_of_fp(fp x, noninteger_action nia = error)
const
@@ -136,7 +150,7 @@ const fp fp_int = fp_int_of_fp(x);
if (fuzzy<fp>::is_integer(fp_int))
then {
// x is (fuzzily) a grid point ==> return that
- return ::round<fp>::to_integer(fp_int); // *** EARLY RETURN ***
+ return jtutil::round<fp>::to_integer(fp_int); // *** EARLY RETURN ***
}
// get to here ==> x isn't (fuzzily) a grid point
@@ -163,13 +177,13 @@ case warning:
// fall through
case round:
- return ::round<fp>::to_integer(fp_int); // *** EARLY RETURN ***
+ return jtutil::round<fp>::to_integer(fp_int); // *** EARLY RETURN ***
case floor:
- return ::round<fp>::floor(fp_int); // *** EARLY RETURN ***
+ return jtutil::round<fp>::floor(fp_int); // *** EARLY RETURN ***
case ceiling:
- return ::round<fp>::ceiling(fp_int); // *** EARLY RETURN ***
+ return jtutil::round<fp>::ceiling(fp_int); // *** EARLY RETURN ***
default:
error_exit(PANIC_EXIT,
@@ -181,6 +195,7 @@ default:
return 0; // dummy return to quiet gcc
// (which doesn't grok that error_exit() never returns)
}
+ } // namespace jtutil::
//******************************************************************************
@@ -191,9 +206,11 @@ return 0; // dummy return to quiet gcc
// do if the result *isn't* fuzzily integral.)
//
// FIXME:
-// Having to explicitly specify the global namespace for ::round<fp>::
+// Having to explicitly specify the namespace for jtutil::round<fp>::
// is ++ugly. :(
//
+namespace jtutil
+ {
template <typename fp>
int linear_map<fp>::delta_int_of_delta_fp(fp delta_x,
noninteger_action nia = error)
@@ -205,7 +222,8 @@ if (fuzzy<fp>::is_integer(fp_delta_int))
then {
// delta_x is (fuzzily) an integer number of grid spacings
// ==> return that
- return ::round<fp>::to_integer(fp_delta_int); // *** EARLY RETURN ***
+ return jtutil::round<fp>::to_integer(fp_delta_int);
+ // *** EARLY RETURN ***
}
// get to here ==> delta_x isn't (fuzzily) an integer number of grid spacings
@@ -232,13 +250,14 @@ case warning:
// fall through
case round:
- return ::round<fp>::to_integer(fp_delta_int); // *** EARLY RETURN ***
+ return jtutil::round<fp>::to_integer(fp_delta_int);
+ // *** EARLY RETURN ***
case floor:
- return ::round<fp>::floor(fp_delta_int); // *** EARLY RETURN ***
+ return jtutil::round<fp>::floor(fp_delta_int); // *** EARLY RETURN ***
case ceiling:
- return ::round<fp>::ceiling(fp_delta_int); // *** EARLY RETURN ***
+ return jtutil::round<fp>::ceiling(fp_delta_int);// *** EARLY RETURN ***
default:
error_exit(PANIC_EXIT,
@@ -250,6 +269,7 @@ default:
return 0; // dummy return to quiet gcc
// (which doesn't grok that error_exit() never returns)
}
+ } // namespace jtutil::
//******************************************************************************
//******************************************************************************
@@ -259,4 +279,4 @@ return 0; // dummy return to quiet gcc
// ***** template instantiation *****
//
-template class linear_map<double>;
+template class jtutil::linear_map<double>;
diff --git a/src/jtutil/linear_map.hh b/src/jtutil/linear_map.hh
index ac427b0..9bd21c1 100644
--- a/src/jtutil/linear_map.hh
+++ b/src/jtutil/linear_map.hh
@@ -1,7 +1,7 @@
// linear_map.hh -- linear mapping from integers <--> floating point values
// $Id$
//
-// linear_map - linear mapping from integers <--> floating point values
+// jtutil::linear_map - linear mapping from integers <--> floating point values
//
//
@@ -24,6 +24,8 @@
//*****************************************************************************
+namespace jtutil
+ {
template <typename fp>
class linear_map
{
@@ -130,3 +132,4 @@ private:
// bounds (inclusive)
const int min_int_, max_int_;
};
+ }; // namespace jtutil::
diff --git a/src/jtutil/miscfp.cc b/src/jtutil/miscfp.cc
index 7b151c1..5ccda53 100644
--- a/src/jtutil/miscfp.cc
+++ b/src/jtutil/miscfp.cc
@@ -1,10 +1,10 @@
// miscfp.cc -- misc floating-point functions
//
-// signum - sign of a floating point number
-// hypot3 - 3D Euclidean distance
-// arctan_xy - 4-quadrant arc tangent
-// modulo_reduce - reduce an angle modulo 2*pi radians (360 degrees)
+// jtutil::signum - sign of a floating point number
+// jtutil::hypot3 - 3D Euclidean distance
+// jtutil::arctan_xy - 4-quadrant arc tangent
+// jtutil::modulo_reduce - reduce an angle modulo 2*pi radians (360 degrees)
//
#include <math.h>
@@ -25,7 +25,7 @@ if (x == 0.0)
then return 0.0;
else return (x > 0.0) ? 1.0 : -1.0;
}
- };
+ } // namespace jtutil::
//******************************************************************************
@@ -50,7 +50,7 @@ double hypot3(double x, double y, double z)
{
return std::sqrt(x*x + y*y + z*z);
}
- };
+ } // namespace jtutil::
//******************************************************************************
@@ -75,7 +75,7 @@ return ((x == 0.0) && (y == 0.0))
?
: std::atan2(y, x); // note reversed argument order (y,x)!!
}
- };
+ } // namespace jtutil::
//******************************************************************************
@@ -112,4 +112,4 @@ if (! (fuzzy<double>::GE(xx, xmin) && fuzzy<double>::LE(xx, xmax)) )
return xx;
}
- }
+ } // namespace jtutil::
diff --git a/src/jtutil/norm.cc b/src/jtutil/norm.cc
index c239c56..7425b0f 100644
--- a/src/jtutil/norm.cc
+++ b/src/jtutil/norm.cc
@@ -23,7 +23,7 @@ template <typename fp>
sum2_ += x*x;
infinity_norm_ = jtutil::max(infinity_norm_, jtutil::abs(x));
}
- }
+ } // namespace jtutil::
//******************************************************************************
@@ -37,7 +37,7 @@ template<typename fp>
{ assert(is_nonempty()); return sqrt(sum2_/fp(N_)); }
template<typename fp>
fp norm<fp>::infinity_norm() const { return infinity_norm_; }
- }
+ } // namespace jtutil::
//******************************************************************************
diff --git a/src/jtutil/round.cc b/src/jtutil/round.cc
index 7ac60f1..123eb48 100644
--- a/src/jtutil/round.cc
+++ b/src/jtutil/round.cc
@@ -2,9 +2,9 @@
// $Id$
//
// *** Implementation Notes ***
-// round::to_integer
-// round::floor
-// round::ceiling
+// jtutil::round::to_integer
+// jtutil::round::floor
+// jtutil::round::ceiling
//
// ***** template instantiations *****
//
@@ -27,6 +27,8 @@
//******************************************************************************
// round to nearest integer, up for exact tie
+namespace jtutil
+ {
template <typename fp>
int round<fp>::to_integer(fp x)
{
@@ -34,9 +36,12 @@ return (x >= 0.0)
? int(x + 0.5) // eg 3.6 --> int(4.1) = 4
: - int( (-x) + 0.5 ); // eg -3.6 --> - int(4.1) = -4
}
+ } // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
int round<fp>::floor(fp x)
{
@@ -44,9 +49,12 @@ return (x >= 0.0)
? int(x)
: - ceiling(-x);
}
+ } // namespace jtutil::
//******************************************************************************
+namespace jtutil
+ {
template <typename fp>
int round<fp>::ceiling(fp x)
{
@@ -54,6 +62,7 @@ return (x >= 0.0)
? int(x) + (x != fp(int(x)))
: - floor(-x);
}
+ } // namespace jtutil::
//******************************************************************************
//******************************************************************************
@@ -64,7 +73,7 @@ return (x >= 0.0)
//
// instantiation for <float>
-template class round<float>;
+template class jtutil::round<float>;
// instantiations for <double>
-template class round<double>;
+template class jtutil::round<double>;
diff --git a/src/jtutil/stdc.h b/src/jtutil/stdc.h
index be1150e..ffb2e1b 100644
--- a/src/jtutil/stdc.h
+++ b/src/jtutil/stdc.h
@@ -96,7 +96,7 @@ extern C_FUNCTION int error_exit(int exit_code, const char *format, ...)
#endif
#ifdef __cplusplus
- } /* close namespace jtutil */
+ }; /* namespace jtutil:: */
#endif
/******************************************************************************/
diff --git a/src/jtutil/test_array.cc b/src/jtutil/test_array.cc
index aace4f2..10eb861 100644
--- a/src/jtutil/test_array.cc
+++ b/src/jtutil/test_array.cc
@@ -1,15 +1,21 @@
// test_array.cc -- test driver for array.hh classes
// $Id$
-#include <assert.h>
-#include <stdio.h>
+#include <cassert>
#include <math.h>
+#include <cstdio>
+using std::printf;
#include "jt/stdc.h"
#include "jt/util.hh"
#include "jt/array.hh"
using jtutil::how_many_in_range;
+using jtutil::fuzzy;
+using jtutil::array1d;
+using jtutil::array2d;
+using jtutil::array3d;
+using jtutil::array4d;
//
// prototypes for non-class functions defined in this file
diff --git a/src/jtutil/test_cpm_map.cc b/src/jtutil/test_cpm_map.cc
index 2aff27e..aa1b724 100644
--- a/src/jtutil/test_cpm_map.cc
+++ b/src/jtutil/test_cpm_map.cc
@@ -6,6 +6,7 @@
#include "jt/stdc.h"
#include "jt/util.hh"
#include "jt/cpm_map.hh"
+using jtutil::cpm_map;
//******************************************************************************
diff --git a/src/jtutil/test_fuzzy.cc b/src/jtutil/test_fuzzy.cc
index 0f0622e..b877adc 100644
--- a/src/jtutil/test_fuzzy.cc
+++ b/src/jtutil/test_fuzzy.cc
@@ -12,6 +12,7 @@
#include "jt/stdc.h"
#include "jt/util.hh"
using jtutil::error_exit;
+using jtutil::fuzzy;
// prototypes
template <typename fp>
diff --git a/src/jtutil/test_linear_map.cc b/src/jtutil/test_linear_map.cc
index 74a93fa..6456b93 100644
--- a/src/jtutil/test_linear_map.cc
+++ b/src/jtutil/test_linear_map.cc
@@ -6,6 +6,8 @@
#include "jt/stdc.h"
#include "jt/util.hh"
#include "jt/linear_map.hh"
+using jtutil::fuzzy;
+using jtutil::linear_map;
//******************************************************************************
diff --git a/src/jtutil/test_modulo.cc b/src/jtutil/test_modulo.cc
index f964532..912dc1b 100644
--- a/src/jtutil/test_modulo.cc
+++ b/src/jtutil/test_modulo.cc
@@ -6,13 +6,16 @@
#include "jt/stdc.h"
#include "jt/util.hh"
using jtutil::error_exit;
+using jtutil::fuzzy;
// prototypes
void tryit(double x, double xmod, double xmin, double xmax, double correct);
+//******************************************************************************
+
int main()
{
-// x xmod xmin xmax correct
+// x xmod xmin xmax correct
tryit( 0.5, 1.0, 0.0, 1.0, 0.5);
tryit( 0.0, 1.0, 0.0, 1.0, 0.0);
tryit( 1.0, 1.0, 0.0, 1.0, 1.0);
@@ -26,6 +29,8 @@ tryit(-145.0, 360.0, 180.0, 270.0, 215.0);
printf("all ok!\n");
}
+//******************************************************************************
+
void tryit(double x, double xmod, double xmin, double xmax, double correct)
{
printf("trying %g mod %g [%g,%g] ==> ",
diff --git a/src/jtutil/test_norm.cc b/src/jtutil/test_norm.cc
index d89f387..77100cb 100644
--- a/src/jtutil/test_norm.cc
+++ b/src/jtutil/test_norm.cc
@@ -1,11 +1,16 @@
// test_norm -- quick-n-dirty test of norm template class
// $Id$
-#include <stdio.h>
-#include <assert.h>
+#include <cstdio>
+#include <cassert>
+#include <cmath>
+using std::printf;
+using std::sqrt;
+
#include "jt/stdc.h"
#include "jt/util.hh"
using jtutil::norm;
+using jtutil::fuzzy;
int main()
{
diff --git a/src/jtutil/test_round.cc b/src/jtutil/test_round.cc
index ac0925e..9bcc280 100644
--- a/src/jtutil/test_round.cc
+++ b/src/jtutil/test_round.cc
@@ -5,6 +5,7 @@
#include <assert.h>
#include "jt/stdc.h"
#include "jt/util.hh"
+using jtutil::round;
//
// Usage:
diff --git a/src/jtutil/util.hh b/src/jtutil/util.hh
index 39f1d15..03aa181 100644
--- a/src/jtutil/util.hh
+++ b/src/jtutil/util.hh
@@ -6,15 +6,15 @@
// jtutil::abs - absolute value template
// jtutil::pow* - raise floating point value to small-integer power
// jtutil::norm<fp> - compute 2-, rms-, and infinity-norms
-// fuzzy::<fp> - fuzzy floating point comparisons and other operations
-// round::<fp> - floating point rounding
+// jtutil::fuzzy::<fp> - fuzzy floating point comparisons and other operations
+// jtutil::round::<fp> - floating point rounding
//
-//******************************************************************************
-
namespace jtutil
{
+//******************************************************************************
+
// how many integers are in the closed interval [low,high]
inline int how_many_in_range(int low, int high)
{ return high - low + 1; }
@@ -83,15 +83,11 @@ double modulo_reduce(double x, double xmod, double xmin, double xmax);
inline fp radians_of_degrees(fp degrees) { return (PI/180.0)*degrees; }
#endif
- }; // close namespace jtutil::
-
//******************************************************************************
//
// this template class computes 2-norms, rms-norms, and/or infinity-norms
//
-namespace jtutil
- {
template <typename fp>
class norm
{
@@ -129,7 +125,6 @@ private:
fp sum2_; // sum(data^2)
fp infinity_norm_; // max |data|
};
- };
//******************************************************************************
@@ -190,3 +185,7 @@ public:
static int floor(fp x); // round down to integer
static int ceiling(fp x); // round up to integer
};
+
+//******************************************************************************
+
+ } // namespace jtutil::