Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / atoi_fast.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $XConsortium: atoi_fast.cc /main/3 1996/06/11 17:35:24 cde-hal $
25  *
26  * Copyright (c) 1992 HaL Computer Systems, Inc.  All rights reserved.
27  * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
28  * States.  Use of a copyright notice is precautionary only and does not
29  * imply publication or disclosure.
30  * 
31  * This software contains confidential information and trade secrets of HaL
32  * Computer Systems, Inc.  Use, disclosure, or reproduction is prohibited
33  * without the prior express written permission of HaL Computer Systems, Inc.
34  * 
35  *                         RESTRICTED RIGHTS LEGEND
36  * Use, duplication, or disclosure by the Government is subject to
37  * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
38  * Technical Data and Computer Software clause at DFARS 252.227-7013.
39  *                        HaL Computer Systems, Inc.
40  *                  1315 Dell Avenue, Campbell, CA  95008
41  * 
42  */
43
44
45
46 #include "utility/atoi_fast.h"
47
48 #define MASK 0xff
49
50 atoi_fast::atoi_fast(int r, int l)
51 {
52    pm_random rdm_generator;
53    init(r, l, rdm_generator);
54 }
55
56 atoi_fast::atoi_fast(int r, int l, pm_random& rdm_generator) 
57 {
58    init(r, l, rdm_generator);
59 }
60
61 void atoi_fast::init(int r, int, pm_random& rdm_generator) 
62 {
63    v_entries = 256;
64    v_range = r; 
65    v_mask = 0;
66
67    v_tbl = new char[v_entries];
68
69    unsigned int i;
70    for ( unsigned i = 0; i < v_entries; i++ )
71      v_tbl[i] = i;
72
73    for ( i = 0; i < v_entries - 1;  i++ ) {
74       char_swap(v_tbl[rdm_generator.rand() % ( v_entries - i ) + i], 
75                 v_tbl[i]
76                );
77    }
78
79    v_no_bytes = 4;
80 }
81
82 atoi_fast::~atoi_fast()
83 {
84    delete v_tbl;
85 }
86
87 struct reg_t {
88    unsigned b4: 8;
89    unsigned b3: 8;
90    unsigned b2: 8;
91    unsigned b1: 8;
92 };
93
94 union u_tag {
95    struct reg_t chars_val;
96    unsigned int hash_val;
97 };
98
99 int atoi_fast::atoi(const key_type& k, int offset) const
100 {
101    char* string = k.get();
102    int l = k.size();
103    return atoi((const char*)string, l, offset, 0);
104 }
105
106 int atoi_fast::atoi(const char* string, int l, int , int rang ) const 
107 {
108    u_tag reg ;
109   
110    reg.hash_val = 0;
111
112    switch (l) {
113      case 0:
114          break;
115      case 1:
116          reg.chars_val.b1 = v_tbl[(unsigned int)string[0]] ;
117          break;
118      case 2:
119          reg.chars_val.b1 = v_tbl[(unsigned int)string[0]] ;
120          reg.chars_val.b2 = v_tbl[(unsigned int)string[1]] ;
121          break;
122      case 3:
123          reg.chars_val.b1 = v_tbl[(unsigned int)string[0]] ;
124          reg.chars_val.b2 = v_tbl[(unsigned int)string[1]] ;
125          reg.chars_val.b3 = v_tbl[(unsigned int)string[2]] ;
126          break;
127      default:
128          reg.chars_val.b1 = v_tbl[(unsigned int)string[0]] ;
129          reg.chars_val.b2 = v_tbl[(unsigned int)string[1]] ;
130          reg.chars_val.b3 = v_tbl[(unsigned int)string[2]] ;
131          reg.chars_val.b4 = v_tbl[(unsigned int)string[3]] ;
132    }
133
134    int x = 0;
135    for ( int j=1; j<l; j++ ) {
136       x ^= string[j];
137    }
138
139    reg.chars_val.b1 |= x;
140    reg.chars_val.b2 |= x;
141
142       reg.chars_val.b3 |= x;
143       reg.chars_val.b4 |= x;
144 /*
145    if ( ( rang == 0 && v_range > 65535 ) ||
146         ( rang > 65535 ) 
147       )
148    {
149       reg.chars_val.b3 |= x;
150       reg.chars_val.b4 |= x;
151    }
152 */
153
154 /*
155    int x = string[0]+offset;
156    reg.chars_val.b1 = x ;
157    reg.chars_val.b2 = x + 1;
158
159    if ( v_range > 65535 ) {
160       reg.chars_val.b3 = x+2;
161       reg.chars_val.b4 = x+3;
162    }
163
164    for ( int j=1; j<l; j++ ) {
165
166       reg.chars_val.b1 += string[j];
167       reg.chars_val.b2 += string[j];
168
169       if ( v_range > 65535 ) {
170          reg.chars_val.b3 += string[j];
171          reg.chars_val.b4 += string[j];
172       }
173    }
174 */
175
176    return (rang == 0 ) ? 
177              ( reg.hash_val % v_range ) 
178            : 
179              ( reg.hash_val % rang );
180
181 }
182
183 int atoi_fast::atoi(const char* str, int offset, int rang ) const 
184 {
185    return atoi(str, strlen(str), offset, rang);
186 }
187
188 ostream& operator<<(ostream& s, atoi_fast& p)
189 {
190    for ( unsigned int i = 0; i < p.v_entries ; i++ )
191       s << int(p.v_tbl[i]) << " ";
192    return s;
193 }
194