atoi_fast.C 4.8 KB

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