00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
#ifndef __IDMap_AMOS_HH
00011 #define __IDMap_AMOS_HH 1
00012
00013
#include "Message_AMOS.hh"
00014
#include <string>
00015
#include <vector>
00016
00017
00018
00019
00020
namespace AMOS {
00021
00022
00035
00036 class IDMap_t :
public IMessagable_t
00037 {
00038
00039
public:
00040
00041
00047
00048 struct HashTriple_t
00049 {
00050 uint8_t
c;
00051 ID_t iid;
00052 ID_t bid;
00053 std::string
eid;
00054
00055
00058 HashTriple_t (
ID_t iid_p,
const std::string & eid_p,
ID_t bid_p)
00059 :
c (0),
iid (iid_p),
eid (eid_p),
bid (bid_p)
00060 { }
00061
00062
00065 ~HashTriple_t ( )
00066 { }
00067
00068
00071
HashTriple_t &
operator= (
const HashTriple_t & s);
00072
00073 };
00074
00075
00076
private:
00077
00078
static const Size_t DEFAULT_NUM_BUCKETS;
00079
00080
00087
00088
struct HashNode_t
00089 {
00090 HashNode_t * next;
00091 HashTriple_t * triple;
00092
00093
00096 HashNode_t ( )
00097 : next (NULL), triple (NULL)
00098 { }
00099
00100
00101
00104 ~HashNode_t ( )
00105 { }
00106
00107
00108
00114
void clear ( );
00115
00116
00117
00123
void clearchain ( )
00124 {
00125
if ( next != NULL )
00126 next -> clearchain( );
00127
clear( );
00128 }
00129 };
00130
00131
00132
00135
00136
class iterator
00137 {
00138
private:
00139 std::vector<HashNode_t> * iid_bucs;
00140 std::vector<HashNode_t> * eid_bucs;
00141 std::vector<HashNode_t>::iterator buc;
00142 HashNode_t * curr;
00143
bool iids;
00144
00145
public:
00146 iterator ( )
00147 { curr = NULL; }
00148 iterator (std::vector<HashNode_t> * iid_bucs_p,
00149 std::vector<HashNode_t> * eid_bucs_p);
00150 HashTriple_t & operator*()
const
00151
{
return *(curr -> triple); }
00152 operator HashTriple_t * ()
const
00153
{
return (curr == NULL ? NULL : curr -> triple); }
00154 HashTriple_t * operator->()
const
00155
{
return (curr == NULL ? NULL : curr -> triple); }
00156 iterator & operator++();
00157 iterator operator++(
int)
00158 {
00159 iterator tmp = *
this;
00160 this->operator++();
00161
return tmp;
00162 }
00163 };
00164
00165
00166
00167 iterator
begin ( )
00168 {
00169
return iterator (&iid_bucs_m, &eid_bucs_m);
00170 }
00171
00172
00173
00174 iterator
end ( )
00175 {
00176
return iterator ( );
00177 }
00178
00179
00180
00183
Size_t minbuckets (
Size_t min);
00184
00185
00186
00192 HashNode_t * hashfunc (
ID_t key)
const
00193
{
00194
return &((HashNode_t &)(iid_bucs_m [key %
getBuckets( )]));
00195 }
00196
00197
00198
00204 HashNode_t * hashfunc (
const std::string & key)
const
00205
{
00206
int c;
00207
unsigned long h = 5381;
00208 std::string::const_iterator i;
00209 std::string::const_iterator end = key .
end( );
00210
00211
for ( i = key .
begin( ); i != end; i ++ )
00212 h = ((h << 5) + h) ^ (*i);
00213
00214
return &((HashNode_t &)(eid_bucs_m [h %
getBuckets( )]));
00215 }
00216
00217
00218
00228
bool lookupnode (
ID_t key, HashNode_t * & node)
const;
00229
00230
00231
00241
bool lookupnode (
const std::string & key, HashNode_t * & node)
const;
00242
00243
00244
00253
void removenode (HashNode_t * curr, HashNode_t * prev);
00254
00255
00256 std::vector<HashNode_t> iid_bucs_m;
00257 std::vector<HashNode_t> eid_bucs_m;
00258
Size_t size_m;
00259
NCode_t type_m;
00260
00261
00262
public:
00263
00264
static const NCode_t NCODE;
00266
00267
00268
00271
00272 class const_iterator
00273 {
00274
private:
00275
const std::vector<HashNode_t> * iid_bucs;
00276
const std::vector<HashNode_t> * eid_bucs;
00277 std::vector<HashNode_t>
::const_iterator buc;
00278
const HashNode_t * curr;
00279
bool iids;
00280
00281
public:
00282 const_iterator ( )
00283 { curr = NULL; }
00284
const_iterator (
const std::vector<HashNode_t> * iid_bucs_p,
00285
const std::vector<HashNode_t> * eid_bucs_p);
00286 const HashTriple_t &
operator*()
const
00287
{
return *(curr -> triple); }
00288 operator const HashTriple_t * ()
const
00289
{
return (curr == NULL ? NULL : curr -> triple); }
00290 const HashTriple_t *
operator->()
const
00291
{
return (curr == NULL ? NULL : curr -> triple); }
00292
const_iterator &
operator++();
00293 const_iterator operator++(
int)
00294 {
00295
const_iterator tmp = *
this;
00296 this->
operator++();
00297
return tmp;
00298 }
00299 };
00300
00301
00302
00305 IDMap_t ( )
00306 : size_m (0), type_m (
NULL_NCODE)
00307 {
00308
resize (DEFAULT_NUM_BUCKETS);
00309 }
00310
00311
00312
00317 IDMap_t (
Size_t buckets)
00318 : size_m (0), type_m (
NULL_NCODE)
00319 {
00320
resize (buckets);
00321 }
00322
00323
00324
00327 IDMap_t (
const IDMap_t & source)
00328 : size_m (0), type_m (
NULL_NCODE)
00329 {
00330 *
this = source;
00331 }
00332
00333
00334
00337 ~IDMap_t ( )
00338 {
00339
clear( );
00340 }
00341
00342
00343
00350 const_iterator begin ( )
const
00351
{
00352
return const_iterator (&iid_bucs_m, &eid_bucs_m);
00353 }
00354
00355
00356
00363 const_iterator end ( )
const
00364
{
00365
return const_iterator ( );
00366 }
00367
00368
00369
00376
void clear ( );
00377
00378
00379
00387
void concat (
const IDMap_t & source);
00388
00389
00390
00395 bool empty ( )
const
00396
{
00397
return (size_m == 0);
00398 }
00399
00400
00401
00410 bool exists (
const std::string & key)
const
00411
{
00412 HashNode_t * curr;
00413
return lookupnode (key, curr);
00414 }
00415
00416
00417
00426 bool exists (
ID_t key)
const
00427
{
00428 HashNode_t * curr;
00429
return lookupnode (key, curr);
00430 }
00431
00432
00433
00438 Size_t getBuckets ( )
const
00439
{
00440
return iid_bucs_m . size( );
00441 }
00442
00443
00444
00445 virtual NCode_t getNCode ( )
const
00446
{
00447
return IDMap_t::NCODE;
00448 }
00449
00450
00451
00456 Size_t getSize ( )
const
00457
{
00458
return size_m;
00459 }
00460
00461
00462
00467 NCode_t getType ( )
const
00468
{
00469
return type_m;
00470 }
00471
00472
00473
00491
const HashTriple_t *
insert (
ID_t iid,
00492
const std::string & eid,
00493
ID_t bid = NULL_ID);
00494
00495
00496
00509 const HashTriple_t *
insert (
const std::string & eid,
ID_t bid = NULL_ID)
00510 {
00511
return insert (
NULL_ID, eid, bid);
00512 }
00513
00514
00515
00527 const HashTriple_t *
insert (
ID_t iid,
ID_t bid = NULL_ID)
00528 {
00529
return insert (iid,
NULL_STRING, bid);
00530 }
00531
00532
00533
00543 ID_t lookupBID (
const std::string & key)
const
00544
{
00545 HashNode_t * curr;
00546
if ( !lookupnode (key, curr) )
00547
return NULL_ID;
00548
return curr -> triple -> bid;
00549 }
00550
00551
00552
00562 ID_t lookupBID (
ID_t key)
const
00563
{
00564 HashNode_t * curr;
00565
if ( !lookupnode (key, curr) )
00566
return NULL_ID;
00567
return curr -> triple -> bid;
00568 }
00569
00570
00571
00581 const std::string &
lookupEID (
ID_t key)
const
00582
{
00583 HashNode_t * curr;
00584
if ( !lookupnode (key, curr) )
00585
return NULL_STRING;
00586
return curr -> triple -> eid;
00587 }
00588
00589
00590
00600 ID_t lookupIID (
const std::string & key)
const
00601
{
00602 HashNode_t * curr;
00603
if ( !lookupnode (key, curr) )
00604
return NULL_ID;
00605
return curr -> triple -> iid;
00606 }
00607
00608
00609
00621
IDMap_t &
operator= (
const IDMap_t & source);
00622
00623
00624
00625
virtual void readMessage (
const Message_t & msg);
00626
00627
00628
00638
void remove (
ID_t key);
00639
00640
00641
00651
void remove (
const std::string & key);
00652
00653
00654
00668
void resize (
Size_t min);
00669
00670
00671
00672
virtual void writeMessage (
Message_t & msg)
const;
00673
00674
00675
00685
void read (std::istream & in);
00686
00687
00688
00694 void setType (
NCode_t type)
00695 {
00696 type_m = type;
00697 }
00698
00699
00700
00710
void write (std::ostream & out)
const;
00711
00712 };
00713
00714 }
00715
00716
#endif // #ifndef __IDMap_AMOS_HH