File: int_sequence.cpp

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (388 lines) | stat: -rw-r--r-- 6,254 bytes parent folder | download | duplicates (5)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*1:*/
#line 6 "./int_sequence.cweb"

#include "int_sequence.h"
#include "symmetry.h"
#include "tl_exception.h"

#include <cstdio> 
#include <climits> 

/*2:*/
#line 42 "./int_sequence.cweb"

IntSequence::IntSequence(const Symmetry&sy,const IntSequence&se)
:data(new int[sy.dimen()]),length(sy.dimen()),destroy(true)
{
int k= 0;
for(int i= 0;i<sy.num();i++)
for(int j= 0;j<sy[i];j++,k++)
operator[](k)= se[i];
}


/*:2*/
#line 14 "./int_sequence.cweb"
;
/*3:*/
#line 63 "./int_sequence.cweb"

IntSequence::IntSequence(const Symmetry&sy,const vector<int> &se)
:data(new int[sy.num()]),length(sy.num()),destroy(true)
{
TL_RAISE_IF(sy.dimen()<=se[se.size()-1],
"Sequence is not reachable by symmetry in IntSequence()");
for(int i= 0;i<length;i++)
operator[](i)= 0;

for(unsigned int i= 0;i<se.size();i++)
operator[](sy.findClass(se[i]))++;
}

/*:3*/
#line 15 "./int_sequence.cweb"
;
/*4:*/
#line 79 "./int_sequence.cweb"

IntSequence::IntSequence(int i,const IntSequence&s)
:data(new int[s.size()+1]),length(s.size()+1),destroy(true)
{
int j= 0;
while(j<s.size()&&s[j]<i)
j++;
for(int jj= 0;jj<j;jj++)
operator[](jj)= s[jj];
operator[](j)= i;
for(int jj= j;jj<s.size();jj++)
operator[](jj+1)= s[jj];
}

/*:4*/
#line 16 "./int_sequence.cweb"
;
/*5:*/
#line 94 "./int_sequence.cweb"

IntSequence::IntSequence(int i,const IntSequence&s,int pos)
:data(new int[s.size()+1]),length(s.size()+1),destroy(true)
{
TL_RAISE_IF(pos<0||pos> s.size(),
"Wrong position for insertion IntSequence constructor");
for(int jj= 0;jj<pos;jj++)
operator[](jj)= s[jj];
operator[](pos)= i;
for(int jj= pos;jj<s.size();jj++)
operator[](jj+1)= s[jj];
}

/*:5*/
#line 17 "./int_sequence.cweb"
;
/*6:*/
#line 108 "./int_sequence.cweb"

const IntSequence&IntSequence::operator= (const IntSequence&s)
{
TL_RAISE_IF(!destroy&&length!=s.length,
"Wrong length for in-place IntSequence::operator=");
if(destroy&&length!=s.length){
delete[]data;
data= new int[s.length];
destroy= true;
length= s.length;
}
memcpy(data,s.data,sizeof(int)*length);
return*this;
}


/*:6*/
#line 18 "./int_sequence.cweb"
;
/*7:*/
#line 125 "./int_sequence.cweb"

bool IntSequence::operator==(const IntSequence&s)const
{
if(size()!=s.size())
return false;

int i= 0;
while(i<size()&&operator[](i)==s[i])
i++;
return i==size();
}

/*:7*/
#line 19 "./int_sequence.cweb"
;
/*8:*/
#line 139 "./int_sequence.cweb"

bool IntSequence::operator<(const IntSequence&s)const
{
int len= min(size(),s.size());

int i= 0;
while(i<len&&operator[](i)==s[i])
i++;
return(i<s.size()&&(i==size()||operator[](i)<s[i]));
}

/*:8*/
#line 20 "./int_sequence.cweb"
;
/*9:*/
#line 151 "./int_sequence.cweb"

bool IntSequence::lessEq(const IntSequence&s)const
{
TL_RAISE_IF(size()!=s.size(),
"Sequence with different lengths in IntSequence::lessEq");

int i= 0;
while(i<size()&&operator[](i)<=s[i])
i++;
return(i==size());
}

/*:9*/
#line 21 "./int_sequence.cweb"
;
/*10:*/
#line 164 "./int_sequence.cweb"

bool IntSequence::less(const IntSequence&s)const
{
TL_RAISE_IF(size()!=s.size(),
"Sequence with different lengths in IntSequence::less");

int i= 0;
while(i<size()&&operator[](i)<s[i])
i++;
return(i==size());
}

/*:10*/
#line 22 "./int_sequence.cweb"
;
/*11:*/
#line 179 "./int_sequence.cweb"

void IntSequence::sort()
{
for(int i= 0;i<length;i++){
int swaps= 0;
for(int j= 0;j<length-1;j++){
if(data[j]> data[j+1]){
int s= data[j+1];
data[j+1]= data[j];
data[j]= s;
swaps++;
}
}
if(swaps==0)
return;
}
}

/*:11*/
#line 23 "./int_sequence.cweb"
;
/*12:*/
#line 200 "./int_sequence.cweb"

void IntSequence::monotone()
{
for(int i= 1;i<length;i++)
if(data[i-1]> data[i])
data[i]= data[i-1];
}

/*:12*/
#line 24 "./int_sequence.cweb"
;
/*13:*/
#line 213 "./int_sequence.cweb"

void IntSequence::pmonotone(const Symmetry&s)
{
int cum= 0;
for(int i= 0;i<s.num();i++){
for(int j= cum+1;j<cum+s[i];j++)
if(data[j-1]> data[j])
data[j]= data[j-1];
cum+= s[i];
}
}

/*:13*/
#line 25 "./int_sequence.cweb"
;
/*14:*/
#line 226 "./int_sequence.cweb"

int IntSequence::sum()const
{
int res= 0;
for(int i= 0;i<length;i++)
res+= operator[](i);
return res;
}

/*:14*/
#line 26 "./int_sequence.cweb"
;
/*15:*/
#line 238 "./int_sequence.cweb"

int IntSequence::mult(int i1,int i2)const
{
int res= 1;
for(int i= i1;i<i2;i++)
res*= operator[](i);
return res;
}

/*:15*/
#line 27 "./int_sequence.cweb"
;
/*16:*/
#line 248 "./int_sequence.cweb"

int IntSequence::getPrefixLength()const
{
int i= 0;
while(i+1<size()&&operator[](i+1)==operator[](0))
i++;
return i+1;
}

/*:16*/
#line 28 "./int_sequence.cweb"
;
/*17:*/
#line 260 "./int_sequence.cweb"

int IntSequence::getNumDistinct()const
{
int res= 0;
if(size()> 0)
res++;
for(int i= 1;i<size();i++)
if(operator[](i)!=operator[](i-1))
res++;
return res;
}

/*:17*/
#line 29 "./int_sequence.cweb"
;
/*18:*/
#line 275 "./int_sequence.cweb"

int IntSequence::getMax()const
{
int res= INT_MIN;
for(int i= 0;i<size();i++)
if(operator[](i)> res)
res= operator[](i);
return res;
}

/*:18*/
#line 30 "./int_sequence.cweb"
;
/*19:*/
#line 286 "./int_sequence.cweb"

void IntSequence::add(int i)
{
for(int j= 0;j<size();j++)
operator[](j)+= i;
}

/*:19*/
#line 31 "./int_sequence.cweb"
;
/*20:*/
#line 294 "./int_sequence.cweb"

void IntSequence::add(int f,const IntSequence&s)
{
TL_RAISE_IF(size()!=s.size(),
"Wrong sequence length in IntSequence::add");
for(int j= 0;j<size();j++)
operator[](j)+= f*s[j];
}

/*:20*/
#line 32 "./int_sequence.cweb"
;
/*21:*/
#line 304 "./int_sequence.cweb"

bool IntSequence::isPositive()const
{
int i= 0;
while(i<size()&&operator[](i)>=0)
i++;
return(i==size());
}

/*:21*/
#line 33 "./int_sequence.cweb"
;
/*22:*/
#line 314 "./int_sequence.cweb"

bool IntSequence::isConstant()const
{
bool res= true;
int i= 1;
while(res&&i<size()){
res= res&&operator[](0)==operator[](i);
i++;
}
return res;
}

/*:22*/
#line 34 "./int_sequence.cweb"
;
/*23:*/
#line 327 "./int_sequence.cweb"

bool IntSequence::isSorted()const
{
bool res= true;
int i= 1;
while(res&&i<size()){
res= res&&operator[](i-1)<=operator[](i);
i++;
}
return res;
}



/*:23*/
#line 35 "./int_sequence.cweb"
;
/*24:*/
#line 342 "./int_sequence.cweb"

void IntSequence::print()const
{
printf("[");
for(int i= 0;i<size();i++)
printf("%2d ",operator[](i));
printf("]\n");
}

/*:24*/
#line 36 "./int_sequence.cweb"
;

/*:1*/