~ubuntu-branches/ubuntu/karmic/grace/karmic

« back to all changes in this revision

Viewing changes to src/setutils.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2002-03-19 14:19:58 UTC
  • Revision ID: james.westby@ubuntu.com-20020319141958-5gxna6vo1ek3zjml
Tags: upstream-5.1.7
ImportĀ upstreamĀ versionĀ 5.1.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Grace - GRaphing, Advanced Computation and Exploration of data
 
3
 * 
 
4
 * Home page: http://plasma-gate.weizmann.ac.il/Grace/
 
5
 * 
 
6
 * Copyright (c) 1991-1995 Paul J Turner, Portland, OR
 
7
 * Copyright (c) 1996-2000 Grace Development Team
 
8
 * 
 
9
 * Maintained by Evgeny Stambulchik <fnevgeny@plasma-gate.weizmann.ac.il>
 
10
 * 
 
11
 * 
 
12
 *                           All Rights Reserved
 
13
 * 
 
14
 *    This program is free software; you can redistribute it and/or modify
 
15
 *    it under the terms of the GNU General Public License as published by
 
16
 *    the Free Software Foundation; either version 2 of the License, or
 
17
 *    (at your option) any later version.
 
18
 * 
 
19
 *    This program is distributed in the hope that it will be useful,
 
20
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
 *    GNU General Public License for more details.
 
23
 * 
 
24
 *    You should have received a copy of the GNU General Public License
 
25
 *    along with this program; if not, write to the Free Software
 
26
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
27
 */
 
28
 
 
29
/*
 
30
 *
 
31
 * routines to allocate, manipulate, and return
 
32
 * information about sets.
 
33
 *
 
34
 */
 
35
 
 
36
#include <config.h>
 
37
#include <cmath.h>
 
38
 
 
39
#include <stdio.h>
 
40
#include <stdlib.h>
 
41
#include <string.h>
 
42
 
 
43
#include "globals.h"
 
44
#include "utils.h"
 
45
#include "files.h"
 
46
#include "graphs.h"
 
47
#include "protos.h"
 
48
 
 
49
extern graph *g;
 
50
 
 
51
/*
 
52
 * return the string version of the set type
 
53
 */
 
54
char *set_types(int it)
 
55
{
 
56
    char *s = "xy";
 
57
 
 
58
    switch (it) {
 
59
    case SET_XY:
 
60
        s = "xy";
 
61
        break;
 
62
    case SET_BAR:
 
63
        s = "bar";
 
64
        break;
 
65
    case SET_BARDY:
 
66
        s = "bardy";
 
67
        break;
 
68
    case SET_BARDYDY:
 
69
        s = "bardydy";
 
70
        break;
 
71
    case SET_XYZ:
 
72
        s = "xyz";
 
73
        break;
 
74
    case SET_XYDX:
 
75
        s = "xydx";
 
76
        break;
 
77
    case SET_XYDY:
 
78
        s = "xydy";
 
79
        break;
 
80
    case SET_XYDXDX:
 
81
        s = "xydxdx";
 
82
        break;
 
83
    case SET_XYDYDY:
 
84
        s = "xydydy";
 
85
        break;
 
86
    case SET_XYDXDY:
 
87
        s = "xydxdy";
 
88
        break;
 
89
    case SET_XYDXDXDYDY:
 
90
        s = "xydxdxdydy";
 
91
        break;
 
92
    case SET_XYHILO:
 
93
        s = "xyhilo";
 
94
        break;
 
95
    case SET_XYR:
 
96
        s = "xyr";
 
97
        break;
 
98
    case SET_XYCOLOR:
 
99
        s = "xycolor";
 
100
        break;
 
101
    case SET_XYCOLPAT:
 
102
        s = "xycolpat";
 
103
        break;
 
104
    case SET_XYVMAP:
 
105
        s = "xyvmap";
 
106
        break;
 
107
    case SET_BOXPLOT:
 
108
        s = "xyboxplot";
 
109
        break;
 
110
    case SET_XYSIZE:
 
111
        s = "xysize";
 
112
        break;
 
113
    }
 
114
    return s;
 
115
}
 
116
 
 
117
int get_settype_by_name(char *s)
 
118
{
 
119
    int i;
 
120
    
 
121
    for (i = 0; i < NUMBER_OF_SETTYPES; i++) {
 
122
        if (strcmp(set_types(i), s) == 0) {
 
123
            return i;
 
124
        }
 
125
    }
 
126
    return SET_BAD;
 
127
}
 
128
 
 
129
int settype_cols(int type)
 
130
{
 
131
    int ncols;
 
132
    
 
133
    switch (type) {
 
134
    case SET_XY:
 
135
    case SET_BAR:
 
136
        ncols = 2;
 
137
        break;
 
138
    case SET_XYDX:
 
139
    case SET_XYDY:
 
140
    case SET_XYZ:
 
141
    case SET_BARDY:
 
142
    case SET_XYR:
 
143
    case SET_XYCOLOR:
 
144
    case SET_XYSIZE:
 
145
        ncols = 3;
 
146
        break;
 
147
    case SET_XYDXDX:
 
148
    case SET_XYDYDY:
 
149
    case SET_XYDXDY:
 
150
    case SET_BARDYDY:
 
151
    case SET_XYCOLPAT:
 
152
    case SET_XYVMAP:
 
153
        ncols = 4;
 
154
        break;
 
155
    case SET_XYHILO:
 
156
        ncols = 5;
 
157
        break;
 
158
    case SET_XYDXDXDYDY:
 
159
    case SET_BOXPLOT:
 
160
        ncols = 6;
 
161
        break;
 
162
    default:
 
163
        ncols = 0;
 
164
        break;
 
165
    }
 
166
    
 
167
    return ncols;
 
168
}
 
169
 
 
170
/*
 
171
 * return the string version of the dataset column
 
172
 */
 
173
char *dataset_colname(int col)
 
174
{
 
175
    char *s;
 
176
 
 
177
    switch (col) {
 
178
    case 0:
 
179
        s = "X";
 
180
        break;
 
181
    case 1:
 
182
        s = "Y";
 
183
        break;
 
184
    case 2:
 
185
        s = "Y1";
 
186
        break;
 
187
    case 3:
 
188
        s = "Y2";
 
189
        break;
 
190
    case 4:
 
191
        s = "Y3";
 
192
        break;
 
193
    case 5:
 
194
        s = "Y4";
 
195
        break;
 
196
    default:
 
197
        s = "?";
 
198
        errmsg("Internal error in dataset_colname()");
 
199
        break;
 
200
    }
 
201
    return s;
 
202
}
 
203
 
 
204
int zero_set_data(Dataset *dsp)
 
205
{
 
206
    int k;
 
207
    
 
208
    if (dsp) {
 
209
        dsp->len = 0;
 
210
        for (k = 0; k < MAX_SET_COLS; k++) {
 
211
            dsp->ex[k] = NULL;
 
212
        }
 
213
        dsp->s = NULL;
 
214
        return RETURN_SUCCESS;
 
215
    } else {
 
216
        return RETURN_FAILURE;
 
217
    }
 
218
}
 
219
 
 
220
/*
 
221
 * free set data
 
222
 */
 
223
int free_set_data(Dataset *dsp)
 
224
{
 
225
    int k;
 
226
    
 
227
    if (dsp) {
 
228
        if (dsp->len) {
 
229
            for (k = 0; k < MAX_SET_COLS; k++) {
 
230
                XCFREE(dsp->ex[k]);
 
231
            }
 
232
            if (dsp->s) {
 
233
                for (k = 0; k < dsp->len; k++) {
 
234
                    XCFREE(dsp->s[k]);
 
235
                }
 
236
                XCFREE(dsp->s);
 
237
            }
 
238
            dsp->len = 0;
 
239
            set_dirtystate();
 
240
        }
 
241
        return RETURN_SUCCESS;
 
242
    } else {
 
243
        return RETURN_FAILURE;
 
244
    }
 
245
}
 
246
 
 
247
/*
 
248
 * free set data, but preserve the parameter settings
 
249
 */
 
250
void killsetdata(int gno, int setno)
 
251
{
 
252
    if (is_valid_setno(gno, setno)) {
 
253
        free_set_data(&g[gno].p[setno].data);
 
254
    }
 
255
}
 
256
 
 
257
/*
 
258
 * (re)allocate data arrays for a set of length len.
 
259
 */
 
260
int setlength(int gno, int setno, int len)
 
261
{
 
262
    plotarr *p;
 
263
    int i, j, ncols, oldlen;
 
264
 
 
265
    if (is_valid_setno(gno, setno) != TRUE) {
 
266
        return RETURN_FAILURE;
 
267
    }
 
268
    
 
269
    p = &g[gno].p[setno];
 
270
 
 
271
    oldlen = p->data.len;
 
272
    if (len == oldlen) {
 
273
        return RETURN_SUCCESS;
 
274
    }
 
275
    if (len < 0) {
 
276
        return RETURN_FAILURE;
 
277
    }
 
278
    
 
279
    ncols = settype_cols(p->type);
 
280
    
 
281
    if (ncols == 0) {
 
282
        errmsg("Set type not found in setlength()!");
 
283
        return RETURN_FAILURE;
 
284
    }
 
285
    
 
286
    for (i = 0; i < ncols; i++) {
 
287
        if ((p->data.ex[i] = xrealloc(p->data.ex[i], len*SIZEOF_DOUBLE)) == NULL
 
288
            && len != 0) {
 
289
            return RETURN_FAILURE;
 
290
        }
 
291
        for (j = oldlen; j < len; j++) {
 
292
            p->data.ex[i][j] = 0.0;
 
293
        }
 
294
    }
 
295
    
 
296
    if (p->data.s != NULL) {
 
297
        for (i = len; i < oldlen; i++) {
 
298
            xfree(p->data.s[i]);
 
299
        }
 
300
        p->data.s = xrealloc(p->data.s, len*sizeof(char *));
 
301
        for (j = oldlen; j < len; j++) {
 
302
            p->data.s[j] = copy_string(NULL, "");
 
303
        }
 
304
    }
 
305
    
 
306
    p->data.len = len;
 
307
 
 
308
    set_dirtystate();
 
309
    
 
310
    return RETURN_SUCCESS;
 
311
}
 
312
 
 
313
/*
 
314
 * moveset 
 
315
 */
 
316
int moveset(int gnofrom, int setfrom, int gnoto, int setto)
 
317
{
 
318
    if (gnoto == gnofrom && setfrom == setto) {
 
319
        return RETURN_FAILURE;
 
320
    }
 
321
 
 
322
    if (is_valid_setno(gnofrom, setfrom) != TRUE) {
 
323
        return RETURN_FAILURE;
 
324
    }
 
325
 
 
326
    if (is_set_active(gnoto, setto)) {
 
327
        killset(gnoto, setto);
 
328
    }
 
329
    activateset(gnoto, setto);
 
330
 
 
331
    memcpy(&g[gnoto].p[setto], &g[gnofrom].p[setfrom], sizeof(plotarr));
 
332
 
 
333
    zero_set_data(&g[gnofrom].p[setfrom].data);
 
334
        
 
335
    g[gnofrom].p[setfrom].hidden = TRUE;
 
336
    
 
337
    set_dirtystate();
 
338
    return RETURN_SUCCESS;
 
339
}
 
340
 
 
341
 
 
342
/*
 
343
 * copy a set to another set, if the to set doesn't exist allocate it
 
344
 */
 
345
int copyset(int gfrom, int setfrom, int gto, int setto)
 
346
{
 
347
    int i, k, len, ncols;
 
348
    double *savec[MAX_SET_COLS];
 
349
    char **saves;
 
350
    char buf[256];
 
351
 
 
352
    if (!is_set_active(gfrom, setfrom)) {
 
353
        return RETURN_FAILURE;
 
354
    }
 
355
    if (!is_valid_gno(gto)) {
 
356
        return RETURN_FAILURE;
 
357
    }
 
358
    if (setfrom == setto && gfrom == gto) {
 
359
        return RETURN_FAILURE;
 
360
    }
 
361
    if (is_set_active(gto, setto)) {
 
362
        killset(gto, setto);
 
363
    }
 
364
    len = getsetlength(gfrom, setfrom);
 
365
    ncols = dataset_cols(gfrom, setfrom);
 
366
    activateset(gto, setto);
 
367
    set_dataset_type(gto, setto, dataset_type(gfrom, setfrom));
 
368
    if (setlength(gto, setto, len) != RETURN_SUCCESS) {
 
369
        return RETURN_FAILURE;
 
370
    }
 
371
    if (g[gfrom].p[setfrom].data.s != NULL) {
 
372
        if ((g[gto].p[setto].data.s = xmalloc(len*sizeof(char *))) == NULL) {
 
373
            return RETURN_FAILURE;
 
374
        }
 
375
    }
 
376
 
 
377
    for (k = 0; k < MAX_SET_COLS; k++) {
 
378
        savec[k] = getcol(gto, setto, k);
 
379
    }
 
380
    saves = get_set_strings(gto, setto);
 
381
    memcpy(&g[gto].p[setto], &g[gfrom].p[setfrom], sizeof(plotarr));
 
382
    for (k = 0; k < ncols; k++) {
 
383
        g[gto].p[setto].data.ex[k] = savec[k];
 
384
        memcpy(g[gto].p[setto].data.ex[k],
 
385
               g[gfrom].p[setfrom].data.ex[k],
 
386
               len*SIZEOF_DOUBLE);
 
387
    }
 
388
    g[gto].p[setto].data.s = saves;
 
389
    if (g[gfrom].p[setfrom].data.s != NULL) {
 
390
        for (i = 0; i < len; i++) {
 
391
             g[gto].p[setto].data.s[i] =
 
392
                copy_string(NULL, g[gfrom].p[setfrom].data.s[i]);
 
393
        }
 
394
    }
 
395
 
 
396
    sprintf(buf, "copy of set G%d.S%d", gfrom, setfrom);
 
397
    setcomment(gto, setto, buf);
 
398
 
 
399
    set_dirtystate();
 
400
    
 
401
    return RETURN_SUCCESS;
 
402
}
 
403
 
 
404
/*
 
405
 * same as copyset(), but doesn't alter the to set appearance
 
406
 */
 
407
int copysetdata(int gfrom, int setfrom, int gto, int setto)
 
408
{
 
409
    int i, k, len, ncols;
 
410
    char buf[256];
 
411
 
 
412
    if (!is_set_active(gfrom, setfrom)) {
 
413
        return RETURN_FAILURE;
 
414
    }
 
415
    if (!is_valid_gno(gto)) {
 
416
        return RETURN_FAILURE;
 
417
    }
 
418
    if (setfrom == setto && gfrom == gto) {
 
419
        return RETURN_FAILURE;
 
420
    }
 
421
    if (is_set_active(gto, setto)) {
 
422
        killsetdata(gto, setto);
 
423
    }
 
424
    len = getsetlength(gfrom, setfrom);
 
425
    ncols = dataset_cols(gfrom, setfrom);
 
426
    activateset(gto, setto);
 
427
    if (dataset_cols(gto, setto) != ncols) {
 
428
        set_dataset_type(gto, setto, dataset_type(gfrom, setfrom));
 
429
    }
 
430
    if (setlength(gto, setto, len) != RETURN_SUCCESS) {
 
431
        return RETURN_FAILURE;
 
432
    }
 
433
    if (g[gfrom].p[setfrom].data.s != NULL) {
 
434
        if ((g[gto].p[setto].data.s = xmalloc(len*sizeof(char *))) == NULL) {
 
435
            return RETURN_FAILURE;
 
436
        }
 
437
    }
 
438
 
 
439
    for (k = 0; k < ncols; k++) {
 
440
        memcpy(g[gto].p[setto].data.ex[k],
 
441
               g[gfrom].p[setfrom].data.ex[k],
 
442
               len*SIZEOF_DOUBLE);
 
443
    }
 
444
    if (g[gfrom].p[setfrom].data.s != NULL) {
 
445
        for (i = 0; i < len; i++) {
 
446
             g[gto].p[setto].data.s[i] =
 
447
                copy_string(NULL, g[gfrom].p[setfrom].data.s[i]);
 
448
        }
 
449
    }
 
450
 
 
451
    sprintf(buf, "copy of setdata G%d.S%d", gfrom, setfrom);
 
452
    setcomment(gto, setto, buf);
 
453
 
 
454
    set_dirtystate();
 
455
    
 
456
    return RETURN_SUCCESS;
 
457
}
 
458
 
 
459
/*
 
460
 * swap a set with another set
 
461
 */
 
462
int swapset(int gno1, int setno1, int gno2, int setno2)
 
463
{
 
464
    plotarr p;
 
465
 
 
466
    if (is_valid_setno(gno1, setno1) == FALSE ||
 
467
        is_valid_setno(gno2, setno2) == FALSE) {
 
468
        return RETURN_FAILURE;
 
469
    }
 
470
    if (setno1 == setno2 && gno1 == gno2) {
 
471
        return RETURN_FAILURE;
 
472
    }
 
473
 
 
474
    memcpy(&p, &g[gno2].p[setno2], sizeof(plotarr));
 
475
    memcpy(&g[gno2].p[setno2], &g[gno1].p[setno1], sizeof(plotarr));
 
476
    memcpy(&g[gno1].p[setno1], &p, sizeof(plotarr));
 
477
 
 
478
    set_dirtystate();
 
479
    
 
480
    return RETURN_SUCCESS;
 
481
}
 
482
 
 
483
/*
 
484
 * kill a set
 
485
 */
 
486
void killset(int gno, int setno)
 
487
{
 
488
    if (is_valid_setno(gno, setno)) {
 
489
        killsetdata(gno, setno);
 
490
        set_default_plotarr(&g[gno].p[setno]);
 
491
    }
 
492
}
 
493
 
 
494
double *getcol(int gno, int setno, int col)
 
495
{
 
496
    if (is_valid_setno(gno, setno)) {
 
497
        return g[gno].p[setno].data.ex[col];
 
498
    } else {
 
499
        return NULL;
 
500
    }
 
501
}
 
502
 
 
503
void setcol(int gno, int setno, int col, double *x, int len)
 
504
{
 
505
    if (is_valid_setno(gno, setno) != TRUE) {
 
506
        return;
 
507
    }
 
508
    g[gno].p[setno].data.ex[col] = x;
 
509
    g[gno].p[setno].data.len = len;
 
510
    set_dirtystate();
 
511
}
 
512
 
 
513
char **get_set_strings(int gno, int setno)
 
514
{
 
515
    if (is_valid_setno(gno, setno)) {
 
516
        return g[gno].p[setno].data.s;
 
517
    } else {
 
518
        return NULL;
 
519
    }
 
520
}
 
521
 
 
522
int set_set_strings(int gno, int setno, int len, char **s)
 
523
{
 
524
    if (is_valid_setno(gno, setno) && len > 0 && s!= NULL) {
 
525
        g[gno].p[setno].data.s = s;
 
526
        g[gno].p[setno].data.len = len;
 
527
        set_dirtystate();
 
528
        return RETURN_SUCCESS;
 
529
    } else {
 
530
        return RETURN_FAILURE;
 
531
    }
 
532
}
 
533
 
 
534
int getsetlength(int gno, int setno)
 
535
{
 
536
    if (is_valid_setno(gno, setno)) {
 
537
        return g[gno].p[setno].data.len;
 
538
    } else {
 
539
        return -1;
 
540
    }
 
541
}
 
542
 
 
543
int setcomment(int gno, int setno, char *s)
 
544
 
545
    if (is_valid_setno(gno, setno) && s != NULL) {
 
546
        strncpy(g[gno].p[setno].comments, s, MAX_STRING_LENGTH - 1);
 
547
        set_dirtystate();
 
548
        return RETURN_SUCCESS;
 
549
    } else {
 
550
        return RETURN_FAILURE;
 
551
    }
 
552
}
 
553
 
 
554
char *getcomment(int gno, int setno)
 
555
 
556
    if (is_valid_setno(gno, setno)) {
 
557
        return g[gno].p[setno].comments;
 
558
    } else {
 
559
        return NULL;
 
560
    }
 
561
}
 
562
 
 
563
int set_legend_string(int gno, int setno, char *s)
 
564
 
565
    if (is_valid_setno(gno, setno) && s != NULL) {
 
566
        strncpy(g[gno].p[setno].lstr, s, MAX_STRING_LENGTH - 1);
 
567
        return RETURN_SUCCESS;
 
568
    } else {
 
569
        return RETURN_FAILURE;
 
570
    }
 
571
}
 
572
 
 
573
char *get_legend_string(int gno, int setno)
 
574
 
575
    if (is_valid_setno(gno, setno)) {
 
576
        return g[gno].p[setno].lstr;
 
577
    } else {
 
578
        return NULL;
 
579
    }
 
580
}
 
581
 
 
582
int set_dataset_type(int gno, int setno, int type)
 
583
 
584
    int old_type = dataset_type(gno, setno);
 
585
    
 
586
    if (old_type < 0) {
 
587
        /* wrong gno/setno */
 
588
        return RETURN_FAILURE;
 
589
    } else if (old_type == type) {
 
590
        /* nothing changed */
 
591
        return RETURN_SUCCESS;
 
592
    } else {
 
593
        int i, len, ncols_old, ncols_new;
 
594
        
 
595
        len = getsetlength(gno, setno);
 
596
        ncols_old = dataset_cols(gno, setno);
 
597
        ncols_new = settype_cols(type);
 
598
        for (i = ncols_old; i < ncols_new; i++) {
 
599
            g[gno].p[setno].data.ex[i] = xcalloc(len, SIZEOF_DOUBLE);
 
600
        }
 
601
        for (i = ncols_new; i < ncols_old; i++) {
 
602
            XCFREE(g[gno].p[setno].data.ex[i]);
 
603
        }
 
604
 
 
605
        g[gno].p[setno].type = type;
 
606
        
 
607
        set_dirtystate();
 
608
        return RETURN_SUCCESS;
 
609
    }
 
610
}
 
611
 
 
612
int dataset_type(int gno, int setno)
 
613
 
614
    if (is_valid_setno(gno, setno)) {
 
615
        return g[gno].p[setno].type;
 
616
    } else {
 
617
        return -1;
 
618
    }
 
619
}
 
620
 
 
621
 
 
622
 
 
623
void set_hotlink(int gno, int setno, int onoroff, char *fname, int src)
 
624
{
 
625
    if (is_valid_setno(gno, setno) != TRUE) {
 
626
        return;
 
627
    }
 
628
    
 
629
    g[gno].p[setno].hotlink = onoroff;
 
630
    if (onoroff && fname != NULL) {
 
631
        strcpy(g[gno].p[setno].hotfile, fname);
 
632
        g[gno].p[setno].hotsrc = src;
 
633
    }
 
634
    set_dirtystate();
 
635
}
 
636
 
 
637
int is_hotlinked(int gno, int setno)
 
638
{
 
639
    if (is_valid_setno(gno, setno) != TRUE) {
 
640
        return FALSE;
 
641
    }
 
642
    
 
643
    if (g[gno].p[setno].hotlink && strlen(g[gno].p[setno].hotfile)) {
 
644
        return g[gno].p[setno].hotlink;
 
645
    } else { 
 
646
        return FALSE;
 
647
    }
 
648
}
 
649
 
 
650
char *get_hotlink_file(int gno, int setno)
 
651
{
 
652
    if (is_valid_setno(gno, setno) != TRUE) {
 
653
        return NULL;
 
654
    } else {
 
655
        return g[gno].p[setno].hotfile;
 
656
    }
 
657
}
 
658
 
 
659
int get_hotlink_src(int gno, int setno)
 
660
{
 
661
    if (is_valid_setno(gno, setno) != TRUE) {
 
662
        return -1;
 
663
    } else {
 
664
        return g[gno].p[setno].hotsrc;
 
665
    }
 
666
}
 
667
 
 
668
void do_update_hotlink(int gno, int setno)
 
669
{
 
670
    if (is_valid_setno(gno, setno) != TRUE) {
 
671
        return;
 
672
    } else {
 
673
        read_xyset_fromfile(gno, setno, g[gno].p[setno].hotfile, 
 
674
                        g[gno].p[setno].hotsrc, g[gno].p[setno].hotlink);
 
675
    }
 
676
}
 
677
 
 
678
 
 
679
/*
 
680
 * get the min/max fields of a set
 
681
 */
 
682
int getsetminmax(int gno, int setno, 
 
683
                    double *xmin, double *xmax, double *ymin, double *ymax)
 
684
{
 
685
    double *x, *y;
 
686
    int len;
 
687
    double x1, x2, y1, y2;
 
688
    int i, first = TRUE;
 
689
    int imin, imax; /* dummy */
 
690
 
 
691
    if (setno == ALL_SETS) {
 
692
        for (i = 0; i < number_of_sets(gno); i++) {
 
693
            if (is_set_drawable(gno, i)) {
 
694
                x = getcol(gno, i, 0);
 
695
                y = getcol(gno, i, 1);
 
696
                len = getsetlength(gno, i);
 
697
                minmax(x, len, &x1, &x2, &imin, &imax);
 
698
                minmax(y, len, &y1, &y2, &imin, &imax);
 
699
                if (first) {
 
700
                    *xmin = x1;
 
701
                    *xmax = x2;
 
702
                    *ymin = y1;
 
703
                    *ymax = y2;
 
704
                    first = FALSE;
 
705
                } else {
 
706
                    *xmin = (x1 < *xmin) ? x1 : *xmin;
 
707
                    *xmax = (x2 > *xmax) ? x2 : *xmax;
 
708
                    *ymin = (y1 < *ymin) ? y1 : *ymin;
 
709
                    *ymax = (y2 > *ymax) ? y2 : *ymax;
 
710
                }
 
711
            }
 
712
        }
 
713
    } else if (is_valid_setno(gno, setno)) {
 
714
        x = getcol(gno, setno, 0);
 
715
        y = getcol(gno, setno, 1);
 
716
        len = getsetlength(gno, setno);
 
717
        minmax(x, len, xmin, xmax, &imin, &imax);
 
718
        minmax(y, len, ymin, ymax, &imin, &imax);
 
719
        first = FALSE;
 
720
    }
 
721
    
 
722
    if (first == FALSE) {
 
723
        return RETURN_SUCCESS;
 
724
    } else {
 
725
        return RETURN_FAILURE;
 
726
    }
 
727
}
 
728
 
 
729
/*
 
730
 * get the min/max fields of a set with fixed x/y range
 
731
 */
 
732
int getsetminmax_c(int gno, int setno, 
 
733
            double *xmin, double *xmax, double *ymin, double *ymax, int ivec)
 
734
{
 
735
    double vmin_t, vmax_t, *vmin, *vmax, bvmin, bvmax, *vec, *bvec;
 
736
    int i, start, stop, n;
 
737
    int first = TRUE, hits;
 
738
 
 
739
    if (ivec == 1) {    
 
740
        bvmin = *xmin;
 
741
        bvmax = *xmax;
 
742
        vmin  = ymin; 
 
743
        vmax  = ymax; 
 
744
    } else {
 
745
        bvmin = *ymin;
 
746
        bvmax = *ymax;
 
747
        vmin  = xmin;
 
748
        vmax  = xmax;
 
749
    }
 
750
    if (setno == ALL_SETS) {
 
751
        start = 0;
 
752
        stop  = number_of_sets(gno) - 1;
 
753
    } else if (is_valid_setno(gno, setno)) {
 
754
        start = setno;
 
755
        stop  = setno;
 
756
    } else {
 
757
        return RETURN_FAILURE;
 
758
    }
 
759
    
 
760
    for (i = start; i <= stop; i++) {
 
761
        if (is_set_drawable(gno, i)) {
 
762
            
 
763
            if (ivec == 1) {
 
764
                bvec = getx(gno, i);
 
765
                vec  = gety(gno, i);
 
766
            } else {
 
767
                bvec = gety(gno, i);
 
768
                vec  = getx(gno, i);
 
769
            }
 
770
            
 
771
            n = getsetlength(gno, i);
 
772
            hits = minmaxrange(bvec, vec, n, bvmin, bvmax, &vmin_t, &vmax_t);
 
773
            if (hits == RETURN_SUCCESS) {
 
774
                if (first) {
 
775
                    *vmin = vmin_t;
 
776
                    *vmax = vmax_t;
 
777
                    first = FALSE;
 
778
                } else {
 
779
                    *vmin = MIN2(vmin_t, *vmin);
 
780
                    *vmax = MAX2(vmax_t, *vmax);
 
781
                }
 
782
            }
 
783
        }
 
784
    }
 
785
    
 
786
    if (first == FALSE) {
 
787
        return RETURN_SUCCESS;
 
788
    } else {
 
789
        return RETURN_FAILURE;
 
790
    }
 
791
}
 
792
 
 
793
 
 
794
/*
 
795
 * compute the mins and maxes of a vector x
 
796
 */
 
797
void minmax(double *x, int n, double *xmin, double *xmax, int *imin, int *imax)
 
798
{
 
799
    int i;
 
800
    
 
801
    *imin = 0;
 
802
    *imax = 0;
 
803
 
 
804
    if (x == NULL) {
 
805
        *xmin = 0.0;
 
806
        *xmax = 0.0;
 
807
        return;
 
808
    }
 
809
    
 
810
    *xmin = x[0];
 
811
    *xmax = x[0];
 
812
    
 
813
    for (i = 1; i < n; i++) {
 
814
        if (x[i] < *xmin) {
 
815
            *xmin = x[i];
 
816
            *imin = i;
 
817
        }
 
818
        if (x[i] > *xmax) {
 
819
            *xmax = x[i];
 
820
            *imax = i;
 
821
        }
 
822
    }
 
823
}
 
824
 
 
825
 
 
826
/*
 
827
 * compute the min and max of vector vec calculated for indices such that
 
828
 * bvec values lie within [bmin, bmax] range
 
829
 * returns RETURN_FAILURE if none found
 
830
 */
 
831
int minmaxrange(double *bvec, double *vec, int n, double bvmin, double bvmax,
 
832
                   double *vmin, double *vmax)
 
833
{
 
834
    int i, first = TRUE;
 
835
    
 
836
    if ((vec == NULL) || (bvec == NULL)) {
 
837
        return RETURN_FAILURE;
 
838
    }
 
839
    
 
840
    for (i = 0; i < n; i++) {
 
841
        if ((bvec[i] >= bvmin) && (bvec[i] <= bvmax)) {
 
842
            if (first == TRUE) {
 
843
                *vmin = vec[i];
 
844
                *vmax = vec[i];
 
845
                first = FALSE;
 
846
            } else {
 
847
                if (vec[i] < *vmin) {
 
848
                    *vmin = vec[i];
 
849
                } else if (vec[i] > *vmax) {
 
850
                    *vmax = vec[i];
 
851
                }
 
852
            }
 
853
        }
 
854
    }
 
855
    
 
856
    if (first == FALSE) {
 
857
        return RETURN_SUCCESS;
 
858
    } else {
 
859
        return RETURN_FAILURE;
 
860
    }
 
861
}
 
862
 
 
863
 
 
864
/*
 
865
 * compute the mins and maxes of a vector x
 
866
 */
 
867
double vmin(double *x, int n)
 
868
{
 
869
    int i;
 
870
    double xmin;
 
871
    if (n <= 0) {
 
872
        return 0.0;
 
873
    }
 
874
    xmin = x[0];
 
875
    for (i = 1; i < n; i++) {
 
876
        if (x[i] < xmin) {
 
877
            xmin = x[i];
 
878
        }
 
879
    }
 
880
    return xmin;
 
881
}
 
882
 
 
883
double vmax(double *x, int n)
 
884
{
 
885
    int i;
 
886
    double xmax;
 
887
    if (n <= 0) {
 
888
        return 0.0;
 
889
    }
 
890
    xmax = x[0];
 
891
    for (i = 1; i < n; i++) {
 
892
        if (x[i] > xmax) {
 
893
            xmax = x[i];
 
894
        }
 
895
    }
 
896
    return xmax;
 
897
}
 
898
 
 
899
int set_point(int gno, int setno, int seti, WPoint wp)
 
900
{
 
901
    if (is_valid_setno(gno, setno) != TRUE) {
 
902
        return RETURN_FAILURE;
 
903
    }
 
904
    if (seti >= getsetlength(gno, setno) || seti < 0) {
 
905
        return RETURN_FAILURE;
 
906
    }
 
907
    (getcol(gno, setno, DATA_X))[seti] = wp.x;
 
908
    (getcol(gno, setno, DATA_Y))[seti] = wp.y;
 
909
    set_dirtystate();
 
910
    return RETURN_SUCCESS;
 
911
}
 
912
 
 
913
int get_point(int gno, int setno, int seti, WPoint *wp)
 
914
{
 
915
    if (is_valid_setno(gno, setno) != TRUE) {
 
916
        return RETURN_FAILURE;
 
917
    }
 
918
    if (seti >= getsetlength(gno, setno) || seti < 0) {
 
919
        return RETURN_FAILURE;
 
920
    }
 
921
    wp->x = (getcol(gno, setno, DATA_X))[seti];
 
922
    wp->y = (getcol(gno, setno, DATA_Y))[seti];
 
923
    return RETURN_SUCCESS;
 
924
}
 
925
 
 
926
void copycol2(int gfrom, int setfrom, int gto, int setto, int col)
 
927
{
 
928
    int i, n1, n2;
 
929
    double *x1, *x2;
 
930
 
 
931
    if (is_valid_setno(gfrom, setfrom) != TRUE ||
 
932
        is_valid_setno(gto, setto) != TRUE) {
 
933
        return;
 
934
    }
 
935
    n1 = getsetlength(gfrom, setfrom);
 
936
    n2 = getsetlength(gto, setto);
 
937
    if (n1 != n2) {
 
938
        return;
 
939
    }
 
940
    x1 = getcol(gfrom, setfrom, col);
 
941
    x2 = getcol(gto, setto, col);
 
942
    for (i = 0; i < n1; i++) {
 
943
        x2[i] = x1[i];
 
944
    }
 
945
    set_dirtystate();
 
946
}
 
947
 
 
948
 
 
949
int pushset(int gno, int setno, int push_type)
 
950
{
 
951
    int i, newsetno;
 
952
    
 
953
    if (is_valid_setno(gno, setno) != TRUE) {
 
954
        return RETURN_FAILURE;
 
955
    } else {
 
956
        switch (push_type) {
 
957
        case PUSH_SET_TOFRONT:
 
958
            newsetno = number_of_sets(gno) - 1;
 
959
            for (i = setno; i < newsetno; i++) {
 
960
                if (swapset(gno, i, gno, i + 1) != RETURN_SUCCESS) {
 
961
                    return RETURN_FAILURE;
 
962
                }
 
963
            }
 
964
            break;
 
965
        case PUSH_SET_TOBACK:
 
966
            newsetno = 0;
 
967
            for (i = setno; i > newsetno; i--) {
 
968
                if (swapset(gno, i, gno, i - 1) != RETURN_SUCCESS) {
 
969
                    return RETURN_FAILURE;
 
970
                }
 
971
            }
 
972
            break;
 
973
        default:
 
974
            return RETURN_FAILURE;
 
975
            break;
 
976
        }
 
977
        return RETURN_SUCCESS;
 
978
    }
 
979
}
 
980
 
 
981
 
 
982
/*
 
983
 * pack all sets leaving no gaps in the set structure
 
984
 */
 
985
void packsets(int gno)
 
986
{
 
987
    int i, j;
 
988
 
 
989
    for (i = 0; i < number_of_sets(gno); i++) {
 
990
        if (is_set_active(gno, i)) {
 
991
            for (j = 0; j < i; j++) {
 
992
                if (is_set_active(gno, j) != TRUE) {
 
993
                    moveset(gno, i, gno, j);
 
994
                }
 
995
            }
 
996
        }
 
997
    }
 
998
}
 
999
 
 
1000
int allocate_set(int gno, int setno)
 
1001
{
 
1002
    if (is_valid_setno(gno, setno)) {
 
1003
        return RETURN_SUCCESS;
 
1004
    } else {
 
1005
        return realloc_graph_plots(gno, setno + 1);
 
1006
    }
 
1007
}    
 
1008
 
 
1009
int activateset(int gno, int setno)
 
1010
{
 
1011
    int retval;
 
1012
    
 
1013
    if (is_valid_gno(gno) != TRUE) {
 
1014
        return RETURN_FAILURE;
 
1015
    } else {
 
1016
        retval = allocate_set(gno, setno);
 
1017
        if (retval == RETURN_SUCCESS) {
 
1018
            set_set_hidden(gno, setno, FALSE);
 
1019
        }
 
1020
        return retval;
 
1021
    }
 
1022
}
 
1023
 
 
1024
static target recent_target = {-1, -1};
 
1025
 
 
1026
int get_recent_setno(void)
 
1027
{
 
1028
    return recent_target.setno;
 
1029
}
 
1030
 
 
1031
int get_recent_gno(void)
 
1032
{
 
1033
    return recent_target.gno;
 
1034
}
 
1035
 
 
1036
/*
 
1037
 * return the next available set in graph gno
 
1038
 * If target is allocated but with no data, choose it (used for loading sets
 
1039
 * from project files when sets aren't packed)
 
1040
 */
 
1041
int nextset(int gno)
 
1042
{
 
1043
    int setno;
 
1044
    int maxplot;
 
1045
 
 
1046
    if (is_valid_gno(gno) != TRUE) {
 
1047
        return (-1);
 
1048
    }
 
1049
    
 
1050
    if ( (target_set.gno == gno) &&
 
1051
         is_valid_setno(target_set.gno, target_set.setno) &&
 
1052
         !is_set_active(gno, target_set.setno)) {
 
1053
        setno = target_set.setno;
 
1054
        target_set.gno = -1;
 
1055
        target_set.setno = -1;
 
1056
    } else {
 
1057
        maxplot = number_of_sets(gno);
 
1058
        for (setno = 0; setno < maxplot; setno++) {
 
1059
            if (!is_set_active(gno, setno)) {
 
1060
                break;
 
1061
            }
 
1062
        }
 
1063
        /* if no sets found, try allocating new one */
 
1064
        if (setno == maxplot && allocate_set(gno, setno) != RETURN_SUCCESS) {
 
1065
            return (-1);
 
1066
        }
 
1067
    }
 
1068
    recent_target.gno = gno;
 
1069
    recent_target.setno = setno;
 
1070
    return (setno);
 
1071
}
 
1072
 
 
1073
int is_set_active(int gno, int setno)
 
1074
{
 
1075
    if (is_valid_setno(gno, setno) && getsetlength(gno, setno) > 0) {
 
1076
        return TRUE;
 
1077
    } else {
 
1078
        return FALSE;
 
1079
    }
 
1080
}
 
1081
 
 
1082
/*
 
1083
 * return number of active set(s) in gno
 
1084
 */
 
1085
int number_of_active_sets(int gno)
 
1086
{
 
1087
    int setno, na;
 
1088
 
 
1089
    if (is_valid_gno(gno) != TRUE) {
 
1090
        return -1;
 
1091
    }
 
1092
    
 
1093
    na = 0;
 
1094
    for (setno = 0; setno < number_of_sets(gno); setno++) {
 
1095
        if (is_set_active(gno, setno) == TRUE) {
 
1096
            na++;
 
1097
        }
 
1098
    }
 
1099
    return na;
 
1100
}
 
1101
 
 
1102
/*
 
1103
 * drop points from a set
 
1104
 */
 
1105
void droppoints(int gno, int setno, int startno, int endno)
 
1106
{
 
1107
    double *x;
 
1108
    char **s;
 
1109
    int i, j, len, ncols, dist;
 
1110
 
 
1111
    if (is_valid_setno(gno, setno) != TRUE) {
 
1112
        return;
 
1113
    }
 
1114
 
 
1115
    dist = endno - startno + 1;
 
1116
    if (dist <= 0) {
 
1117
        return;
 
1118
    }
 
1119
    
 
1120
    len = getsetlength(gno, setno);
 
1121
    
 
1122
    if (dist == len) {
 
1123
        killsetdata(gno, setno);
 
1124
        return;
 
1125
    }
 
1126
    
 
1127
    ncols = dataset_cols(gno, setno);
 
1128
    for (j = 0; j < ncols; j++) {
 
1129
        x = getcol(gno, setno, j);
 
1130
        for (i = endno + 1; i < len; i++) {
 
1131
            x[i - dist] = x[i];
 
1132
        }
 
1133
    }
 
1134
    if ((s = get_set_strings(gno, setno)) != NULL) {
 
1135
        for (i = endno + 1; i < len; i++) {
 
1136
            s[i - dist] = copy_string(s[i - dist], s[i]);
 
1137
        }
 
1138
    }
 
1139
    setlength(gno, setno, len - dist);
 
1140
}
 
1141
 
 
1142
/*
 
1143
 * join several sets together; all but the first set in the list will be killed 
 
1144
 */
 
1145
int join_sets(int gno, int *sets, int nsets)
 
1146
{
 
1147
    int i, j, n, setno, setno_final, ncols, old_length, new_length;
 
1148
    double *x1, *x2;
 
1149
    char **s1, **s2;
 
1150
 
 
1151
    if (nsets < 2) {
 
1152
        errmsg("nsets < 2");
 
1153
        return RETURN_FAILURE;
 
1154
    }
 
1155
    
 
1156
    setno_final = sets[0];
 
1157
    ncols = dataset_cols(gno, setno_final);
 
1158
    for (i = 0; i < nsets; i++) {
 
1159
        setno = sets[i];
 
1160
        if (is_valid_setno(gno, setno) != TRUE) {
 
1161
            errmsg("Invalid setno in the list");
 
1162
            return RETURN_FAILURE;
 
1163
        }
 
1164
        if (dataset_cols(gno, setno) != ncols) {
 
1165
            errmsg("Can't join datasets with different number of cols");
 
1166
            return RETURN_FAILURE;
 
1167
        }
 
1168
    }
 
1169
    
 
1170
    new_length = getsetlength(gno, setno_final);
 
1171
    for (i = 1; i < nsets; i++) {
 
1172
        setno = sets[i];
 
1173
        old_length = new_length;
 
1174
        new_length += getsetlength(gno, setno);
 
1175
        if (setlength(gno, setno_final, new_length) != RETURN_SUCCESS) {
 
1176
            return RETURN_FAILURE;
 
1177
        }
 
1178
        for (j = 0; j < ncols; j++) {
 
1179
            x1 = getcol(gno, setno_final, j);
 
1180
            x2 = getcol(gno, setno, j);
 
1181
            for (n = old_length; n < new_length; n++) {
 
1182
                x1[n] = x2[n - old_length];
 
1183
            }
 
1184
        }
 
1185
        s1 = get_set_strings(gno, setno_final);
 
1186
        s2 = get_set_strings(gno, setno);
 
1187
        if (s1 != NULL && s2 != NULL) {
 
1188
            for (n = old_length; n < new_length; n++) {
 
1189
                s1[n] = copy_string(s1[n], s2[n - old_length]);
 
1190
            }
 
1191
        }
 
1192
        killset(gno, setno);
 
1193
    }
 
1194
    
 
1195
    return RETURN_SUCCESS;
 
1196
}
 
1197
 
 
1198
void reverse_set(int gno, int setno)
 
1199
{
 
1200
    int n, i, j, k, ncols;
 
1201
    double *x;
 
1202
    char **s;
 
1203
 
 
1204
    if (!is_valid_setno(gno, setno)) {
 
1205
        return;
 
1206
    }
 
1207
    n = getsetlength(gno, setno);
 
1208
    ncols = dataset_cols(gno, setno);
 
1209
    for (k = 0; k < ncols; k++) {
 
1210
        x = getcol(gno, setno, k);
 
1211
        for (i = 0; i < n / 2; i++) {
 
1212
            j = (n - 1) - i;
 
1213
            fswap(&x[i], &x[j]);
 
1214
        }
 
1215
    }
 
1216
    if ((s = get_set_strings(gno, setno)) != NULL) {
 
1217
        char *stmp;
 
1218
        for (i = 0; i < n / 2; i++) {
 
1219
            j = (n - 1) - i;
 
1220
            stmp = s[i];
 
1221
            s[i] = s[j];
 
1222
            s[j] = stmp;
 
1223
        }
 
1224
    }
 
1225
    set_dirtystate();
 
1226
}
 
1227
/*
 
1228
 * sort a set
 
1229
 */
 
1230
static double *vptr;
 
1231
 
 
1232
/*
 
1233
 * for ascending and descending sorts
 
1234
 */
 
1235
 
 
1236
static int compare_points1(const void *p1, const void *p2)
 
1237
{
 
1238
    const int *i1, *i2;
 
1239
    double a, b;
 
1240
    i1 = (const int *)p1;
 
1241
    i2 = (const int *)p2;
 
1242
    a = vptr[*i1];
 
1243
    b = vptr[*i2];
 
1244
    if (a < b) {
 
1245
        return -1;
 
1246
    }
 
1247
    if (a > b) {
 
1248
        return 1;
 
1249
    }
 
1250
    return 0;
 
1251
}
 
1252
 
 
1253
static int compare_points2(const void *p1, const void *p2)
 
1254
{
 
1255
    const int *i1, *i2;
 
1256
    double a, b;
 
1257
    i1 = (const int *)p1;
 
1258
    i2 = (const int *)p2;
 
1259
    a = vptr[*i1];
 
1260
    b = vptr[*i2];
 
1261
    if (a > b) {
 
1262
        return -1;
 
1263
    }
 
1264
    if (a < b) {
 
1265
        return 1;
 
1266
    }
 
1267
    return 0;
 
1268
}
 
1269
 
 
1270
void sortset(int gno, int setno, int sorton, int stype)
 
1271
{
 
1272
    int i, j, nc, len, *ind;
 
1273
    double *x, *xtmp;
 
1274
    char **s, **stmp;
 
1275
 
 
1276
    /* get the vector to sort on */
 
1277
    vptr = getcol(gno, setno, sorton);
 
1278
    if (vptr == NULL) {
 
1279
        errmsg("NULL vector in sort, operation cancelled, check set type");
 
1280
        return;
 
1281
    }
 
1282
 
 
1283
    len = getsetlength(gno, setno);
 
1284
    if (len <= 1) {
 
1285
        return;
 
1286
    }
 
1287
    
 
1288
    /* allocate memory for permuted indices */
 
1289
    ind = xmalloc(len*SIZEOF_INT);
 
1290
    if (ind == NULL) {
 
1291
        return;
 
1292
    }
 
1293
    /* allocate memory for temporary array */
 
1294
    xtmp = xmalloc(len*SIZEOF_DOUBLE);
 
1295
    if (xtmp == NULL) {
 
1296
        xfree(ind);
 
1297
        return;
 
1298
    }
 
1299
    
 
1300
    s = get_set_strings(gno, setno);
 
1301
    if (s != NULL) {
 
1302
        stmp = xmalloc(len*sizeof(char *));
 
1303
        if (stmp == NULL) {
 
1304
            xfree(xtmp);
 
1305
            xfree(ind);
 
1306
        }
 
1307
    } else {
 
1308
        stmp = NULL;
 
1309
    }
 
1310
    
 
1311
    /* initialize indices */
 
1312
    for (i = 0; i < len; i++) {
 
1313
        ind[i] = i;
 
1314
    }
 
1315
 
 
1316
    /* sort */
 
1317
    qsort(ind, len, SIZEOF_INT, stype ? compare_points2 : compare_points1);
 
1318
 
 
1319
    /* straighten things out - done one vector at a time for storage */
 
1320
    
 
1321
    nc = dataset_cols(gno, setno);
 
1322
    /* loop over the number of columns */
 
1323
    for (j = 0; j < nc; j++) {
 
1324
        /* get this vector and put into the temporary vector in the right order */
 
1325
        x = getcol(gno, setno, j);
 
1326
        for (i = 0; i < len; i++) {
 
1327
            xtmp[i] = x[ind[i]];
 
1328
        }
 
1329
        
 
1330
        /* load it back to the set */
 
1331
        for (i = 0; i < len; i++) {
 
1332
            x[i] = xtmp[i];
 
1333
        }
 
1334
    }
 
1335
    
 
1336
    /* same with strings, if any */
 
1337
    if (s != NULL) {
 
1338
        for (i = 0; i < len; i++) {
 
1339
            stmp[i] = s[ind[i]];
 
1340
        }
 
1341
 
 
1342
        for (i = 0; i < len; i++) {
 
1343
            s[i] = stmp[i];
 
1344
        }
 
1345
    }
 
1346
    
 
1347
    /* free allocated temporary arrays */
 
1348
    xfree(stmp);
 
1349
    xfree(xtmp);
 
1350
    xfree(ind);
 
1351
 
 
1352
    set_dirtystate();
 
1353
}
 
1354
 
 
1355
/*
 
1356
 * sort two arrays
 
1357
 */
 
1358
void sort_xy(double *tmp1, double *tmp2, int up, int sorton, int stype)
 
1359
{
 
1360
 
 
1361
    int d, i, j;
 
1362
    int lo = 0;
 
1363
    double t1, t2;
 
1364
 
 
1365
    if (sorton == 1) {
 
1366
        double *ttmp;
 
1367
 
 
1368
        ttmp = tmp1;
 
1369
        tmp1 = tmp2;
 
1370
        tmp2 = ttmp;
 
1371
    }
 
1372
    up--;
 
1373
 
 
1374
    for (d = up - lo + 1; d > 1;) {
 
1375
        if (d < 5)
 
1376
            d = 1;
 
1377
        else
 
1378
            d = (5 * d - 1) / 11;
 
1379
        for (i = up - d; i >= lo; i--) {
 
1380
            t1 = tmp1[i];
 
1381
            t2 = tmp2[i];
 
1382
            if (!stype) {
 
1383
                for (j = i + d; j <= up && (t1 > tmp1[j]); j += d) {
 
1384
                    tmp1[j - d] = tmp1[j];
 
1385
                    tmp2[j - d] = tmp2[j];
 
1386
                }
 
1387
                tmp1[j - d] = t1;
 
1388
                tmp2[j - d] = t2;
 
1389
            } else {
 
1390
                for (j = i + d; j <= up && (t1 < tmp1[j]); j += d) {
 
1391
                    tmp1[j - d] = tmp1[j];
 
1392
                    tmp2[j - d] = tmp2[j];
 
1393
                }
 
1394
                tmp1[j - d] = t1;
 
1395
                tmp2[j - d] = t2;
 
1396
            }
 
1397
        }
 
1398
    }
 
1399
    set_dirtystate();
 
1400
}
 
1401
 
 
1402
/*
 
1403
 * delete the point pt in setno
 
1404
 */
 
1405
void del_point(int gno, int setno, int pt)
 
1406
{
 
1407
    droppoints(gno, setno, pt, pt);
 
1408
}
 
1409
 
 
1410
/*
 
1411
 * add a point to setno
 
1412
 */
 
1413
void add_point(int gno, int setno, double px, double py)
 
1414
{
 
1415
    int len;
 
1416
    double *x, *y;
 
1417
 
 
1418
    if (is_valid_setno(gno, setno)) {
 
1419
         len = getsetlength(gno, setno);
 
1420
         setlength(gno, setno, len + 1);
 
1421
         x = getx(gno, setno);
 
1422
         y = gety(gno, setno);
 
1423
         x[len] = px;
 
1424
         y[len] = py;
 
1425
    }
 
1426
}
 
1427
 
 
1428
void zero_datapoint(Datapoint *dpoint)
 
1429
{
 
1430
    int k;
 
1431
    
 
1432
    for (k = 0; k < MAX_SET_COLS; k++) {
 
1433
        dpoint->ex[k] = 0.0;
 
1434
    }
 
1435
    dpoint->s = NULL;
 
1436
}
 
1437
 
 
1438
/*
 
1439
 * add a point to setno at ind
 
1440
 */
 
1441
int add_point_at(int gno, int setno, int ind, const Datapoint *dpoint)
 
1442
{
 
1443
    int len, col, ncols;
 
1444
    double *ex;
 
1445
    char **s;
 
1446
 
 
1447
    if (is_valid_setno(gno, setno)) {
 
1448
        len = getsetlength(gno, setno);
 
1449
        if (ind < 0 || ind > len) {
 
1450
            return RETURN_FAILURE;
 
1451
        }
 
1452
        len++;
 
1453
        setlength(gno, setno, len);
 
1454
        ncols = dataset_cols(gno, setno);
 
1455
        for (col = 0; col < ncols; col++) {
 
1456
            ex = getcol(gno, setno, col);
 
1457
            if (ind < len - 1) {
 
1458
                memmove(ex + ind + 1, ex + ind, (len - ind - 1)*SIZEOF_DOUBLE);
 
1459
            }
 
1460
            ex[ind] = dpoint->ex[col];
 
1461
        }
 
1462
        s = get_set_strings(gno, setno);
 
1463
        if (s != NULL) {
 
1464
            if (ind < len - 1) {
 
1465
                memmove(s + ind + 1, s + ind, (len - ind - 1)*sizeof(char *));
 
1466
            }
 
1467
            s[ind] = copy_string(NULL, dpoint->s);
 
1468
        }
 
1469
        set_dirtystate();
 
1470
        return RETURN_SUCCESS;
 
1471
    } else {
 
1472
        return RETURN_FAILURE;
 
1473
    }
 
1474
}
 
1475
 
 
1476
int get_datapoint(int gno, int setno, int ind, int *ncols, Datapoint *dpoint)
 
1477
{
 
1478
    int n, col;
 
1479
    double *ex;
 
1480
    char **s;
 
1481
    
 
1482
    n = getsetlength(gno, setno);
 
1483
    if (ind < 0 || ind >= n) {
 
1484
        return RETURN_FAILURE;
 
1485
    } else {
 
1486
        *ncols = dataset_cols(gno, setno);
 
1487
        for (col = 0; col < *ncols; col++) {
 
1488
            ex = getcol(gno, setno, col);
 
1489
            dpoint->ex[col] = ex[ind];
 
1490
        }
 
1491
        s = get_set_strings(gno, setno);
 
1492
        if (s != NULL) {
 
1493
            dpoint->s = s[ind];
 
1494
        } else {
 
1495
            dpoint->s = NULL;
 
1496
        }
 
1497
        return RETURN_SUCCESS;
 
1498
    }
 
1499
}
 
1500
 
 
1501
void delete_byindex(int gno, int setno, int *ind)
 
1502
{
 
1503
    int i, j, cnt = 0;
 
1504
    int ncols = dataset_cols(gno, setno);
 
1505
 
 
1506
    if (is_valid_setno(gno, setno) != TRUE) {
 
1507
        return;
 
1508
    }
 
1509
    
 
1510
    for (i = 0; i < getsetlength(gno, setno); i++) {
 
1511
        if (ind[i]) {
 
1512
            cnt++;
 
1513
        }
 
1514
    }
 
1515
    if (cnt == getsetlength(gno, setno)) {
 
1516
        killset(gno, setno);
 
1517
        return;
 
1518
    }
 
1519
    cnt = 0;
 
1520
    for (i = 0; i < getsetlength(gno, setno); i++) {
 
1521
        if (ind[i] == 0) {
 
1522
            for (j = 0; j < ncols; j++) {
 
1523
                (getcol(gno, setno, j))[cnt] = (getcol(gno, setno, j))[i];
 
1524
            }
 
1525
            cnt++;
 
1526
        }
 
1527
    }
 
1528
    setlength(gno, setno, cnt);
 
1529
}
 
1530
 
 
1531
 
 
1532
/*
 
1533
 * move a set to another set, in possibly another graph
 
1534
 */
 
1535
int do_moveset(int gfrom, int setfrom, int gto, int setto)
 
1536
{
 
1537
    int retval;
 
1538
    char buf[64];
 
1539
    
 
1540
    retval = moveset(gfrom, setfrom, gto, setto);
 
1541
    if (retval != RETURN_SUCCESS) {
 
1542
        sprintf(buf,
 
1543
            "Error moving G%d.S%d to G%d.S%d",
 
1544
            gfrom, setfrom, gto, setto);
 
1545
        errmsg(buf);
 
1546
    }
 
1547
    return retval;
 
1548
}
 
1549
 
 
1550
/*
 
1551
 * do_copyset
 
1552
 */
 
1553
int do_copyset(int gfrom, int setfrom, int gto, int setto)
 
1554
{
 
1555
    int retval;
 
1556
    char buf[64];
 
1557
    
 
1558
    retval = copyset(gfrom, setfrom, gto, setto);
 
1559
    if (retval != RETURN_SUCCESS) {
 
1560
        sprintf(buf,
 
1561
            "Error copying G%d.S%d to G%d.S%d",
 
1562
            gfrom, setfrom, gto, setto);
 
1563
        errmsg(buf);
 
1564
    }
 
1565
    return retval;
 
1566
}
 
1567
 
 
1568
/*
 
1569
 * do_swapset
 
1570
 */
 
1571
int do_swapset(int gfrom, int setfrom, int gto, int setto)
 
1572
{
 
1573
    int retval;
 
1574
    char buf[64];
 
1575
    
 
1576
    retval = swapset(gfrom, setfrom, gto, setto);
 
1577
    if (retval != RETURN_SUCCESS) {
 
1578
        sprintf(buf,
 
1579
            "Error swapping G%d.S%d with G%d.S%d",
 
1580
            gfrom, setfrom, gto, setto);
 
1581
        errmsg(buf);
 
1582
    }
 
1583
    return retval;
 
1584
}
 
1585
 
 
1586
/*
 
1587
 * split a set into lpart length sets
 
1588
 */
 
1589
void do_splitsets(int gno, int setno, int lpart)
 
1590
{
 
1591
    int i, j, k, ncols, len, plen, tmpset, npsets;
 
1592
    double *x;
 
1593
    char s[256];
 
1594
    plotarr p;
 
1595
    Dataset ds, dstmp;
 
1596
 
 
1597
    if ((len = getsetlength(gno, setno)) < 2) {
 
1598
        errmsg("Set length < 2");
 
1599
        return;
 
1600
    }
 
1601
    if (lpart >= len) {
 
1602
        errmsg("Split length >= set length");
 
1603
        return;
 
1604
    }
 
1605
    if (lpart <= 0) {
 
1606
        errmsg("Split length <= 0");
 
1607
        return;
 
1608
    }
 
1609
 
 
1610
    npsets = (len - 1)/lpart + 1;
 
1611
 
 
1612
    /* get number of columns in this set */
 
1613
    ncols = dataset_cols(gno, setno);
 
1614
 
 
1615
    p = g[gno].p[setno];
 
1616
 
 
1617
    /* save the contents to a temporary buffer */
 
1618
    memcpy(&ds, &p.data, sizeof(Dataset));
 
1619
 
 
1620
    /* zero data contents of the original set */
 
1621
    zero_set_data(&g[gno].p[setno].data);
 
1622
 
 
1623
    /* now load each set */
 
1624
    for (i = 0; i < npsets; i++) {
 
1625
        plen = MIN2(lpart, len - i*lpart); 
 
1626
        tmpset = nextset(gno);
 
1627
        if (!is_valid_setno(gno, tmpset)) {
 
1628
            errmsg("Can't create new set");
 
1629
            return;
 
1630
        }
 
1631
        
 
1632
        /* set the plot parameters */
 
1633
        dstmp = g[gno].p[tmpset].data;
 
1634
        g[gno].p[tmpset] = p;
 
1635
        g[gno].p[tmpset].data = dstmp;
 
1636
 
 
1637
        set_set_hidden(gno, tmpset, FALSE);
 
1638
        if (setlength(gno, tmpset, plen) != RETURN_SUCCESS) {
 
1639
            /* should not happen */
 
1640
            return;
 
1641
        }
 
1642
        if (ds.s) {
 
1643
            g[gno].p[tmpset].data.s = xmalloc(plen*sizeof(char *));
 
1644
        }
 
1645
        
 
1646
        /* load the data into each column */
 
1647
        for (k = 0; k < ncols; k++) {
 
1648
            x = getcol(gno, tmpset, k);
 
1649
            for (j = 0; j < plen; j++) {
 
1650
                x[j] = ds.ex[k][i*lpart + j];
 
1651
            }
 
1652
        }
 
1653
        if (ds.s) {
 
1654
            for (j = 0; j < plen; j++) {
 
1655
                g[gno].p[tmpset].data.s[j] =
 
1656
                    copy_string(NULL, ds.s[i*lpart + j]);
 
1657
            }
 
1658
        }
 
1659
        
 
1660
        sprintf(s, "partition %d of set G%d.S%d", i + 1, gno, setno);
 
1661
        setcomment(gno, tmpset, s);
 
1662
    }
 
1663
    
 
1664
    free_set_data(&ds);
 
1665
}
 
1666
 
 
1667
/*
 
1668
 * drop points from an active set
 
1669
 */
 
1670
void do_drop_points(int gno, int setno, int startno, int endno)
 
1671
{
 
1672
    int setlength;
 
1673
    char buf[256];
 
1674
 
 
1675
    if (!is_set_active(gno, setno)) {
 
1676
        sprintf(buf, "Set %d not active", setno);
 
1677
        errmsg(buf);
 
1678
        return;
 
1679
    }
 
1680
 
 
1681
    setlength = getsetlength(gno, setno);
 
1682
    if (startno < 0) {
 
1683
        startno = setlength + 1 + startno;
 
1684
    }
 
1685
    if (endno < 0) {
 
1686
        endno = setlength + 1 + endno;
 
1687
    }
 
1688
 
 
1689
    if (startno > endno) {
 
1690
        iswap(&startno, &endno);
 
1691
    }
 
1692
 
 
1693
    if (startno < 0) {
 
1694
        errmsg("Start # < 0");
 
1695
        return;
 
1696
    }
 
1697
    if (endno >= setlength) {
 
1698
        errmsg("Ending # >= set length");
 
1699
        return;
 
1700
    }
 
1701
 
 
1702
    droppoints(gno, setno, startno, endno);
 
1703
}
 
1704
 
 
1705
 
 
1706
/*
 
1707
 * sort sets, only works on sets of type XY
 
1708
 */
 
1709
void do_sort(int setno, int sorton, int stype)
 
1710
{
 
1711
    int i, gno = get_cg();
 
1712
    char buf[256];
 
1713
 
 
1714
    if (setno == -1) {
 
1715
        for (i = 0; i < number_of_sets(gno); i++) {
 
1716
            if (is_set_active(gno, i)) {
 
1717
                sortset(gno, i, sorton, stype);
 
1718
            }
 
1719
        }
 
1720
    } else {
 
1721
        if (!is_set_active(gno, setno)) {
 
1722
            sprintf(buf, "Set %d not active", setno);
 
1723
            errmsg(buf);
 
1724
            return;
 
1725
        } else {
 
1726
            sortset(gno, setno, sorton, stype);
 
1727
        }
 
1728
    }
 
1729
}
 
1730
 
 
1731
 
 
1732
double setybase(int gno, int setno)
 
1733
{
 
1734
    double dummy, *y, ybase = 0.0;
 
1735
    int len;
 
1736
 
 
1737
    if (is_valid_setno(gno, setno) != TRUE) {
 
1738
        return 0.0;
 
1739
    }
 
1740
    
 
1741
    y = getcol(gno, setno, DATA_Y);
 
1742
    len = getsetlength(gno, setno);
 
1743
    
 
1744
    switch (g[gno].p[setno].baseline_type) {
 
1745
    case BASELINE_TYPE_0:
 
1746
        ybase = 0.0;
 
1747
        break;
 
1748
    case BASELINE_TYPE_SMIN:
 
1749
        ybase = vmin(y, len);
 
1750
        break;
 
1751
    case BASELINE_TYPE_SAVG:
 
1752
        stasum(y, len, &ybase, &dummy);
 
1753
        break;
 
1754
    case BASELINE_TYPE_SMAX:
 
1755
        ybase = vmax(y, len);
 
1756
        break;
 
1757
    case BASELINE_TYPE_GMIN:
 
1758
        ybase = g[gno].w.yg1;
 
1759
        break;
 
1760
    case BASELINE_TYPE_GMAX:
 
1761
        ybase = g[gno].w.yg2;
 
1762
        break;
 
1763
    default:
 
1764
        errmsg("Wrong type of baseline");
 
1765
    }
 
1766
    
 
1767
    return(ybase);
 
1768
}
 
1769
 
 
1770
 
 
1771
int dataset_cols(int gno, int setno)
 
1772
{
 
1773
    return settype_cols(dataset_type(gno, setno));
 
1774
}
 
1775
 
 
1776
int load_comments_to_legend(int gno, int setno)
 
1777
{
 
1778
    return set_legend_string(gno, setno, getcomment(gno, setno));
 
1779
}
 
1780
 
 
1781
int filter_set(int gno, int setno, char *rarray)
 
1782
{
 
1783
    int i, ip, j, ncols;
 
1784
    Dataset *dsp;
 
1785
    
 
1786
    if (is_valid_setno(gno, setno) != TRUE) {
 
1787
        return RETURN_FAILURE;
 
1788
    }
 
1789
    if (rarray == NULL) {
 
1790
        return RETURN_SUCCESS;
 
1791
    }
 
1792
    ncols = dataset_cols(gno, setno);
 
1793
    dsp = &(g[gno].p[setno].data);
 
1794
    ip = 0;
 
1795
    for (i = 0; i < dsp->len; i++) {
 
1796
        if (rarray[i]) {
 
1797
            for (j = 0; j < ncols; j++) {
 
1798
                dsp->ex[j][ip] = dsp->ex[j][i];
 
1799
            }
 
1800
            if (dsp->s != NULL) {
 
1801
                dsp->s[ip] = copy_string(dsp->s[ip], dsp->s[i]);
 
1802
            }
 
1803
            ip++;
 
1804
        }
 
1805
    }
 
1806
    setlength(gno, setno, ip);
 
1807
    return RETURN_SUCCESS;
 
1808
}