2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
24 * $XConsortium: atoi_fast.cc /main/3 1996/06/11 17:35:24 cde-hal $
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.
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.
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
46 #include "utility/atoi_fast.h"
50 atoi_fast::atoi_fast(int r, int l)
52 pm_random rdm_generator;
53 init(r, l, rdm_generator);
56 atoi_fast::atoi_fast(int r, int l, pm_random& rdm_generator)
58 init(r, l, rdm_generator);
61 void atoi_fast::init(int r, int, pm_random& rdm_generator)
67 v_tbl = new char[v_entries];
70 for ( unsigned i = 0; i < v_entries; i++ )
73 for ( i = 0; i < v_entries - 1; i++ ) {
74 char_swap(v_tbl[rdm_generator.rand() % ( v_entries - i ) + i],
82 atoi_fast::~atoi_fast()
95 struct reg_t chars_val;
96 unsigned int hash_val;
99 int atoi_fast::atoi(const key_type& k, int offset) const
101 char* string = k.get();
103 return atoi((const char*)string, l, offset, 0);
106 int atoi_fast::atoi(const char* string, int l, int , int rang ) const
116 reg.chars_val.b1 = v_tbl[(unsigned int)string[0]] ;
119 reg.chars_val.b1 = v_tbl[(unsigned int)string[0]] ;
120 reg.chars_val.b2 = v_tbl[(unsigned int)string[1]] ;
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]] ;
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]] ;
135 for ( int j=1; j<l; j++ ) {
139 reg.chars_val.b1 |= x;
140 reg.chars_val.b2 |= x;
142 reg.chars_val.b3 |= x;
143 reg.chars_val.b4 |= x;
145 if ( ( rang == 0 && v_range > 65535 ) ||
149 reg.chars_val.b3 |= x;
150 reg.chars_val.b4 |= x;
155 int x = string[0]+offset;
156 reg.chars_val.b1 = x ;
157 reg.chars_val.b2 = x + 1;
159 if ( v_range > 65535 ) {
160 reg.chars_val.b3 = x+2;
161 reg.chars_val.b4 = x+3;
164 for ( int j=1; j<l; j++ ) {
166 reg.chars_val.b1 += string[j];
167 reg.chars_val.b2 += string[j];
169 if ( v_range > 65535 ) {
170 reg.chars_val.b3 += string[j];
171 reg.chars_val.b4 += string[j];
176 return (rang == 0 ) ?
177 ( reg.hash_val % v_range )
179 ( reg.hash_val % rang );
183 int atoi_fast::atoi(const char* str, int offset, int rang ) const
185 return atoi(str, strlen(str), offset, rang);
188 ostream& operator<<(ostream& s, atoi_fast& p)
190 for ( unsigned int i = 0; i < p.v_entries ; i++ )
191 s << int(p.v_tbl[i]) << " ";