123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /*
- * $XConsortium: data_t.cc /main/4 1996/07/18 14:31:45 drk $
- *
- * Copyright (c) 1993 HAL Computer Systems International, Ltd.
- * All rights reserved. Unpublished -- rights reserved under
- * the Copyright Laws of the United States. USE OF A COPYRIGHT
- * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
- * OR DISCLOSURE.
- *
- * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
- * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
- * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
- * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
- * INTERNATIONAL, LTD.
- *
- * RESTRICTED RIGHTS LEGEND
- * Use, duplication, or disclosure by the Government is subject
- * to the restrictions as set forth in subparagraph (c)(l)(ii)
- * of the Rights in Technical Data and Computer Software clause
- * at DFARS 252.227-7013.
- *
- * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
- * 1315 Dell Avenue
- * Campbell, CA 95008
- *
- */
-
- #include "dynhash/data_t.h"
- //memory_pool data_t::data_t_space_pool;
- extern memory_pool g_memory_pool;
- #ifdef C_API
- atoi_larson* data_t::larson_convertor_ptr;
- atoi_pearson* data_t::pearson_convertor_ptr;
- #else
- atoi_larson data_t::larson_convertor;
- atoi_pearson data_t::pearson_convertor(SHRT_MAX, 256);
- #endif
- data_t::data_t(data_t& d)
- {
- flag = d.flag;
- dt = d.dt;
- switch ( flag ) {
- case INT:
- {
- key.int_key = d.key.int_key ;
- flag = INT;
- break;
- }
- case STRING:
- {
- int sz = strlen(d.key.str_key);
- key.str_key = new char[sz+1];
- *((char *) memcpy(key.str_key, d.key.str_key, sz) + sz) = '\0';
- key.str_key[sz] = 0;
- flag = STRING;
- break;
- }
- case VOID:
- flag = VOID;
- }
- }
-
- /* // inline
- data_t::data_t(int t, voidPtr d) : dt(d)
- {
- flag = data_t::INT;
- key.int_key = t;
- }
- */
-
- data_t::data_t(const char* str, int sz, voidPtr d) : dt(d)
- {
- flag = data_t::STRING;
- key.str_key = new char[sz+1];
- if ( sz > 0 ) {
- int len = MIN(strlen(str), (unsigned int) sz);
- *((char *) memcpy(key.str_key, str, len) + len) = '\0';
- }
- key.str_key[sz] = 0;
- }
- /*
- int data_t::size()
- {
- switch ( flag ) {
- case INT:
- case VOID:
- return 2 + strlen(form("%d", key.int_key)) + sizeof(dt);
- case STRING:
- return 2 + strlen(key.str_key) + sizeof(dt);
- }
- }
- */
-
- int data_t::operator==(data_t& d)
- {
- if ( flag != d.flag ) {
- debug(cerr, flag);
- debug(cerr, d.flag);
- debug(cerr, *this);
- debug(cerr, d);
- throw(stringException("data_t type mismatches in operator==()"));
- }
- switch (flag) {
- case INT:
- return key.int_key == d.key.int_key ;
- case STRING:
- return strcmp(key.str_key, d.key.str_key) == 0 ;
- case VOID:
- throw(stringException("VOID type in operator==()"));
- }
- return 0;
- }
- data_t& data_t::operator =(data_t& d)
- {
- flag = d.flag;
- dt = d.dt;
- switch ( flag ) {
- case INT:
- key.int_key = d.key.int_key ;
- flag = INT;
- break;
- case STRING:
- {
- unsigned int d_sz = strlen(d.key.str_key);
- if ( strlen(key.str_key) < d_sz ) {
- delete key.str_key;
- key.str_key = new char[d_sz+1];
- }
- *((char *) memcpy(key.str_key, d.key.str_key, d_sz) + d_sz) = '\0';
- flag = STRING;
- break;
- }
- case VOID:
- flag = VOID;
- }
- return *this;
- }
- data_t::~data_t()
- {
- switch ( flag ) {
- case INT:
- break;
- case STRING:
- delete key.str_key;
- break;
- case VOID:
- break;
- }
- }
-
- static char buf[BUFSIZ];
- istream& operator >>(istream& i, data_t& d)
- {
- if ( !i.getline(buf, BUFSIZ) || buf[0] == '\n' )
- return i;
- char* key_ptr = strchr(buf, '\t');
- char* voidPtr_ptr = strrchr(buf, '\t');
- *key_ptr = 0;
- *voidPtr_ptr = 0;
- key_ptr++;
- voidPtr_ptr++;
- //////////////////////////
- // delete the string
- //////////////////////////
- if ( d.flag == data_t::STRING )
- delete d.key.str_key;
- d.flag = (data_t::flag_type)atoi(buf);
- d.dt = (voidPtr)(size_t)atoi(voidPtr_ptr);
- if ( d.flag == data_t::INT )
- d.key.int_key = atoi(key_ptr);
- else
- if ( d.flag == data_t::STRING ) {
- int sz = strlen(key_ptr);
- d.key.str_key = new char[sz+1];
- *((char *) memcpy(d.key.str_key, key_ptr, sz) + sz) = '\0';
- }
- return i;
- }
- ostream& operator <<(ostream& o, data_t& d)
- {
- if ( d.flag == data_t::VOID )
- return o;
- o << d.flag << " ";
- switch ( d.flag ) {
- case data_t::INT:
- o << d.key.int_key;
- break;
- case data_t::STRING:
- o << d.key.str_key;
- break;
- default:
- break;
- }
- o << " " << (long)(d.dt);
- return o;
- }
- Boolean data_t::binaryIn(buffer& buf)
- {
- char c; buf.get(c);
- flag = (data_t::flag_type)c;
- char* cp;
- unsigned int sz;
- switch ( flag ) {
- case data_t::VOID:
- break;
- case data_t::INT:
- buf.get(*(unsigned int*)&key.int_key);
- break;
- case data_t::STRING:
-
- buf.get(sz);
- key.str_key = new char[sz+1];
- buf.get(key.str_key, sz);
- key.str_key[sz] = 0;
- break;
- }
- cp = (char*)&dt;
- buf.get(*(long*)cp);
- return true;
- }
- Boolean data_t::binaryOut(buffer& buf)
- {
- buf.put((char)flag);
- unsigned int sz;
- switch ( flag ) {
- case data_t::VOID:
- break;
- case data_t::INT:
- buf.put((unsigned int)key.int_key);
- break;
- case data_t::STRING:
- sz = strlen(key.str_key);
- buf.put(sz);
- buf.put(key.str_key, sz);
- break;
- }
- buf.put((long)dt);
- return true;
- }
- ostream& data_t::asciiOut(ostream& o, print_func_ptr_t print_f)
- {
- print_f(o, this);
- return o;
- }
- int data_t::bucket_num(int k, int p, int M)
- {
- switch ( flag ) {
- case INT:
- return abs( key.int_key * k ) % p % M ;
- case STRING:
- //return abs( larson_convertor.atoi(key.str_key) * k ) % p % M ;
- //return abs( pearson_convertor.atoi(key.str_key, strlen(key.str_key), k
- return abs( pearson_convertor.atoi(key.str_key, strlen(key.str_key), k
- , p) ) % M ;
- default:
- throw(stringException("VOID type in bucket_num()"));
- }
- }
- int data_t::slot_num(int k, int rotate, int prime, int M )
- {
- int x;
- switch ( flag ) {
- case INT:
- x = ( abs( k * key.int_key ) % prime + rotate ) % M ;
- break;
- case STRING:
- x = ( abs( pearson_convertor.atoi(key.str_key, strlen(key.str_key), k, prime)) + rotate ) % M ;
- break;
- default:
- throw(stringException("VOID type in slot_num()"));
- }
- return x;
- }
- void* data_t::operator new( size_t x )
- {
- //return (void*)data_t_space_pool.alloc(x);
- return (void*)(g_memory_pool).alloc(x);
- }
- void data_t::operator delete( void* ptr )
- {
- //data_t_space_pool.free((char*)ptr);
- (g_memory_pool).free((char*)ptr);
- }
|