Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/template.hpp
3206 views
1
#ifndef FILE_TEMPLATE
2
#define FILE_TEMPLATE
3
4
/**************************************************************************/
5
/* File: template.hh */
6
/* Author: Joachim Schoeberl */
7
/* Date: 01. Jun. 95 */
8
/**************************************************************************/
9
10
/*
11
templates, global types, defines and variables
12
*/
13
14
/// The following value may be adapted to the hardware !
15
#ifndef CLOCKS_PER_SEC
16
#define CLOCKS_PER_SEC 1000000
17
#endif
18
19
20
// #include <iostream>
21
/** output stream for testing.
22
testout is opened by main */
23
extern ostream * testout;
24
25
/** use instead of cout */
26
extern ostream * mycout;
27
28
/** error output stream */
29
extern ostream * myerr;
30
31
/** Error messages display.
32
Error messages are displayed by this function */
33
extern void MyError (const char * ch);
34
35
36
/** Rings the bell.
37
Produces nr beeps. */
38
extern void MyBeep (int nr = 1);
39
40
41
template <class T>
42
inline void Swap (T & a, T & b)
43
{
44
T temp = a;
45
a = b;
46
b = temp;
47
}
48
49
/*
50
template <class T>
51
inline void swap (T & a, T & b)
52
{
53
T temp = a;
54
a = b;
55
b = temp;
56
}
57
*/
58
59
60
61
/**
62
INDEX is a typedef for (at least) 4-byte integer
63
*/
64
typedef int INDEX;
65
66
/**
67
BOOL is a typedef for boolean variables
68
*/
69
// typedef int BOOL;
70
71
typedef int ELIND;
72
typedef int PIND;
73
74
75
class twoint
76
{
77
public: ///
78
int i1, i2; ///
79
twoint() {};
80
///
81
twoint(int ii1, int ii2) {i1 = ii1; i2 = ii2;}
82
friend int operator== (const twoint& t1, const twoint& t2);
83
///
84
void Swap() {int x = i1; i1 = i2; i2 = x;}
85
void Sort() {if (i1 > i2) {Swap();}}
86
};
87
88
inline int operator== (const twoint& t1, const twoint& t2)
89
{
90
return t1.i1 == t2.i1 && t1.i2 == t2.i2;
91
}
92
93
class threeint
94
{
95
public: ///
96
int i1, i2, i3; ///
97
threeint() {};
98
///
99
threeint(int ii1, int ii2, int ii3) {i1 = ii1; i2 = ii2; i3 = ii3;}
100
};
101
102
///
103
class twodouble
104
{
105
public:
106
///
107
double d1, d2;
108
///
109
twodouble() {d1 = 0; d2 = 0;};
110
///
111
twodouble(double id1, double id2) {d1 = id1; d2 = id2;}
112
///
113
void Swap() {double x = d1; d1 = d2; d2 = x;}
114
};
115
116
class fourint { public: int i1, i2, i3, i4; fourint() {}; };
117
118
119
///
120
class INDEX_2;
121
ostream & operator<<(ostream & s, const INDEX_2 & i2);
122
123
124
class INDEX_2
125
{
126
///
127
INDEX i[2];
128
129
public:
130
///
131
INDEX_2 () { }
132
///
133
INDEX_2 (INDEX ai1, INDEX ai2)
134
{ i[0] = ai1; i[1] = ai2; }
135
136
///
137
INDEX_2 (const INDEX_2 & in2)
138
{ i[0] = in2.i[0]; i[1] = in2.i[1]; }
139
140
///
141
int operator== (const INDEX_2 & in2) const
142
{ return i[0] == in2.i[0] && i[1] == in2.i[1]; }
143
144
///
145
146
147
INDEX_2 Sort ()
148
{
149
if (i[0] > i[1])
150
{
151
INDEX hi = i[0];
152
i[0] = i[1];
153
i[1] = hi;
154
}
155
return *this;
156
}
157
158
static INDEX_2 Sort (int i1, int i2)
159
{
160
if (i1 > i2)
161
return INDEX_2 (i2,i1);
162
else
163
return INDEX_2 (i1,i2);
164
}
165
166
167
///
168
INDEX & I1 () { return i[0]; }
169
///
170
INDEX & I2 () { return i[1]; }
171
///
172
INDEX & I (int j) { return i[j-1]; }
173
///
174
const INDEX & I1 () const { return i[0]; }
175
///
176
const INDEX & I2 () const { return i[1]; }
177
///
178
const INDEX & I (int j) const { return i[j-1]; }
179
///
180
int & operator[] (int j) { return i[j]; }
181
///
182
const int & operator[] (int j) const { return i[j]; }
183
///
184
friend ostream & operator<<(ostream & s, const INDEX_2 & i2);
185
};
186
187
188
///
189
class INDEX_3
190
{
191
///
192
INDEX i[3];
193
194
public:
195
///
196
INDEX_3 () { }
197
///
198
INDEX_3 (INDEX ai1, INDEX ai2, INDEX ai3)
199
{ i[0] = ai1; i[1] = ai2; i[2] = ai3; }
200
201
///
202
INDEX_3 (const INDEX_3 & in2)
203
{ i[0] = in2.i[0]; i[1] = in2.i[1]; i[2] = in2.i[2]; }
204
205
206
static INDEX_3 Sort (INDEX_3 i3)
207
{
208
return i3.Sort();
209
}
210
211
static INDEX_3 Sort (int i1, int i2, int i3)
212
{
213
if (i1 > i2) Swap (i1, i2);
214
if (i2 > i3) Swap (i2, i3);
215
if (i1 > i2) Swap (i1, i2);
216
return INDEX_3 (i1, i2, i3);
217
}
218
219
INDEX_3 Sort ()
220
{
221
if (i[0] > i[1]) Swap (i[0], i[1]);
222
if (i[1] > i[2]) Swap (i[1], i[2]);
223
if (i[0] > i[1]) Swap (i[0], i[1]);
224
return *this;
225
}
226
227
int operator== (const INDEX_3 & in2) const
228
{ return i[0] == in2.i[0] && i[1] == in2.i[1] && i[2] == in2.i[2];}
229
230
///
231
INDEX & I1 () { return i[0]; }
232
///
233
INDEX & I2 () { return i[1]; }
234
///
235
INDEX & I3 () { return i[2]; }
236
///
237
INDEX & I (int j) { return i[j-1]; }
238
///
239
const INDEX & I1 () const { return i[0]; }
240
///
241
const INDEX & I2 () const { return i[1]; }
242
///
243
const INDEX & I3 () const { return i[2]; }
244
///
245
const INDEX & I (int j) const { return i[j-1]; }
246
///
247
int & operator[] (int j) { return i[j]; }
248
///
249
const int & operator[] (int j) const { return i[j]; }
250
251
///
252
friend ostream & operator<<(ostream & s, const INDEX_3 & i3);
253
};
254
255
256
257
///
258
class INDEX_4
259
{
260
///
261
INDEX i[4];
262
263
public:
264
///
265
INDEX_4 () { }
266
///
267
INDEX_4 (INDEX ai1, INDEX ai2, INDEX ai3, INDEX ai4)
268
{ i[0] = ai1; i[1] = ai2; i[2] = ai3; i[3] = ai4; }
269
270
///
271
INDEX_4 (const INDEX_4 & in2)
272
{ i[0] = in2.i[0]; i[1] = in2.i[1]; i[2] = in2.i[2]; i[3] = in2.i[3]; }
273
274
///
275
void Sort ();
276
277
///
278
int operator== (const INDEX_4 & in2) const
279
{ return
280
i[0] == in2.i[0] && i[1] == in2.i[1] &&
281
i[2] == in2.i[2] && i[3] == in2.i[3]; }
282
283
///
284
INDEX & I1 () { return i[0]; }
285
///
286
INDEX & I2 () { return i[1]; }
287
///
288
INDEX & I3 () { return i[2]; }
289
///
290
INDEX & I4 () { return i[3]; }
291
///
292
INDEX & I (int j) { return i[j-1]; }
293
///
294
const INDEX & I1 () const { return i[0]; }
295
///
296
const INDEX & I2 () const { return i[1]; }
297
///
298
const INDEX & I3 () const { return i[2]; }
299
///
300
const INDEX & I4 () const { return i[3]; }
301
///
302
const INDEX & I (int j) const { return i[j-1]; }
303
///
304
int & operator[] (int j) { return i[j]; }
305
///
306
const int & operator[] (int j) const { return i[j]; }
307
308
///
309
friend ostream & operator<<(ostream & s, const INDEX_4 & i4);
310
};
311
312
313
314
315
316
317
318
319
/// The sort preserves quads !!!
320
class INDEX_4Q
321
{
322
///
323
INDEX i[4];
324
325
public:
326
///
327
INDEX_4Q () { }
328
///
329
INDEX_4Q (INDEX ai1, INDEX ai2, INDEX ai3, INDEX ai4)
330
{ i[0] = ai1; i[1] = ai2; i[2] = ai3; i[3] = ai4; }
331
332
///
333
INDEX_4Q (const INDEX_4Q & in2)
334
{ i[0] = in2.i[0]; i[1] = in2.i[1]; i[2] = in2.i[2]; i[3] = in2.i[3]; }
335
336
///
337
void Sort ();
338
339
///
340
int operator== (const INDEX_4Q & in2) const
341
{ return
342
i[0] == in2.i[0] && i[1] == in2.i[1] &&
343
i[2] == in2.i[2] && i[3] == in2.i[3]; }
344
345
///
346
INDEX & I1 () { return i[0]; }
347
///
348
INDEX & I2 () { return i[1]; }
349
///
350
INDEX & I3 () { return i[2]; }
351
///
352
INDEX & I4 () { return i[3]; }
353
///
354
INDEX & I (int j) { return i[j-1]; }
355
///
356
const INDEX & I1 () const { return i[0]; }
357
///
358
const INDEX & I2 () const { return i[1]; }
359
///
360
const INDEX & I3 () const { return i[2]; }
361
///
362
const INDEX & I4 () const { return i[3]; }
363
///
364
const INDEX & I (int j) const { return i[j-1]; }
365
///
366
friend ostream & operator<<(ostream & s, const INDEX_4Q & i4);
367
};
368
369
370
371
372
373
374
375
376
377
378
379
380
///
381
template <class T>
382
inline T min2 (T a, T b)
383
{
384
///
385
return (a < b) ? a : b;
386
}
387
///
388
template <class T>
389
inline T max2 (T a, T b)
390
{
391
///
392
return (a > b) ? a : b;
393
}
394
///
395
template <class T>
396
inline T min3 (T a, T b, T c)
397
{
398
///
399
return (a < b) ? (a < c) ? a : c
400
: (b < c) ? b : c;
401
}
402
///
403
template <class T>
404
inline T max3 (T a, T b, T c)
405
{
406
///
407
return (a > b) ? ((a > c) ? a : c)
408
: ((b > c) ? b : c);
409
}
410
411
///
412
413
///
414
template <class T>
415
inline int sgn (T a)
416
{
417
return (a > 0) ? 1 : ( ( a < 0) ? -1 : 0 );
418
}
419
420
///
421
template <class T>
422
inline T sqr (const T a)
423
{
424
return a * a;
425
}
426
427
///
428
template <class T>
429
inline T pow3 (const T a)
430
{
431
return a * a * a;
432
}
433
434
435
436
/*
437
template <class T>
438
void BubbleSort (int size, T * data);
439
440
template <class T>
441
void MergeSort (int size, T * data, T * help);
442
*/
443
444
445
446
447
448
#endif
449
450