File: reorder.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 (233 lines) | stat: -rw-r--r-- 5,454 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
#include	<string>
#include	<math.h>
#include	<stdio.h>
#include        <stdlib.h>

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

/* Defined here */
bool            reorder_columnsCmd(void);
static bool     reorder_cols_randomly();
static bool     reorder_cols_order(bool ascending, int which, int n);

// Following is unsafe, but I know indices are OK
inline  void swap(GriColumn *A, int a, int b)
{
    if (A->size() > 0) {
        double *ap = A->begin();
	double tmp = *(ap + (a));
	*(ap + (a)) = *(ap + (b));
	*(ap + (b)) = tmp;
    }
}

/*
 * `reorder columns randomly|{ascending in x|y|z}|{descending in x|y|z}'
 */
bool
reorder_columnsCmd()
{
    int             n;
    bool            ascending;
    switch (_nword) {
    case 3:
	if (word_is(2, "randomly")) {
	    return reorder_cols_randomly();
	} else {
	    err("Third word must be `randomly', if only 3 words");
	    demonstrate_command_usage();
	    return false;
	}
    case 5:
	if (word_is(2, "ascending")) {
	    ascending = true;
	} else if (word_is(2, "descending")) {
	    ascending = false;
	} else {
	    err("Third word must be `ascending' or `descending'");
	    demonstrate_command_usage();
	    return false;
	}
	if (!word_is(3, "in")) {
	    err("Fourth word must be `in'");
	    demonstrate_command_usage();
	    return false;
	}
	n = _colX.size();		/* presume no x means no cols */
	if (word_is(4, "x")) {
	    reorder_cols_order(ascending, 0, n);
	} else if (word_is(4, "y")) {
	    reorder_cols_order(ascending, 1, n);
	} else if (word_is(4, "z")) {
	    reorder_cols_order(ascending, 2, n);
	} else {
	    err("Fifth word must be `x', `y', or `z'");
	    demonstrate_command_usage();
	    return false;
	}
	if (n < 1) {
	    warning("No x column exists, so ignoring `reorder' command");
	    return true;
	}
	break;			/* not reached */
    default:
	NUMBER_WORDS_ERROR;
	return false;
    }
    return false;			/* not reached */
}

static          bool
reorder_cols_randomly()
{
    int             i, inew, n;
    n = _colX.size();		/* presume no x means no cols */
    if (n < 1) {
	warning("No x column exists, so ignoring `reorder' command");
	return true;
    }
    // Seed, then do a few random numbers
#if defined(HAVE_DRAND48)
    srand48(getpid());
#else
    srand(getpid());
#endif
    for (i = 0; i < n; i++) {
#if defined(HAVE_DRAND48)	// range is 0 to 1, but do modulus in case
	inew = int(drand48() * n) % n;
#else
	inew = int(rand() % n);
#endif
	swap(&_colX, i, inew);
	swap(&_colY, i, inew);
	swap(&_colZ, i, inew);
	swap(&_colU, i, inew);
	swap(&_colV, i, inew);
	swap(&_colR, i, inew);
	swap(&_colTHETA, i, inew);
    }
    return true;
}

/* `reorder columns ascending|descending in x|y|z' */
/* Use bubble-sort (slow, but easy for me to code) */
/* Also, this code could be made a lot more terse */
static          bool
reorder_cols_order(bool ascending, int which, int n)
{
    int             i, inew;
    switch (which) {
    case 0:			/* by x */
	if (_colX.size() < 1) {
	    warning("No x column exists, so cannot `reorder' by it");
	    return true;
	}
	if (ascending) {
	    for (i = 0; i < n; i++) {
		for (inew = i; inew < n; inew++) {
		    if (_colX[i] > _colX[inew]) {
			swap(&_colX, i, inew);
			swap(&_colY, i, inew);
			swap(&_colZ, i, inew);
			swap(&_colU, i, inew);
			swap(&_colV, i, inew);
			swap(&_colR, i, inew);
			swap(&_colTHETA, i, inew);
		    }
		}
	    }
	} else {
	    /* descending */
	    for (i = n - 1; i >= 0; i--) {
		for (inew = i; inew >= 0; inew--) {
		    if (_colX[i] > _colX[inew]) {
			swap(&_colX, i, inew);
			swap(&_colY, i, inew);
			swap(&_colZ, i, inew);
			swap(&_colU, i, inew);
			swap(&_colV, i, inew);
			swap(&_colR, i, inew);
			swap(&_colTHETA, i, inew);
		    }
		}
	    }
	}
	break;
    case 1:			/* by y */
	if (_colY.size() < 1) {
	    warning("No y column exists, so cannot `reorder' by it");
	    return true;
	}
	if (ascending) {
	    for (i = 0; i < n; i++) {
		for (inew = i; inew < n; inew++) {
		    if (_colY[i] > _colY[inew]) {
			swap(&_colX, i, inew);
			swap(&_colY, i, inew);
			swap(&_colZ, i, inew);
			swap(&_colU, i, inew);
			swap(&_colV, i, inew);
			swap(&_colR, i, inew);
			swap(&_colTHETA, i, inew);
		    }
		}
	    }
	} else {
	    /* descending */
	    for (i = n - 1; i >= 0; i--) {
		for (inew = i; inew >= 0; inew--) {
		    if (_colY[i] > _colY[inew]) {
			swap(&_colX, i, inew);
			swap(&_colY, i, inew);
			swap(&_colZ, i, inew);
			swap(&_colU, i, inew);
			swap(&_colV, i, inew);
			swap(&_colR, i, inew);
			swap(&_colTHETA, i, inew);
		    }
		}
	    }
	}
	break;
    case 2:			/* by z */
	if (_colZ.size() < 1) {
	    warning("No z column exists, so cannot `reorder' by it");
	    return true;
	}
	if (ascending) {
	    for (i = 0; i < n; i++) {
		for (inew = i; inew < n; inew++) {
		    if (_colZ[i] > _colZ[inew]) {
			swap(&_colX, i, inew);
			swap(&_colY, i, inew);
			swap(&_colZ, i, inew);
			swap(&_colU, i, inew);
			swap(&_colV, i, inew);
			swap(&_colR, i, inew);
			swap(&_colTHETA, i, inew);
		    }
		}
	    }
	} else {
	    /* descending */
	    for (i = n - 1; i >= 0; i--) {
		for (inew = i; inew >= 0; inew--) {
		    if (_colZ[i] > _colZ[inew]) {
			swap(&_colX, i, inew);
			swap(&_colY, i, inew);
			swap(&_colZ, i, inew);
			swap(&_colU, i, inew);
			swap(&_colV, i, inew);
			swap(&_colR, i, inew);
			swap(&_colTHETA, i, inew);
		    }
		}
	    }
	}
	break;
    default:
	return false;		/* not reached */
    }
    return true;
}