?? basics.java
字號:
// It must be defined "const" because it is used to dimension arrays.
static const int IO_BUFFER_SIZE = 1024;
// This is the maximum length of a string read from a file
static const int MAX_INPUT_STRING_SIZE = 1000;
extern const int DEFAULT_PRECISION;
extern const Real REAL_EPSILON;
extern const StoredReal STORED_REAL_EPSILON;
// This is the maximum length of a quoted string we can read from a file
static const int MAX_QUOTED_STRING = 200000;
// See basicCore.c for these values.
extern const char *WHITE_SPACE;
extern const float DEFAULT_PAGE_X;
extern const float DEFAULT_PAGE_Y;
extern const float DEFAULT_GRAPH_X;
extern const float DEFAULT_GRAPH_Y;
extern MLCOStream* globalLogStream;
extern const Real CONFIDENCE_INTERVAL_PROBABILITY;
extern const Real CONFIDENCE_INTERVAL_Z; // value such that area under standard
// normal curve going left & right Z,
// has CONFIDENCE_INTERVAL_PROBABILITY
class OptionServer;
extern OptionServer *optionServer;
# if defined(__CENTERLINE__)
extern "C" { int centerline_true(void); }
# endif
#define RCSID(str)
// DECLARE_DISPLAY declares operator<< for the given class (.h file)
// DEF_DISPLAY defines it (for use in .c file)
// For templated classes do
// template <class T> DEF_DISPLAY(DblLinkList<T>)
//
// Note: you should NOT place a semicolon after either DECLARE_DISPLAY or
// DEF_DISPLAY when you use it in code.
#define DECLARE_DISPLAY(class) \
MLCOStream& operator<<(MLCOStream& s, const class& c);
#define DEF_DISPLAY(class) \
MLCOStream& operator<<(MLCOStream& s, const class& c) \
{c.display(s); return s;}
extern const MString TRUE_STRING;
extern const MString FALSE_STRING;
extern const MString EMPTY_STRING;
const MString& bool_to_string(Bool boolValue);
inline Real square_real(Real x) {return x*x;}
inline double square_double(double x) {return x*x;}
Real Mround(Real x, int digits);
MString get_env_default(const MString& envVarName, const MString& defaultName);
MString get_env(const MString& envVarName);
int get_env_int(const MString& envVarName, int val);
Bool get_env_bool(const MString& envVarName, Bool val);
// Log base 2. Solaris doesn't have log2().
double log_bin(double num);
// Lose the least significant digits of Reals in an array. Sometimes things
// just don't add up right...
template <class Element> class Array;
template <class Element> class Array2;
extern void lose_lsb_array(Array<Real>& a);
template<class T> inline void mlc_swap(T& a, T& b)
{T temp; temp = a; a = b; b = temp;}
// MLCThreads.h is included in all cases in order to define some
// stub functions.
#include <MLCThreads.h>
// Note: The following concerns performance in using the MLC methods that
// are passed strings.
// The following class (MLC) has several methods that are passed
// error messages. These error messages used to be passed as MString&s.
// If normal invocation is:
// class_method(other_args, "This is an error message");
// the MString(const char*) constructor/destructor pair was invoked on
// each call. When this occured inside a tight loop the overhead
// could be quite high. If function a was calling function b in a tight
// loop, and b was invoking one of the said class methods, we couldn't
// even expect any help from a smart compiler to speed things up.
// What was more disappointing about the performance loss was that no more
// than one MString ever needed to be constructed--no such MString was used
// unless the method immediately aborted. There were only 2 ways to speed
// this up, either
// a) invoke the methods with static const MStrings, or
// b) declare the calling arguments to be const char* const, so an MString
// constructor was not called more than once.
// We selected b.
// Name-space class for min/max, and clamping reals within ranges (with
// abort on more than clampingEpsilon from the range), and comparison of
// rational numbers with epsilons used to nullify small differences.
class MLC {
NO_DEFAULT_OPS(MLC);
private:
// Member data
// Amount by which values to be clamped my exceed the range, without
// aborting.
Real clampingEpsilon;
StoredReal storedClampingEpsilon;
static Real realEpsilon;
static StoredReal storedRealEpsilon;
static int dribbleState;
// dribble each 10% = 100% / 10
//@@ removed from kit
// static const int DRIBBLE_SCALE = 10;
// performance measurement
timespec_t start, stop;
const MString* perfExpName;
public:
//@@ GNU gets very upset when real_epsilon() and stored_real_epsilon()
// in the constructor. They don't seem to be defined at that point.
MLC(Real clampEpsilon = realEpsilon * 10,
StoredReal storedClampEpsilon = storedRealEpsilon * 10)
: clampingEpsilon(clampEpsilon),
storedClampingEpsilon(storedClampEpsilon) {}
void set_epsilon_multiplier(int mult)
{ASSERT(mult >= 0);
clampingEpsilon = mult * real_epsilon();
storedClampingEpsilon = mult * stored_real_epsilon();}
static Real real_epsilon() { return realEpsilon;}
static StoredReal stored_real_epsilon() { return storedRealEpsilon;}
static void get_epsilon(Real & val) {val = realEpsilon;}
static void get_epsilon(StoredReal & val) {val = storedRealEpsilon;}
// Amount by which values to be clamped may exceed the range, without
// aborting.
Real clamping_epsilon() const {return clampingEpsilon;}
StoredReal stored_clamping_epsilon() const {return storedClampingEpsilon;}
void get_clamping_epsilon(Real& val) const {val = clampingEpsilon;}
void get_clamping_epsilon(StoredReal& val) const
{val = storedClampingEpsilon;}
// approx_less() and approx_greater() are disjoint with approx_equal().
Bool approx_equal(Real lhs, Real rhs, int precMultiplier = 1) const
{DBG(ASSERT(precMultiplier >= 0));
return (fabs(lhs - rhs) <=
clampingEpsilon * precMultiplier *
MLC::max(Real(1), MLC::min(fabs(lhs), fabs(rhs))));
}
Bool approx_greater(Real lhs, Real rhs, int precMultiplier = 1) const
{return (approx_equal(lhs, rhs, precMultiplier) ? FALSE : (lhs > rhs));}
Bool approx_less(Real lhs, Real rhs, int precMultiplier = 1) const
{return (approx_equal(lhs, rhs, precMultiplier) ? FALSE : (lhs < rhs));}
void verify_approx_equal(Real lhs, Real rhs, const char* const errMsg,
int precMultiplier = 1) const
{if (!approx_equal(lhs, rhs, precMultiplier))
err << errMsg << endl << lhs << " versus " << rhs << fatal_error;
}
Bool approx_equal(StoredReal lhs, StoredReal rhs,
int precMultiplier = 1) const
{DBG(ASSERT(precMultiplier >= 0));
return (fabs(lhs - rhs) <=
storedClampingEpsilon * precMultiplier *
MLC::max(StoredReal(1),
StoredReal(MLC::min(fabs(lhs), fabs(rhs)))));
}
Bool approx_greater(StoredReal lhs, StoredReal rhs,
int precMultiplier = 1) const
{return (approx_equal(lhs, rhs, precMultiplier) ? FALSE : (lhs > rhs));}
Bool approx_less(StoredReal lhs, StoredReal rhs,
int precMultiplier = 1) const
{return (approx_equal(lhs, rhs, precMultiplier) ? FALSE : (lhs < rhs));}
void verify_approx_equal(StoredReal lhs, StoredReal rhs,
const char* const errMsg,
int precMultiplier = 1) const
{if (!approx_equal(lhs, rhs, precMultiplier))
err << errMsg << endl << lhs << " versus " << rhs << fatal_error;
}
Real real_max(const Array<Real> &realArray);
Real real_max(const Array<Real> &realArray, int &idx);
Real real_min(const Array<Real> &realArray);
Real real_min(const Array<Real> &realArray, int &idx);
// These are approximate equality functions for Arrays of Reals
// and StoredReals.
Bool approx_equal(const Array<Real>& a1, const Array<Real>& a2,
int precMult = 1);
Bool approx_equal(const Array<StoredReal>& a1,
const Array<StoredReal>& a2, int precMult = 1);
//void verify_approx_equal(const Array<Real>& a1, const Array<Real>& a2,
// const char* const errMsg, int precMult = 1);
//void verify_approx_equal(const Array<StoredReal>& a1,
// const Array<StoredReal>& a2,
// const char* const errMsg, int precMult = 1);
// These are approximate equality functions for Array2s of Reals
// and StoredReals.
Bool approx_equal(const Array2<Real>& a1, const Array2<Real>& a2,
int precMult = 1);
Bool approx_equal(const Array2<StoredReal>& a1,
const Array2<StoredReal>& a2, int precMult = 1);
//void verify_approx_equal(const Array2<Real>& a1, const Array2<Real>& a2,
// const char* const errMsg, int precMult = 1);
//void verify_approx_equal(const Array2<StoredReal>& a1,
// const Array2<StoredReal>& a2,
// const char* const errMsg, int precMult = 1);
// The verify_strictly_XXX functions do not use "greater than" or
// "less than". They use "must exceed by epsilon" and
// "must be at least epsilon less than".
// They are not mutually disjoint.
static void verify_strictly_in_range(Real source, Real lowBound,
Real highBound,
const char* const
additionalErrMsg = NULL);
static void verify_strictly_greater(Real lhs, Real rhs,
const char* const
additionalErrMsg = NULL);
static void verify_strictly_less(Real lhs, Real rhs,
const char* const additionalErrMsg = NULL);
static void verify_strictly_in_range(StoredReal source,
StoredReal lowBound,
StoredReal highBound,
const char* const
additionalErrMsg = NULL);
static void verify_strictly_greater(StoredReal lhs, StoredReal rhs,
const char* const
additionalErrMsg = NULL);
static void verify_strictly_less(StoredReal lhs, StoredReal rhs,
const char* const additionalErrMsg = NULL);
// The clampXXX functions will take a value that is outside, by epsilon,
// that desired, and clamp it to the range.
void clamp_to_range(Real& source, Real lowBound, Real highBound,
const char* const additionalErrMsg = NULL,
int precMultiplier = 1) const;
void clamp_above(Real& source, Real lowBound,
const char* const additionalErrMsg = NULL,
int precMultiplier = 1) const;
void clamp_below(Real& source, Real highBound,
const char* const additionalErrMsg = NULL,
int precMultiplier = 1) const;
void clamp_to_range(StoredReal& source, StoredReal lowBound,
StoredReal highBound,
const char* const additionalErrMsg = NULL,
int precMultiplier = 1) const;
void clamp_above(StoredReal& source, StoredReal lowBound,
const char* const additionalErrMsg = NULL,
int precMultiplier = 1) const;
void clamp_below(StoredReal& source, StoredReal highBound,
const char* const additionalErrMsg = NULL,
int precMultiplier = 1) const;
// @@ These should be template members once the compiler supports them.
static int max(int lhs, int rhs)
{return (lhs >= rhs) ? lhs : rhs;}
static int min(int lhs, int rhs)
{return (lhs <= rhs) ? lhs : rhs;}
static Real max(Real lhs, Real rhs)
{return (lhs >= rhs) ? lhs : rhs;}
static Real min(Real lhs, Real rhs)
{return (lhs <= rhs) ? lhs : rhs;}
static StoredReal max(StoredReal lhs, StoredReal rhs)
{return (lhs >= rhs) ? lhs : rhs;}
static StoredReal min(StoredReal lhs, StoredReal rhs)
{return (lhs <= rhs) ? lhs : rhs;}
// dribble
static void reset_dribble();
static void dribble_wrapper(Real dribble);
static void finish_dribble();
#ifdef TEST_PERFORMANCE
// performance measurement; this is in basics.h so that we can redefine
// TEST_PERFORMANCE in a separate file
void start_performance_meas(const MString& str) {
clock_gettime(CLOCK_REALTIME, &start);
Mcout << "\n--- " << str << " started at "
<< start.tv_sec << " sec " << start.tv_nsec << " nanosec ...\n";
perfExpName = &str;
}
void stop_performance_meas() {
clock_gettime(CLOCK_REALTIME, &stop);
// need unbuffered output for debugging
Mcout << "\n ... finished at "
<< stop.tv_sec << " sec " << stop.tv_nsec << " nanosec ---\n";
Mcout << *perfExpName << " took " << (stop.tv_sec - start.tv_sec) +
1.e-9 * (stop.tv_nsec - start.tv_nsec) << " seconds\n";
}
#else
// start_performance_meas does not compile without an assignment
void start_performance_meas(const MString&) { }
void stop_performance_meas() { }
#endif // TEST_PERFORMANCE
};
extern MLC mlc;
// Compare two Array<Real>s for equality, where the desired
// precision multiplier. Always treat as StoredRead for comparison
// purposes.
Bool stored_real_equal(const Array<Real>& a1, const Array<Real>& a2,
int precMultiplier = 1);
Bool stored_real_equal(const Array<Real>& a1, const Array<StoredReal>& a2,
int precMultiplier = 1);
Bool stored_real_equal(const Array<StoredReal>& a1, const Array<Real>& a2,
int precMultiplier = 1);
Bool stored_real_equal(const Array<StoredReal>& a1,
const Array<StoredReal>& a2,
int precMultiplier = 1);
Bool stored_real_equal(const Array2<Real>& a1, const Array2<Real>& a2,
int precMultiplier = 1);
// This class contains flags which can be used to determine if other
// static classes in basics are fully initialized.
class StaticInit {
NO_DEFAULT_OPS(StaticInit);
public:
// Public member data
Bool is_initialized; // Using an accessor function may cause a crash
// if this is accessed at the wrong time.
// Methods
StaticInit() { is_initialized = TRUE; }
// The following line is just a way of using variables so we
// don't get variable declared but never used.
// iostream_init doesn't appear in this form under MSVC
#if defined(PC_MSVC) || defined(GNU)
void use_vars() { (void)mlcInit; }
#elif defined(MIPS_PRO)
// Since 7.21, the iostream_init has been fixed and actually
// our statement causes a warning if it's there.
# if (_COMPILER_VERSION <= 720)
void use_vars() { (void)mlcInit; (void)iostream_init; }
# else
void use_vars() { (void)mlcInit; }
# endif
#else
#error "compiler not supported"
#endif
~StaticInit() { is_initialized = FALSE; }
};
// Ternary value that extends Bool
class NYU {
// Member data
enum {unset = -1, no = 0, yes = 1} value;
public:
NYU() {value = unset;}
NYU(Bool b) {if (b) value = yes; else value = no;}
NYU(int b) {if (b == 1) value = yes; else if (b == 0) value = no;
else err << "NYU::int constructor not 0 or 1" << fatal_error;}
const NYU& operator=(const NYU& nyu) { value = nyu.value; return *this;}
Bool operator=(Bool b) {return b ? (value = yes) : (value = no);}
Bool operator=(int b) {if (b == 1) return value = yes;
else if (b == 0) return value = no;
else {err << "NYU::int constructor not 0 or 1" << fatal_error; return 0;}}
Bool operator!() const {return !Bool(*this);}
Bool is_unset() const {return value == unset;}
operator Bool() const {if (value == unset)
err << "NYU::attempt to use unset value" << fatal_error;
return value == yes;}
void display(MLCOStream& stream = Mcout) const;
};
DECLARE_DISPLAY(NYU)
// Check this member to see if initialization is complete.
extern StaticInit basicStatics;
*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -