atoi_pearson.C 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 libraries 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. #include "utility/config.h"
  44. #include "utility/atoi_pearson.h"
  45. #define MASK 0xff
  46. atoi_pearson::atoi_pearson(int r, int l): v_shared(false)
  47. {
  48. pm_random rdm_generator;
  49. init(r, l, rdm_generator);
  50. }
  51. atoi_pearson::atoi_pearson(int r, int l, pm_random& rdm_generator) :
  52. v_shared(false)
  53. {
  54. init(r, l, rdm_generator);
  55. }
  56. atoi_pearson::atoi_pearson(int r, int, char* shared_tbl) :
  57. v_entries(256), v_range(r), v_mask(0xff),
  58. v_no_bytes(4), v_tbl(shared_tbl), v_shared(true)
  59. {
  60. }
  61. void atoi_pearson::init(int r, int, pm_random& rdm_generator)
  62. {
  63. v_entries = 256;
  64. v_range = r;
  65. v_mask = 0xff;
  66. v_no_bytes = 4;
  67. v_tbl = new char[v_entries];
  68. unsigned int i;
  69. for ( i = 0; i < v_entries; i++ )
  70. v_tbl[i] = i;
  71. /*
  72. MESSAGE(cerr, "atoi_pearson::init()");
  73. for ( i = 0; i < v_entries; i++ )
  74. cerr << int(v_tbl[i]) << " ";
  75. cerr << "\n";
  76. */
  77. int l;
  78. for ( i = 0; i < v_entries - 1; i++ ) {
  79. l = rdm_generator.rand() % ( v_entries - i ) + i;
  80. char_swap(v_tbl[l], v_tbl[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. atoi_pearson::~atoi_pearson()
  90. {
  91. if ( v_shared == false )
  92. delete v_tbl;
  93. }
  94. struct reg_t {
  95. #ifdef MMDB_BIG_ENDIAN
  96. unsigned b4: 8;
  97. unsigned b3: 8;
  98. unsigned b2: 8;
  99. unsigned b1: 8;
  100. #endif
  101. #ifdef MMDB_LITTLE_ENDIAN
  102. unsigned b1: 8;
  103. unsigned b2: 8;
  104. unsigned b3: 8;
  105. unsigned b4: 8;
  106. #endif
  107. };
  108. union u_tag {
  109. struct reg_t chars_val;
  110. unsigned int hash_val;
  111. } ;
  112. int atoi_pearson::atoi(const key_type& k, int offset) const
  113. {
  114. char* string = k.get();
  115. int l = k.size();
  116. return atoi((const char*)string, l, offset, 0);
  117. }
  118. int atoi_pearson::atoi(const char* string, int l, int offset, int rang ) const
  119. {
  120. u_tag reg ;
  121. reg.hash_val = 0;
  122. int x = string[0] + offset;
  123. reg.chars_val.b1 = v_tbl[x & MASK];
  124. reg.chars_val.b2 = v_tbl[(x+1) & MASK];
  125. if ( v_range > 65535 ) {
  126. reg.chars_val.b3 = v_tbl[(x + 2) & MASK];
  127. reg.chars_val.b4 = v_tbl[(x + 3) & MASK];
  128. }
  129. for ( int j= 1; j<l; j++ ) {
  130. reg.chars_val.b1 = v_tbl[ reg.chars_val.b1 ^ string[j] ];
  131. reg.chars_val.b2 = v_tbl[ reg.chars_val.b2 ^ string[j] ];
  132. if ( v_range > 65535 ) {
  133. reg.chars_val.b3 = v_tbl[( reg.chars_val.b3 ^ string[j] ) ];
  134. reg.chars_val.b4 = v_tbl[( reg.chars_val.b4 ^ string[j] ) ];
  135. }
  136. }
  137. return (rang == 0 ) ?
  138. ( reg.hash_val % v_range )
  139. :
  140. ( reg.hash_val % rang );
  141. }
  142. int atoi_pearson::atoi(const char* str, int offset, int rang ) const
  143. {
  144. return atoi(str, strlen(str), offset, rang);
  145. }
  146. ostream& operator<<(ostream& s, atoi_pearson& p)
  147. {
  148. for ( unsigned int i = 0; i < p.v_entries ; i++ )
  149. s << int(p.v_tbl[i]) << " ";
  150. return s;
  151. }
  152. /* ########################################### */
  153. /* */
  154. /* ########################################### */
  155. /*
  156. int atoi_sum::atoi(const char* string, int l)
  157. {
  158. }
  159. atoi_sum::size()
  160. {
  161. }
  162. ostream& operator<<(ostream& s, atoi_sum& t)
  163. {
  164. return s;
  165. }
  166. */