File: hdiff_dim.c

package info (click to toggle)
libhdf4 4.2.10-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 25,488 kB
  • ctags: 12,954
  • sloc: ansic: 146,962; sh: 14,905; fortran: 12,480; makefile: 922; yacc: 680; pascal: 418; lex: 170; csh: 41
file content (537 lines) | stat: -rw-r--r-- 15,544 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at      *
 * http://hdfgroup.org/products/hdf4/doc/Copyright.html.  If you do not have *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */



#include <assert.h>
#include "hdf.h"
#include "mfhdf.h"
#include "hdiff_dim.h"


/* match name between 2 diff_dim_table_t type lists */
typedef struct diff_match_dim_name_t {
    int32 ref;                    /* reference */
    char  dim_name[H4_MAX_NC_NAME];  /* name */
    int   flags[2];               /* name exists 1, no 0 */  
} diff_match_dim_name_t;

/* table for diff_match_dim_name_t */
typedef struct diff_match_dim_table_t {
    int        size;
    int        nobjs;
    diff_match_dim_name_t *objs;
} diff_match_dim_table_t;

/*-------------------------------------------------------------------------
 * local prototypes
 *-------------------------------------------------------------------------
 */

/* methods for diff_match_dim_table_t */
static void diff_match_dim_table_free( diff_match_dim_table_t *table );
static void diff_match_dim_table_init( diff_match_dim_table_t **tbl );
static void diff_match_dim_table_add ( diff_match_dim_table_t *table, unsigned *flags, char* dim_name, int32 ref);


/*-------------------------------------------------------------------------
 * Function: diff_match_dim
 *
 * Purpose: diff "lone" dimensions. 
 *  Find common dimension names; the algorithm used for this search is the 
 *  cosequential match algorithm and is described in 
 *  Folk, Michael; Zoellick, Bill. (1992). File Structures. Addison-Wesley.
 *
 * Return: void
 *
 * Programmer: Pedro Vicente Nunes, pvn@hdfgroup.org
 *
 * Date: July 16, 2007
 *
 *-------------------------------------------------------------------------
 */
uint32 diff_match_dim(int32 sd1_id,
                      int32 sd2_id,
                      diff_dim_table_t *td1_1,
                      diff_dim_table_t *td1_2,
                      diff_dim_table_t *td2_1,
                      diff_dim_table_t *td2_2,
                      diff_opt_t * opt)
{
    int                    cmp;
    int                    more_names_exist;
    int                    curr1;
    int                    curr2;
    diff_match_dim_table_t *mattbl_file1=NULL;
    diff_match_dim_table_t *mattbl_file2=NULL;
    unsigned               inlist[2]; 
    int                    i;
    uint32                 nfound = 0;
    
#if defined (HDIFF_DEBUG)
    for (i = 0; i < td1_1->nobjs; i++) 
    {
        printf("%s\n", td1_1->objs[i].dim_name);
    }
    for (i = 0; i < td1_2->nobjs; i++) 
    {
        printf("%s\n", td1_2->objs[i].dim_name);
    }
#endif
    
    
   /*-------------------------------------------------------------------------
    * build the list for file 1
    *-------------------------------------------------------------------------
    */
    
    
    more_names_exist = (td1_1->nobjs>0 && td1_2->nobjs>0) ? 1 : 0;
    curr1=0;
    curr2=0;
    
    diff_match_dim_table_init( &mattbl_file1 );
    
    while ( more_names_exist )
    {
        cmp = strcmp( td1_1->objs[curr1].dim_name, td1_2->objs[curr2].dim_name );
        if ( cmp == 0 )
        {
            inlist[0]=1; inlist[1]=1;
            diff_match_dim_table_add(mattbl_file1,inlist,
                td1_1->objs[curr1].dim_name,
                td1_1->objs[curr1].ref);
            
            curr1++;
            curr2++;
        }
        else if ( cmp < 0 )
        {
            inlist[0]=1; inlist[1]=0;
            diff_match_dim_table_add(mattbl_file1,inlist,
                td1_1->objs[curr1].dim_name,
                td1_1->objs[curr1].ref);
            curr1++;
        }
        else 
        {
            inlist[0]=0; inlist[1]=1;
            diff_match_dim_table_add(mattbl_file1,inlist,
                td1_2->objs[curr2].dim_name,
                td1_2->objs[curr2].ref);
            curr2++;
        }
        
        more_names_exist = (curr1<td1_1->nobjs && curr2<td1_1->nobjs) ? 1 : 0;
        
        
    } /* end while */
    
    /* td1_1 did not end */
    if (curr1<td1_1->nobjs)
    {
        while ( curr1<td1_1->nobjs )
        {
            inlist[0]=1; inlist[1]=0;
            diff_match_dim_table_add(mattbl_file1,inlist,
                td1_1->objs[curr1].dim_name,
                td1_1->objs[curr1].ref);
            curr1++;
        }
    }
    
    /* td1_2 did not end */
    if (curr2<td1_2->nobjs)
    {
        while ( curr2<td1_2->nobjs )
        {
            inlist[0]=0; inlist[1]=1;
            diff_match_dim_table_add(mattbl_file1,inlist,
                td1_2->objs[curr2].dim_name,
                td1_2->objs[curr2].ref);
            curr2++;
        }
    }
    
   /*-------------------------------------------------------------------------
    * print the list
    *-------------------------------------------------------------------------
    */
#if defined (HDIFF_DEBUG)
    {
        char     c1, c2;
        printf("---------------------------------------\n");
        printf("list1     list2\n");
        printf("---------------------------------------\n");
        for (i = 0; i < mattbl_file1->nobjs; i++)
        {
            c1 = (char)((mattbl_file1->objs[i].flags[0]) ? 'x' : ' ');
            c2 = (char)((mattbl_file1->objs[i].flags[1]) ? 'x' : ' ');
            printf("%5c %6c    %-15s\n", c1, c2, mattbl_file1->objs[i].dim_name);
        }
        printf("\n");
    }
#endif
    
    
   /*-------------------------------------------------------------------------
    * build the list for file 2
    *-------------------------------------------------------------------------
    */
      
    more_names_exist = (td2_1->nobjs>0 && td2_2->nobjs>0) ? 1 : 0;
    curr1=0;
    curr2=0;
    
    diff_match_dim_table_init( &mattbl_file2 );
    
    while ( more_names_exist )
    {
        cmp = strcmp( td2_1->objs[curr1].dim_name, td2_2->objs[curr2].dim_name );
        if ( cmp == 0 )
        {
            inlist[0]=1; inlist[1]=1;
            diff_match_dim_table_add(mattbl_file2,inlist,
                td2_1->objs[curr1].dim_name,
                td2_1->objs[curr1].ref);
            
            curr1++;
            curr2++;
        }
        else if ( cmp < 0 )
        {
            inlist[0]=1; inlist[1]=0;
            diff_match_dim_table_add(mattbl_file2,inlist,
                td2_1->objs[curr1].dim_name,
                td2_1->objs[curr1].ref);
            curr1++;
        }
        else 
        {
            inlist[0]=0; inlist[1]=1;
            diff_match_dim_table_add(mattbl_file2,inlist,
                td2_2->objs[curr2].dim_name,
                td2_2->objs[curr2].ref);
            curr2++;
        }
        
        more_names_exist = (curr1<td2_1->nobjs && curr2<td2_1->nobjs) ? 1 : 0;
        
        
    } /* end while */
    
    /* td2_1 did not end */
    if (curr1<td2_1->nobjs)
    {
        while ( curr1<td2_1->nobjs )
        {
            inlist[0]=1; inlist[1]=0;
            diff_match_dim_table_add(mattbl_file2,inlist,
                td2_1->objs[curr1].dim_name,
                td2_1->objs[curr1].ref);
            curr1++;
        }
    }
    
    /* td2_2 did not end */
    if (curr2<td2_2->nobjs)
    {
        while ( curr2<td2_2->nobjs )
        {
            inlist[0]=0; inlist[1]=1;
            diff_match_dim_table_add(mattbl_file2,inlist,
                td2_2->objs[curr2].dim_name,
                td2_2->objs[curr2].ref);
            curr2++;
        }
    }
    
   /*-------------------------------------------------------------------------
    * print the list
    *-------------------------------------------------------------------------
    */
#if defined (HDIFF_DEBUG)
    {
        char     c1, c2;
        printf("---------------------------------------\n");
        printf("list1     list2\n");
        printf("---------------------------------------\n");
        for (i = 0; i < mattbl_file2->nobjs; i++)
        {
            c1 = (char)((mattbl_file2->objs[i].flags[0]) ? 'x' : ' ');
            c2 = (char)((mattbl_file2->objs[i].flags[1]) ? 'x' : ' ');
            printf("%5c %6c    %-15s\n", c1, c2, mattbl_file2->objs[i].dim_name);
        }
        printf("\n");
    }
#endif
       
   /*-------------------------------------------------------------------------
    * get objects from list1 not in list2
    *-------------------------------------------------------------------------
    */
    
    for (i = 0; i < mattbl_file1->nobjs; i++)
    {
        if ( mattbl_file1->objs[i].flags[0] && ( ! mattbl_file1->objs[i].flags[1] ) )
        {
            int j;
            
            /* search in file 2 for this dimension */
            for (j = 0; j < mattbl_file2->nobjs; j++)
            {
                
                /* same name */
                if ( strcmp( mattbl_file1->objs[i].dim_name, mattbl_file2->objs[j].dim_name ) == 0 )
                {
                    
                    /* and also a lone dim in file 2 */
                    if ( mattbl_file2->objs[j].flags[0] && ( ! mattbl_file2->objs[j].flags[1] ) )
                    {
                        
                        int32 ref1 = mattbl_file1->objs[i].ref;
                        int32 ref2 = mattbl_file2->objs[j].ref;
                        
                        nfound+=diff_sds(sd1_id,
                            sd2_id,
                            ref1,
                            ref2,
                            opt);
                        
                        
                    }
                    
                    
                    
                }
                
                
            }
            
        
        }
    
    }
    
    
    /* free tables */
    diff_match_dim_table_free(mattbl_file1);
    diff_match_dim_table_free(mattbl_file2);
    
    return nfound;
}





/*-------------------------------------------------------------------------
 * Function: diff_match_dim_table_add
 *
 * Purpose: add an entry from a list of dimension names into the match table 
 *
 * Programmer: Pedro Vicente, pvn@hdfgroup.org
 *
 * Date: July 16, 2007
 *
 *-------------------------------------------------------------------------
 */

static 
void diff_match_dim_table_add (diff_match_dim_table_t *table, 
                               unsigned *flags, 
                               char* dim_name, 
                               int32 ref)
{
    int i;
    
    /* check if name already on match table */
    for (i = 0; i < table->nobjs; i++) 
    {
        /* insert information at position i */
        if ( strcmp(dim_name, table->objs[i].dim_name)==0 )
        {
            if (table->objs[i].flags[0] == 0)
                table->objs[i].flags[0] = flags[0];
            if (table->objs[i].flags[1] == 0)
                table->objs[i].flags[1] = flags[1];
            return;
        }
    }
    
    
    if (table->nobjs == table->size) 
    {
        table->size *= 2;
        table->objs = (diff_match_dim_name_t*)realloc(table->objs, table->size * sizeof(diff_match_dim_name_t));
        
        for (i = table->nobjs; i < table->size; i++) {
            table->objs[i].ref = -1;
            table->objs[i].flags[0] = table->objs[i].flags[1] = -1;
        }
    }
    
    i = table->nobjs++;
    table->objs[i].ref = ref;
    strcpy(table->objs[i].dim_name,dim_name);
    table->objs[i].flags[0] = flags[0];
    table->objs[i].flags[1] = flags[1];
}


/*-------------------------------------------------------------------------
 * Function: diff_match_dim_table_init
 *
 * Purpose: initialize match table
 *
 * Return: void
 *
 * Programmer: Pedro Vicente, pvn@hdfgroup.org
 *
 * Date: July 16, 2007
 *
 *-------------------------------------------------------------------------
 */

static 
void diff_match_dim_table_init( diff_match_dim_table_t **tbl )
{
    int i;
    diff_match_dim_table_t *table = (diff_match_dim_table_t*) malloc(sizeof(diff_match_dim_table_t));
    
    table->size = 20;
    table->nobjs = 0;
    table->objs = (diff_match_dim_name_t*) malloc(table->size * sizeof(diff_match_dim_name_t));
    
    for (i = 0; i < table->size; i++) {
        table->objs[i].ref = -1;
        table->objs[i].flags[0] = table->objs[i].flags[1] = -1;
    }
    
    *tbl = table;
}



/*-------------------------------------------------------------------------
 * Function: diff_match_dim_table_free
 *
 * Purpose: free match table 
 *
 * Return: void
 *
 * Programmer: Pedro Vicente, pvn@hdfgroup.org
 *
 * Date: July 16, 2007
 *
 *-------------------------------------------------------------------------
 */

static 
void diff_match_dim_table_free( diff_match_dim_table_t *table )
{
    free(table->objs);
    free(table);
}



/*-------------------------------------------------------------------------
 * Function: diff_dim_table_add
 *
 * Purpose: add an entry of pair REF/NAME into a dimension table
 *
 * Return: void
 *
 * Programmer: Pedro Vicente, pvn@hdfgroup.org
 *
 * Date: July 16, 2007
 *
 *-------------------------------------------------------------------------
 */

void diff_dim_table_add(diff_dim_table_t *table, 
                        int ref, 
                        char* name)
{
    int i;
    
    assert(table);
    
    if (table->nobjs == table->size) 
    {
        table->size *= 2;
        table->objs = (diff_dim_name_t*)realloc(table->objs, table->size * sizeof(diff_dim_name_t));
        
        for (i = table->nobjs; i < table->size; i++) {
            table->objs[i].ref = -1;
        }
    }
    
    i = table->nobjs++;
    table->objs[i].ref = ref;
    strcpy(table->objs[i].dim_name,name);
}



/*-------------------------------------------------------------------------
 * Function: diff_dim_table_init
 *
 * Purpose: initialize dimension table
 *
 * Return: void
 *
 * Programmer: Pedro Vicente, pvn@hdfgroup.org
 *
 * Date: July 16, 2007
 *
 *-------------------------------------------------------------------------
 */

void diff_dim_table_init( diff_dim_table_t **tbl )
{
    int i;
    diff_dim_table_t* table = (diff_dim_table_t*) malloc(sizeof(diff_dim_table_t));
    
    table->size = 20;
    table->nobjs = 0;
    table->objs = (diff_dim_name_t*) malloc(table->size * sizeof(diff_dim_name_t));
    
    for (i = 0; i < table->size; i++) {
        table->objs[i].ref = -1;
    }
    
    *tbl = table;
}

/*-------------------------------------------------------------------------
 * Function: diff_dim_table_free
 *
 * Purpose: free dimension table 
 *
 * Return: void
 *
 * Programmer: Pedro Vicente, pvn@hdfgroup.org
 *
 * Date: July 16, 2007
 *
 *-------------------------------------------------------------------------
 */

void diff_dim_table_free( diff_dim_table_t *table )
{
    free(table->objs);
    free(table);
}