CoCoALib-0.9905 date: 23 May 2007


MachineInteger.H

Go to the documentation of this file.
00001 #ifndef CoCoA_MachineInteger_H
00002 #define CoCoA_MachineInteger_H
00003 
00004 //   Copyright (c)  2007  John Abbott
00005 
00006 //   This file is part of the source of CoCoALib, the CoCoA Library.
00007 
00008 //   CoCoALib is free software; you can redistribute it and/or modify
00009 //   it under the terms of the GNU General Public License (version 2)
00010 //   as published by the Free Software Foundation.  A copy of the full
00011 //   licence may be found in the file COPYING in this directory.
00012 
00013 //   CoCoALib is distributed in the hope that it will be useful,
00014 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 //   GNU General Public License for more details.
00017 
00018 //   You should have received a copy of the GNU General Public License
00019 //   along with CoCoA; if not, write to the Free Software
00020 //   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022 
00023 #include "CoCoA/assert.H"
00024 
00025 #include <limits>
00026 //using std::numeric_limits;
00027 #include <iosfwd>
00028 //using std::ostream;
00029 
00030 namespace CoCoA
00031 {
00032 
00033   // This class is deliberately inline.
00034   class MachineInteger
00035   {
00036   public:
00037     MachineInteger(signed char n): myValue(n), IamNegative(n<0) {}
00038     MachineInteger(unsigned char n): myValue(n), IamNegative(false) {}
00039     MachineInteger(short n): myValue(n), IamNegative(n<0) {}
00040     MachineInteger(unsigned short n): myValue(n), IamNegative(false) {}
00041     MachineInteger(int n): myValue(n), IamNegative(n<0) {}
00042     MachineInteger(unsigned int n): myValue(n), IamNegative(false) {}
00043     MachineInteger(long n): myValue(n), IamNegative(n<0) {}
00044     MachineInteger(unsigned long n): myValue(n), IamNegative(false) {}
00045     friend bool IsNegative(const MachineInteger& n);
00046     friend unsigned long AsUnsignedLong(const MachineInteger& n);
00047     friend long AsSignedLong(const MachineInteger& n);
00048     friend unsigned long abs(const MachineInteger& n);
00049   private:
00050     unsigned long myValue;
00051     bool IamNegative;
00052   };
00053 
00054   inline  bool IsNegative(const MachineInteger& n)
00055   { return n.IamNegative; }
00056 
00057   inline unsigned long AsUnsignedLong(const MachineInteger& n)
00058   {
00059     CoCoA_ASSERT(!n.IamNegative);
00060     return n.myValue;
00061   }
00062 
00063   inline long AsSignedLong(const MachineInteger& n)
00064   {
00065     CoCoA_ASSERT(n.IamNegative || n.myValue <= static_cast<unsigned long>(std::numeric_limits<long>::max()));
00066     return n.myValue;
00067   }
00068 
00069   inline unsigned long abs(const MachineInteger& n)
00070   { if (n.IamNegative) return -n.myValue; else return n.myValue; }
00071 
00072   std::ostream& operator<<(std::ostream& out, const MachineInteger& n);
00073 
00074   inline bool IsZero(const MachineInteger& n)
00075   { return AsUnsignedLong(n) == 0; }
00076 
00077 //  Will these two fns ever be useful???
00078 //   inline bool IsOne(const MachineInteger& n)
00079 //   { return (!IsNegative(n) && AsUnsignedLong(n) == 1); }
00080 
00081 //   inline bool IsMinusOne(const MachineInteger& n)
00082 //   { return (IsNegative(n) && AsSignedLong(n) == -1); }
00083 
00084   inline int sign(const MachineInteger& n)
00085   { if (IsNegative(n)) return -1; if (IsZero(n)) return 0; return 1; }
00086 
00087   inline bool IsOdd(const MachineInteger& n)
00088   { return abs(n)&1; }
00089 
00090   inline bool IsEven(const MachineInteger& n)
00091   { return !IsOdd(n); }
00092 
00093 
00094   int cmp(const MachineInteger& n1, const MachineInteger& n2);
00095   unsigned long gcd(const MachineInteger& a, const MachineInteger& b);
00096 
00097 
00098 } // end of namespace CoCoA
00099 
00100 
00101 
00102 // RCS header/log in the next few lines
00103 // $Header: /Volumes/Home/cocoa/cvs-repository/CoCoALib-0.99/include/CoCoA/MachineInteger.H,v 1.1 2007/05/21 12:57:28 abbott Exp $
00104 // $Log: MachineInteger.H,v $
00105 // Revision 1.1  2007/05/21 12:57:28  abbott
00106 // New class for passing machine integers as args; includes some simple
00107 // operations on machine integers (cmp, gcd, IsNegative,...).  Operations
00108 // between ZZ and machine integers modified to use the new class.  Inexact
00109 // integer division (of a ZZ) by a negative value now triggers an error;
00110 // new error for reporting inexact integer division by a negative value.
00111 //
00112 //
00113 
00114 #endif

Generated on Wed May 23 13:45:22 2007 for CoCoALib by  doxygen 1.4.6