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_pearson.cc /main/6 1996/06/11 17:35:45 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/config.h"
47 #include "utility/atoi_pearson.h"
51 atoi_pearson::atoi_pearson(int r, int l): v_shared(false)
53 pm_random rdm_generator;
54 init(r, l, rdm_generator);
57 atoi_pearson::atoi_pearson(int r, int l, pm_random& rdm_generator) :
60 init(r, l, rdm_generator);
63 atoi_pearson::atoi_pearson(int r, int, char* shared_tbl) :
64 v_entries(256), v_range(r), v_mask(0xff),
65 v_no_bytes(4), v_tbl(shared_tbl), v_shared(true)
69 void atoi_pearson::init(int r, int, pm_random& rdm_generator)
76 v_tbl = new char[v_entries];
78 for ( int i = 0; i < v_entries; i++ )
82 MESSAGE(cerr, "atoi_pearson::init()");
83 for ( i = 0; i < v_entries; i++ )
84 cerr << int(v_tbl[i]) << " ";
89 for ( i = 0; i < v_entries - 1; i++ ) {
90 l = rdm_generator.rand() % ( v_entries - i ) + i;
91 char_swap(v_tbl[l], v_tbl[i]);
95 MESSAGE(cerr, "atoi_pearson::init()");
96 for ( i = 0; i < v_entries; i++ )
97 cerr << int(v_tbl[i]) << " ";
102 atoi_pearson::~atoi_pearson()
104 if ( v_shared == false )
109 #ifdef MMDB_BIG_ENDIAN
116 #ifdef MMDB_LITTLE_ENDIAN
125 struct reg_t chars_val;
126 unsigned int hash_val;
129 int atoi_pearson::atoi(const key_type& k, int offset) const
131 char* string = k.get();
133 return atoi((const char*)string, l, offset, 0);
136 int atoi_pearson::atoi(const char* string, int l, int offset, int rang ) const
142 int x = string[0] + offset;
143 reg.chars_val.b1 = v_tbl[x & MASK];
144 reg.chars_val.b2 = v_tbl[(x+1) & MASK];
146 if ( v_range > 65535 ) {
147 reg.chars_val.b3 = v_tbl[(x + 2) & MASK];
148 reg.chars_val.b4 = v_tbl[(x + 3) & MASK];
151 for ( int j= 1; j<l; j++ ) {
152 reg.chars_val.b1 = v_tbl[ reg.chars_val.b1 ^ string[j] ];
153 reg.chars_val.b2 = v_tbl[ reg.chars_val.b2 ^ string[j] ];
154 if ( v_range > 65535 ) {
155 reg.chars_val.b3 = v_tbl[( reg.chars_val.b3 ^ string[j] ) ];
156 reg.chars_val.b4 = v_tbl[( reg.chars_val.b4 ^ string[j] ) ];
163 return (rang == 0 ) ?
164 ( reg.hash_val % v_range )
166 ( reg.hash_val % rang );
170 int atoi_pearson::atoi(const char* str, int offset, int rang ) const
172 return atoi(str, strlen(str), offset, rang);
175 ostream& operator<<(ostream& s, atoi_pearson& p)
177 for ( int i = 0; i < p.v_entries ; i++ )
178 s << int(p.v_tbl[i]) << " ";
182 /* ########################################### */
184 /* ########################################### */
187 int atoi_sum::atoi(const char* string, int l)
195 ostream& operator<<(ostream& s, atoi_sum& t)