@@ -352,16 +352,27 @@ impl<K:Hash + Eq,V> Map<K, V> for HashMap<K, V> {
352
352
}
353
353
354
354
/// Return a mutable reference to the value corresponding to the key
355
+ #[cfg(stage0)]
355
356
fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V> {
356
357
let idx = match self.bucket_for_key(k) {
357
358
FoundEntry(idx) => idx,
358
359
TableFull | FoundHole(_) => return None
359
360
};
360
- unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
361
+ unsafe {
361
362
Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx)))
362
363
}
363
364
}
364
365
366
+ /// Return a mutable reference to the value corresponding to the key
367
+ #[cfg(not(stage0))]
368
+ fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V> {
369
+ let idx = match self.bucket_for_key(k) {
370
+ FoundEntry(idx) => idx,
371
+ TableFull | FoundHole(_) => return None
372
+ };
373
+ Some(self.mut_value_for_bucket(idx))
374
+ }
375
+
365
376
/// Insert a key-value pair into the map. An existing value for a
366
377
/// key is replaced by the new value. Return true if the key did
367
378
/// not already exist in the map.
@@ -424,6 +435,7 @@ pub impl<K: Hash + Eq, V> HashMap<K, V> {
424
435
425
436
/// Return the value corresponding to the key in the map, or insert
426
437
/// and return the value if it doesn't exist.
438
+ #[cfg(stage0)]
427
439
fn find_or_insert<'a>(&'a mut self, k: K, v: V) -> &'a V {
428
440
if self.size >= self.resize_at {
429
441
// n.b.: We could also do this after searching, so
@@ -447,13 +459,43 @@ pub impl<K: Hash + Eq, V> HashMap<K, V> {
447
459
},
448
460
};
449
461
450
- unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
462
+ unsafe {
451
463
::cast::transmute_region(self.value_for_bucket(idx))
452
464
}
453
465
}
454
466
467
+ /// Return the value corresponding to the key in the map, or insert
468
+ /// and return the value if it doesn't exist.
469
+ #[cfg(not(stage0))]
470
+ fn find_or_insert<'a>(&'a mut self, k: K, v: V) -> &'a V {
471
+ if self.size >= self.resize_at {
472
+ // n.b.: We could also do this after searching, so
473
+ // that we do not resize if this call to insert is
474
+ // simply going to update a key in place. My sense
475
+ // though is that it's worse to have to search through
476
+ // buckets to find the right spot twice than to just
477
+ // resize in this corner case.
478
+ self.expand();
479
+ }
480
+
481
+ let hash = k.hash_keyed(self.k0, self.k1) as uint;
482
+ let idx = match self.bucket_for_key_with_hash(hash, &k) {
483
+ TableFull => fail!(~" Internal logic error"),
484
+ FoundEntry(idx) => idx,
485
+ FoundHole(idx) => {
486
+ self.buckets[idx] = Some(Bucket{hash: hash, key: k,
487
+ value: v});
488
+ self.size += 1;
489
+ idx
490
+ },
491
+ };
492
+
493
+ self.value_for_bucket(idx)
494
+ }
495
+
455
496
/// Return the value corresponding to the key in the map, or create,
456
497
/// insert, and return a new value if it doesn't exist.
498
+ #[cfg(stage0)]
457
499
fn find_or_insert_with<'a>(&'a mut self, k: K, f: &fn(&K) -> V) -> &'a V {
458
500
if self.size >= self.resize_at {
459
501
// n.b.: We could also do this after searching, so
@@ -478,11 +520,41 @@ pub impl<K: Hash + Eq, V> HashMap<K, V> {
478
520
},
479
521
};
480
522
481
- unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
523
+ unsafe {
482
524
::cast::transmute_region(self.value_for_bucket(idx))
483
525
}
484
526
}
485
527
528
+ /// Return the value corresponding to the key in the map, or create,
529
+ /// insert, and return a new value if it doesn't exist.
530
+ #[cfg(not(stage0))]
531
+ fn find_or_insert_with<'a>(&'a mut self, k: K, f: &fn(&K) -> V) -> &'a V {
532
+ if self.size >= self.resize_at {
533
+ // n.b.: We could also do this after searching, so
534
+ // that we do not resize if this call to insert is
535
+ // simply going to update a key in place. My sense
536
+ // though is that it's worse to have to search through
537
+ // buckets to find the right spot twice than to just
538
+ // resize in this corner case.
539
+ self.expand();
540
+ }
541
+
542
+ let hash = k.hash_keyed(self.k0, self.k1) as uint;
543
+ let idx = match self.bucket_for_key_with_hash(hash, &k) {
544
+ TableFull => fail!(~" Internal logic error"),
545
+ FoundEntry(idx) => idx,
546
+ FoundHole(idx) => {
547
+ let v = f(&k);
548
+ self.buckets[idx] = Some(Bucket{hash: hash, key: k,
549
+ value: v});
550
+ self.size += 1;
551
+ idx
552
+ },
553
+ };
554
+
555
+ self.value_for_bucket(idx)
556
+ }
557
+
486
558
fn consume(&mut self, f: &fn(K, V)) {
487
559
let mut buckets = ~[];
488
560
self.buckets <-> buckets;
0 commit comments