File: StaticElement.h

package info (click to toggle)
givaro 4.0.2-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,140 kB
  • ctags: 5,874
  • sloc: cpp: 31,446; makefile: 1,032; sh: 662; csh: 196
file content (193 lines) | stat: -rw-r--r-- 6,068 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//==================================================================
// Copyright(c)'1994-2009 by The Givaro group
// This file is part of Givaro.
// Givaro is governed by the CeCILL-B license under French law
// and abiding by the rules of distribution of free software.
// see the COPYRIGHT file for more details.
// -----------------------------------------------------------------
// Time-stamp: <12 Jun 15 18:43:52 Jean-Guillaume.Dumas@imag.fr>
// -----------------------------------------------------------------
// author: Jean-Guillaume.Dumas
// date: 2004
//==================================================================

/*! @file StaticElement.h
 * @ingroup zpz
 * @brief NO DOC
 */

#ifndef __GIVARO_static_element_H
#define __GIVARO_static_element_H

#include <iostream>
#include "gmp++/gmp++.h"

namespace Givaro {

	//! Static Element
template <class DomainStyle>
struct StaticElement {
    typedef DomainStyle Domain;
protected:
    static Domain _domain;
    typedef typename Domain::Element Rep;
    Rep _elem;
public:
    static void setDomain(const Domain& D) {
        StaticElement::_domain = D;
    }

    static const Domain & getDomain() {
        return StaticElement::_domain;
    }

    StaticElement()  { _domain.init(_elem); }


    StaticElement(const Integer& i) { _domain.init( (_elem), i); }
    StaticElement(const double& i) { _domain.init( (_elem), i); }
    StaticElement(const int32_t& i) { _domain.init( (_elem), (int64_t)i); }
    StaticElement(const uint32_t& i) { _domain.init( (_elem), (uint64_t)i); }
    StaticElement(const int64_t& i) { _domain.init( (_elem), i); }
    StaticElement(const uint64_t& i) { _domain.init( (_elem), i); }


    operator short() const		{ int64_t tmp; return (short)_domain.convert(tmp,_elem); }
    operator unsigned short() const 	{ uint64_t tmp; return (unsigned short)_domain.convert(tmp,_elem); }
    operator unsigned char() const 	{ uint64_t tmp; return (unsigned char)_domain.convert(tmp,_elem); }
    operator uint32_t() const	{ uint64_t tmp; return (uint32_t)_domain.convert(tmp,_elem); }
    operator int() const		{ int64_t tmp; return (int)_domain.convert(tmp,_elem); }
    operator float() const		{ double tmp; return (float)_domain.convert(tmp,_elem); }
    operator uint64_t() const	{ uint64_t tmp; return _domain.convert(tmp,_elem); }
    operator int64_t() const		{ int64_t tmp; return _domain.convert(tmp,_elem); }
    operator double() const		{ double tmp; return (double)_domain.convert(tmp,_elem); }
    operator Integer() const		{ Integer tmp; return _domain.convert(tmp,_elem); }
    template<class INITCST>
    operator INITCST() const 		{ INITCST tmp; return _domain.convert(tmp,_elem); }


    template<class INITCST>
    StaticElement(const INITCST& i) { _domain.init( (_elem), i); }

    template<class INITCST>
    StaticElement(const INITCST& i, const Domain& D) {
        setDomain(D);
        _domain.init((_elem), i);
    }

    StaticElement& operator=( const StaticElement& e) {
        _domain.assign((_elem), e._elem); return  *this;
    }

    bool operator==(const StaticElement& e) {
        return _domain.areEqual((_elem), e._elem);
    }

    bool operator!=(const StaticElement& e) {
        return !_domain.areEqual((_elem), e._elem);
    }

    bool isZero() const {
        return StaticElement::_domain.isZero(this->_elem);
    }

    static bool isZero(const StaticElement& e) {
        return e.isZero();
    }

    bool isOne() const {
        return StaticElement::_domain.isOne(this->_elem);
    }

    static bool isOne(const StaticElement& e) {
        return e.isOne();
    }

    bool isMOne() const {
        return StaticElement::_domain.isMOne(this->_elem);
    }

    static bool isMOne(const StaticElement& e) {
        return e.isMOne();
    }


    inline const StaticElement operator* (const StaticElement& e) const {
        StaticElement tmp;
        StaticElement(_domain.mul(tmp._elem,(_elem), e._elem));
        return tmp;
    }
    inline const StaticElement operator/ (const StaticElement& e) const {
        StaticElement tmp;
        StaticElement(_domain.div(tmp._elem,(_elem), e._elem));
        return tmp;
    }

    inline const StaticElement operator+ (const StaticElement& e) const {
        StaticElement tmp;
        StaticElement(_domain.add(tmp._elem,(_elem), e._elem));
        return tmp;
    }

    inline const StaticElement operator- (const StaticElement& e) const {
        StaticElement tmp;
        StaticElement(_domain.sub(tmp._elem,(_elem), e._elem));
        return tmp;
    }

    inline const StaticElement operator- () const {
        StaticElement tmp;
        StaticElement(_domain.neg(tmp._elem,(_elem)));
        return tmp;
    }



    inline StaticElement& operator*= (const StaticElement& e) {
        _domain.mulin((_elem), e._elem); return *this;
    }

    inline StaticElement& operator/= (const StaticElement& e) {
        _domain.divin((_elem), e._elem); return *this;
    }

    inline StaticElement& operator+= (const StaticElement& e) {
        _domain.addin((_elem), e._elem); return *this;
    }

    inline StaticElement& operator-= (const StaticElement& e) {
        _domain.subin((_elem), e._elem); return *this;
    }

    inline StaticElement& operator-- () {
        _domain.subin(this->_elem, _domain.one); return *this;
    }

    inline StaticElement& operator++ () {
        _domain.addin(this->_elem, _domain.one); return *this;
    }

    inline StaticElement operator-- (int) {
        StaticElement tmp(*this);
        _domain.subin(this->_elem, _domain.one); return tmp;
    }

    inline StaticElement operator++ (int) {
        StaticElement tmp(*this);
        _domain.addin(this->_elem, _domain.one); return tmp;
    }

    friend inline std::istream& operator>> (std::istream& i, StaticElement& a) {
        return _domain.read(i,(a._elem));
    }


    friend inline std::ostream& operator<< (std::ostream& o, const StaticElement& a) {
        return _domain.write(o,(a._elem));
    }

};

} // namespace Givaro

#endif // __GIVARO_static_element_H