10 #pragma pack(push, 1) // pack class size 
   11 template <
class TKey, 
class TDat>
 
   19     HashCd(-1), Key(), Dat(){}
 
   21     HashCd(_HashCd), Key(_Key), Dat(){}
 
   23     HashCd(SIn), Key(SIn), Dat(SIn){}
 
   25     HashCd.
Save(SOut); Key.Save(SOut); Dat.Save(SOut);}
 
   28     if (
this==&HashKeyDat || (HashCd==HashKeyDat.
HashCd 
   29       && Key==HashKeyDat.
Key && Dat==HashKeyDat.
Dat)){
return true;}
 
   32     if (
this!=&HashKeyDat){
 
   33       HashCd=HashKeyDat.
HashCd; Key=HashKeyDat.
Key;
 
   41 template<
class TKey, 
class TDat>
 
   51     KeyDatI(_HashKeyDatI.KeyDatI), EndI(_HashKeyDatI.EndI){}
 
   53     KeyDatI((TPHKeyDat*)_KeyDatI), EndI((TPHKeyDat*)_EndI){}
 
   56     KeyDatI=HashKeyDatI.
KeyDatI; EndI=HashKeyDatI.
EndI; 
return *
this;}
 
   58     return KeyDatI==HashKeyDatI.
KeyDatI;}
 
   60     return KeyDatI<HashKeyDatI.
KeyDatI;}
 
   69   bool IsEmpty()
 const { 
return KeyDatI == NULL; }
 
   80 template<
class TKey, 
class TDat, 
class THashFunc = TDefaultHashFunc<TKey> >
 
   98       Hash(_Hash), CmpKey(_CmpKey), Asc(_Asc) { }
 
   99     bool operator () (
const int& KeyId1, 
const int& KeyId2)
 const {
 
  101         if (Asc) { 
return Hash.
GetKey(KeyId1) < Hash.
GetKey(KeyId2); }
 
  102         else { 
return Hash.
GetKey(KeyId2) < Hash.
GetKey(KeyId1); } }
 
  104         if (Asc) { 
return Hash[KeyId1] < Hash[KeyId2]; }
 
  105         else { 
return Hash[KeyId2] < Hash[KeyId1]; } } }
 
  109     TPHKeyDat& KeyDat=Table[KeyId];
 
  112     const TPHKeyDat& KeyDat=Table[KeyId];
 
  118     Table(), NumVals(0){}
 
  120     Table(PHash.Table), NumVals(PHash.NumVals){}
 
  124     Table(SIn), NumVals(SIn){
 
  130     Table.
Save(SOut); NumVals.
Save(SOut);
 
  144       int64 MemUsed = 
sizeof(int);
 
  145       for (
int TableN = 0; TableN < Table.
Len(); TableN++) {
 
  160   void Gen(
const int& ExpectVals){
 
  163   void Clr(
const bool& DoDel=
true);
 
  166   void SetLen(
const int& Length) {NumVals=Length;}
 
  172   int AddKey(
const TKey& Key);
 
  173   int AddKey11(
const int& Idx, 
const TKey& Key, 
bool& Found);
 
  174   int AddKey12(
const int& Idx, 
const TKey& Key, 
bool& Found);
 
  175   int AddKey13(
const int& Idx, 
const TKey& Key);
 
  176   int AddKey1(
const TKey& Key, 
bool& Found);
 
  177   int AddKey2(
const int& Idx, 
const TKey& Key, 
bool& Found);
 
  179     int KeyId=
AddKey(Key); 
return Table[KeyId].Dat=KeyId;}
 
  182   TDat& 
AddDat(
const TKey& Key, 
const TDat& Dat){
 
  183     return Table[
AddKey(Key)].Dat=Dat;}
 
  186   int GetKeyId(
const TKey& Key) 
const;
 
  192   bool IsKey(
const TKey& Key, 
int& KeyId)
 const { KeyId=
GetKeyId(Key); 
return KeyId!=-1;}
 
  194     return (0<=KeyId)&&(KeyId<Table.
Len())&&(Table[KeyId].HashCd!=-1);}
 
  200   void GetKeyDat(
const int& KeyId, TKey& Key, TDat& Dat)
 const {
 
  202     Key=KeyDat.
Key; Dat=KeyDat.
Dat;}
 
  205     else {
return false;}}
 
  224 template<
class TKey, 
class TDat, 
class THashFunc>
 
  226   3ul, 5ul, 11ul, 23ul,
 
  227   53ul,         97ul,         193ul,       389ul,       769ul,
 
  228   1543ul,       3079ul,       6151ul,      12289ul,     24593ul,
 
  229   49157ul,      98317ul,      196613ul,    393241ul,    786433ul,
 
  230   1572869ul,    3145739ul,    6291469ul,   12582917ul,  25165843ul,
 
  231   50331653ul,   100663319ul,  201326611ul, 402653189ul, 805306457ul,
 
  232   1610612741ul, 3221225473ul, 4294967291ul
 
  235 template<
class TKey, 
class TDat, 
class THashFunc>
 
  237   const uint* f=(
const uint*)HashPrimeT, *m, *l=(
const uint*)HashPrimeT + (int)HashPrimes;
 
  238   int h, len = (int)HashPrimes;
 
  240     h = len >> 1;  m = f + h;
 
  241     if (*m < Val) { f = m;  f++;  len = len - h - 1; }
 
  244   return f == l ? *(l - 1) : *f;
 
  247 template<
class TKey, 
class TDat, 
class THashFunc>
 
  249   if (Len() != Hash.
Len()) { 
return false; }
 
  250   for (
int i = FFirstKeyId(); FNextKeyId(i); ) {
 
  251     const TKey& Key = GetKey(i);
 
  252     if (! Hash.
IsKey(Key)) { 
return false; }
 
  253     if (GetDat(Key) != Hash.
GetDat(Key)) { 
return false; }
 
  258 template<
class TKey, 
class TDat, 
class THashFunc>
 
  263   const int BegTableN=abs(Key.GetPrimHashCd()%Table.Len());
 
  264   const int HashCd=abs(Key.GetSecHashCd());
 
  266   int TableN = BegTableN;
 
  267   while (Table[TableN].HashCd != -1 || 
 
  268     (!__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, HashCd))) {
 
  269     TableN = (TableN + 1) % Table.Len();    
 
  271   Table[TableN].Key = Key;
 
  272   __sync_fetch_and_add(&NumVals.Val, 1);
 
  276 template<
class TKey, 
class TDat, 
class THashFunc>
 
  281   const int Length = Table.Len();
 
  282   const int HashCd=abs(Key.GetSecHashCd());
 
  284   int TableN = BegTableN;
 
  287     if (Table[TableN].HashCd.Val != -1) {
 
  288       if (Table[TableN].Key == Key) {
 
  292     } 
else if (__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, HashCd)) {
 
  297     if (TableN >= Length) {
 
  302   Table[TableN].Key = Key;
 
  306 template<
class TKey, 
class TDat, 
class THashFunc>
 
  311   const int Length = Table.Len();
 
  319   int TableN = BegTableN;
 
  321     int HashCd = Table[TableN].HashCd.Val;
 
  324       if (__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, 1)) {
 
  332         while (!__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, 2, 2)) {
 
  336       if (Table[TableN].Key == Key) {
 
  343       if (TableN >= Length) {
 
  350   Table[TableN].Key = Key;
 
  351   Table[TableN].HashCd.Val = 2;
 
  357 template<
class TKey, 
class TDat, 
class THashFunc>
 
  362   const int Length = Table.Len();
 
  370   int TableN = BegTableN;
 
  372     int HashCd = Table[TableN].HashCd.Val;
 
  375       if (__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, 1)) {
 
  382       if (TableN >= Length) {
 
  389   Table[TableN].Key = Key;
 
  390   Table[TableN].HashCd.Val = 2;
 
  395 template<
class TKey, 
class TDat, 
class THashFunc>
 
  397   const int Length = Table.Len();
 
  399   const int BegTableN = abs(Key.GetPrimHashCd() % Length);
 
  400   const int HashCd = abs(Key.GetSecHashCd());
 
  404   int TableN = BegTableN;
 
  405   while (Table[TableN].HashCd.Val != -1) {
 
  406     if (Table[TableN].Key == Key) {
 
  411     if (TableN >= Length) {
 
  420   Table[TableN].HashCd.Val = HashCd;
 
  421   Table[TableN].Key = Key;
 
  427 template<
class TKey, 
class TDat, 
class THashFunc>
 
  430   const int Length = Table.Len();
 
  431   const int HashCd = abs(Key.GetSecHashCd());
 
  435   int TableN = BegTableN;
 
  436   while (Table[TableN].HashCd.Val != -1) {
 
  437     if (Table[TableN].Key == Key) {
 
  442     if (TableN >= Length) {
 
  451   Table[TableN].HashCd.Val = HashCd;
 
  452   Table[TableN].Key = Key;
 
  458 template<
class TKey, 
class TDat, 
class THashFunc>
 
  460   const int BegTableN=abs(Key.GetPrimHashCd()%Table.Len());
 
  463   int TableN = BegTableN;
 
  464   while (Table[TableN].HashCd != -1) {
 
  465     if (Table[TableN].Key == Key) { 
return TableN; }
 
  466     TableN = (TableN + 1) % Table.Len();
 
  467     if (TableN == BegTableN) { 
return -1; }
 
  473 template<
class TKey, 
class TDat, 
class THashFunc>
 
  483 template<
class TKey, 
class TDat, 
class THashFunc>
 
  485   do {KeyId++;} 
while ((KeyId<Table.Len())&&(Table[KeyId].HashCd==-1));
 
  486   return KeyId<Table.Len();
 
  489 template<
class TKey, 
class TDat, 
class THashFunc>
 
  492   int KeyId=FFirstKeyId();
 
  493   while (FNextKeyId(KeyId)){
 
  494     KeyV.
Add(GetKey(KeyId));}
 
  497 template<
class TKey, 
class TDat, 
class THashFunc>
 
  500   int KeyId=FFirstKeyId();
 
  501   while (FNextKeyId(KeyId)){
 
  502     DatV.
Add(GetPHashKeyDat(KeyId).Dat);}
 
  505 template<
class TKey, 
class TDat, 
class THashFunc>
 
  507   KeyDatPrV.Gen(Len(), 0);
 
  509   int KeyId=FFirstKeyId();
 
  510   while (FNextKeyId(KeyId)){
 
  511     GetKeyDat(KeyId, Key, Dat);
 
  516 template<
class TKey, 
class TDat, 
class THashFunc>
 
  518   DatKeyPrV.Gen(Len(), 0);
 
  520   int KeyId=FFirstKeyId();
 
  521   while (FNextKeyId(KeyId)){
 
  522     GetKeyDat(KeyId, Key, Dat);
 
  527 template<
class TKey, 
class TDat, 
class THashFunc>
 
  529   KeyDatKdV.Gen(Len(), 0);
 
  531   int KeyId=FFirstKeyId();
 
  532   while (FNextKeyId(KeyId)){
 
  533     GetKeyDat(KeyId, Key, Dat);
 
  538 template<
class TKey, 
class TDat, 
class THashFunc>
 
  540   DatKeyKdV.Gen(Len(), 0);
 
  542   int KeyId=FFirstKeyId();
 
  543   while (FNextKeyId(KeyId)){
 
  544     GetKeyDat(KeyId, Key, Dat);
 
  549 template<
class TKey, 
class TDat, 
class THashFunc>
 
  552     Table.Swap(Hash.
Table);
 
  557 template<
class TKey, 
class TDat, 
class THashFunc>
 
  559   printf(
"*** ERROR *** THashMP<TKey, TDat, THashFunc>::GetRndKeyId called\n");
 
  563 template<
class TKey, 
class TDat, 
class THashFunc>
 
  565   printf(
"*** ERROR *** THashMP<TKey, TDat, THashFunc>::GetRndKeyId called\n");
 
TPair< TKey, TDat > TKeyDatP
 
THashMP(const int &ExpectVals)
 
TIter GetI(const TKey &Key) const 
 
THashMPKeyDatI(const TPHKeyDat *_KeyDatI, const TPHKeyDat *_EndI)
 
TIter EndI() const 
Returns an iterator referring to the past-the-end element in the vector. 
 
TDat & GetDat(const TKey &Key)
 
uint GetNextPrime(const uint &Val) const 
 
TSizeTy Reserved() const 
Returns the size of allocated storage capacity. 
 
void Save(TSOut &SOut) const 
 
void Save(TSOut &SOut) const 
 
::TSize GetMemUsed() const 
 
int AddKey1(const TKey &Key, bool &Found)
 
const TPHKeyDat & GetPHashKeyDat(const int &KeyId) const 
 
TSizeTy Len() const 
Returns the number of elements in the vector. 
 
THashMPKeyDatCmp(THashMP< TKey, TDat, THashFunc > &_Hash, const bool &_CmpKey, const bool &_Asc)
 
void SetLen(const int &Length)
 
void GetKeyV(TVec< TKey > &KeyV) const 
 
bool operator()(const int &KeyId1, const int &KeyId2) const 
 
const TDat & operator[](const int &KeyId) const 
 
TSizeTy GetMemUsed() const 
Returns the memory footprint (the number of bytes) of the vector. 
 
THashMP & operator=(const THashMP &Hash)
 
bool IsKeyId(const int &KeyId) const 
 
TDat & AddDatId(const TKey &Key)
 
void Save(TSOut &SOut) const 
 
THashMPKeyDat & operator=(const THashMPKeyDat &HashKeyDat)
 
bool IsKey(const TKey &Key, int &KeyId) const 
 
THashMPKeyDatI & operator=(const THashMPKeyDatI &HashKeyDatI)
 
THashMPKeyDatI(const THashMPKeyDatI &_HashKeyDatI)
 
void GetDatKeyKdV(TVec< TKeyDat< TDat, TKey > > &DatKeyKdV) const 
 
static const unsigned int HashPrimeT[HashPrimes]
 
int AddKey2(const int &Idx, const TKey &Key, bool &Found)
 
const THashMP< TKey, TDat, THashFunc > & Hash
 
bool operator<(const THashMPKeyDatI &HashKeyDatI) const 
 
void GetDatKeyPrV(TVec< TPair< TDat, TKey > > &DatKeyPrV) const 
 
bool IsKey(const TKey &Key) const 
 
int AddKey11(const int &Idx, const TKey &Key, bool &Found)
 
bool FNextKeyId(int &KeyId) const 
 
THashMPKeyDat< TKey, TDat > TPHKeyDat
 
THashMPKeyDat(const int &_HashCd, const TKey &_Key)
 
THashMPKeyDat< TKey, TDat > TPHKeyDat
 
bool IsKeyGetDat(const TKey &Key, TDat &Dat) const 
 
bool IsEmpty() const 
Tests whether the iterator has been initialized. 
 
int GetReservedKeyIds() const 
 
TPHKeyDat * operator->() const 
 
int GetRndKeyId(TRnd &Rnd) const 
Get an index of a random element. If the hash table has many deleted keys, this may take a long time...
 
void Gen(const int &ExpectVals)
 
TPHKeyDat & operator*() const 
 
THashMP(const THashMP &PHash)
 
bool IsEnd() const 
Tests whether the iterator is pointing to the past-end element. 
 
TDat & operator()(const TKey &Key)
 
TIter BegI() const 
Returns an iterator pointing to the first element in the vector. 
 
void Pack()
Reduces vector capacity (frees memory) to match its size. 
 
const TKey & GetKey() const 
 
const TDat & GetDat() const 
 
Hash-Table with multiprocessing support. 
 
bool operator==(const THashMPKeyDatI &HashKeyDatI) const 
 
void Clr(const bool &DoDel=true)
 
void GetKeyDatKdV(TVec< TKeyDat< TKey, TDat > > &KeyDatKdV) const 
 
TDat & AddDat(const TKey &Key, const TDat &Dat)
 
int AddKey(const TKey &Key)
 
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements. 
 
int AddKey12(const int &Idx, const TKey &Key, bool &Found)
 
bool operator==(const THashMP &Hash) const 
 
int AddKey13(const int &Idx, const TKey &Key)
 
void GetKeyDat(const int &KeyId, TKey &Key, TDat &Dat) const 
 
TDat & operator[](const int &KeyId)
 
THashMPKeyDatI & operator++(int)
 
THashMPKeyDatI< TKey, TDat > TIter
 
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element. 
 
TDat & AddDat(const TKey &Key)
 
TPHKeyDat & operator()() const 
 
int GetKeyId(const TKey &Key) const 
 
bool operator==(const THashMPKeyDat &HashKeyDat) const 
 
const TDat & GetDat(const TKey &Key) const 
 
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const 
 
TPHKeyDat & GetPHashKeyDat(const int &KeyId)
 
THashMPKeyDatI & operator--(int)
 
void GetDatV(TVec< TDat > &DatV) const 
 
void Swap(TRec &Rec1, TRec &Rec2)
 
Vector is a sequence TVal objects representing an array that can change in size. 
 
void Save(TSOut &SOut) const 
 
const TKey & GetKey(const int &KeyId) const