Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / atoi_pearson.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_pearson.cc /main/6 1996/06/11 17:35:45 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/config.h"
47 #include "utility/atoi_pearson.h"
48
49 #define MASK 0xff
50
51 atoi_pearson::atoi_pearson(int r, int l): v_shared(false)
52 {
53    pm_random rdm_generator;
54    init(r, l, rdm_generator);
55 }
56
57 atoi_pearson::atoi_pearson(int r, int l, pm_random& rdm_generator) :
58    v_shared(false)
59 {
60    init(r, l, rdm_generator);
61 }
62
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)
66 {
67 }
68
69 void atoi_pearson::init(int r, int, pm_random& rdm_generator) 
70 {
71    v_entries = 256;
72    v_range = r; 
73    v_mask = 0xff;
74    v_no_bytes = 4;
75
76    v_tbl = new char[v_entries];
77
78    unsigned int i;
79    for ( i = 0; i < v_entries; i++ )
80       v_tbl[i] = i;
81
82 /*
83 MESSAGE(cerr, "atoi_pearson::init()");
84    for ( i = 0; i < v_entries;  i++ ) 
85      cerr << int(v_tbl[i]) << " ";
86    cerr << "\n";
87 */
88
89    int l;
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]);
93    }
94
95 /*
96 MESSAGE(cerr, "atoi_pearson::init()");
97    for ( i = 0; i < v_entries;  i++ ) 
98      cerr << int(v_tbl[i]) << " ";
99    cerr << "\n";
100 */
101 }
102
103 atoi_pearson::~atoi_pearson()
104 {
105    if ( v_shared == false )
106       delete v_tbl;
107 }
108
109 struct reg_t {
110 #ifdef MMDB_BIG_ENDIAN
111    unsigned b4: 8;
112    unsigned b3: 8;
113    unsigned b2: 8;
114    unsigned b1: 8;
115 #endif
116
117 #ifdef MMDB_LITTLE_ENDIAN
118    unsigned b1: 8;
119    unsigned b2: 8;
120    unsigned b3: 8;
121    unsigned b4: 8;
122 #endif
123 };
124
125 union u_tag {
126    struct reg_t chars_val;
127    unsigned int hash_val;
128 } ;
129
130 int atoi_pearson::atoi(const key_type& k, int offset) const
131 {
132    char* string = k.get();
133    int l = k.size();
134    return atoi((const char*)string, l, offset, 0);
135 }
136
137 int atoi_pearson::atoi(const char* string, int l, int offset, int rang ) const 
138 {
139    u_tag reg ;
140
141    reg.hash_val = 0;
142
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];
146
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];
150    }
151
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] ) ];
158       }
159
160    }
161
162
163
164    return (rang == 0 ) ? 
165              ( reg.hash_val % v_range ) 
166            : 
167              ( reg.hash_val % rang );
168
169 }
170
171 int atoi_pearson::atoi(const char* str, int offset, int rang ) const 
172 {
173    return atoi(str, strlen(str), offset, rang);
174 }
175
176 ostream& operator<<(ostream& s, atoi_pearson& p)
177 {
178    for ( unsigned int i = 0; i < p.v_entries ; i++ )
179       s << int(p.v_tbl[i]) << " ";
180    return s;
181 }
182
183 /* ########################################### */
184 /*                                             */
185 /* ########################################### */
186
187 /*
188 int atoi_sum::atoi(const char* string, int l) 
189 {
190 }
191
192 atoi_sum::size()
193 {
194 }
195
196 ostream& operator<<(ostream& s, atoi_sum& t)
197 {
198    return s;
199 }
200 */