Skip to content

Commit 591eefd

Browse files
committed
improve hashmap/treemap documentation
1 parent 988ce71 commit 591eefd

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/libcore/hashmap.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,15 @@ pub mod linear {
253253
}
254254

255255
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Container {
256+
/// Return the number of elements in the map
256257
pure fn len(&self) -> uint { self.size }
258+
259+
/// Return true if the map contains no elements
257260
pure fn is_empty(&self) -> bool { self.len() == 0 }
258261
}
259262

260263
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Mutable {
264+
/// Clear the map, removing all key-value pairs.
261265
fn clear(&mut self) {
262266
for uint::range(0, self.buckets.len()) |idx| {
263267
self.buckets[idx] = None;
@@ -267,13 +271,15 @@ pub mod linear {
267271
}
268272

269273
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Map<K, V> {
274+
/// Return true if the map contains a value for the specified key
270275
pure fn contains_key(&self, k: &K) -> bool {
271276
match self.bucket_for_key(self.buckets, k) {
272277
FoundEntry(_) => {true}
273278
TableFull | FoundHole(_) => {false}
274279
}
275280
}
276281

282+
/// Visit all key-value pairs
277283
pure fn each(&self, blk: fn(k: &K, v: &V) -> bool) {
278284
for vec::each(self.buckets) |slot| {
279285
let mut broke = false;
@@ -286,14 +292,17 @@ pub mod linear {
286292
}
287293
}
288294

295+
/// Visit all keys
289296
pure fn each_key(&self, blk: fn(k: &K) -> bool) {
290297
self.each(|k, _v| blk(k))
291298
}
292299

300+
/// Visit all values
293301
pure fn each_value(&self, blk: fn(v: &V) -> bool) {
294302
self.each(|_k, v| blk(v))
295303
}
296304

305+
/// Return the value corresponding to the key in the map
297306
pure fn find(&self, k: &K) -> Option<&self/V> {
298307
match self.bucket_for_key(self.buckets, k) {
299308
FoundEntry(idx) => {
@@ -314,6 +323,9 @@ pub mod linear {
314323
}
315324
}
316325

326+
/// Insert a key-value pair into the map. An existing value for a
327+
/// key is replaced by the new value. Return true if the key did
328+
/// not already exist in the map.
317329
fn insert(&mut self, k: K, v: V) -> bool {
318330
if self.size >= self.resize_at {
319331
// n.b.: We could also do this after searching, so
@@ -329,6 +341,8 @@ pub mod linear {
329341
self.insert_internal(hash, move k, move v)
330342
}
331343

344+
/// Remove a key-value pair from the map. Return true if the key
345+
/// was present in the map, otherwise false.
332346
fn remove(&mut self, k: &K) -> bool {
333347
match self.pop(k) {
334348
Some(_) => true,
@@ -448,11 +462,15 @@ pub mod linear {
448462
}
449463

450464
impl <T: Hash IterBytes Eq> LinearSet<T>: Container {
465+
/// Return the number of elements in the set
451466
pure fn len(&self) -> uint { self.map.len() }
467+
468+
/// Return true if the set contains no elements
452469
pure fn is_empty(&self) -> bool { self.map.is_empty() }
453470
}
454471

455472
impl <T: Hash IterBytes Eq> LinearSet<T>: Mutable {
473+
/// Clear the set, removing all values.
456474
fn clear(&mut self) { self.map.clear() }
457475
}
458476

src/libstd/treemap.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ impl <K: Ord, V> TreeMap<K, V> {
140140
/// Create an empty TreeMap
141141
static pure fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} }
142142

143-
144143
/// Visit all key-value pairs in reverse order
145144
pure fn each_reverse(&self, f: fn(&K, &V) -> bool) {
146145
each_reverse(&self.root, f);
@@ -207,10 +206,10 @@ impl <T: Eq Ord> TreeSet<T>: Eq {
207206
}
208207

209208
impl <T: Ord> TreeSet<T>: Container {
210-
/// Return the number of elements in the map
209+
/// Return the number of elements in the set
211210
pure fn len(&self) -> uint { self.map.len() }
212211

213-
/// Return true if the map contains no elements
212+
/// Return true if the set contains no elements
214213
pure fn is_empty(&self) -> bool { self.map.is_empty() }
215214
}
216215

0 commit comments

Comments
 (0)