Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

matvec::HashTable Class Reference

#include <hashtable.h>

List of all members.


Detailed Description

a hash table

Definition at line 68 of file hashtable.h.

Public Types

enum  hashaction { INSERT, GETIDNO }

Public Methods

 HashTable (void)
 HashTable (const unsigned n, const size_t s)
 HashTable (const unsigned n, const char str[])
 HashTable (const HashTable &A)
 ~HashTable (void)
const HashTable & operator= (const HashTable &A)
HashTable & resize (const unsigned n, const size_t s)
HashTable & resize (const unsigned n)
unsigned insert (const void *v)
size_t data_len (void)
unsigned get_id (const void *v)
unsigned size (void) const
void change_id (const unsigned oldid, const unsigned newid)
void reorder (void)
void maxsize (const unsigned ms)
void release (void)
void save (const char *fname, const int io_mode=std::ios::out)
void input (const char fname[])
void save_to_disk (std::ostream &stream, const int relse=1)
void input_from_disk (std::istream &stream)
void display (displaytype dply)
void copyfrom (const HashTable &A)
const void * find (const unsigned id) const

Protected Methods

unsigned hash (const char *v, HashTable::hashaction action)

Private Attributes

unsigned tablesize
unsigned ext_tablesize
unsigned act_tablesize
unsigned datasize
unsigned id_changed
HashNode ** hash_table
HashNodehash_storage


Member Enumeration Documentation

enum matvec::HashTable::hashaction
 

Enumeration values:
INSERT 
GETIDNO 

Definition at line 70 of file hashtable.h.

00070 {INSERT,GETIDNO};


Constructor & Destructor Documentation

matvec::HashTable::HashTable void   
 

Definition at line 128 of file hashtable.cpp.

References act_tablesize, datasize, ext_tablesize, hash_storage, hash_table, id_changed, and tablesize.

00129 {
00130    datasize      = 0;
00131    tablesize     = 0;
00132    id_changed    = 0;
00133    ext_tablesize = 0;
00134    act_tablesize = 0;
00135 
00136    hash_table    = 0;
00137    hash_storage  = 0;
00138 }

matvec::HashTable::HashTable const unsigned    n,
const size_t    s
 

Definition at line 140 of file hashtable.cpp.

References datasize, ext_tablesize, hash_storage, hash_table, id_changed, resize(), and tablesize.

00141 {
00142    datasize      = 0;
00143    tablesize     = 0;
00144    id_changed    = 0;
00145    ext_tablesize = 0;
00146 
00147    hash_table    = 0;
00148    hash_storage  = 0;
00149    resize(n,s);
00150 }

matvec::HashTable::HashTable const unsigned    n,
const char    str[]
 

Definition at line 152 of file hashtable.cpp.

References datasize, ext_tablesize, hash_storage, hash_table, id_changed, resize(), and tablesize.

00153 {
00154 
00155    // str could be any string
00156    datasize      = 0;
00157    tablesize     = 0;
00158    id_changed    = 0;
00159    ext_tablesize = 0;
00160 
00161    hash_table    = 0;
00162    hash_storage  = 0;
00163    resize(n,0);
00164 }

matvec::HashTable::HashTable const HashTable &    A [inline]
 

Definition at line 74 of file hashtable.h.

References copyfrom().

00074 {copyfrom(A);}

matvec::HashTable::~HashTable void    [inline]
 

Definition at line 75 of file hashtable.h.

References release().

00075 {release();}


Member Function Documentation

void matvec::HashTable::change_id const unsigned    oldid,
const unsigned    newid
 

Definition at line 262 of file hashtable.cpp.

References hash_table, matvec::HashNode::id, and id_changed.

Referenced by matvec::Population::renum().

00263 {
00264    ///////////////////////////////////////////////////////////////////////
00265    //  once calling this routine, you must call it for each non-empty
00266    //  HashNode (id starts from 1 to act_tablesize). newif must starts
00267    //  1 to act_tablesize, too.
00268    //  After calling for each non-empty HashNode, then must call reorder()
00269    //  to re-order hash_table so that hash_table[i] points HashNode
00270    //  with id i+1 where i starts from 0 to act_tablesize.
00271    ///////////////////////////////////////////////////////////////////////
00272    if (oldid >act_tablesize || newid>act_tablesize) throw exception("HashTable.change_id(oldid,newid): invalid oldid or newid");
00273    hash_table[oldid-1]->id = newid;
00274    id_changed = 1;
00275 }

void matvec::HashTable::copyfrom const HashTable &    A
 

Definition at line 166 of file hashtable.cpp.

References act_tablesize, matvec::HashNode::data, datasize, ext_tablesize, hash_storage, hash_table, id_changed, matvec::HashNode::id_no(), maxsize(), resize(), and tablesize.

Referenced by matvec::Data::copyfrom(), HashTable(), insert(), and operator=().

00167 {
00168    if (this == &A) return;
00169    resize(A.tablesize,A.datasize);
00170    maxsize(A.ext_tablesize);
00171    id_changed = A.id_changed;
00172    act_tablesize = A.act_tablesize;
00173    HashNode *node;
00174    for (unsigned i=0; i<ext_tablesize; i++) {
00175       node = &A.hash_storage[i];
00176       if (node->data) {
00177          hash_storage[i] = *node;
00178          hash_table[node->id_no()-1] = &hash_storage[i];
00179       }
00180    }
00181 }

size_t matvec::HashTable::data_len void    [inline]
 

Definition at line 83 of file hashtable.h.

References datasize.

00083 {return datasize;}

void matvec::HashTable::display displaytype    dply
 

Definition at line 469 of file hashtable.cpp.

References act_tablesize, matvec::displaytype, and hash_table.

00470 {
00471    for (unsigned i=0; i<act_tablesize; i++) dply(hash_table[i]->data);
00472 }

const void * matvec::HashTable::find const unsigned    id const
 

Definition at line 293 of file hashtable.cpp.

References act_tablesize, matvec::HashNode::data, and hash_table.

Referenced by matvec::Population::ind_name(), matvec::Population::input_data(), matvec::Population::input_markerData(), matvec::Field::operator<(), matvec::Field::operator==(), matvec::Field::operator>(), matvec::Field::out_to_stream(), matvec::Population::output_descentGraph(), matvec::Population::output_ibdMatrix(), matvec::Data::print(), matvec::Model::re_hash_data(), matvec::Model::save(), matvec::GLMM::save(), matvec::Field::sub(), and matvec::Model::trait_effect_level().

00294 {
00295    if (id == 0 || id > act_tablesize) {
00296      throw exception("HashTable.find(id): range error");
00297    }
00298    return (const void *)(hash_table[id-1]->data); // index starts from 0
00299 }

unsigned matvec::HashTable::get_id const void *    v
 

Definition at line 287 of file hashtable.cpp.

References hash().

Referenced by matvec::Population::get_id(), matvec::Model::hashxact(), matvec::Pedigree::input(), matvec::Population::input_data(), matvec::Population::input_descentGraph(), matvec::Population::input_markerData(), matvec::Population::output_pdq(), matvec::Model::re_hash_data(), matvec::Model::save_pos_val(), and matvec::Population::sub().

00288 {
00289    const char *v = (const char *)vv;
00290    return hash(v,HashTable::GETIDNO); // if 0, means it cannot found
00291 }

unsigned matvec::HashTable::hash const char *    v,
HashTable::hashaction    action
[protected]
 

Definition at line 189 of file hashtable.cpp.

References act_tablesize, datasize, matvec::HashNode::equal(), ext_tablesize, HASH_CONST, hash_storage, hash_table, matvec::HashNode::id, matvec::HashNode::id_no(), matvec::HashNode::insert(), and matvec::warning().

Referenced by get_id(), and insert().

00190 {
00191    register int i;
00192    unsigned long h,new_h,skip;
00193    unsigned strsize;
00194    HashNode *node;
00195 
00196    if (datasize==0) {
00197       strsize = strlen(v)+1;
00198       if (strsize==1) {
00199          warning("HashTable.hash(v): empty v or zero size of v");
00200          return 0;
00201       }
00202    }
00203    else strsize = datasize;
00204    for (h=0, skip=1, i=0; i<strsize; i++) {
00205       h = (h*HASH_CONST + v[i]) % ext_tablesize;
00206       skip += 2*h;
00207    }
00208    node = &hash_storage[h];
00209    for (int ntry=0; ntry<500; ntry++) {
00210       if ( node->id == 0) {                // node is empty
00211          if (action == HashTable::INSERT) {
00212             hash_table[act_tablesize] = node;
00213             act_tablesize++;
00214             node->insert(v,strsize,act_tablesize);
00215             return act_tablesize;
00216          }
00217          else if (action == HashTable::GETIDNO) {
00218             return 0;
00219          }
00220          else {
00221             warning("HashTable.hash(), unknown hash action type");
00222          }
00223       }
00224       else {   // node is not empty
00225          if (node->equal(v)) {
00226             return node->id_no();     // for both INSERT and GETIDNO
00227          }
00228          else {
00229             new_h = (h + skip) % ext_tablesize;
00230             h = (new_h == h) ? (h+1)% ext_tablesize : new_h;
00231             node = &hash_storage[h];
00232          }
00233       }
00234    }
00235    return 0;
00236 }

void matvec::HashTable::input const char    fname[]
 

Definition at line 394 of file hashtable.cpp.

References input_from_disk().

00395 {
00396    std::ifstream hfile(fname,std::ios::in);
00397    if (!hfile) throw exception(" HashTable::input(): cannot open file");
00398    input_from_disk(hfile);
00399    hfile.close();
00400 }

void matvec::HashTable::input_from_disk std::istream &    stream
 

Definition at line 434 of file hashtable.cpp.

References act_tablesize, matvec::HashNode::data, matvec::HashNode::datasize, datasize, hash_storage, hash_table, matvec::HashNode::id, maxsize(), and resize().

Referenced by input().

00435 {
00436    unsigned ts,act_ts,ext_ts,tpos,id;
00437    size_t ds = 0;
00438    ts = act_ts = ext_ts = tpos = id = 0;
00439    stream.read((char*) &ts,sizeof(unsigned));
00440    stream.read((char*) &act_ts,sizeof(unsigned));
00441    stream.read((char*) &ext_ts,sizeof(unsigned));
00442    stream.read((char*) &ds,sizeof(size_t));
00443 
00444    resize(ts,ds);       // allocate momery for hash_storage, hash_table
00445    maxsize(ext_ts);
00446    act_tablesize = act_ts;
00447 
00448    HashNode *node;
00449    for (ts=0; ts<act_tablesize; ts++) {
00450       stream.read((char*) &tpos,sizeof(unsigned));
00451       stream.read((char*) &(id),sizeof(unsigned));
00452       stream.read((char*) &(ds),sizeof(size_t));
00453       node = &hash_storage[tpos];
00454       node->id = id;
00455       if (datasize == 0) {
00456          node->datasize = ds;
00457          if(ds>0){
00458            node->data = new char [ds];
00459          }
00460          else {
00461            node->data = 0;
00462          }
00463       }
00464       stream.read((char*)node->data,ds);
00465       hash_table[id-1] = node;
00466    }
00467 }

unsigned matvec::HashTable::insert const void *    v
 

Definition at line 238 of file hashtable.cpp.

References act_tablesize, copyfrom(), matvec::HashNode::data, ext_tablesize, hash(), hash_table, maxsize(), matvec::next_prime(), and matvec::warning().

Referenced by matvec::Model::hashxact(), matvec::Pedigree::input(), matvec::Data::input(), matvec::Model::re_hash_data(), matvec::Population::sub(), and matvec::Field::sub().

00239 {
00240    if (ext_tablesize==0) {
00241       warning("HashTable.insert(): HashTable is null, do resize() first");
00242       return 0;
00243    }
00244    const char *v = (const char *)vv;
00245    unsigned id;
00246    id = hash(v,HashTable::INSERT);   // return its id of vv in HashTable
00247    if (id == 0) {
00248       unsigned k = act_tablesize;
00249       HashTable TMP;
00250       TMP.copyfrom(*this);
00251       maxsize(static_cast<unsigned>(next_prime(static_cast<long>(ext_tablesize+51))));
00252       for (unsigned i=0; i<k; i++) {
00253          id = hash(TMP.hash_table[i]->data,HashTable::INSERT);
00254          if (id == 0) throw exception("hashtable size too small");
00255       }
00256       id = hash(v,HashTable::INSERT);
00257       if (id == 0) throw exception("hashtable size too small");
00258    }
00259    return id;
00260 }

void matvec::HashTable::maxsize const unsigned    ms
 

Definition at line 345 of file hashtable.cpp.

References act_tablesize, matvec::check_ptr(), datasize, ext_tablesize, hash_storage, hash_table, and matvec::HashNode::resize().

Referenced by copyfrom(), input_from_disk(), and insert().

00346 {
00347    if (ext_tablesize == ms) return;
00348    ext_tablesize = ms;
00349    if(hash_storage){
00350      delete [] hash_storage;
00351      hash_storage=0;
00352    }
00353    if (ext_tablesize>0){
00354      hash_storage = new HashNode [ext_tablesize];
00355    }
00356    else{
00357      hash_storage = 0;
00358    }
00359    check_ptr(hash_storage);
00360    for (int i=0; i<ext_tablesize; ++i) hash_storage[i].resize(datasize);
00361 
00362    if(hash_table){
00363      delete [] hash_table;
00364      hash_table=0;
00365    }
00366    if(ext_tablesize>0){
00367      hash_table = new HashNode*[ext_tablesize];
00368    }
00369    else {
00370      hash_table = 0;
00371    }
00372    check_ptr(hash_table);
00373    act_tablesize = 0;
00374 }

const HashTable & matvec::HashTable::operator= const HashTable &    A
 

Definition at line 183 of file hashtable.cpp.

References copyfrom().

00184 {
00185    copyfrom(A);
00186    return *this;
00187 }

void matvec::HashTable::release void   
 

Definition at line 376 of file hashtable.cpp.

References datasize, ext_tablesize, hash_storage, hash_table, and tablesize.

Referenced by matvec::Pedigree::input(), resize(), save_to_disk(), and ~HashTable().

00377 {
00378    if (hash_storage) {delete [] hash_storage; hash_storage = 0;}
00379    if (hash_table)   {delete [] hash_table; hash_table = 0;}
00380    tablesize     = 0;
00381    datasize      = 0;
00382    ext_tablesize = 0;
00383 }

void matvec::HashTable::reorder void   
 

Definition at line 277 of file hashtable.cpp.

References ext_tablesize, hash_storage, hash_table, id_changed, and matvec::HashNode::id_no().

Referenced by matvec::Population::renum().

00278 {
00279    if (id_changed == 0) return;
00280    unsigned i,id;
00281    for (i=0; i<ext_tablesize; i++) {
00282       id = hash_storage[i].id_no();
00283       if (id > 0) hash_table[id-1] = &(hash_storage[i]);
00284    }
00285 }

HashTable& matvec::HashTable::resize const unsigned    n [inline]
 

Definition at line 80 of file hashtable.h.

References resize().

00080 {return resize(n,0);}

HashTable & matvec::HashTable::resize const unsigned    n,
const size_t    s
 

Definition at line 301 of file hashtable.cpp.

References act_tablesize, matvec::check_ptr(), datasize, ext_tablesize, hash_storage, hash_table, matvec::HashNode::id, matvec::next_prime(), release(), matvec::HashNode::resize(), and tablesize.

Referenced by matvec::Model::assign_id_xact(), copyfrom(), HashTable(), matvec::Data::input(), input_from_disk(), matvec::Model::re_hash_data(), resize(), matvec::Population::sub(), and matvec::Field::sub().

00302 {
00303    if (n==0) {
00304       release();
00305       return *this;
00306    }
00307    unsigned i;
00308    unsigned tmpsize = static_cast<unsigned>(next_prime(static_cast<long>(1.30*n + 50.0)));
00309                                                     //  30% of tablesize
00310    if (tmpsize != ext_tablesize || datasize != s) {   // definitely delete them
00311       ext_tablesize = tmpsize;
00312       datasize = s;
00313       if(hash_storage){
00314         delete [] hash_storage;
00315         hash_storage=0;
00316       }
00317       if (ext_tablesize>0){
00318         hash_storage = new HashNode [ext_tablesize];
00319       }
00320       else{
00321         hash_storage = 0;
00322       }
00323       check_ptr(hash_storage);
00324       for (int i=0; i<ext_tablesize; ++i) hash_storage[i].resize(datasize);
00325       if(hash_table){
00326         delete [] hash_table;
00327         hash_table=0;
00328       }
00329       if(ext_tablesize>0){
00330         hash_table = new HashNode*[ext_tablesize];
00331       }
00332       else {
00333         hash_table = 0;
00334       }
00335       check_ptr(hash_table);
00336    }
00337    else {
00338       for (i=0; i<act_tablesize; i++) hash_table[i]->id = 0;
00339    }
00340    tablesize = n;
00341    act_tablesize = 0;
00342    return *this;
00343 }

void matvec::HashTable::save const char *    fname,
const int    io_mode = std::ios::out
 

void matvec::HashTable::save_to_disk std::ostream &    stream,
const int    relse = 1
 

Definition at line 407 of file hashtable.cpp.

References act_tablesize, matvec::HashNode::data, matvec::HashNode::datasize, datasize, ext_tablesize, hash_storage, matvec::HashNode::id, release(), and tablesize.

00408 {
00409    unsigned ts,act_ts,ext_ts,tpos;
00410    size_t   ds;
00411    ts = tablesize;
00412    act_ts = act_tablesize;
00413    ext_ts = ext_tablesize;
00414    ds = datasize;
00415 
00416    stream.write((char*)&ts,sizeof(unsigned));
00417    stream.write((char*)&act_ts,sizeof(unsigned));
00418    stream.write((char*)&ext_ts,sizeof(unsigned));
00419    stream.write((char*)&ds,sizeof(size_t));
00420 
00421    HashNode *node;
00422    for (tpos=0; tpos<ext_tablesize; tpos++) {
00423       node = &hash_storage[tpos];
00424       if (node->data) {
00425          stream.write((char*) &tpos,sizeof(unsigned));
00426          stream.write((char*) &(node->id),sizeof(unsigned));
00427          stream.write((char*) &(node->datasize),sizeof(size_t));
00428          stream.write((char*)node->data,node->datasize);
00429       }
00430    }
00431    if (relse) release();
00432 }

unsigned matvec::HashTable::size void    const [inline]
 

Definition at line 85 of file hashtable.h.

References act_tablesize.

Referenced by matvec::Model::assign_id_xact(), matvec::Population::ind_name(), matvec::Data::input(), matvec::Model::re_hash_data(), matvec::Model::save_pos_val(), and matvec::Field::sub().

00085 {return act_tablesize;}


Member Data Documentation

unsigned matvec::HashTable::act_tablesize [private]
 

Definition at line 101 of file hashtable.h.

Referenced by copyfrom(), display(), find(), hash(), HashTable(), input_from_disk(), insert(), maxsize(), resize(), save_to_disk(), and size().

unsigned matvec::HashTable::datasize [private]
 

Definition at line 101 of file hashtable.h.

Referenced by copyfrom(), data_len(), hash(), HashTable(), input_from_disk(), maxsize(), release(), resize(), and save_to_disk().

unsigned matvec::HashTable::ext_tablesize [private]
 

Definition at line 101 of file hashtable.h.

Referenced by copyfrom(), hash(), HashTable(), insert(), maxsize(), release(), reorder(), resize(), and save_to_disk().

HashNode* matvec::HashTable::hash_storage [private]
 

Definition at line 103 of file hashtable.h.

Referenced by copyfrom(), hash(), HashTable(), input_from_disk(), maxsize(), release(), reorder(), resize(), and save_to_disk().

HashNode** matvec::HashTable::hash_table [private]
 

Definition at line 102 of file hashtable.h.

Referenced by change_id(), copyfrom(), display(), find(), hash(), HashTable(), input_from_disk(), insert(), maxsize(), release(), reorder(), and resize().

unsigned matvec::HashTable::id_changed [private]
 

Definition at line 101 of file hashtable.h.

Referenced by change_id(), copyfrom(), HashTable(), and reorder().

unsigned matvec::HashTable::tablesize [private]
 

Definition at line 101 of file hashtable.h.

Referenced by copyfrom(), HashTable(), release(), resize(), and save_to_disk().


The documentation for this class was generated from the following files:
Generated on Thu Jun 16 17:14:31 2005 for Matvec by doxygen1.2.16