File: gr_coll.cc

package info (click to toggle)
gri 2.4.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,540 kB
  • ctags: 1,966
  • sloc: cpp: 32,542; lisp: 3,243; perl: 806; makefile: 548; sh: 253
file content (595 lines) | stat: -rw-r--r-- 14,194 bytes parent folder | download
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Classes for Gri.  See gr_coll.hh for docs
//#define DEBUG_GR_COLL 1		// uncomment to debug

#include <string>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stddef.h>

#include "gr.hh"
#include "extern.hh"
#include "gr_coll.hh"

static const unsigned int CAPACITY_DEFAULT = 32;


GriString::GriString(const char *s)
{
    capacity = 1 + strlen(s);
    value = new char[capacity];
    if (!value) OUT_OF_MEMORY;
    strcpy(value, s);
}
GriString::GriString(const GriString& n)
{
    char *cp = n.getValue();
    capacity = 1 + strlen(cp);
    value = new char[capacity];
    if (!value) OUT_OF_MEMORY;
    strcpy(value, cp);
}
GriString::GriString(unsigned int len)
{
    capacity = 1 + len;
    value = new char[capacity];
    if (!value) OUT_OF_MEMORY;
    value[0] = '\0';
}

void GriString::convert_slash_to_MSDOS()
{
    unsigned int l = strlen(value);
    for (unsigned int i = 0; i < l; i++)
        if (value[i] == '/')
            value[i] = '\\';
}
// Read line from file, enlarging space if needed.  Leave newline
// at end; if file ends before newline, tack one on anyhow.
//
// RETURN true if file at EOF before reading any data
bool GriString::line_from_FILE(FILE *fp)
{
    unsigned int i = 0;
    if (feof(fp)) {
	value[0] = '\0';
	return true;
    }
    do {
	value[i] = getc(fp);
	if (i >= capacity - 1) { // Get more space if required
	    capacity += capacity;
	    char *more_space = new char[capacity];
	    if (!more_space) OUT_OF_MEMORY;
	    for (unsigned int j = 0; j <= i; j++)
		more_space[j] = value[j];
	    delete [] value;
	    value = more_space;
	}
	if (value[i++] == '\n')
	    break;
    } while (!feof(fp));
    if (feof(fp))
	value[i - 1] = '\n';	// tack newline on
    value[i] = '\0';
    return false;
}

// Read word from file, enlarging if neccessary.  Leave newline if
// found, but if hit eof do not insert extra newline.

// RETURN true if hit EOF

// CHANGE 28AUG95: DO
// NOT KEEP NEWLINE; PUT IT BACK INTO FILE SO 'READ WORD' CAN FLUSH COMMENTS
// AND EXTRA JUNK AT EOL ... PROVISIONAL CHANGE.

bool GriString::word_from_FILE(FILE *fp)
{
        if (feof(fp)) {
                value[0] = '\0';
                return true;
        }

	// The default (single or multiple whitespace) is VERY 
	// different from TAB, since the latter requires precisely
	// one TAB.  Also, in the default case, whitespace is skipped, 
	// but not in the TAB case.
        extern char _input_data_separator;
        if (_input_data_separator == ' ') {
		unsigned int i = 0;
		value[i] = getc(fp);
		if (feof(fp)) {
			value[0] = '\0';
			return true;
		}
		// Flush any existing whitespace
		while (isspace(value[i])) {
			value[i] = getc(fp);
			if (feof(fp)) {
 				value[i] = '\0';
				return true;
			}
		}
		i++;
		do {
			value[i] = getc(fp);
			if (i >= capacity - 1) { // Get more space if required
				capacity += capacity;
				char *more_space = new char[capacity];
				if (!more_space) OUT_OF_MEMORY;
				for (unsigned int j = 0; j <= i; j++)
					more_space[j] = value[j];
				delete [] value;
				value = more_space;
			}
			if (value[i] == '\n') {
				ungetc(value[i], fp);
				break;
			}
			if (isspace(value[i]))
				break;
			i++;
		} while (!feof(fp));
		if (feof(fp))
			i--;
		value[i] = '\0';
		return false;
        } else if (_input_data_separator == '\t') {
		// For 'set input data separator TAB' case, require
		// just one tab.  Two tabs implies missing data.
		unsigned int i = 0;
		value[i] = getc(fp);
		if (feof(fp)) {
			value[0] = '\0';
			return true;
		}
		if (value[i] == '\t') {
			value[0] = '\0';
			return false;
		}
		i++;
		do {
			value[i] = getc(fp);
			if (i >= capacity - 1) { // Get more space if required
				capacity += capacity;
				char *more_space = new char[capacity];
				if (!more_space) OUT_OF_MEMORY;
				for (unsigned int j = 0; j <= i; j++)
					more_space[j] = value[j];
				delete [] value;
				value = more_space;
			}
			if (value[i] == '\n') {
				ungetc(value[i], fp);
				break;
			}
			if (value[i] == '\t') {
				break;
			}
			i++;
		} while (!feof(fp));
		if (feof(fp))
			i--;
		value[i] = '\0';
		return false;
        } else {
		gr_Error("Internal error _input_data_separator value is not understood.");
		return false;
	}
}


void GriString::fromSTR(const char *s)
{
    unsigned int len = strlen(s);
    if (capacity < len + 1) {
	delete [] value;
	capacity = len + 1;
	value = new char[capacity];
	if (!value) OUT_OF_MEMORY;
    }
    strcpy(value, s);
}
void GriString::catSTR(const char *s)
{
    unsigned int len    = strlen(s);
    unsigned int oldlen = strlen(value);
    if ((1 + len + oldlen) > capacity) {
	capacity += len + 1;
	char *tmp = new char[capacity];
	if (!tmp) OUT_OF_MEMORY;
	strcpy(tmp, value);
	strcat(tmp, s);
	delete [] value;
	value = tmp;
    } else {
	strcat(value, s);
    }
}
#if 0
void GriString::sed(const char *cmd)
// Assume, *WITHOUT CHECKING*, that cmd is of one of the forms
// 	s/A/B/	where 'A' and 'B' are strings
// 	s:A:B:	or any other 'stop-char', as in sed unix command
// Also, require B to be a shorter string than A. (easily fixed)
{
    switch (cmd[0]) {
    case 's': {
	int len_cmd = strlen(cmd);
	int len_value = strlen(value);
	char *copy = new char[1 + strlen(value)];
	char stop = cmd[1];
	char *A = new char [1 + len_cmd];
	char *B = new char [1 + len_cmd];
	int lenA = 0;
	int iA;
	for (iA = 2; iA < len_cmd; iA++) {
	    if (cmd[iA] == stop)
		break;
	    A[lenA++] = cmd[iA];
	}
	A[lenA] = '\0';
	int lenB = 0;
	int iB;
	for (iB = iA + 1; iB < len_cmd; iB++) {
	    if (cmd[iB] == stop)
		break;
	    B[lenB++] = cmd[iB];
	}
	B[lenB] = '\0';
	Require2(lenB <= lenA, gr_Error("sed requires lenB <= lenA\n"));
	int iCOPY = 0;
	for (int iVALUE = 0; iVALUE < len_value; iVALUE++) {
	    for (iA = 0; iA < lenA; iA++) {
		if (iVALUE + iA > len_value - 1) {
		    break;
		}
		if (value[iVALUE + iA] != A[iA]) {
		    break;
		}
	    }
	    if (iA == lenA) {
		for (iB = 0; iB < lenB; iB++) {
		    copy[iCOPY++] = B[iB++];
		}
		iVALUE += lenA - 1;
	    } else {
		copy[iCOPY++] = value[iVALUE];
	    }
	}
	copy[iCOPY] = '\0';
	delete [] A;
	delete [] B;
	strcpy(value, copy);
	delete [] copy;
	break;
      }
    default:
	gr_Error("GriString's sed() command can only do 's' commands");
    }
}
#endif

// Return reference to indicated item, getting new space if exceeds
// capacity.
char&
GriString::operator[](unsigned int offset)
{
    if (offset < capacity)
	return value[offset];
    // Must get more space
    char *cp = new char[capacity = offset + 1];
    if (!cp) OUT_OF_MEMORY;
    strcpy(cp, value);
    delete [] value;
    value = cp;
    // Set to null characters, to prevent returning junk
    for (unsigned int i = strlen(value); i < capacity; i++)
	value[i] = '\0';
    return value[offset];
}

void
GriString::draw(double xcm, double ycm, gr_textStyle s, double angle) const
{
    if (strlen(value) == 0)
	return;
    gr_show_at(value, xcm, ycm, s, angle);
    // Figure bounding box
    double width, ascent, descent;
    gr_stringwidth(value, &width, &ascent, &descent);
#if 0
    printf("DEBUG: GriString::Draw with value `%s' xcm=%.1f ycm=%.1f width=%.1f ascent=%.1f descent=%.1f angle=%.1f\n",
	   value, xcm,ycm,width,ascent, descent, angle);
#endif
    double tmpx[4], tmpy[4];	// 0123 from lower-left anticlockwise
    switch (s) {
    case TEXT_LJUST:
	gr_rotate_xy(     0.0, -descent, angle, tmpx + 0, tmpy + 0);
	gr_rotate_xy(   width, -descent, angle, tmpx + 1, tmpy + 1);
	gr_rotate_xy(   width,   ascent, angle, tmpx + 2, tmpy + 2);
	gr_rotate_xy(     0.0,   ascent, angle, tmpx + 3, tmpy + 3);
	break;
    case TEXT_RJUST:
	gr_rotate_xy(  -width, -descent, angle, tmpx + 0, tmpy + 0);
	gr_rotate_xy(     0.0, -descent, angle, tmpx + 1, tmpy + 1);
	gr_rotate_xy(     0.0,   ascent, angle, tmpx + 2, tmpy + 2);
	gr_rotate_xy(  -width,   ascent, angle, tmpx + 3, tmpy + 3);
	break;
    case TEXT_CENTERED:
	gr_rotate_xy(-width/2, -descent, angle, tmpx + 0, tmpy + 0);
	gr_rotate_xy( width/2, -descent, angle, tmpx + 1, tmpy + 1);
	gr_rotate_xy( width/2,   ascent, angle, tmpx + 2, tmpy + 2);
	gr_rotate_xy(-width/2,   ascent, angle, tmpx + 3, tmpy + 3);
	break;
    }
    tmpx[0] += xcm, tmpy[0] += ycm;
    tmpx[1] += xcm, tmpy[1] += ycm;
    tmpx[2] += xcm, tmpy[2] += ycm;
    tmpx[3] += xcm, tmpy[3] += ycm;
    rectangle box(vector_min(tmpx, 4), vector_min(tmpy, 4),
		  vector_max(tmpx, 4), vector_max(tmpy, 4));
    bounding_box_update(box);
}

GriDvector::GriDvector() 
{
    the_depth = 0;
    the_capacity = CAPACITY_DEFAULT;
    contents = new double [the_capacity];
    if (!contents) OUT_OF_MEMORY;
}
GriDvector::GriDvector(unsigned int length)
{
    the_depth = 0;
    the_capacity = length;
    contents = new double [the_capacity];
    if (!contents) OUT_OF_MEMORY;
}
GriDvector::~GriDvector()
{
    delete [] contents;
}
// Get more storage 
void GriDvector::expand()
{
    if (!the_capacity)
	the_capacity = CAPACITY_DEFAULT;
    the_capacity *= 2;
    double *tmp;
    tmp = new double[the_capacity];
    if (!tmp) OUT_OF_MEMORY;
    for (unsigned i = 0; i < the_depth; i++)
	tmp[i] = contents[i];
    delete [] contents;
    contents = tmp;
}
// Get more storage, to specified level
void GriDvector::expand(unsigned int capacity)
{
    if (capacity == the_capacity)
	return;
    if (capacity < the_capacity)
	return;			// Ignore 
    the_capacity = capacity == 0 ? CAPACITY_DEFAULT : capacity;
    double *tmp;
    tmp = new double[the_capacity];
    if (!tmp) OUT_OF_MEMORY;
    for (unsigned int i = 0; i < the_depth; i++)
	tmp[i] = contents[i];
    delete [] contents;
    contents = tmp;
}
// Compact down to 'the_depth' or CAPACITY_DEFAULT, whichever is smaller
void GriDvector::compact()
{
    unsigned int old_capacity = the_capacity;
    the_capacity = the_depth > CAPACITY_DEFAULT 
	? the_depth : CAPACITY_DEFAULT;
    if (the_capacity != old_capacity) {
	double *tmp;
	tmp = new double[the_capacity];
	if (!tmp) OUT_OF_MEMORY;
	for (unsigned int i = 0; i < the_depth; i++)
	    tmp[i] = contents[i];
	delete [] contents;
	contents = tmp;
    }
}
void GriDvector::push_back(double value)
{
    while (the_depth > the_capacity - 1)
	expand();
    contents[the_depth++] = value;
}
void GriDvector::pop_back()	// retains storage
{
    if (the_depth)
	the_depth--;
}
double GriDvector::topElement()
{
    return contents[the_depth - 1];
}
void GriDvector::erase(/* iterator */ double *pos_ /*unsigned int offset*/)
{
    unsigned offset = pos_ - contents;
    for (unsigned i = offset; i < the_depth - 1; i++)	
	contents[i] = contents[i + 1];
    the_depth--;
}
double& GriDvector::operator[](unsigned int offset)
{
    if (offset <= the_depth - 1)
	return contents[offset];
    else {
	gr_Error("Trying to access GriDvector out of bounds.");
	return contents[0];	// never done, prevent compiler warning
    }
}
double GriDvector::min()
{
    int first = 1;
    double the_min = 0.0;	// prevent compiler warning
    for (unsigned int i = 0; i < the_depth; i++)
	if (!gr_missing(contents[i])) {
	    if (first)
		the_min = contents[i];
	    else
		if (contents[i] < the_min)
		    the_min = contents[i];
	    first = 0;
	}
    if (!first)
	return the_min;
    else
	return gr_currentmissingvalue();
}
double GriDvector::max()
{
    int first = 1;
    double the_max = 0.0;	// prevent compiler warning
    for (unsigned int i = 0; i < the_depth; i++)
	if (!gr_missing(contents[i])) {
	    if (first)
		the_max = contents[i];
	    else
		if (contents[i] > the_max)
		    the_max = contents[i];
	    first = 0;
	}
    if (!first)
	return the_max;
    else
	return gr_currentmissingvalue();
}
double GriDvector::median()
{
    double q1, q2, q3;
    histogram_stats(contents, the_depth, &q1, &q2, &q3) ;
    return q2;
}
double GriDvector::mean()
{
    double sum = 0.0;
    long good = 0;
    for (unsigned int i = 0; i < the_depth; i++)
	if (!gr_missing(contents[i])) {
	    sum += contents[i];
	    good++;
	}
    if (good)
	return double(sum / good);
    else
	return gr_currentmissingvalue();
}
double GriDvector::stddev()
{
    double the_mean = mean();
    double sum = 0.0;
    long good = 0;
    for (unsigned int i = 0; i < the_depth; i++)
	if (!gr_missing(contents[i])) {
	    sum += (contents[i] - the_mean) * (contents[i] - the_mean);
	    good++;
	}
    if (good > 1)
	return double(sqrt(sum / (good - 1)));
    else
	return gr_currentmissingvalue();
}
double * GriDvector::begin()
{
    return contents;
}
void GriDvector::setDepth(unsigned int depth)
{
    if (depth < the_depth) {
	the_depth = depth;
	compact();
    } else {
	expand(depth);
	the_depth = depth;
    }
}
size_t GriDvector::size()
{
    return the_depth;
}
unsigned int GriDvector::size_legit()
{
    unsigned int num = 0;
    for (unsigned int i = 0; i < the_depth; i++)
	if (!gr_missing(contents[i]))
	    num++;
    return num;
}
unsigned int GriDvector::capacity()
{
    return the_capacity;
}

GriColumn::GriColumn()
{
    name = new char[2];
    if (!name) OUT_OF_MEMORY;
    name[0] = '\0';
}
GriColumn::~GriColumn()
{
    delete [] name;
}
void GriColumn::setName(const char *theName)
{
    delete [] name;
    name = new char[1 + strlen(theName)];
    if (!name) OUT_OF_MEMORY;
    strcpy(name, theName);
}
char *GriColumn::getName()
{
    return name;
}

RpnItem::RpnItem()
{
    name = new char[1];
    if (!name) OUT_OF_MEMORY;
    name[0] = '\0';
    value = 0.0;
    type = NUMBER;
}
RpnItem::RpnItem(const RpnItem& n)
{
    name = new char[1 + strlen(n.getName())];
    if (!name) OUT_OF_MEMORY;
    strcpy(name, n.getName());
    value = n.getValue();
    type = n.getType();
}
void
RpnItem::set(const char *the_name, double the_value, operand_type the_type)
{
    if (strlen(the_name) > strlen(name)) {
	delete [] name;
	name = new char[1 + strlen(the_name)];
	if (!name) OUT_OF_MEMORY;
    }
    strcpy(name, the_name);
    value = the_value;
    type = the_type;
}
RpnItem& RpnItem::operator=(const RpnItem& n)
{
    char *cp = n.getName();
    if (strlen(cp) > strlen(name)) {
	delete [] name;
	name = new char[1 + strlen(cp)];
	if (!name) OUT_OF_MEMORY;
    }
    strcpy(name, cp);
    value = n.getValue();
    type = n.getType();
    return *this;
}