ステップ付きの線形プロービングを使用して衝突を解決する効率的なハッシュテーブルを実装しようとしています。この機能は、可能な限り効率的である必要があります。不必要な=
操作==
はありません。私のコードは機能していますが、効率的ではありません。この効率は、社内システムによって評価されます。それはより良い必要があります。
キーと値のペアを表す2つのクラスがあります:CKey
とCValue
。これらのクラスにはそれぞれ、標準コンストラクター、コピーコンストラクター、およびオーバーライドされた演算子=
とがあり==
ます。どちらにも、getValue()
内部プライベート変数の値を返すメソッドが含まれています。getHashLPS()
の中には、ハッシュテーブル内CKey
のハッシュされた位置を返すメソッドもあります。
int getHashLPS(int tableSize,int step, int collision) const
{
return ((value + (i*step)) % tableSize);
}
ハッシュ表。
class CTable
{
struct CItem {
CKey key;
CValue value;
};
CItem **table;
int valueCounter;
}
メソッド
// return collisions count
int insert(const CKey& key, const CValue& val)
{
int position, collision = 0;
while(true)
{
position = key.getHashLPS(tableSize, step, collision); // get position
if(table[position] == NULL) // free space
{
table[position] = new CItem; // save item
table[position]->key = CKey(key);
table[position]->value = CValue(val);
valueCounter++;
break;
}
if(table[position]->key == key) // same keys => overwrite value
{
table[position]->value = val;
break;
}
collision++; // current positions is full, try another
if(collision >= tableSize) // full table
return -1;
}
return collision;
}
// return collisions count
int remove(const CKey& key)
{
int position, collision = 0;
while(true)
{
position = key.getHashLPS(tableSize, step, collision);
if(table[position] == NULL) // free position - key isn't in table or is unreachable bacause of wrong rehashing
return -1;
if(table[position]->key == key) // found
{
table[position] = NULL; // remove it
valueCounter--;
int newPosition, collisionRehash = 0;
for(int i = 0; i < tableSize; i++, collisionRehash = 0) // rehash table
{
if(table[i] != NULL) // if there is a item, rehash it
{
while(true)
{
newPosition = table[i]->key.getHashLPS(tableSize, step, collisionRehash++);
if(newPosition == i) // same position like before
break;
if(table[newPosition] == NULL) // new position and there is a free space
{
table[newPosition] = table[i]; // copy from old, insert to new
table[i] = NULL; // remove from old
break;
}
}
}
}
break;
}
collision++; // there is some item on newPosition, let's count another
if(collision >= valueCounter) // item isn't in table
return -1;
}
return collision;
}
どちらの関数も(私自身の目的で)衝突カウントを返し-1
、検索対象CKey
がテーブルにない場合、またはテーブルがいっぱいの場合に戻ります。
墓石は禁止されています。取り外した後の再ハッシュは必須です。