00001 /*========================================================================== 00002 * Copyright (c) 2001 Carnegie Mellon University. All Rights Reserved. 00003 * 00004 * Use of the Lemur Toolkit for Language Modeling and Information Retrieval 00005 * is subject to the terms of the software license set forth in the LICENSE 00006 * file included with this software, and also available at 00007 * http://www.cs.cmu.edu/~lemur/license.html 00008 * 00009 *========================================================================== 00010 */ 00011 00012 00013 // ---------------------------------------------------------------------- 00014 // Number.hpp 00015 // 10.5.95 00016 // ---------------------------------------------------------------------- 00017 00018 #ifndef _NUMBERH_ 00019 #define _NUMBERH_ 00020 00021 // #pragma interface 00022 00023 #include "common_headers.hpp" 00024 00025 template <class U> 00026 class Number 00027 { 00028 public: 00029 Number() {} 00030 Number(const U& u_p) { u = u_p; } 00031 00032 int hash() const 00033 { 00034 int i, hashval=1, nBytes=sizeof(u); 00035 char *p = (char*) &u; 00036 for (i=0; i<nBytes; i++) 00037 hashval = p[i] + 31*hashval; 00038 if (hashval<0) hashval*=-1; 00039 return hashval; 00040 } 00041 int Hash() const { return hash(); } 00042 00043 void read(istream &is) { is.read(&u, sizeof(u)); } 00044 void write(ostream &os) { os.write(&u, sizeof(u)); } 00045 00046 int operator<(const Number<U>& that) const { return (u< that.u? 1: 0); } 00047 int operator<=(const Number<U>& that) const { return (u<=that.u? 1: 0); } 00048 int operator>(const Number<U>& that) const { return (u> that.u? 1: 0); } 00049 int operator>=(const Number<U>& that) const { return (u>=that.u? 1: 0); } 00050 int operator==(const Number<U>& that) const { return (u==that.u? 1: 0); } 00051 int operator!=(const Number<U>& that) const { return (u!=that.u? 1: 0); } 00052 00053 Number<U> operator+=(const Number<U>& that) { u+=that.u; return u; } 00054 Number<U> operator-=(const Number<U>& that) { u-=that.u; return u; } 00055 Number<U> operator*=(const Number<U>& that) { u*=that.u; return u; } 00056 Number<U> operator/=(const Number<U>& that) { u/=that.u; return u; } 00057 00058 Number<U> operator+(const Number<U>& that) { return u+that.u; } 00059 Number<U> operator-(const Number<U>& that) { return u-that.u; } 00060 Number<U> operator*(const Number<U>& that) { return u*that.u; } 00061 Number<U> operator/(const Number<U>& that) { return u/that.u; } 00062 00063 00064 template <U> friend ostream& operator<<(ostream&, const Number<U>&) { 00065 os << s.u; return os;} 00066 // changed to remove dependence on GUIDE_DECLS, C. Zhai, 2/20/2001 00067 00068 // friend ostream& operator<<(ostream&, const Number<U>&); 00069 // this old version didn't work! (C. Zhai) 00070 00071 operator U() const { return u; } 00072 00073 private: 00074 U u; 00075 }; 00076 00077 // the following doesn't seem to work 00078 00079 /* It's now inside the class declaration. C. Zhai, 2/20/2001 00080 template <class U> 00081 ostream& operator<<(ostream& os, const Number<U>& s) {os<<s.u;return os;} 00082 */ 00083 00084 #endif 00085