File: delete.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 (350 lines) | stat: -rw-r--r-- 8,843 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
// #define DEBUG_STORAGE
#include <string>
#include <stdlib.h>
#include <stddef.h>

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

bool            delete_columnsCmd(void);
static void     delete_all_columns(void);
static bool     delete_columns_randomly(void);
bool            delete_columns_where_missing(void);
bool            delete_gridCmd(void);
extern char     _grTempString[];

// `delete .variable.'
// `delete \synonym'
// `delete columns [randomly .fraction.]'
// `delete grid'
// `delete x|y scale'
bool
deleteCmd()
{
    if (_nword == 1) {
	/* Missing item */
	err("`delete' what?");
	return false;
    } else if (is_var(_word[1]) || is_syn(_word[1])) {
	/* Deleting variable/synonym (s) */
	int             i;
	for (i = 1; i < _nword; i++) {
	    if (is_var(_word[i])) {
		if (!delete_var(_word[i])) {
		    warning("`delete' can't delete non-existent variable `\\",
			    _word[i], "'", "\\");
		    return false;
		}
	    } else if (is_syn(_word[i])) {
		if (!delete_syn(_word[i])) {
		    warning("`delete' can't delete non-existent synonym `\\",
			    _word[i], "'", "\\");
		    return false;
		}
	    }
	}
	return true;
    } else if (word_is(1, "columns")) {
	return delete_columnsCmd();
    } else if (word_is(1, "grid")) {
	/* Delete the grid */
	if (_nword == 2) {
	    delete_gridCmd();
	    return true;
	} else {
	    demonstrate_command_usage();
	    err("Extra words in command.");
	    return false;
	}
    } else if (word_is(1, "scale")) {
	/* Delete both the x and y scales */
	_yincreasing = true;
	_xscale_exists = false;
	_need_x_axis = true;
	_yscale_exists = false;
	_need_y_axis = true;
	_user_set_x_axis = false;
	_user_set_y_axis = false;
	gr_setxtransform(gr_axis_LINEAR);
	gr_setxlabel("x");
	gr_setytransform(gr_axis_LINEAR);
	gr_setylabel("y");
	return true;
    } else if (word_is(1, "x")) {
	/* Delete the x scale */
	if (word_is(2, "scale") && _nword == 3) {
	    _xscale_exists = false;
	    _need_x_axis = true;
	    _user_set_x_axis = false;
	    gr_setxtransform(gr_axis_LINEAR);
	    gr_setxlabel("x");
	    return true;
	} else {
	    demonstrate_command_usage();
	    err("`delete x' what?");
	    return false;
	}
    } else if (word_is(1, "y")) {
	/* Delete the y scale */
	if (word_is(2, "scale") && _nword == 3) {
	    _yscale_exists = false;
	    _need_y_axis = true;
	    _yincreasing = true;
	    _user_set_y_axis = false;
	    gr_setytransform(gr_axis_LINEAR);
	    gr_setylabel("y");
	    return true;
	} else {
	    demonstrate_command_usage();
	    err("`delete y' what?");
	    return false;
	}
    } else {
	demonstrate_command_usage();
	err("`delete' what?");
	return false;
    }
}

// `delete columns [{randomly .fraction.}|{where missing}]'
bool
delete_columnsCmd()
{
    switch(_nword) {
    case 2:
	// `delete columns'
	delete_all_columns();
	return true;
    case 4:
	// `delete columns randomly .fraction.'
	if (word_is(2, "randomly")) {
	    return delete_columns_randomly();
	} else if (word_is(2, "where") || word_is(3, "missing")) {
	    return delete_columns_where_missing();
	} else {
	    demonstrate_command_usage();	    
	    err("Syntax must be `randomly .f.' or `where missing'");
	    return false;
	}
    default:
	demonstrate_command_usage();
	NUMBER_WORDS_ERROR;
	return false;
    }
}

static void
delete_all_columns()
{
    _colX.setDepth(0);
    _colX.compact();
    _colY.setDepth(0);
    _colY.compact();
    _colZ.setDepth(0);
    _colZ.compact();
    _colU.setDepth(0);
    _colU.compact();
    _colV.setDepth(0);
    _colV.compact();
    _colR.setDepth(0);
    _colR.compact();
    _colTHETA.setDepth(0);
    _colTHETA.compact();
}

static bool
delete_columns_randomly()
{
    double fraction;
    if (!getdnum(_word[3], &fraction)) {
	READ_WORD_ERROR(".fraction.");
	return false;
    }
    if (fraction < 0.0) {
	warning("`delete columns randomly' clipping .fraction. to 0 (did no deletions)");
	return true;		// do nothing
    }
    if (fraction > 1.0) {
	warning("`delete columns randomly' clipping .fraction. to 1");
	delete_all_columns();
	return true;
    }
    unsigned int length = _colX.size();
    double miss = gr_currentmissingvalue();
    int good = 0;
    vector<int> ok((size_t)length, 0);
    unsigned int i;
    for (i = 0; i < length; i++) {
	ok[i] = (char)0;
	if (_colX.size() > 0 && (_colX[i] == miss))
	    continue;
	if (_colY.size() > 0 && (_colY[i] == miss))
	    continue;
	if (_colZ.size() > 0 && (_colZ[i] == miss))
	    continue;
	if (_colU.size() > 0 && (_colU[i] == miss))
	    continue;
	if (_colV.size() > 0 && (_colV[i] == miss))
	    continue;
	if (_colR.size() > 0 && (_colR[i] == miss))
	    continue;
	if (_colTHETA.size() > 0 && (_colTHETA[i] == miss))
	    continue;
	ok[i] = (char)1;
	good++;
    }
    // Create vector of whether to kill a given index
    vector<int> kill((size_t)length, 0);
    // Laborously get correct number of data to discard.  Originally
    // I just tried to remove the given number, but that adds
    // an extra element of randomness.
    unsigned int subset = (unsigned int)(floor(good * fraction + 0.5));
    unsigned int collisions = 0;
#if defined(HAVE_DRAND48)
    srand48(getpid());
#else
    srand(getpid());
#endif
    for (i = 0; i < subset; i++) {
#if defined(HAVE_DRAND48)	// range is 0 to 1, but do modulus in case
	int index = int(drand48() * length) % length;
#else
	int index = int(rand() % length);
#endif
	if (ok[index]) {
	    if (kill[index]) {
		if (collisions++ > length) {
		    sprintf(_grTempString, "`delete columns randomly' could only delete %d columns\n", i);
		    warning(_grTempString);
		    break;
		}
		i--;
	    } else {
		kill[index] = 1;
	    }
	} else {
	    i--;
	}
    }
    for (i = 0; i < length; i++) {
	if (kill[i]) {
	    if (_colX.size())
		_colX[i] = miss;
	    if (_colY.size())
		_colY[i] = miss;
	    if (_colZ.size())
		_colZ[i] = miss;
	    if (_colU.size())
		_colU[i] = miss;
	    if (_colV.size())
		_colV[i] = miss;
	    if (_colR.size())
		_colR[i] = miss;
	    if (_colTHETA.size())
		_colTHETA[i] = miss;
	}
    }	
    return true;
}

bool
delete_columns_where_missing()
{
    int haveX, haveY, haveZ, haveU, haveV, haveR, haveTHETA;
    haveX = haveY = haveZ = haveU = haveV = haveR = haveTHETA = 0;
    unsigned int length = _colX.size();
    unsigned int i;
    for (i = 0; i < length; i++) {
	if (_colX.size()) haveX = 1;
	if (_colY.size()) haveY = 1;
	if (_colZ.size()) haveZ = 1;
	if (_colU.size()) haveU = 1;
	if (_colV.size()) haveV = 1;
	if (_colR.size()) haveR = 1;
	if (_colTHETA.size()) haveTHETA = 1;
    }
    double *xP = _colX.begin();
    double *yP = _colY.begin();
    double *zP = _colZ.begin();
    double *uP = _colU.begin();
    double *vP = _colV.begin();
    double *rP = _colR.begin();
    double *thetaP = _colTHETA.begin();
    vector<int> kill((size_t)length, 0);
    int num_to_kill = 0;
    for (i = 0; i < length; i++) {
	if (haveX && gr_missing(xP[i])) {
	    kill[i] = 1; num_to_kill++; continue;
	}
	if (haveY && gr_missing(yP[i])) {
	    kill[i] = 1; num_to_kill++; continue;
	}
	if (haveZ && gr_missing(zP[i])) {
	    kill[i] = 1; num_to_kill++; continue;
	}
	if (haveU && gr_missing(uP[i])) {
	    kill[i] = 1; num_to_kill++; continue;
	}
	if (haveV && gr_missing(vP[i])) {
	    kill[i] = 1; num_to_kill++; continue;
	}
	if (haveR && gr_missing(rP[i])) {
	    kill[i] = 1; num_to_kill++; continue;
	}
	if (haveTHETA && gr_missing(thetaP[i])) {
	    kill[i] = 1; num_to_kill++; continue;
	}
    }
    if (!num_to_kill) {
	return true;
    }
    for (i = length - 1; i != 0; i--) {
	if (kill[i]) {
	    if (haveX) _colX.erase(_colX.begin() + i);
	    if (haveY) _colY.erase(_colY.begin() + i);
	    if (haveZ) _colZ.erase(_colZ.begin() + i);
	    if (haveU) _colU.erase(_colU.begin() + i);
	    if (haveV) _colV.erase(_colV.begin() + i);
	    if (haveR) _colR.erase(_colR.begin() + i);
	    if (haveTHETA) _colTHETA.erase(_colTHETA.begin() + i);
	}
    }
    PUT_VAR("..num_col_data..", double(_colX.size()));
    PUT_VAR("..num_col_data_missing..", 0);
    length -= num_to_kill;
    return true;
}

bool
delete_gridCmd()
{
    Require(_nword == 2, err("Must have `delete grid'"));
    if (!strcmp(_word[1], "grid")) {
	_f_xy.set_size(0, 0);
	_legit_xy.set_size(0, 0);
	if (_grid_exists == true) {
	    _f_min = _f_max = gr_currentmissingvalue();
	    _grid_exists = false;
	}
	if (_xgrid_exists == true) {
#if defined(DEBUG_STORAGE)
	    printf("delete clearing _xmatrix=%x\n", _xmatrix);
#endif
	    delete [] _xmatrix;
	    _num_xmatrix_data = 0;
	    _xgrid_exists = false;
	}
	if (_ygrid_exists == true) {
#if defined(DEBUG_STORAGE)
	    printf("delete clearing _ymatrix=%x\n", _ymatrix);
#endif
	    delete [] _ymatrix;
	    _num_ymatrix_data = 0;
	    _ygrid_exists = false;
	}
    } else {
	err("Must have `delete grid'");
	return false;
    }
    return true;
}