Skip to content

Commit 4ab8350

Browse files
committed
---
yaml --- r: 138726 b: refs/heads/try2 c: 2fa2ad5 h: refs/heads/master v: v3
1 parent c8e6b4c commit 4ab8350

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: c4075492ad46a343bd99d6f258e38ed9c2320418
8+
refs/heads/try2: 2fa2ad59958d0028c856fb68359edb9a7bd9cab8
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcore/cmp.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ pub pure fn gt<T:Ord>(v1: &T, v2: &T) -> bool {
150150
(*v1).gt(v2)
151151
}
152152

153+
/// The equivalence relation. Two values may be equivalent even if they are
154+
/// of different types. The most common use case for this relation is
155+
/// container types; e.g. it is often desirable to be able to use `&str`
156+
/// values to look up entries in a container with `~str` keys.
157+
pub trait Equiv<T> {
158+
pure fn equiv(&self, other: &T) -> bool;
159+
}
160+
153161
#[inline(always)]
154162
pub pure fn min<T:Ord>(v1: T, v2: T) -> T {
155163
if v1 < v2 { v1 } else { v2 }

branches/try2/src/libcore/container.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
//! Container traits
1212
13+
use cmp::Equiv;
1314
use option::Option;
1415

1516
pub trait Container {

branches/try2/src/libcore/hashmap.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/// Open addressing with linear probing.
1414
pub mod linear {
1515
use container::{Container, Mutable, Map, Set};
16-
use cmp::Eq;
16+
use cmp::{Eq, Equiv};
1717
use hash::Hash;
1818
use to_bytes::IterBytes;
1919
use iter::BaseIter;
@@ -107,6 +107,15 @@ pub mod linear {
107107
self.bucket_for_key_with_hash(hash, k)
108108
}
109109

110+
#[inline(always)]
111+
pure fn bucket_for_key_equiv<Q:Hash + IterBytes + Equiv<K>>(
112+
&self,
113+
k: &Q)
114+
-> SearchResult {
115+
let hash = k.hash_keyed(self.k0, self.k1) as uint;
116+
self.bucket_for_key_with_hash_equiv(hash, k)
117+
}
118+
110119
#[inline(always)]
111120
pure fn bucket_for_key_with_hash(&self,
112121
hash: uint,
@@ -122,6 +131,24 @@ pub mod linear {
122131
TableFull
123132
}
124133

134+
#[inline(always)]
135+
pure fn bucket_for_key_with_hash_equiv<Q:Equiv<K>>(&self,
136+
hash: uint,
137+
k: &Q)
138+
-> SearchResult {
139+
let _ = for self.bucket_sequence(hash) |i| {
140+
match self.buckets[i] {
141+
Some(ref bkt) => {
142+
if bkt.hash == hash && k.equiv(&bkt.key) {
143+
return FoundEntry(i);
144+
}
145+
},
146+
None => return FoundHole(i)
147+
}
148+
};
149+
TableFull
150+
}
151+
125152
/// Expand the capacity of the array to the next power of two
126153
/// and re-insert each of the existing buckets.
127154
#[inline(always)]
@@ -450,6 +477,28 @@ pub mod linear {
450477
None => fail!(fmt!("No entry found for key: %?", k)),
451478
}
452479
}
480+
481+
/// Return true if the map contains a value for the specified key,
482+
/// using equivalence
483+
pure fn contains_key_equiv<Q:Hash + IterBytes + Equiv<K>>(
484+
&self,
485+
key: &Q)
486+
-> bool {
487+
match self.bucket_for_key_equiv(key) {
488+
FoundEntry(_) => {true}
489+
TableFull | FoundHole(_) => {false}
490+
}
491+
}
492+
493+
/// Return the value corresponding to the key in the map, using
494+
/// equivalence
495+
pure fn find_equiv<Q:Hash + IterBytes + Equiv<K>>(&self, k: &Q)
496+
-> Option<&self/V> {
497+
match self.bucket_for_key_equiv(k) {
498+
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
499+
TableFull | FoundHole(_) => None,
500+
}
501+
}
453502
}
454503

455504
impl<K:Hash + IterBytes + Eq,V:Eq> Eq for LinearMap<K, V> {

branches/try2/src/libcore/str.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use at_vec;
2121
use cast;
2222
use char;
23-
use cmp::{TotalOrd, Ordering, Less, Equal, Greater};
23+
use cmp::{Equiv, TotalOrd, Ordering, Less, Equal, Greater};
2424
use libc;
2525
use option::{None, Option, Some};
2626
use ptr;
@@ -898,6 +898,12 @@ impl Ord for @str {
898898
pure fn gt(&self, other: &@str) -> bool { gt((*self), (*other)) }
899899
}
900900

901+
#[cfg(notest)]
902+
impl Equiv<~str> for &str {
903+
#[inline(always)]
904+
pure fn equiv(&self, other: &~str) -> bool { eq_slice(*self, *other) }
905+
}
906+
901907
/*
902908
Section: Iterating through strings
903909
*/

branches/try2/src/libcore/vec.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use container::{Container, Mutable};
1616
use cast;
17-
use cmp::{Eq, Ord, TotalOrd, Ordering, Less, Equal, Greater};
17+
use cmp::{Eq, Equiv, Ord, TotalOrd, Ordering, Less, Equal, Greater};
1818
use iter::BaseIter;
1919
use iter;
2020
use kinds::Copy;
@@ -1572,6 +1572,12 @@ impl<T:Eq> Eq for @[T] {
15721572
pure fn ne(&self, other: &@[T]) -> bool { !(*self).eq(other) }
15731573
}
15741574

1575+
#[cfg(notest)]
1576+
impl<T:Eq> Equiv<~[T]> for &[T] {
1577+
#[inline(always)]
1578+
pure fn equiv(&self, other: &~[T]) -> bool { eq(*self, *other) }
1579+
}
1580+
15751581
// Lexicographical comparison
15761582

15771583
pure fn cmp<T: TotalOrd>(a: &[T], b: &[T]) -> Ordering {

0 commit comments

Comments
 (0)