Skip to content

Commit 47c25a7

Browse files
committed
---
yaml --- r: 50928 b: refs/heads/try c: d774333 h: refs/heads/master v: v3
1 parent 59b56e8 commit 47c25a7

File tree

5 files changed

+89
-45
lines changed

5 files changed

+89
-45
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 5f13e9ccc2e3328d4cd8ca49f84e6840dd998346
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: f7a2371c176663d59062ec5158f39faecba45768
5-
refs/heads/try: 89a7407c999449c56f9b00145cfbdfbb49fbde7c
5+
refs/heads/try: d77433386be3d5afa201cfd40dbaaa4d20dbbd80
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libcore/container.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ pub trait Map<K, V>: Mutable {
3838
/// Iterate over the map and mutate the contained values
3939
fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool);
4040

41-
/// Return the value corresponding to the key in the map
41+
/// Return a reference to the value corresponding to the key
4242
fn find(&self, key: &K) -> Option<&'self V>;
4343

44+
/// Return a mutable reference to the value corresponding to the key
45+
//fn find_mut(&mut self, key: &K) -> Option<&'self mut V>;
46+
4447
/// Insert a key-value pair into the map. An existing value for a
4548
/// key is replaced by the new value. Return true if the key did
4649
/// not already exist in the map.

branches/try/src/libcore/hashmap.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub mod linear {
2424
use rand;
2525
use uint;
2626
use vec;
27+
use util::unreachable;
2728

2829
static INITIAL_CAPACITY: uint = 32u; // 2^5
2930

@@ -192,6 +193,14 @@ pub mod linear {
192193
}
193194
}
194195
196+
#[inline(always)]
197+
fn mut_value_for_bucket(&mut self, idx: uint) -> &'self mut V {
198+
match self.buckets[idx] {
199+
Some(ref mut bkt) => &mut bkt.value,
200+
None => unreachable()
201+
}
202+
}
203+
195204
/// Inserts the key value pair into the buckets.
196205
/// Assumes that there will be a bucket.
197206
/// True if there was no previous entry with that key
@@ -338,7 +347,7 @@ pub mod linear {
338347
}
339348
}
340349
341-
/// Return the value corresponding to the key in the map
350+
/// Return a reference to the value corresponding to the key
342351
fn find(&self, k: &K) -> Option<&'self V> {
343352
match self.bucket_for_key(k) {
344353
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
@@ -410,6 +419,17 @@ pub mod linear {
410419
old_value
411420
}
412421
422+
/// Return a mutable reference to the value corresponding to the key
423+
fn find_mut(&mut self, k: &K) -> Option<&'self mut V> {
424+
let idx = match self.bucket_for_key(k) {
425+
FoundEntry(idx) => idx,
426+
TableFull | FoundHole(_) => return None
427+
};
428+
unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
429+
Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx)))
430+
}
431+
}
432+
413433
/// Return the value corresponding to the key in the map, or insert
414434
/// and return the value if it doesn't exist.
415435
fn find_or_insert(&mut self, k: K, v: V) -> &'self V {
@@ -655,6 +675,19 @@ pub mod linear {
655675
fail_unless!(*m.get(&2) == 4);
656676
}
657677

678+
#[test]
679+
fn test_find_mut() {
680+
let mut m = LinearMap::new();
681+
fail_unless!(m.insert(1, 12));
682+
fail_unless!(m.insert(2, 8));
683+
fail_unless!(m.insert(5, 14));
684+
let new = 100;
685+
match m.find_mut(&5) {
686+
None => fail!(), Some(x) => *x = new
687+
}
688+
assert_eq!(m.find(&5), Some(&new));
689+
}
690+
658691
#[test]
659692
pub fn test_insert_overwrite() {
660693
let mut m = LinearMap::new();

branches/try/src/librustc/middle/trans/tvec.rs

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use middle::ty;
2727
use util::common::indenter;
2828
use util::ppaux::ty_to_str;
2929

30-
use core::option::None;
3130
use core::uint;
3231
use core::vec;
3332
use syntax::ast;
@@ -414,52 +413,30 @@ pub fn write_content(bcx: block,
414413
return bcx;
415414
}
416415

417-
let elem = unpack_datum!(bcx, {
416+
let tmpdatum = unpack_datum!(bcx, {
418417
expr::trans_to_datum(bcx, element)
419418
});
420419

421-
let next_bcx = sub_block(bcx, ~"expr_repeat: while next");
422-
let loop_bcx = loop_scope_block(bcx, next_bcx, None, ~"expr_repeat", None);
423-
let cond_bcx = scope_block(loop_bcx, None, ~"expr_repeat: loop cond");
424-
let set_bcx = scope_block(loop_bcx, None, ~"expr_repeat: body: set");
425-
let inc_bcx = scope_block(loop_bcx, None, ~"expr_repeat: body: inc");
426-
Br(bcx, loop_bcx.llbb);
427-
428-
let loop_counter = {
429-
// i = 0
430-
let i = alloca(loop_bcx, bcx.ccx().int_type);
431-
Store(loop_bcx, C_uint(bcx.ccx(), 0), i);
432-
433-
Br(loop_bcx, cond_bcx.llbb);
434-
i
435-
};
436-
437-
{ // i < count
438-
let lhs = Load(cond_bcx, loop_counter);
439-
let rhs = C_uint(bcx.ccx(), count);
440-
let cond_val = ICmp(cond_bcx, lib::llvm::IntULT, lhs, rhs);
441-
442-
CondBr(cond_bcx, cond_val, set_bcx.llbb, next_bcx.llbb);
443-
}
444-
445-
{ // v[i] = elem
446-
let i = Load(set_bcx, loop_counter);
447-
let lleltptr = InBoundsGEP(set_bcx, lldest, [i]);
448-
let set_bcx = elem.copy_to(set_bcx, INIT, lleltptr);
420+
let mut temp_cleanups = ~[];
449421

450-
Br(set_bcx, inc_bcx.llbb);
422+
for uint::range(0, count) |i| {
423+
let lleltptr = GEPi(bcx, lldest, [i]);
424+
if i < count - 1 {
425+
// Copy all but the last one in.
426+
bcx = tmpdatum.copy_to(bcx, INIT, lleltptr);
427+
} else {
428+
// Move the last one in.
429+
bcx = tmpdatum.move_to(bcx, INIT, lleltptr);
430+
}
431+
add_clean_temp_mem(bcx, lleltptr, vt.unit_ty);
432+
temp_cleanups.push(lleltptr);
451433
}
452434

453-
{ // i += 1
454-
let i = Load(inc_bcx, loop_counter);
455-
let plusone = Add(inc_bcx, i, C_uint(bcx.ccx(), 1));
456-
Store(inc_bcx, plusone, loop_counter);
457-
458-
Br(inc_bcx, cond_bcx.llbb);
435+
for vec::each(temp_cleanups) |cleanup| {
436+
revoke_clean(bcx, *cleanup);
459437
}
460438

461-
return next_bcx;
462-
439+
return bcx;
463440
}
464441
}
465442
}

branches/try/src/libstd/treemap.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> {
135135
mutate_values(&mut self.root, f);
136136
}
137137

138-
/// Return the value corresponding to the key in the map
138+
/// Return a reference to the value corresponding to the key
139139
fn find(&self, key: &K) -> Option<&'self V> {
140140
let mut current: &'self Option<~TreeNode<K, V>> = &self.root;
141141
loop {
@@ -189,6 +189,12 @@ pub impl<K: TotalOrd, V> TreeMap<K, V> {
189189
fn iter(&self) -> TreeMapIterator<'self, K, V> {
190190
TreeMapIterator{stack: ~[], node: &self.root}
191191
}
192+
193+
/// Return a mutable reference to the value corresponding to the key
194+
#[inline(always)]
195+
fn find_mut(&mut self, key: &K) -> Option<&'self mut V> {
196+
find_mut(&mut self.root, key)
197+
}
192198
}
193199

194200
/// Lazy forward iterator over a map
@@ -584,8 +590,20 @@ fn split<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>) {
584590
}
585591
}
586592

587-
fn insert<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>, key: K,
588-
value: V) -> bool {
593+
fn find_mut<K: TotalOrd, V>(node: &'r mut Option<~TreeNode<K, V>>, key: &K) -> Option<&'r mut V> {
594+
match *node {
595+
Some(ref mut x) => {
596+
match key.cmp(&x.key) {
597+
Less => find_mut(&mut x.left, key),
598+
Greater => find_mut(&mut x.right, key),
599+
Equal => Some(&mut x.value),
600+
}
601+
}
602+
None => None
603+
}
604+
}
605+
606+
fn insert<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>, key: K, value: V) -> bool {
589607
match *node {
590608
Some(ref mut save) => {
591609
match key.cmp(&save.key) {
@@ -716,6 +734,19 @@ mod test_treemap {
716734
fail_unless!(m.find(&2) == None);
717735
}
718736

737+
#[test]
738+
fn test_find_mut() {
739+
let mut m = TreeMap::new();
740+
fail_unless!(m.insert(1, 12));
741+
fail_unless!(m.insert(2, 8));
742+
fail_unless!(m.insert(5, 14));
743+
let new = 100;
744+
match m.find_mut(&5) {
745+
None => fail!(), Some(x) => *x = new
746+
}
747+
assert_eq!(m.find(&5), Some(&new));
748+
}
749+
719750
#[test]
720751
fn insert_replace() {
721752
let mut m = TreeMap::new();

0 commit comments

Comments
 (0)