~ubuntu-branches/ubuntu/karmic/scilab/karmic

« back to all changes in this revision

Viewing changes to routines/graphics/gsort.h

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2002-03-21 16:57:43 UTC
  • Revision ID: james.westby@ubuntu.com-20020321165743-e9mv12c1tb1plztg
Tags: upstream-2.6
ImportĀ upstreamĀ versionĀ 2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*------------------------------------------------------------------------
 
2
 *    Graphic library
 
3
 *    Copyright (C) 1998-2000 Enpc/Jean-Philippe Chancelier
 
4
 *    jpc@cereve.enpc.fr 
 
5
 --------------------------------------------------------------------------*/
 
6
 
 
7
static void CNAME(ColSort,TYPE)();
 
8
static void CNAME(RowSort,TYPE)();
 
9
static void CNAME(GlobalSort,TYPE)();
 
10
static void CNAME(LexiRow,TYPE)();
 
11
static void CNAME(LexiCol,TYPE)();
 
12
static void CNAME(inita,TYPE)();
 
13
static void CNAME(afficher,TYPE)();
 
14
static void CNAME(sorttest,TYPE)();
 
15
 
 
16
 
 
17
/******************************************************
 
18
 * Generic code for Sorting Matrices a[i+n*j] 
 
19
 * This code is inserted in gsort.c 
 
20
 * with TYPE == double or type = int 
 
21
 ******************************************************/
 
22
 
 
23
static int CNAME(swapcode,TYPE)(parmi, parmj, n) 
 
24
     char *parmi,*parmj;
 
25
     int n;
 
26
{               
 
27
  int i = n;
 
28
  register TYPE *pi = (TYPE *) (parmi);                 
 
29
  register TYPE *pj = (TYPE *) (parmj); 
 
30
  do {                                          
 
31
    register TYPE t = *pi;              
 
32
    *pi++ = *pj;                                
 
33
    *pj++ = t;                          
 
34
  } while (--i > 0);                            
 
35
  return(0);
 
36
}
 
37
 
 
38
static int CNAME(compareC,TYPE)(i,j)
 
39
     char *i,*j;
 
40
{
 
41
  if ( *((TYPE *)i) > *((TYPE *)j))
 
42
    return (1);
 
43
  if ( *((TYPE *)i) < *((TYPE *)j))
 
44
    return (-1);
 
45
  return (0);
 
46
}
 
47
 
 
48
static int CNAME(compareD,TYPE)(i,j)
 
49
     char *i,*j;
 
50
{
 
51
  if ( *((TYPE *)i) < *((TYPE *)j))
 
52
    return (1);
 
53
  if ( *((TYPE *)i) > *((TYPE *)j))
 
54
    return (-1);
 
55
  return (0);
 
56
}
 
57
 
 
58
/******************************************************
 
59
 * Column sort of a matrix 
 
60
 ******************************************************/
 
61
 
 
62
static void CNAME(ColSort,TYPE)(a,ind,flag,n,p,dir)
 
63
     TYPE *a;
 
64
     int *ind;
 
65
     int flag,n,p;
 
66
     char dir;
 
67
{
 
68
  int i,j;
 
69
  if ( flag == 1) 
 
70
    {
 
71
      for ( j= 0 ; j < p ; j++ ) 
 
72
        {
 
73
          for ( i = 0 ; i < n ; i++) 
 
74
            ind[i+n*j]= i+1;
 
75
        }
 
76
    }
 
77
  for ( j= 0 ; j < p ; j++ ) 
 
78
    {
 
79
      sciqsort((char *) (a+n*j),(char *) (ind+n*j),flag, n, 
 
80
               sizeof(TYPE),sizeof(int), 
 
81
               (dir == 'c' ) ? CNAME(compareC,TYPE) : CNAME(compareD,TYPE),
 
82
               CNAME(swapcode,TYPE),swapcodeind);
 
83
    }
 
84
}
 
85
 
 
86
/******************************************************
 
87
 * Row sort of a matrix 
 
88
 ******************************************************/
 
89
 
 
90
static void CNAME(RowSort,TYPE)(a,ind,flag,n,p,dir)
 
91
     TYPE *a;
 
92
     int *ind;
 
93
     int n,p,flag;
 
94
     char dir;
 
95
{  
 
96
  int i,j;
 
97
  if ( flag == 1) 
 
98
    {
 
99
      for ( i = 0 ; i < n ; i++) 
 
100
        {
 
101
          for ( j= 0 ; j < p ; j++ ) 
 
102
            {
 
103
              ind[i+n*j]= j+1;
 
104
            }
 
105
        }
 
106
    }
 
107
  for ( i = 0 ; i < n ; i++) 
 
108
    {
 
109
      sciqsort((char *) (a+i),(char *) (ind+i),flag, p, 
 
110
               n*sizeof(TYPE),n*sizeof(int), 
 
111
               (dir == 'c' ) ? CNAME(compareC,TYPE):CNAME(compareD,TYPE),
 
112
               CNAME(swapcode,TYPE),swapcodeind);
 
113
    }
 
114
}
 
115
 
 
116
 
 
117
/******************************************************
 
118
 * Global sort of a Matrix
 
119
 ******************************************************/
 
120
 
 
121
static void CNAME(GlobalSort,TYPE)(a,ind,flag,n,p,dir)
 
122
     TYPE *a;
 
123
     int *ind;
 
124
     int n,p,flag;
 
125
     char dir;
 
126
{  
 
127
  int i,j;
 
128
  if ( flag == 1) 
 
129
    {
 
130
      for ( i = 0 ; i < n*p ; i++) 
 
131
        ind[i]= i+1;
 
132
    }
 
133
  sciqsort((char *) (a),(char *) (ind),flag, n*p, 
 
134
           sizeof(TYPE),sizeof(int), 
 
135
           (dir == 'c' ) ? CNAME(compareC,TYPE):CNAME(compareD,TYPE),
 
136
           CNAME(swapcode,TYPE),swapcodeind);
 
137
}
 
138
 
 
139
/*******************************************************
 
140
 *  lexicographic order with Rows ind is of size n
 
141
 *  ind gives the permutation of the rows which is applied 
 
142
 *  to sort them 
 
143
 ******************************************************/
 
144
 
 
145
static int CNAME(lexicols,TYPE) =1;
 
146
static int CNAME(lexirows,TYPE) =1;
 
147
 
 
148
static int CNAME(setLexiSize,TYPE)(n,p) 
 
149
     int p,n;
 
150
{
 
151
  CNAME(lexicols,TYPE) = p;
 
152
  CNAME(lexirows,TYPE) = n;
 
153
}
 
154
 
 
155
static  int CNAME(LexiRowcompareC,TYPE)(int *i, int *j)
 
156
{
 
157
  int jc;
 
158
  for ( jc = 0 ; jc < CNAME(lexicols,TYPE) ; jc++) 
 
159
    {
 
160
      if (*i > *j)
 
161
        return (1);
 
162
      if (*i < *j)
 
163
        return (-1);
 
164
      i += CNAME(lexirows,TYPE);
 
165
      j += CNAME(lexirows,TYPE);
 
166
    }
 
167
  return (0);
 
168
}
 
169
static  int CNAME(LexiRowcompareD,TYPE)(int *i, int *j)
 
170
{
 
171
  int jc;
 
172
  for ( jc = 0 ; jc < CNAME(lexicols,TYPE) ; jc++) 
 
173
    {
 
174
      if (*i < *j)
 
175
        return (1);
 
176
      if (*i > *j)
 
177
        return (-1);
 
178
      i += CNAME(lexirows,TYPE);
 
179
      j += CNAME(lexirows,TYPE);
 
180
    }
 
181
  return (0);
 
182
}
 
183
 
 
184
static int CNAME(LexiRowswapcode,TYPE)(parmi, parmj, n) 
 
185
     char *parmi,*parmj;
 
186
     int n;
 
187
{               
 
188
  int i = n,j;
 
189
  register TYPE *pi = (TYPE *) (parmi);                 
 
190
  register TYPE *pj = (TYPE *) (parmj); 
 
191
  if ( n!= 1) printf(" swapcode avec n != 1\n");
 
192
  do { 
 
193
    for ( j = 0 ; j < CNAME(lexicols,TYPE) ; j++) 
 
194
      {
 
195
        register TYPE t = *(pi +CNAME(lexirows,TYPE)*j);                
 
196
        *(pi + CNAME(lexirows,TYPE)*j) = *(pj+CNAME(lexirows,TYPE)*j);                          
 
197
        *(pj + CNAME(lexirows,TYPE)*j) = t;     
 
198
      }
 
199
    pi++;
 
200
    pj++;
 
201
  } while (--i > 0);                            
 
202
  return(0);
 
203
}
 
204
 
 
205
 
 
206
static void CNAME(LexiRow,TYPE)(a,ind,flag,n,p,dir)
 
207
     int *a,*ind;
 
208
     int n,p;
 
209
     char dir;
 
210
{
 
211
  int i,j;
 
212
  CNAME(setLexiSize,TYPE)(n,p);
 
213
  if ( flag == 1) 
 
214
    {
 
215
      for ( i = 0 ; i < n ; i++) 
 
216
          ind[i]= i+1;
 
217
    }
 
218
  sciqsort((char *) (a),(char *) (ind),flag, n, 
 
219
           sizeof(TYPE),sizeof(int), 
 
220
           (dir == 'c' ) ? CNAME(LexiRowcompareC,TYPE):CNAME(LexiRowcompareD,TYPE),
 
221
           CNAME(LexiRowswapcode,TYPE),swapcodeind);
 
222
}
 
223
 
 
224
/******************************************************
 
225
 *  lexicographic order with Cols ind is of size p
 
226
 *  ind gives the permutation of the column which is applied 
 
227
 *  to sort them 
 
228
 ******************************************************/
 
229
 
 
230
static  int CNAME(LexiColcompareC,TYPE)(i,j)
 
231
     TYPE *i,*j;
 
232
{
 
233
  int ic;
 
234
  for ( ic = 0 ; ic < CNAME(lexirows,TYPE) ; ic++) 
 
235
    {
 
236
      if (*i > *j)
 
237
        return (1);
 
238
      if (*i < *j)
 
239
        return (-1);
 
240
      i++;
 
241
      j++;
 
242
    }
 
243
  return (0);
 
244
}
 
245
static  int CNAME(LexiColcompareD,TYPE)(i,j)
 
246
     TYPE *i,*j;
 
247
{
 
248
  int ic;
 
249
  for ( ic = 0 ; ic < CNAME(lexirows,TYPE) ; ic++) 
 
250
    {
 
251
      if (*i < *j)
 
252
        return (1);
 
253
      if (*i > *j)
 
254
        return (-1);
 
255
      i++;
 
256
      j++;
 
257
    }
 
258
  return (0);
 
259
}
 
260
 
 
261
static int CNAME(LexiColswapcode,TYPE)(parmi, parmj, n) 
 
262
     char *parmi,*parmj;
 
263
     int n;
 
264
{               
 
265
  int i = n,ir;
 
266
  register TYPE *pi = (TYPE *) (parmi);                 
 
267
  register TYPE *pj = (TYPE *) (parmj); 
 
268
  if ( n!= 1) printf(" swapcode avec n != 1\n");
 
269
  do { 
 
270
    for ( ir = 0 ; ir < CNAME(lexirows,TYPE) ; ir++) 
 
271
      {
 
272
        register TYPE t = *(pi +ir);            
 
273
        *(pi +ir) = *(pj+ir);                           
 
274
        *(pj +ir) = t;  
 
275
      }
 
276
    pi += CNAME(lexirows,TYPE) ;
 
277
    pj += CNAME(lexirows,TYPE) ;
 
278
  } while (--i > 0);                            
 
279
  return(0);
 
280
}
 
281
 
 
282
 
 
283
static void CNAME(LexiCol,TYPE)(a,ind,flag,n,p,dir)
 
284
     TYPE *a;
 
285
     int *ind;
 
286
     int n,p;
 
287
     char dir;
 
288
{
 
289
  int i,j;
 
290
  CNAME(setLexiSize,TYPE)(n,p);
 
291
  if ( flag == 1) 
 
292
    {
 
293
      for ( i = 0 ; i < p ; i++) 
 
294
          ind[i]= i+1;
 
295
    }
 
296
  sciqsort((char *) (a),(char *) (ind),flag, p, 
 
297
           n*sizeof(TYPE),sizeof(int), 
 
298
           (dir == 'c' ) ? CNAME(LexiColcompareC,TYPE):CNAME(LexiColcompareD,TYPE),
 
299
           CNAME(LexiColswapcode,TYPE),
 
300
           swapcodeind);
 
301
}
 
302
 
 
303
 
 
304
 
 
305
#ifdef TEST 
 
306
 
 
307
#define N 2
 
308
#define P 2 
 
309
 
 
310
static TYPE CNAME(aa,TYPE)[4*4] = {4,4,1,1,6,7,2,1,3,4,5,2,9,8,7,6};
 
311
/*static TYPE aa[4*4] = {6,6,6,6,6,6,6,6,6,6,5,5,5,5,5,5}; */
 
312
 
 
313
static void CNAME(inita,TYPE)(a,n,p)
 
314
     TYPE *a;
 
315
     int n,p;
 
316
{
 
317
  int i;
 
318
  if ( n == 4 && p == 4  ) 
 
319
    for (i=0; i < n*p; i++) a[i]=CNAME(aa,TYPE)[i];
 
320
  else 
 
321
    for (i=0; i < n*p; i++) a[i]=n*p-i;
 
322
  CNAME(afficher,TYPE)(a,"a",n,p,sizeof(TYPE));
 
323
}
 
324
 
 
325
 
 
326
static void CNAME(afficher,TYPE)(a,name,n,p)
 
327
     char *name;
 
328
     TYPE *a;
 
329
     int n,p;
 
330
{
 
331
  int i,j;
 
332
  printf("%s=\n",name);
 
333
  for ( i = 0 ; i < n ; i++) 
 
334
    {
 
335
      for ( j= 0 ; j < p ; j++ ) 
 
336
        {
 
337
          printf("%4.2f ", a[i+N*j]);
 
338
        }
 
339
      printf("\n");
 
340
    }
 
341
}
 
342
 
 
343
 
 
344
static void CNAME(sorttest,TYPE)()
 
345
{
 
346
  TYPE a[N*P],b[N*P];
 
347
  int ind[N*P];
 
348
  int i,flag,j;
 
349
  int n=N,p=N;
 
350
  flag=1;
 
351
 
 
352
  /** Global sort example **/
 
353
  CNAME(inita,TYPE)(a,n,p) ;
 
354
  CNAME(GlobalSort,TYPE)(a,ind,flag,n,p,'c');
 
355
  CNAME(afficher,TYPE)(a,"glob a",n,p);
 
356
  afficherint(ind,"glob ind",n,p);
 
357
 
 
358
  /** Column sort example **/
 
359
  CNAME(inita,TYPE)(a,n,p) ;
 
360
  CNAME(ColSort,TYPE)(a,ind,flag,n,p,'c');
 
361
  CNAME(afficher,TYPE)(a,"col a",n,p);
 
362
  afficherint(ind,"col ind",n,p);
 
363
 
 
364
  /** Row sort example **/
 
365
  CNAME(inita,TYPE)(a,n,p) ;
 
366
  CNAME(RowSort,TYPE)(a,ind,flag,n,p,'c');
 
367
  CNAME(afficher,TYPE)(a,"row a",n,p);
 
368
  afficherint(ind,"row ind",n,p);
 
369
 
 
370
  /** Lexicographic Col sort **/
 
371
  CNAME(inita,TYPE)(a,n,p) ;
 
372
  CNAME(LexiCol,TYPE)(a,ind,flag,n,p,'c');
 
373
  CNAME(afficher,TYPE)(a,"lexico col a",n,p);
 
374
  afficherint(ind,"lexico col ind",1,p);
 
375
 
 
376
  /** Lexicographic Row sort **/
 
377
  CNAME(inita,TYPE)(a,n,p) ;
 
378
  CNAME(LexiRow,TYPE)(a,ind,flag,n,p,'c');
 
379
  CNAME(afficher,TYPE)(a,"lexico Row a",n,p);
 
380
  afficherint(ind,"lexico Row ind",n,1);
 
381
}
 
382
 
 
383
#endif
 
384
 
 
385
 
 
386
 
 
387
 
 
388