数据结构之散列例程

int hash( const string & key, int tableSzie )
{
   int hashVal = 0;
   for(int i = 0; i< key.length(); i++)
          hashVal += key[i];

    return hashVal % tableSize;
}

int hash( const string & key, int tableSize )
{
   return( key[0] + 27*key[1] +729*key[2]) % tableSize;
}

/**
 * A hash routine for string objects.
 */
int hash( const string & key, int tableSize )
{
  int hashVal = 0;

  for( int i=0; i<key.length() ; i++)
      hashVal = 37 * hashVal + key[i];

  hashVal %= tableSize;
  if( hashVal < 0 )
       hashVal += tableSize;

  return hashVal;
}


/*图 5-6 分离链接散列表的类构架*/
template<typename HashedObj>
class HashTable
{
  public:
     explicit HashTable( int size = 101 );
      
      bool contains( const HashedObj & x ) const;
       
       void makeEmpty();
       void insert( const HashedObj & x );
       void remove( const HashedObj & x );

  private:
      vector<list<HashedObj> > theLists;
       int currentSize;

       void rehash();
        int myhash( const HashedObj & x ) const;
};
  int hash( const string & key );
  int hash( int key );


/*散列表的myHash成员函数*/
int myhash( const HashedObj & x ) const
{
  int hashVal = hash(x);

  hashVal %= theLists.size();
  if( hashVal < 0 )
       hashVal += theLists.size();
  return hashVal;
}

/* Fig5-8 可以作为HashedObj 使用的类的一个例子*/
// Example of an Employee class
class Employee
{
  public:
      const string & getName() const
         { return name; }
   
  bool operator==( const Employee & rhs ) const
          { return getName() == ths.tetName(); }
  bool operator != ( const Employee & rhs ) const
             { return !( *this == rhs;}
         //Additional pulic members not shown
  pribate:
       string name;
       double salary;
        int seniority;
      
          //Additional private members not shown
};
int hash( const Employee & item )
{
    return hash( item.getName() );
}

/*fig5-9 分离链接散列表的makeEmpty、contains、remove 例程*/
void makeEmpty()
{ 
   for( int i=0; i< theLists.size(); i++ )
       theLists[i].clear();
}

bool contains( const HashedObj & x ) const
{
   const list<HashedObj> & whichList = theLists[ myhash( x ) ];
   return find( whichList.begin(), whichList.end(), x ) != whichList.end();
}
bool remove( const HashedObj & x )
{
  list<HashedObj> & whichList = theLists[ myhash(x ) ];
   list<HashedObj>::iterator itr = find(whichList.begin(),whichList.end(),x);

   if( itr == whichList.end() )
        return false;
    
    whichList.erase( itr );
      --currentSize;
      return true;
}

/*fig5-10 分离链接散列表的insert例程*/
bool insert( const HashedObj & x )
{
   list<HashedObj> & whichList = theLists[ myhash(x) ];
    if( fid( whichList.begin(), whichList.end(), x) != whichList.end() )
          return false;
      whichList.push_back( x );
 
        // Rehash; see Section 5.5
     if( ++currentSize > theLists.size() )
         rehash();
 
      return true;
}

/*fig 5-14 使用探测策略的散列表的类接口,包括嵌套的HashEntry类*/
class HashTable
{
  public:
    explicit HashTable( int size = 101 );

    bool contains( const HashedObj & x ) const;

     void makeEmpty();
      bool insert( const HashedObj & x );
      bool remove( const HashedObj & x );
      
      enum EntryType( ACTIV,EMPTY,DELETED );
  private:
     struct HashEntry
        {
           HashedObj element;
           EntryType info;
    
            HashEntry( const HashedObj & e = HashedObj(), EntryType i = EMPTY )
               : element(e), info(i) {}
        };
       vector<HashEntry> array;
         int currentSize;

         bool isActive( int currentPos ) const;
         int findPos( const HashedObj & x ) const;
         void rehash();
         int myhash( const HashedObj & x ) const;
};

/*fig5-15 初始化平方探测散列表的例程*/
explicit HashTable( int size = 101 ) :array( nextPrime( size ))
    { makeEmpty()  ; }

void makeEmpty()
{
   currentSize = 0;
    for( int i = 0; i< array.size(); i++)
          array[i].info = EMPTY;
}

/*fig 5-16 使用平方探测进行散列的contains例程(和它的private助手)*/
bool contains( const HashedObj & x ) const
  { return isActive( findPos(x) );}

int findPos( const HashedObj & x ) const
{
  int offset = 1;
  int currentPos = myhash(x );
  
  while( array[ currentPos ].info != EMPTY &&
         array[ currentPos ].element != x )
 {
  currentPos += offset; // Compue ith probe
  offset += 2;
  if( currentPos >= array.size() )
      currentPos -= array.size();
 }
  return currentPos;
}

bool isActive( int currentPos ) const
       { return array [ currentPos ].info == ACTIVE; }

/* 使用平方探测的散列表的insert和remove例程*/
bool insert( const HashedObj & x )
{
  //Insert x as active
   int currentPos = findPos( x );
   if( isActive( currentPos ) )
       return false;
  
   array[ currentPos ] = HashEntry( x, ACTIVE );

     //Rehash; see Section 5.5
   if( ++currentSize > array.size() / 2 )
       rehash();

     return true;
}

bool remove( const HashedObj & x )
{ 
  int currentPos = findPos( x );
  if( !isActive( currentPos ) )
      return false;

  array[ currentPos ].info = DELETED;
  return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值