Add GNU LGPL headers to all .c .C and .h files
[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    for ( int i = 0; i < v_entries; i++ )
79       v_tbl[i] = i;
80
81 /*
82 MESSAGE(cerr, "atoi_pearson::init()");
83    for ( i = 0; i < v_entries;  i++ ) 
84      cerr << int(v_tbl[i]) << " ";
85    cerr << "\n";
86 */
87
88    int l;
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]);
92    }
93
94 /*
95 MESSAGE(cerr, "atoi_pearson::init()");
96    for ( i = 0; i < v_entries;  i++ ) 
97      cerr << int(v_tbl[i]) << " ";
98    cerr << "\n";
99 */
100 }
101
102 atoi_pearson::~atoi_pearson()
103 {
104    if ( v_shared == false )
105       delete v_tbl;
106 }
107
108 struct reg_t {
109 #ifdef MMDB_BIG_ENDIAN
110    unsigned b4: 8;
111    unsigned b3: 8;
112    unsigned b2: 8;
113    unsigned b1: 8;
114 #endif
115
116 #ifdef MMDB_LITTLE_ENDIAN
117    unsigned b1: 8;
118    unsigned b2: 8;
119    unsigned b3: 8;
120    unsigned b4: 8;
121 #endif
122 };
123
124 union u_tag {
125    struct reg_t chars_val;
126    unsigned int hash_val;
127 } ;
128
129 int atoi_pearson::atoi(const key_type& k, int offset) const
130 {
131    char* string = k.get();
132    int l = k.size();
133    return atoi((const char*)string, l, offset, 0);
134 }
135
136 int atoi_pearson::atoi(const char* string, int l, int offset, int rang ) const 
137 {
138    u_tag reg ;
139
140    reg.hash_val = 0;
141
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];
145
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];
149    }
150
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] ) ];
157       }
158
159    }
160
161
162
163    return (rang == 0 ) ? 
164              ( reg.hash_val % v_range ) 
165            : 
166              ( reg.hash_val % rang );
167
168 }
169
170 int atoi_pearson::atoi(const char* str, int offset, int rang ) const 
171 {
172    return atoi(str, strlen(str), offset, rang);
173 }
174
175 ostream& operator<<(ostream& s, atoi_pearson& p)
176 {
177    for ( int i = 0; i < p.v_entries ; i++ )
178       s << int(p.v_tbl[i]) << " ";
179    return s;
180 }
181
182 /* ########################################### */
183 /*                                             */
184 /* ########################################### */
185
186 /*
187 int atoi_sum::atoi(const char* string, int l) 
188 {
189 }
190
191 atoi_sum::size()
192 {
193 }
194
195 ostream& operator<<(ostream& s, atoi_sum& t)
196 {
197    return s;
198 }
199 */