data_t.C 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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: data_t.cc /main/4 1996/07/18 14:31:45 drk $
  25. *
  26. * Copyright (c) 1993 HAL Computer Systems International, Ltd.
  27. * All rights reserved. Unpublished -- rights reserved under
  28. * the Copyright Laws of the United States. USE OF A COPYRIGHT
  29. * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
  30. * OR DISCLOSURE.
  31. *
  32. * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
  33. * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
  34. * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
  35. * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
  36. * INTERNATIONAL, LTD.
  37. *
  38. * RESTRICTED RIGHTS LEGEND
  39. * Use, duplication, or disclosure by the Government is subject
  40. * to the restrictions as set forth in subparagraph (c)(l)(ii)
  41. * of the Rights in Technical Data and Computer Software clause
  42. * at DFARS 252.227-7013.
  43. *
  44. * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
  45. * 1315 Dell Avenue
  46. * Campbell, CA 95008
  47. *
  48. */
  49. #include "dynhash/data_t.h"
  50. //memory_pool data_t::data_t_space_pool;
  51. extern memory_pool g_memory_pool;
  52. #ifdef C_API
  53. atoi_larson* data_t::larson_convertor_ptr;
  54. atoi_pearson* data_t::pearson_convertor_ptr;
  55. #else
  56. atoi_larson data_t::larson_convertor;
  57. atoi_pearson data_t::pearson_convertor(SHRT_MAX, 256);
  58. #endif
  59. data_t::data_t(data_t& d)
  60. {
  61. flag = d.flag;
  62. dt = d.dt;
  63. switch ( flag ) {
  64. case INT:
  65. {
  66. key.int_key = d.key.int_key ;
  67. flag = INT;
  68. break;
  69. }
  70. case STRING:
  71. {
  72. int sz = strlen(d.key.str_key);
  73. key.str_key = new char[sz+1];
  74. *((char *) memcpy(key.str_key, d.key.str_key, sz) + sz) = '\0';
  75. key.str_key[sz] = 0;
  76. flag = STRING;
  77. break;
  78. }
  79. case VOID:
  80. flag = VOID;
  81. }
  82. }
  83. /* // inline
  84. data_t::data_t(int t, voidPtr d) : dt(d)
  85. {
  86. flag = data_t::INT;
  87. key.int_key = t;
  88. }
  89. */
  90. data_t::data_t(const char* str, int sz, voidPtr d) : dt(d)
  91. {
  92. flag = data_t::STRING;
  93. key.str_key = new char[sz+1];
  94. if ( sz > 0 ) {
  95. int len = MIN(strlen(str), (unsigned int) sz);
  96. *((char *) memcpy(key.str_key, str, len) + len) = '\0';
  97. }
  98. key.str_key[sz] = 0;
  99. }
  100. /*
  101. int data_t::size()
  102. {
  103. switch ( flag ) {
  104. case INT:
  105. case VOID:
  106. return 2 + strlen(form("%d", key.int_key)) + sizeof(dt);
  107. case STRING:
  108. return 2 + strlen(key.str_key) + sizeof(dt);
  109. }
  110. }
  111. */
  112. int data_t::operator==(data_t& d)
  113. {
  114. if ( flag != d.flag ) {
  115. debug(cerr, flag);
  116. debug(cerr, d.flag);
  117. debug(cerr, *this);
  118. debug(cerr, d);
  119. throw(stringException("data_t type mismatches in operator==()"));
  120. }
  121. switch (flag) {
  122. case INT:
  123. return key.int_key == d.key.int_key ;
  124. case STRING:
  125. return strcmp(key.str_key, d.key.str_key) == 0 ;
  126. case VOID:
  127. throw(stringException("VOID type in operator==()"));
  128. }
  129. return 0;
  130. }
  131. data_t& data_t::operator =(data_t& d)
  132. {
  133. flag = d.flag;
  134. dt = d.dt;
  135. switch ( flag ) {
  136. case INT:
  137. key.int_key = d.key.int_key ;
  138. flag = INT;
  139. break;
  140. case STRING:
  141. {
  142. unsigned int d_sz = strlen(d.key.str_key);
  143. if ( strlen(key.str_key) < d_sz ) {
  144. delete key.str_key;
  145. key.str_key = new char[d_sz+1];
  146. }
  147. *((char *) memcpy(key.str_key, d.key.str_key, d_sz) + d_sz) = '\0';
  148. flag = STRING;
  149. break;
  150. }
  151. case VOID:
  152. flag = VOID;
  153. }
  154. return *this;
  155. }
  156. data_t::~data_t()
  157. {
  158. switch ( flag ) {
  159. case INT:
  160. break;
  161. case STRING:
  162. delete key.str_key;
  163. break;
  164. case VOID:
  165. break;
  166. }
  167. }
  168. static char buf[BUFSIZ];
  169. istream& operator >>(istream& i, data_t& d)
  170. {
  171. if ( !i.getline(buf, BUFSIZ) || buf[0] == '\n' )
  172. return i;
  173. char* key_ptr = strchr(buf, '\t');
  174. char* voidPtr_ptr = strrchr(buf, '\t');
  175. *key_ptr = 0;
  176. *voidPtr_ptr = 0;
  177. key_ptr++;
  178. voidPtr_ptr++;
  179. //////////////////////////
  180. // delete the string
  181. //////////////////////////
  182. if ( d.flag == data_t::STRING )
  183. delete d.key.str_key;
  184. d.flag = (data_t::flag_type)atoi(buf);
  185. d.dt = (voidPtr)(size_t)atoi(voidPtr_ptr);
  186. if ( d.flag == data_t::INT )
  187. d.key.int_key = atoi(key_ptr);
  188. else
  189. if ( d.flag == data_t::STRING ) {
  190. int sz = strlen(key_ptr);
  191. d.key.str_key = new char[sz+1];
  192. *((char *) memcpy(d.key.str_key, key_ptr, sz) + sz) = '\0';
  193. }
  194. return i;
  195. }
  196. ostream& operator <<(ostream& o, data_t& d)
  197. {
  198. if ( d.flag == data_t::VOID )
  199. return o;
  200. o << d.flag << " ";
  201. switch ( d.flag ) {
  202. case data_t::INT:
  203. o << d.key.int_key;
  204. break;
  205. case data_t::STRING:
  206. o << d.key.str_key;
  207. break;
  208. default:
  209. break;
  210. }
  211. o << " " << (long)(d.dt);
  212. return o;
  213. }
  214. Boolean data_t::binaryIn(buffer& buf)
  215. {
  216. char c; buf.get(c);
  217. flag = (data_t::flag_type)c;
  218. char* cp;
  219. unsigned int sz;
  220. switch ( flag ) {
  221. case data_t::VOID:
  222. break;
  223. case data_t::INT:
  224. buf.get(*(unsigned int*)&key.int_key);
  225. break;
  226. case data_t::STRING:
  227. buf.get(sz);
  228. key.str_key = new char[sz+1];
  229. buf.get(key.str_key, sz);
  230. key.str_key[sz] = 0;
  231. break;
  232. }
  233. cp = (char*)&dt;
  234. buf.get(*(long*)cp);
  235. return true;
  236. }
  237. Boolean data_t::binaryOut(buffer& buf)
  238. {
  239. buf.put((char)flag);
  240. unsigned int sz;
  241. switch ( flag ) {
  242. case data_t::VOID:
  243. break;
  244. case data_t::INT:
  245. buf.put((unsigned int)key.int_key);
  246. break;
  247. case data_t::STRING:
  248. sz = strlen(key.str_key);
  249. buf.put(sz);
  250. buf.put(key.str_key, sz);
  251. break;
  252. }
  253. buf.put((long)dt);
  254. return true;
  255. }
  256. ostream& data_t::asciiOut(ostream& o, print_func_ptr_t print_f)
  257. {
  258. print_f(o, this);
  259. return o;
  260. }
  261. int data_t::bucket_num(int k, int p, int M)
  262. {
  263. switch ( flag ) {
  264. case INT:
  265. return abs( key.int_key * k ) % p % M ;
  266. case STRING:
  267. //return abs( larson_convertor.atoi(key.str_key) * k ) % p % M ;
  268. //return abs( pearson_convertor.atoi(key.str_key, strlen(key.str_key), k
  269. return abs( pearson_convertor.atoi(key.str_key, strlen(key.str_key), k
  270. , p) ) % M ;
  271. default:
  272. throw(stringException("VOID type in bucket_num()"));
  273. }
  274. }
  275. int data_t::slot_num(int k, int rotate, int prime, int M )
  276. {
  277. int x;
  278. switch ( flag ) {
  279. case INT:
  280. x = ( abs( k * key.int_key ) % prime + rotate ) % M ;
  281. break;
  282. case STRING:
  283. x = ( abs( pearson_convertor.atoi(key.str_key, strlen(key.str_key), k, prime)) + rotate ) % M ;
  284. break;
  285. default:
  286. throw(stringException("VOID type in slot_num()"));
  287. }
  288. return x;
  289. }
  290. void* data_t::operator new( size_t x )
  291. {
  292. //return (void*)data_t_space_pool.alloc(x);
  293. return (void*)(g_memory_pool).alloc(x);
  294. }
  295. void data_t::operator delete( void* ptr )
  296. {
  297. //data_t_space_pool.free((char*)ptr);
  298. (g_memory_pool).free((char*)ptr);
  299. }