FreeWRL/FreeX3D  3.0.0
sorter.cc
1 /*
2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9 **
10 ** http://oss.sgi.com/projects/FreeB
11 **
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17 **
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
23 **
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
33 */
34 
35 /*
36  * sorter.c++
37  *
38  */
39 
40 #include "glimports.h"
41 #include "sorter.h"
42 #include "mystdio.h"
43 
44 Sorter::Sorter( int _es )
45 {
46  es = _es;
47 }
48 
49 void
50 Sorter::qsort( void *a, int n )
51 {
52  qs1( (char *)a, ((char *)a)+n*es);
53 }
54 
55 int
56 Sorter::qscmp( char *, char * )
57 {
58  _glu_dprintf( "Sorter::qscmp: pure virtual called\n" );
59  return 0;
60 }
61 
62 
63 void
64 Sorter::qsexc( char *, char * )
65 {
66  _glu_dprintf( "Sorter::qsexc: pure virtual called\n" );
67 }
68 
69 
70 void
71 Sorter::qstexc( char *, char *, char * )
72 {
73  _glu_dprintf( "Sorter::qstexc: pure virtual called\n" );
74 }
75 
76 void
77 Sorter::qs1( char *a, char *l )
78 {
79  char *i, *j;
80  char *lp, *hp;
81  int c;
82  unsigned int n;
83 
84 start:
85  if((n=l-a) <= (unsigned int)es)
86  return;
87  n = es * (n / (2*es));
88  hp = lp = a+n;
89  i = a;
90  j = l-es;
91  while(1) {
92  if(i < lp) {
93  if((c = qscmp(i, lp)) == 0) {
94  qsexc(i, lp -= es);
95  continue;
96  }
97  if(c < 0) {
98  i += es;
99  continue;
100  }
101  }
102 
103 loop:
104  if(j > hp) {
105  if((c = qscmp(hp, j)) == 0) {
106  qsexc(hp += es, j);
107  goto loop;
108  }
109  if(c > 0) {
110  if(i == lp) {
111  qstexc(i, hp += es, j);
112  i = lp += es;
113  goto loop;
114  }
115  qsexc(i, j);
116  j -= es;
117  i += es;
118  continue;
119  }
120  j -= es;
121  goto loop;
122  }
123 
124  if(i == lp) {
125  if(lp-a >= l-hp) {
126  qs1(hp+es, l);
127  l = lp;
128  } else {
129  qs1(a, lp);
130  a = hp+es;
131  }
132  goto start;
133  }
134 
135  qstexc(j, lp -= es, i);
136  j = hp -= es;
137  }
138 }
139