7fd59977 |
1 | |
2 | #include <stdlib.h> |
3 | #include <Standard_Integer.hxx> |
4 | #include <Standard_ConstructionError.hxx> |
5 | #include <Standard_RangeError.hxx> |
6 | #ifndef _Standard_Stream_HeaderFile |
7 | #include <Standard_Stream.hxx> |
8 | #endif |
9 | #ifndef _Standard_OStream_HeaderFile |
10 | #include <Standard_OStream.hxx> |
11 | #endif |
12 | |
13 | // ------------------------------------------------------------------ |
14 | // CharToInt : Converts a character in an integer value |
15 | // ------------------------------------------------------------------ |
16 | Handle_Standard_Type& Standard_Integer_Type_() |
17 | { |
18 | static Handle_Standard_Type _aType = new |
19 | Standard_Type("Standard_Integer",sizeof(Standard_Integer),0,NULL); |
20 | |
21 | return _aType; |
22 | } |
23 | |
24 | Standard_Integer CharToInt(const Standard_Character me) |
25 | { |
26 | if (!IsDigit(me)) { |
27 | Standard_ConstructionError::Raise(); |
28 | } |
29 | |
30 | Standard_Character S[2]; |
31 | S[0] = me; |
32 | S[1] = 0; |
33 | return atoi(S); |
34 | } |
35 | |
36 | // ------------------------------------------------------------------ |
37 | // CharToInt : Converts a string in an integer value |
38 | // ------------------------------------------------------------------ |
39 | |
40 | Standard_Integer CharToInt(const Standard_CString me) |
41 | { |
42 | const Standard_Size Len = strlen(me); |
43 | for (Standard_Size I = 0; I < Len; I++) |
44 | if (!IsDigit(me[I])) { |
45 | Standard_ConstructionError::Raise(); |
46 | } |
47 | return atoi(me); |
48 | } |
49 | |
50 | // ------------------------------------------------------------------ |
51 | // ShallowCopy : Copy of an integer |
52 | // ------------------------------------------------------------------ |
53 | |
54 | Standard_Integer ShallowCopy (const Standard_Integer me) |
55 | { return me; } |
56 | |
57 | // ------------------------------------------------------------------ |
58 | // ShallowDump : Writes an integer value |
59 | // ------------------------------------------------------------------ |
60 | Standard_EXPORT void ShallowDump (const Standard_Integer Value, Standard_OStream& s) |
61 | { s << Value << " Standard_Integer" << "\n"; } |
62 | |
63 | // ------------------------------------------------------------------ |
64 | // NextPrime : Compute the first prime number greater or equal than an integer |
65 | // ------------------------------------------------------------------ |
66 | |
67 | #define VALUESNBR 4 |
68 | |
69 | long NextPrime (const long me ) |
70 | { |
71 | |
72 | struct svalue {int signiaib ; |
73 | int nbr ;} ; |
74 | |
75 | struct svalue values[VALUESNBR] ; |
76 | long ia ; |
77 | long maxia ; |
78 | long ib[4] ; |
79 | int n[4] ; |
80 | // int signiaib[4] = { -1 , +1 , +1 , -1 } ; |
81 | int signiaib[4]; |
82 | signiaib[0] = -1; |
83 | signiaib[1] = 1; |
84 | signiaib[2] = 1; |
85 | signiaib[3] = -1; |
86 | long remain ; |
87 | |
88 | int nbvalues ; |
89 | int loop ; |
90 | int nindd ; |
91 | long minn ; |
92 | long maxvn ; |
93 | long premret = 0 ; |
94 | |
95 | if (me < 0 || me > |
96 | #if defined (__alpha) || defined(DECOSF1) |
97 | 127149130704178201 |
98 | #else |
99 | 2147483647 |
100 | #endif |
101 | ){ |
102 | Standard_RangeError:: |
103 | Raise("Try to apply NextPrime method with negative, null or too large value."); |
104 | } |
105 | |
106 | if ( me <= 7 ) { |
107 | if ( me <= 1 ) |
108 | return 1 ; |
109 | else if ( me <= 2 ) |
110 | return 2 ; |
111 | else if ( me <= 3 ) |
112 | return 3 ; |
113 | else if ( me <= 5 ) |
114 | return 5 ; |
115 | else if ( me <= 7 ) |
116 | return 7 ; |
117 | } |
118 | |
119 | minn = ( me - 1 ) / 6 ; // n minimum |
120 | while ( 6*minn+1 < me ) { |
121 | minn += 1 ; |
122 | } |
123 | |
124 | maxia = long( sqrt((double ) me ) / 6 + 1 ) ; |
125 | |
126 | maxvn = minn + VALUESNBR ; |
127 | |
128 | nbvalues = 0 ; |
129 | for ( nindd = 0 ; nindd < VALUESNBR ; nindd++ ) { |
130 | if ( 6*(nindd+minn)-1 < me ) { |
131 | values[nindd].nbr = 1 ; |
132 | values[nindd].signiaib = -1 ; |
133 | nbvalues += 1 ; |
134 | } |
135 | else { |
136 | values[nindd].nbr = 0 ; |
137 | values[nindd].signiaib = 0 ; |
138 | } |
139 | } |
140 | |
141 | for ( ia = 1 ; ia <= maxia ; ia++ ) { |
142 | if ( nbvalues == VALUESNBR*2 ) { |
143 | break ; |
144 | } |
145 | remain = -VALUESNBR ; |
146 | ib[0] = ( minn + ia - remain ) / (6*ia - 1) ; |
147 | n[0] = int ( 6*ia*ib[0] - ia - ib[0] - minn ) ; |
148 | ib[1] = ( minn - ia - remain ) / (6*ia - 1) ; |
149 | n[1] = int ( 6*ia*ib[1] + ia - ib[1] - minn ) ; |
150 | ib[2] = ( minn + ia - remain ) / (6*ia + 1) ; |
151 | n[2] = int ( 6*ia*ib[2] - ia + ib[2] - minn ) ; |
152 | ib[3] = ( minn - ia - remain ) / (6*ia + 1) ; |
153 | n[3] = int ( 6*ia*ib[3] + ia + ib[3] - minn ) ; |
154 | for ( loop = 0 ; loop < 4 ; loop++ ) { |
155 | if ( n[loop] >= 0 && n[loop] < VALUESNBR ) { |
156 | if ( ( values[n[loop]].nbr == 0 ) || |
157 | ( values[n[loop]].signiaib == signiaib[loop] ) ) { |
158 | values[n[loop]].signiaib = -signiaib[loop] ; |
159 | values[n[loop]].nbr += 1 ; |
160 | if ( values[n[loop]].nbr <= 2 ) |
161 | nbvalues += 1 ; |
162 | } |
163 | } |
164 | } |
165 | } |
166 | for ( nindd = 0 ; nindd < VALUESNBR ; nindd++ ) { |
167 | if ( values[nindd].nbr == 0 ) { |
168 | if ( me <= 6*(nindd+minn)-1 ) { |
169 | premret = 6*(nindd+minn)-1 ; |
170 | break ; |
171 | } |
172 | else if ( me <= 6*(nindd+minn)+1 ) { |
173 | premret = 6*(nindd+minn)+1 ; |
174 | break ; |
175 | } |
176 | } |
177 | else if ( values[nindd].nbr == 1 ) { |
178 | if ( values[nindd].signiaib > 0 ) { |
179 | if ( me <= 6*(nindd+minn)-1 ) { |
180 | premret = 6*(nindd+minn)-1 ; |
181 | break ; |
182 | } |
183 | } |
184 | else { |
185 | if ( me <= 6*(nindd+minn)+1 ) { |
186 | premret = 6*(nindd+minn)+1 ; |
187 | break ; |
188 | } |
189 | } |
190 | } |
191 | } |
192 | |
193 | if ( premret != 0 ) { |
194 | return premret ; |
195 | } |
196 | |
197 | return NextPrime ( 6*(maxvn-1)+2) ; |
198 | |
199 | } |
200 | static const Standard_Integer Primes[] = { |
201 | 101, 1009, 2003, 3001, 4001, 5003, 6007, 7001, 8009, 9001, |
202 | 10007, 12007, 14009, 16007, 18013, |
203 | 20011, 23003, 26003, 29009, |
204 | 33013, 37003, |
205 | 41011, 46021, |
206 | 51001, 57037, |
207 | 65003, |
208 | 100019}; // catch the biggest |
209 | |
210 | const Standard_Integer NbPrimes = 26; // does not include the biggest |
211 | |
212 | Standard_Integer NextPrimeForMap(const Standard_Integer N) |
213 | { |
214 | Standard_Integer i; |
215 | for (i = 0; i < NbPrimes; i++) |
216 | if (Primes[i] > N) break; |
217 | return Primes[i]; |
218 | } |