📄 mscalar.h
字号:
// file: $isip/class/math/scalar/MScalar/MScalar.h// version: $Id: MScalar.h,v 1.86 2002/08/02 23:46:55 zheng Exp $//// make sure definitions are only made once//#ifndef ISIP_MSCALAR#define ISIP_MSCALAR// isip include files//#ifndef ISIP_MSCALAR_METHODS#include "MScalarMethods.h"#endif#ifndef ISIP_STRING#include <String.h>#endif#ifndef ISIP_RANDOM#include <Random.h>#endif// MScalar: the scalar template class is inherited by other// scalar classes. TIntegral is an Integral type, TSize is a size// specific data type. most of the methods in this class are inline// wrappers to the MScalarMethods class functions. this is done to// allow us to write one method per file, thus avoiding the compilation// overhead inherent in compiling template classes with multiple// explicit instantiations.//template<class TIntegral, class TSize>class MScalar { //--------------------------------------------------------------------------- // // public constants // //---------------------------------------------------------------------------public: // define the class name // static const String CLASS_NAME; //---------------------------------------- // // other important constants // //---------------------------------------- // random number related constants // static const double RAND_BYTE_MAX = Integral::TWO_EXP8; static const double RAND_USHORT_MAX = Integral::TWO_EXP16; static const double RAND_ULONG_MAX = Integral::TWO_EXP32; static const double RAND_ULLONG_MAX = Integral::TWO_EXP64; static const double RAND_SHORT_MAX = Integral::TWO_EXP15; static const double RAND_LONG_MAX = Integral::TWO_EXP31; static const double RAND_LLONG_MAX = Integral::TWO_EXP63; // i/o related constants // static const String DEF_PARAM; //---------------------------------------- // // default values and arguments // //---------------------------------------- // define the default value(s) of the class data // static const TIntegral DEF_VALUE; //---------------------------------------- // // error codes // //---------------------------------------- static const long ERR = 20000; //--------------------------------------------------------------------------- // // protected data // //---------------------------------------------------------------------------protected: // the container for the value of this item // TIntegral value_d; // declare a static debug level for all class instantiations: // we don't want every object to waste space on a debug level. let // them share this across all classes. // static Integral::DEBUG debug_level_d; //--------------------------------------------------------------------------- // // required public methods // //---------------------------------------------------------------------------public: // method: name // static const String& name() { return CLASS_NAME; } // method: diagnose // static boolean diagnose(Integral::DEBUG debug_level) { return MScalarMethods:: diagnose<MScalar<TIntegral, TSize>, TIntegral>(debug_level); } // method: setDebug // static boolean setDebug(Integral::DEBUG level) { debug_level_d = level; return true; } // other debug methods // boolean debug(const unichar* msg) const; // method: destructor // ~MScalar() {} // method: default constructor // MScalar() {} // method: copy constructor // MScalar(const MScalar& arg) { assign(arg); } // method: assign // boolean assign(const MScalar& arg) { value_d = (TIntegral)arg; return true; } // method operator= // we need to have the overloaded operator '=' defined in the // derived classes instead of the base template class. this is // required since c++ predefines the operator '=' for any class by // default and this hides any other definition of this operator in // the base class. // // method: sofSize // long sofSize() const { return (long)sizeof(TSize); } // method: read // boolean read(Sof& sof, long tag, const String& name = CLASS_NAME) { return MScalarMethods::read(*this, sof, tag, name); } // method: write // boolean write(Sof& sof, long tag, const String& name = CLASS_NAME) const { return MScalarMethods::write(*this, sof, tag, name); } // method: readData // boolean readData(Sof& sof, const String& pname = DEF_PARAM, long size = SofParser::FULL_OBJECT, boolean param = true, boolean nested = false) { return MScalarMethods::readData(*this, sof, pname, size, param, nested); } // method: writeData // boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const { return MScalarMethods::writeData(*this, sof, pname); } // method: eq // boolean eq(const MScalar& arg_a) const { return (value_d == arg_a.value_d); } // memory management methods: // new and delete methods are omitted because they are defined in the // classes that instantiate this template // // method: clear // boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE) { value_d = (TIntegral)DEF_VALUE; return true; } //--------------------------------------------------------------------------- // // class-specific public methods: // extensions to required methods // //--------------------------------------------------------------------------- // method: constructor // note that this method is useful to have for the diagnose method. // it is masked by higher-level classes that use this as a template // and have their own version of this constructor. // MScalar(TIntegral arg) { assign(arg); } // method: assign // template<class TAIntegral> boolean assign(TAIntegral arg) { value_d = (TIntegral)arg; return true; } // method: assign // boolean assign(const String& arg) { arg.get(value_d); return true; } // method: get // boolean get(String& arg) const { return arg.assign(value_d); } // method: memSize // long memSize() const { return (long)sizeof(value_d); } // method: presentValue // define functions which are used to wrap member functions that have // different implementations for different data types, and return the // correct value by using template specialization. // TIntegral presentValue(double arg) const { return (TIntegral)Integral::round(arg); } // method: presentValue // the complex function overload is a dummy function. all of the // instantiations are specialized. // TIntegral presentValue(complexdouble arg) const { return Error::handle(name(), L"presentValue", Error::TEMPLATE_TYPE, __FILE__, __LINE__); } // method: almostEqual // boolean almostEqual(TIntegral arg) const { return Integral::almostEqual(value_d, arg); } // method: almostEqual // boolean almostEqual(TIntegral arg, double percent, double bound = Integral::DEF_BOUND) const { return Integral::almostEqual(value_d, arg, percent, bound); } //--------------------------------------------------------------------------- // // class-specific public methods: // operator overloads // //--------------------------------------------------------------------------- // method: operator TIntegral() // operator TIntegral() const { return value_d; } // method: operator + // TIntegral operator+ (TIntegral arg) const { return value_d + arg; } // method: operator - // TIntegral operator- (TIntegral arg) const { return value_d - arg; } // method: operator * // TIntegral operator* (TIntegral arg) const { return value_d * arg; } // method: operator / // TIntegral operator/ (TIntegral arg) const { return value_d / arg; } // method: operator % // TIntegral operator% (TIntegral arg) const { return value_d % arg; } // method: operator ++ // TIntegral operator++ () { return ++value_d; } // method: operator -- // TIntegral operator-- () { return --value_d; } // method: operator ++ // TIntegral operator++ (int) { return value_d++; } // method: operator -- // TIntegral operator-- (int) { return value_d--; } // method: operator += // MScalar& operator+= (TIntegral arg) { value_d += arg; return *this; } // method: operator -= // MScalar& operator-= (TIntegral arg) { value_d -= arg; return *this; } // method: operator *= // MScalar& operator*= (TIntegral arg) { value_d *= arg; return *this; } // method: operator /= // MScalar& operator/= (TIntegral arg) { value_d /= arg; return *this; } // method: operator %= // MScalar& operator%= (TIntegral arg) { value_d %= arg; return *this; } // method: operator == // boolean operator == (TIntegral arg) const { return eq(arg); } // method: operator != // boolean operator != (TIntegral arg) const { return ne(arg); } // method: operator < // boolean operator < (TIntegral arg) const { return lt(arg); } // method: operator <= // boolean operator <= (TIntegral arg) const { return le(arg); } // method: operator > // boolean operator > (TIntegral arg) const { return gt(arg); } // method: operator >= // boolean operator >= (TIntegral arg) const { return ge(arg); } //--------------------------------------------------------------------------- // // class-specific public methods: // equality and comparison methods // //--------------------------------------------------------------------------- // method: eq // boolean eq(TIntegral arg) const { return (value_d == arg); } // method: eq // boolean eq(TIntegral arg1, TIntegral arg2) const { return (arg1 == arg2); } // method: ne // boolean ne(TIntegral arg) const { return (value_d != arg); } // method: lt // boolean lt(TIntegral arg) const { return (value_d < arg); } // method: le // boolean le(TIntegral arg) const { return (value_d <= arg); } // method: gt // boolean gt(TIntegral arg) const { return (value_d > arg); } // method: ge // boolean ge(TIntegral arg) const { return (value_d >= arg); } //--------------------------------------------------------------------------- // // class-specific public methods: // bitwise methods // //-------------------------------------------------------------------------- // method: band // TIntegral band(TIntegral arg) { return band(value_d, arg); } // method: band // TIntegral band(TIntegral arg1, TIntegral arg2) { return (value_d = (arg1 & arg2)); } // method: bor // TIntegral bor(TIntegral arg) { return bor(value_d, arg); } // method: bor // TIntegral bor(TIntegral arg1, TIntegral arg2) { return (value_d = (arg1 | arg2)); } // method: bxor // TIntegral bxor(TIntegral arg) { return bxor(value_d, arg); } // method: bxor // TIntegral bxor(TIntegral arg1, TIntegral arg2) { return (value_d = (arg1 ^ arg2)); } // method: brs // TIntegral brs(TIntegral incr) { return brs(value_d, incr); } // method: brs // TIntegral brs(TIntegral arg, TIntegral incr) { return (value_d = (arg >> incr)); } // method: bls // TIntegral bls(TIntegral incr) { return bls(value_d, incr); } // method: bls // TIntegral bls(TIntegral arg, TIntegral incr) { return (value_d = (arg << incr)); } // method: bcmpl // TIntegral bcmpl() { return bcmpl(value_d); } // method: bcmpl // TIntegral bcmpl(TIntegral arg) { return (value_d = (~arg)); } //--------------------------------------------------------------------------- // // class-specific public methods: // basic mathematical methods // //--------------------------------------------------------------------------- // method: add // TIntegral add(TIntegral arg2) { return add(value_d, arg2); } // method: add // TIntegral add(TIntegral arg1, TIntegral arg2) { return (value_d = arg1 + arg2); } // method: sub // TIntegral sub(TIntegral arg2) { return sub(value_d, arg2); } // method: sub // TIntegral sub(TIntegral arg1, TIntegral arg2) { return (value_d = arg1 - arg2); } // method: mult // TIntegral mult(TIntegral arg2) { return mult(value_d, arg2); } // method: mult // TIntegral mult(TIntegral arg1, TIntegral arg2) { return (value_d = arg1 * arg2); } // method: div // TIntegral div(TIntegral arg2) { return div(value_d, arg2); } // method: div // TIntegral div(TIntegral arg1, TIntegral arg2) { return (value_d = arg1 / arg2); } // method: mod // TIntegral mod(TIntegral arg2) { return mod(value_d, arg2); } // method: mod // TIntegral mod(TIntegral arg1, TIntegral arg2) { return (value_d = arg1 % arg2); } //--------------------------------------------------------------------------- // // class-specific public methods: // other mathematical methods (listed alphabetically) // //-------------------------------------------------------------------------- // method: abs // TIntegral abs() { return abs(value_d); } // method: abs // TIntegral abs(TIntegral arg) { if (arg > (TIntegral)0) { value_d = arg; } else { value_d = -arg; } return value_d; } // method: acos // TIntegral acos() { return acos(value_d); } // method: acos // TIntegral acos(TIntegral arg) { return (value_d = (TIntegral)Integral::acos(arg)); } // method: acosh // TIntegral acosh() { return acosh(value_d); } // method: acosh // TIntegral acosh(TIntegral arg) { return (value_d = (TIntegral)Integral::acosh(arg)); } // method: asin // TIntegral asin() { return asin(value_d); } // method: asin // TIntegral asin(TIntegral arg) { return (value_d = (TIntegral)Integral::asin(arg)); } // method: asinh // TIntegral asinh() { return asinh(value_d); } // method: asinh // TIntegral asinh(TIntegral arg) { return (value_d = (TIntegral)Integral::asinh(arg)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -