1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
16 #include <Standard_Integer.hxx>
17 #include <Standard_ConstructionError.hxx>
18 #include <Standard_RangeError.hxx>
19 #include <Standard_Stream.hxx>
20 #include <Standard_OStream.hxx>
22 // ------------------------------------------------------------------
23 // CharToInt : Converts a character in an integer value
24 // ------------------------------------------------------------------
25 const Handle_Standard_Type& Standard_Integer_Type_()
27 static Handle_Standard_Type _aType = new
28 Standard_Type("Standard_Integer",sizeof(Standard_Integer),0,NULL);
33 Standard_Integer CharToInt(const Standard_Character me)
36 Standard_ConstructionError::Raise();
39 Standard_Character S[2];
45 // ------------------------------------------------------------------
46 // CharToInt : Converts a string in an integer value
47 // ------------------------------------------------------------------
49 Standard_Integer CharToInt(const Standard_CString me)
51 const Standard_Size Len = strlen(me);
52 for (Standard_Size I = 0; I < Len; I++)
53 if (!IsDigit(me[I])) {
54 Standard_ConstructionError::Raise();
59 // ------------------------------------------------------------------
60 // ShallowCopy : Copy of an integer
61 // ------------------------------------------------------------------
63 Standard_Integer ShallowCopy (const Standard_Integer me)
66 // ------------------------------------------------------------------
67 // ShallowDump : Writes an integer value
68 // ------------------------------------------------------------------
69 Standard_EXPORT void ShallowDump (const Standard_Integer Value, Standard_OStream& s)
70 { s << Value << " Standard_Integer" << "\n"; }
72 // ------------------------------------------------------------------
73 // NextPrime : Compute the first prime number greater or equal than an integer
74 // ------------------------------------------------------------------
78 long NextPrime (const long me )
81 struct svalue {int signiaib ;
84 struct svalue values[VALUESNBR] ;
89 // int signiaib[4] = { -1 , +1 , +1 , -1 } ;
105 #if defined (__alpha) || defined(DECOSF1)
111 Standard_RangeError::
112 Raise("Try to apply NextPrime method with negative, null or too large value.");
128 minn = ( me - 1 ) / 6 ; // n minimum
129 while ( 6*minn+1 < me ) {
133 maxia = long( sqrt((double ) me ) / 6 + 1 ) ;
135 maxvn = minn + VALUESNBR ;
138 for ( nindd = 0 ; nindd < VALUESNBR ; nindd++ ) {
139 if ( 6*(nindd+minn)-1 < me ) {
140 values[nindd].nbr = 1 ;
141 values[nindd].signiaib = -1 ;
145 values[nindd].nbr = 0 ;
146 values[nindd].signiaib = 0 ;
150 for ( ia = 1 ; ia <= maxia ; ia++ ) {
151 if ( nbvalues == VALUESNBR*2 ) {
154 remain = -VALUESNBR ;
155 ib[0] = ( minn + ia - remain ) / (6*ia - 1) ;
156 n[0] = int ( 6*ia*ib[0] - ia - ib[0] - minn ) ;
157 ib[1] = ( minn - ia - remain ) / (6*ia - 1) ;
158 n[1] = int ( 6*ia*ib[1] + ia - ib[1] - minn ) ;
159 ib[2] = ( minn + ia - remain ) / (6*ia + 1) ;
160 n[2] = int ( 6*ia*ib[2] - ia + ib[2] - minn ) ;
161 ib[3] = ( minn - ia - remain ) / (6*ia + 1) ;
162 n[3] = int ( 6*ia*ib[3] + ia + ib[3] - minn ) ;
163 for ( loop = 0 ; loop < 4 ; loop++ ) {
164 if ( n[loop] >= 0 && n[loop] < VALUESNBR ) {
165 if ( ( values[n[loop]].nbr == 0 ) ||
166 ( values[n[loop]].signiaib == signiaib[loop] ) ) {
167 values[n[loop]].signiaib = -signiaib[loop] ;
168 values[n[loop]].nbr += 1 ;
169 if ( values[n[loop]].nbr <= 2 )
175 for ( nindd = 0 ; nindd < VALUESNBR ; nindd++ ) {
176 if ( values[nindd].nbr == 0 ) {
177 if ( me <= 6*(nindd+minn)-1 ) {
178 premret = 6*(nindd+minn)-1 ;
181 else if ( me <= 6*(nindd+minn)+1 ) {
182 premret = 6*(nindd+minn)+1 ;
186 else if ( values[nindd].nbr == 1 ) {
187 if ( values[nindd].signiaib > 0 ) {
188 if ( me <= 6*(nindd+minn)-1 ) {
189 premret = 6*(nindd+minn)-1 ;
194 if ( me <= 6*(nindd+minn)+1 ) {
195 premret = 6*(nindd+minn)+1 ;
202 if ( premret != 0 ) {
206 return NextPrime ( 6*(maxvn-1)+2) ;
209 static const Standard_Integer Primes[] = {
210 101, 1009, 2003, 3001, 4001, 5003, 6007, 7001, 8009, 9001,
211 10007, 12007, 14009, 16007, 18013,
212 20011, 23003, 26003, 29009,
217 100019}; // catch the biggest
219 const Standard_Integer NbPrimes = 26; // does not include the biggest
221 Standard_Integer NextPrimeForMap(const Standard_Integer N)
224 for (i = 0; i < NbPrimes; i++)
225 if (Primes[i] > N) break;