From 08555ac2f37b48d03dee307a8356a5c709b31efb Mon Sep 17 00:00:00 2001 From: jthorn Date: Wed, 27 Mar 2002 20:37:34 +0000 Subject: 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 --- src/jtutil/README | 3 +- src/jtutil/array.cc | 102 +++++++++++++++++++++++++++++------------- src/jtutil/array.hh | 38 +++++++++++----- src/jtutil/cpm_map.cc | 17 +++++-- src/jtutil/cpm_map.hh | 3 ++ src/jtutil/fuzzy.cc | 30 +++++++++---- src/jtutil/linear_map.cc | 52 ++++++++++++++------- src/jtutil/linear_map.hh | 5 ++- src/jtutil/miscfp.cc | 16 +++---- src/jtutil/norm.cc | 4 +- src/jtutil/round.cc | 19 +++++--- src/jtutil/stdc.h | 2 +- src/jtutil/test_array.cc | 10 ++++- src/jtutil/test_cpm_map.cc | 1 + src/jtutil/test_fuzzy.cc | 1 + src/jtutil/test_linear_map.cc | 2 + src/jtutil/test_modulo.cc | 7 ++- src/jtutil/test_norm.cc | 9 +++- src/jtutil/test_round.cc | 1 + src/jtutil/util.hh | 17 ++++--- 20 files changed, 235 insertions(+), 104 deletions(-) (limited to 'src/jtutil') 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 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 -#include +#include +#include // 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 array1d::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 array1d::~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 array2d::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 array2d::~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 array3d::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 array3d::~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 array4d::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 array4d::~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; -template class array2d; +template class jtutil::array1d; +template class jtutil::array2d; -template class array1d; -template class array2d; -template class array3d; -template class array4d; +template class jtutil::array1d; +template class jtutil::array2d; +template class jtutil::array3d; +template class jtutil::array4d; -template class array1d; -template class array2d; -template class array3d; -template class array4d; +template class jtutil::array1d; +template class jtutil::array2d; +template class jtutil::array3d; +template class jtutil::array4d; #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: -// -// // only used for NULL +// +// // 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 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 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 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 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 cpm_map::cpm_map(int min_i_in, int max_i_in, fp fixed_point) @@ -47,6 +49,7 @@ assert( fuzzy::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 cpm_map::cpm_map(int min_i_in, int max_i_in, int sample_i, int sample_j, @@ -67,6 +72,7 @@ cpm_map::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 cpm_map::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::floor (sample_j) : fuzzy::ceiling(sample_j)) ); } + } // namespace jtutil:: //****************************************************************************** //****************************************************************************** @@ -116,4 +125,4 @@ assert( // // instantiation for -template class cpm_map; +template class jtutil::cpm_map; 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 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 bool fuzzy::EQ(fp x, fp y) { @@ -23,18 +25,24 @@ fp epsilon = jtutil::max(tolerance, tolerance*max_abs); return jtutil::abs(x-y) <= epsilon; } + } // namespace jtutil:: //****************************************************************************** +namespace jtutil + { template bool fuzzy::is_integer(fp x) { int i = round::to_integer(x); return EQ(x, fp(i)); } + } // namespace jtutil:: //****************************************************************************** +namespace jtutil + { template int fuzzy::floor(fp x) { @@ -42,9 +50,12 @@ return fuzzy::is_integer(x) ? round::to_integer(x) : round::floor(x); } + } // namespace jtutil:: //****************************************************************************** +namespace jtutil + { template int fuzzy::ceiling(fp x) { @@ -52,6 +63,7 @@ return fuzzy::is_integer(x) ? round::to_integer(x) : round::ceiling(x); } + } // namespace jtutil:: //****************************************************************************** //****************************************************************************** @@ -62,9 +74,9 @@ return fuzzy::is_integer(x) // // instantiation for -template class fuzzy; -float fuzzy::tolerance = 1.0e-5; // about 100 * FLT_EPSILON +template class jtutil::fuzzy; +float jtutil::fuzzy::tolerance = 1.0e-5; // about 100 * FLT_EPSILON // instantiations for -template class fuzzy; -double fuzzy::tolerance = 1.0e-12; // about 1e4 * DBL_EPSILON +template class jtutil::fuzzy; +double jtutil::fuzzy::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 object. // +namespace jtutil + { template linear_map::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::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 object with a subrange // of an existing one. // +namespace jtutil + { template linear_map::linear_map(const linear_map &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:: . // +namespace jtutil + { template void linear_map::constructor_common(fp min_fp_in, fp max_fp_in) // assumes @@ -92,6 +100,7 @@ if (fuzzy::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::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 fp linear_map::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:: +// Having to explicitly specify the namespace for jtutil::round:: // is ++ugly. :( // +namespace jtutil + { template int linear_map::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::is_integer(fp_int)) then { // x is (fuzzily) a grid point ==> return that - return ::round::to_integer(fp_int); // *** EARLY RETURN *** + return jtutil::round::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::to_integer(fp_int); // *** EARLY RETURN *** + return jtutil::round::to_integer(fp_int); // *** EARLY RETURN *** case floor: - return ::round::floor(fp_int); // *** EARLY RETURN *** + return jtutil::round::floor(fp_int); // *** EARLY RETURN *** case ceiling: - return ::round::ceiling(fp_int); // *** EARLY RETURN *** + return jtutil::round::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:: +// Having to explicitly specify the namespace for jtutil::round:: // is ++ugly. :( // +namespace jtutil + { template int linear_map::delta_int_of_delta_fp(fp delta_x, noninteger_action nia = error) @@ -205,7 +222,8 @@ if (fuzzy::is_integer(fp_delta_int)) then { // delta_x is (fuzzily) an integer number of grid spacings // ==> return that - return ::round::to_integer(fp_delta_int); // *** EARLY RETURN *** + return jtutil::round::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::to_integer(fp_delta_int); // *** EARLY RETURN *** + return jtutil::round::to_integer(fp_delta_int); + // *** EARLY RETURN *** case floor: - return ::round::floor(fp_delta_int); // *** EARLY RETURN *** + return jtutil::round::floor(fp_delta_int); // *** EARLY RETURN *** case ceiling: - return ::round::ceiling(fp_delta_int); // *** EARLY RETURN *** + return jtutil::round::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; +template class jtutil::linear_map; 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 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 @@ -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::GE(xx, xmin) && fuzzy::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 sum2_ += x*x; infinity_norm_ = jtutil::max(infinity_norm_, jtutil::abs(x)); } - } + } // namespace jtutil:: //****************************************************************************** @@ -37,7 +37,7 @@ template { assert(is_nonempty()); return sqrt(sum2_/fp(N_)); } template fp norm::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 int round::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 int round::floor(fp x) { @@ -44,9 +49,12 @@ return (x >= 0.0) ? int(x) : - ceiling(-x); } + } // namespace jtutil:: //****************************************************************************** +namespace jtutil + { template int round::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 -template class round; +template class jtutil::round; // instantiations for -template class round; +template class jtutil::round; 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 -#include +#include #include +#include +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 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 -#include +#include +#include +#include +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 #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 - compute 2-, rms-, and infinity-norms -// fuzzy:: - fuzzy floating point comparisons and other operations -// round:: - floating point rounding +// jtutil::fuzzy:: - fuzzy floating point comparisons and other operations +// jtutil::round:: - 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 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:: -- cgit v1.2.3