作者ric2k1 (Ric)
看板EE_DSnP
標題Re: [問題] myHash 的 functions
時間Tue Jan 4 21:28:30 2011
※ 引述《TommyKSHS (湯米)》之銘言:
: 我想問的是
: check() 的 comment :
: 127 // check if k is in the hash...
: 128 // if yes, update n and return true;
: 129 // else return false;
: 就是 update n 的意思我不太懂…
: 意思是 check () 會改變 hash table 裡面的某項元素嗎?
bool check(const HashKey& k, HashData& n) const
check 的意思就是檢查 hash 裡面是否有已經含有 HashNode(k, *) 的 data,
如果沒找到,return false;
如果找到,就把項對應的 HashData assign 到 n 上面 (因為 n 是用 ref傳進來的)
這樣做的原因有三:
1. 檢查的時候順便取得資料。
2. 拿到的 n 時實際上存在 Hash 裏頭的 HashData (其分身),
而不只是它的一份 copy (if pass by object)
3. 省去 copy HashData 的時間
: 另外我也不懂 forceInsert() 和 replaceInsert() 的用意是什麼 Orz
: ( 已經爬過文了還是不懂 QQ
void forceInsert(const HashKey& k, const HashData& d)
通常用在 check() return false 之後,
就是確定 k 不在 Hash 裡面之後,呼叫 forceInsert 把你想要 insert 的 data 存入
Example:
if (hash.check(k, n)) {
// This is n is a valid data
}
else {
// d is what you want to insert
forceInsert(k, d);
}
bool replaceInsert(const HashKey& k, const HashData& d)
先檢查 Hash 裡面是否有含有 HashKey = k 的 HashNode,
如果不在,就 insert and return true (successful)
否則 ==> HashNode(k, *) 本來就在 Hash 裡面,
強制將它取代成 HashNode(k, d) 且 return false
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.36.54.155
1F:推 TommyKSHS:嗯嗯 謝謝老師! 我懂了 01/04 21:41