source: git/factory/int_int.h @ f5d2647

spielwiese
Last change on this file since f5d2647 was b15cf85, checked in by Martin Lee <martinlee84@…>, 11 years ago
fix: memory leak Conflicts: factory/canonicalform.h
  • Property mode set to 100644
File size: 6.2 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3#ifndef INCL_INT_INT_H
4#define INCL_INT_INT_H
5
6// #include "config.h"
7
8#ifndef NOSTREAMIO
9#ifdef HAVE_IOSTREAM
10#include <iostream>
11#define OSTREAM std::ostream
12#elif defined(HAVE_IOSTREAM_H)
13#include <iostream.h>
14#define OSTREAM ostream
15#endif
16#endif /* NOSTREAMIO */
17
18#include "cf_assert.h"
19
20#include "int_cf.h"
21// #include <factory/cf_gmp.h>
22#include "gmpext.h"
23
24#ifdef HAVE_OMALLOC
25#ifndef OM_NDEBUG
26#define OM_NDEBUG
27#endif
28#  include <omalloc/omalloc.h>
29#endif
30
31class InternalInteger : public InternalCF
32{
33private:
34    mpz_t thempi;
35
36    // auxilliary methods
37    inline InternalCF * normalizeMyself ();
38    inline InternalCF * uiNormalizeMyself ();
39
40    static inline InternalCF * normalizeMPI ( mpz_ptr );
41    static inline InternalCF * uiNormalizeMPI ( mpz_ptr );
42
43    static inline mpz_ptr MPI ( const InternalCF * const c );
44#ifdef HAVE_OMALLOC
45  static const omBin InternalInteger_bin;
46#endif
47public:
48#ifdef HAVE_OMALLOC
49  void* operator new(size_t)
50    {
51      void* addr;
52      omTypeAllocBin(void*, addr, InternalInteger_bin);
53      return addr;
54    }
55  void operator delete(void* addr, size_t)
56    {
57      omFreeBin(addr, InternalInteger_bin);
58    }
59#endif
60
61    InternalInteger();
62    InternalInteger( const InternalCF& )
63    {
64        ASSERT( 0, "ups there is something wrong in your code" );
65    }
66    InternalInteger( const int i );
67    InternalInteger( const long i );
68    InternalInteger( const char * str, const int base=10 );
69    InternalInteger( const mpz_ptr );
70    ~InternalInteger();
71    InternalCF* deepCopyObject() const;
72    const char * classname() const { return "InternalInteger"; }
73#ifndef NOSTREAMIO
74    void print( OSTREAM&, char* );
75#endif /* NOSTREAMIO */
76    InternalCF* genZero();
77    InternalCF* genOne();
78
79    bool is_imm() const;
80
81    int levelcoeff() const { return IntegerDomain; }
82    InternalCF* neg();
83
84    int comparesame( InternalCF* );
85
86    InternalCF* addsame( InternalCF* );
87    InternalCF* subsame( InternalCF* );
88    InternalCF* mulsame( InternalCF* );
89    InternalCF* dividesame( InternalCF* );
90    InternalCF* modulosame( InternalCF* );
91    InternalCF* divsame( InternalCF* );
92    InternalCF* modsame( InternalCF* );
93    void divremsame( InternalCF*, InternalCF*&, InternalCF*& );
94    bool divremsamet( InternalCF*, InternalCF*&, InternalCF*& );
95
96    int comparecoeff( InternalCF* );
97
98    InternalCF* addcoeff( InternalCF* );
99    InternalCF* subcoeff( InternalCF*, bool );
100    InternalCF* mulcoeff( InternalCF* );
101    InternalCF* dividecoeff( InternalCF*, bool );
102    InternalCF* modulocoeff( InternalCF*, bool );
103    InternalCF* divcoeff( InternalCF*, bool );
104    InternalCF* modcoeff( InternalCF*, bool );
105    void divremcoeff( InternalCF*, InternalCF*&, InternalCF*&, bool );
106    bool divremcoefft( InternalCF*, InternalCF*&, InternalCF*&, bool );
107
108    InternalCF * bgcdsame ( const InternalCF * const ) const;
109    InternalCF * bgcdcoeff ( const InternalCF * const );
110
111    InternalCF * bextgcdsame ( InternalCF *, CanonicalForm &, CanonicalForm & );
112    InternalCF * bextgcdcoeff ( InternalCF *, CanonicalForm &, CanonicalForm & );
113
114    long intval() const;
115
116    int intmod( int p ) const;
117
118    int sign() const;
119
120    InternalCF* sqrt();
121
122    int ilog2();
123
124    friend class InternalRational;
125    friend void gmp_numerator ( const CanonicalForm & f, mpz_ptr result);
126    friend void gmp_denominator ( const CanonicalForm & f, mpz_ptr result );
127    friend mpz_ptr getmpi ( InternalCF * value, bool symmetric );
128    friend void getmpi ( InternalCF * value, mpz_t mpi);
129};
130
131//{{{ inline InternalCF * InternalInteger::normalizeMyself, uiNormalizeMyself ()
132//{{{ docu
133//
134// normalizeMyself(), uiNormalizeMyself() - normalize CO.
135//
136// If CO fits into an immediate integer, delete CO and return the
137// immediate.  Otherwise, return a pointer to CO.
138//
139// `uiNormalizeMyself()' is the same as `normalizeMyself()'
140// except that CO is expected to be non-begative.  In this case,
141// we may use `mpz_get_ui()' to convert the underlying mpi into
142// an immediate which is slightly faster than the signed variant.
143//
144// Note: We do not mind reference counting at this point!  CO is
145// deleted unconditionally!
146//
147//}}}
148inline InternalCF *
149InternalInteger::normalizeMyself ()
150{
151    ASSERT( getRefCount() == 1, "internal error: must not delete CO" );
152
153    if ( mpz_is_imm( thempi ) ) {
154        InternalCF * result = int2imm( mpz_get_si( thempi ) );
155        delete this;
156        return result;
157    } else
158        return this;
159}
160
161inline InternalCF *
162InternalInteger::uiNormalizeMyself ()
163{
164    ASSERT( getRefCount() == 1, "internal error: must not delete CO" );
165
166    if ( mpz_is_imm( thempi ) ) {
167        InternalCF * result = int2imm( mpz_get_ui( thempi ) );
168        delete this;
169        return result;
170    } else
171        return this;
172}
173//}}}
174
175//{{{ static inline InternalCF * InternalInteger::normalizeMPI, uiNormalizeMPI ( mpz_ptr aMpi )
176//{{{ docu
177//
178// normalizeMPI(), uiNormalizeMPI() - normalize a mpi.
179//
180// If `aMpi' fits into an immediate integer, clear `aMpi' and
181// return the immediate.  Otherwise, return a new
182// `InternalInteger' with `aMpi' as underlying mpi.
183//
184// `uiNormalizeMPI()' is the same as `normalizeMPI()' except that
185// `aMpi' is expected to be non-begative.  In this case, we may
186// use `mpz_get_ui()' to convert `aMpi' into an immediate which
187// is slightly faster than the signed variant.
188//
189//}}}
190inline InternalCF *
191InternalInteger::normalizeMPI ( mpz_ptr aMpi )
192{
193    if ( mpz_is_imm( aMpi ) ) {
194        InternalCF * result = int2imm( mpz_get_si( aMpi ) );
195        mpz_clear( aMpi );
196        return result;
197    } else
198        return new InternalInteger( aMpi );
199}
200
201inline InternalCF *
202InternalInteger::uiNormalizeMPI ( mpz_ptr aMpi )
203{
204    if ( mpz_is_imm( aMpi ) ) {
205        InternalCF * result = int2imm( mpz_get_ui( aMpi ) );
206        mpz_clear( aMpi );
207        return result;
208    } else
209        return new InternalInteger( aMpi );
210}
211//}}}
212
213//{{{ inline mpz_ptr InternalInteger::MPI ( const InternalCF * const c )
214//{{{ docu
215//
216// MPI() - return underlying mpi of `c'.
217//
218// `c' is expected to be an `InternalInteger *'.  `c's underlying
219// mpi is returned.
220//
221//}}}
222inline mpz_ptr
223InternalInteger::MPI ( const InternalCF * const c )
224{
225    return (((InternalInteger*)c)->thempi);
226}
227//}}}
228
229#endif /* ! INCL_INT_INT_H */
Note: See TracBrowser for help on using the repository browser.