-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuple.hpp
executable file
·204 lines (173 loc) · 4.11 KB
/
tuple.hpp
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
194
195
196
197
198
199
200
201
202
203
204
#ifndef TUPLE_HPP
#define TUPLE_HPP
#include <initializer_list>
#include <iostream>
#include <sstream>
using namespace std;
/*
N-Tuples of integers, used to represent the basis
for multivariate polynomials.
Structure:
class Tuple<N>{
public:
Tuple();
Tuple(initializer_list<unsigned int>);
inline unsigned int& operator [](unsigned int);
inline const unsigned int& operator [](unsigned int) const;
string toString() const;
bool isConst() const;
Tuple<N> operator+(const Tuple<N>) const;
Tuple<N> operator-(const Tuple<N>) const;
bool operator<=(const Tuple<N>) const; a<=b if a divides b
};
ostream& operator<<(ostream&, const Tuple<N>&);
unsigned int abs(Tuple<N>);
Tuple<N> LCM(Tuple<N>, Tuple<N>);
class lex;
class grlex;
class grevlex;
*/
const string _var[]={"x", "y", "z", "u", "v", "w"};
template<unsigned int N>
class Tuple{
public:
unsigned data[N];
Tuple(){
for(int i=0; i<N; i++)
data[i]=0;
}
Tuple(initializer_list<unsigned int> list){
int it=0;
if(list.size()!=N)
cout<<"Wrong number of arguments given in the initialization of N-tuple!\n";
else
for(int i:list)
data[it++]=i;
}
inline unsigned int& operator [](unsigned int i){
return data[i];
}
inline const unsigned int& operator [](unsigned int i) const{
return data[i];
}
string toString() const{
string s="";
for(int i=0; i<N; i++){
if(data[i]==0)
continue;
if(s!="")
s+="*";
#if N>6
s+="x_";
stringstream ss;
ss<<i+1;
string n;
ss>>n;
s+=n;
#endif
#if N<=6
s+=_var[i];
#endif
if(data[i]!=1){
s+="^";
stringstream ss2;
ss2<<data[i];
string e;
ss2>>e;
s+=e;
}
}
if(s=="")
return "1";
return s;
}
bool isConst() const{
for(int i=0; i<N; i++)
if(data[i]!=0)
return false;
return true;
}
Tuple<N> operator+(const Tuple<N> b) const{
Tuple ret;
for(int i=0; i<N; i++)
ret[i]=data[i]+b[i];
return ret;
}
Tuple<N> operator-(const Tuple<N> b) const{
Tuple ret;
for(int i=0; i<N; i++)
ret[i]=data[i]-b[i];
return ret;
}
bool operator<=(const Tuple<N> b) const{
for(int i=0; i<N; i++)
if(data[i]>b[i])
return false;
return true;
}
};
template<unsigned int N>
ostream& operator<<(ostream& os, const Tuple<N>& t){
os<<t.toString();
return os;
}
template<unsigned int N>
unsigned int abs(Tuple<N> t){
int s=0;
for(auto a:t.data)
s+=a;
return s;
}
template<unsigned int N>
Tuple<N> LCM(Tuple<N> a, Tuple<N> b){
Tuple<N> ret;
for(int i=0; i<N; i++)
ret[i]=max(a[i],b[i]);
return ret;
}
template<unsigned int N>
class lex{
public:
bool operator()(Tuple<N> a, Tuple<N> b){
for(int i=0; i<N; i++){
if(a[i]<b[i])
return true;
if(a[i]>b[i])
return false;
}
return false;
}
};
template<unsigned int N>
class grlex{
public:
bool operator()(Tuple<N> a, Tuple<N> b){
if(abs(a)==abs(b)){
for(int i=0; i<N; i++){
if(a[i]<b[i])
return true;
if(a[i]>b[i])
return false;
}
return false;
}
return abs(a)<abs(b);
}
};
template<unsigned int N>
class grevlex{
public:
bool operator()(Tuple<N> a, Tuple<N> b){
if(abs(a)==abs(b)){
for(int i=N-1; i>=0; i--){
if(a[i]>b[i])
return true;
if(a[i]<b[i])
return false;
}
return false;
}
return abs(a)<abs(b);
}
};
#endif