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];
79 for ( i = 0; i < v_entries; i++ )
83 MESSAGE(cerr, "atoi_pearson::init()");
84 for ( i = 0; i < v_entries; i++ )
85 cerr << int(v_tbl[i]) << " ";
90 for ( i = 0; i < v_entries - 1; i++ ) {
91 l = rdm_generator.rand() % ( v_entries - i ) + i;
92 char_swap(v_tbl[l], v_tbl[i]);
96 MESSAGE(cerr, "atoi_pearson::init()");
97 for ( i = 0; i < v_entries; i++ )
98 cerr << int(v_tbl[i]) << " ";
103 atoi_pearson::~atoi_pearson()
105 if ( v_shared == false )
110 #ifdef MMDB_BIG_ENDIAN
117 #ifdef MMDB_LITTLE_ENDIAN
126 struct reg_t chars_val;
127 unsigned int hash_val;
130 int atoi_pearson::atoi(const key_type& k, int offset) const
132 char* string = k.get();
134 return atoi((const char*)string, l, offset, 0);
137 int atoi_pearson::atoi(const char* string, int l, int offset, int rang ) const
143 int x = string[0] + offset;
144 reg.chars_val.b1 = v_tbl[x & MASK];
145 reg.chars_val.b2 = v_tbl[(x+1) & MASK];
147 if ( v_range > 65535 ) {
148 reg.chars_val.b3 = v_tbl[(x + 2) & MASK];
149 reg.chars_val.b4 = v_tbl[(x + 3) & MASK];
152 for ( int j= 1; j<l; j++ ) {
153 reg.chars_val.b1 = v_tbl[ reg.chars_val.b1 ^ string[j] ];
154 reg.chars_val.b2 = v_tbl[ reg.chars_val.b2 ^ string[j] ];
155 if ( v_range > 65535 ) {
156 reg.chars_val.b3 = v_tbl[( reg.chars_val.b3 ^ string[j] ) ];
157 reg.chars_val.b4 = v_tbl[( reg.chars_val.b4 ^ string[j] ) ];
164 return (rang == 0 ) ?
165 ( reg.hash_val % v_range )
167 ( reg.hash_val % rang );
171 int atoi_pearson::atoi(const char* str, int offset, int rang ) const
173 return atoi(str, strlen(str), offset, rang);
176 ostream& operator<<(ostream& s, atoi_pearson& p)
178 for ( unsigned int i = 0; i < p.v_entries ; i++ )
179 s << int(p.v_tbl[i]) << " ";
183 /* ########################################### */
185 /* ########################################### */
188 int atoi_sum::atoi(const char* string, int l)
196 ostream& operator<<(ostream& s, atoi_sum& t)