25.07.2017 Views

Intro-CSharp-Book-v2015

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Глава 18. Речници, хеш-таблици и множества 755<br />

public V this[K key]<br />

{<br />

get<br />

{<br />

return this.Get(key);<br />

}<br />

set<br />

{<br />

this.Set(key, value);<br />

}<br />

}<br />

public int Count<br />

{<br />

get<br />

{<br />

return this.size;<br />

}<br />

}<br />

public V Set(K key, V value)<br />

{<br />

List chain = this.FindChain(key, true);<br />

for (int i = 0; i < chain.Count; i++)<br />

{<br />

KeyValuePair entry = chain[i];<br />

if (entry.Key.Equals(key))<br />

{<br />

// Key found -> replace its value<br />

//with the new value<br />

KeyValuePair newEntry =<br />

new KeyValuePair(key, value);<br />

chain[i] = newEntry;<br />

return entry.Value;<br />

}<br />

}<br />

chain.Add(new KeyValuePair(key, value));<br />

if (size++ >= threshold)<br />

{<br />

this.Expand();<br />

}<br />

}<br />

return default(V);<br />

/// <br />

/// Expands the underling table<br />

///

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!