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