More stdlib hashmap work. Add a simple test and XFAIL it due to a valgrind-spotted UMR.
This commit is contained in:
parent
5b2451c231
commit
6277b462e9
@ -395,6 +395,7 @@ TEST_XFAILS_X86 := $(TASK_XFAILS) \
|
|||||||
test/run-pass/generic-recursive-tag.rs \
|
test/run-pass/generic-recursive-tag.rs \
|
||||||
test/run-pass/iter-ret.rs \
|
test/run-pass/iter-ret.rs \
|
||||||
test/run-pass/lib-deque.rs \
|
test/run-pass/lib-deque.rs \
|
||||||
|
test/run-pass/lib-map.rs \
|
||||||
test/run-pass/mlist-cycle.rs \
|
test/run-pass/mlist-cycle.rs \
|
||||||
test/run-pass/obj-as.rs \
|
test/run-pass/obj-as.rs \
|
||||||
test/run-pass/task-comm.rs \
|
test/run-pass/task-comm.rs \
|
||||||
@ -468,6 +469,7 @@ TEST_XFAILS_LLVM := $(TASK_XFAILS) \
|
|||||||
lazy-init.rs \
|
lazy-init.rs \
|
||||||
lazychan.rs \
|
lazychan.rs \
|
||||||
lib-deque.rs \
|
lib-deque.rs \
|
||||||
|
lib-map.rs \
|
||||||
lib-rand.rs \
|
lib-rand.rs \
|
||||||
linear-for-loop.rs \
|
linear-for-loop.rs \
|
||||||
list.rs \
|
list.rs \
|
||||||
|
@ -10,7 +10,7 @@ import std._vec;
|
|||||||
|
|
||||||
|
|
||||||
type hashfn[K] = fn(&K) -> uint;
|
type hashfn[K] = fn(&K) -> uint;
|
||||||
type eqfn[K] = fn(&K) -> bool;
|
type eqfn[K] = fn(&K, &K) -> bool;
|
||||||
|
|
||||||
type hashmap[K, V] = obj {
|
type hashmap[K, V] = obj {
|
||||||
fn insert(&K key, &V val);
|
fn insert(&K key, &V val);
|
||||||
@ -46,18 +46,18 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
|
|||||||
// is always a power of 2), so that all buckets are probed for a
|
// is always a power of 2), so that all buckets are probed for a
|
||||||
// fixed key.
|
// fixed key.
|
||||||
|
|
||||||
fn hashl[K](hashfn[K] hasher, uint nbkts, &K key) -> uint {
|
fn hashl[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint {
|
||||||
ret (hasher(key) >>> (sys.rustrt.size_of[uint]() * 8u / 2u))
|
ret (hasher(key) >>> (sys.rustrt.size_of[uint]() * 8u / 2u))
|
||||||
% nbkts;
|
% nbkts;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hashr[K](hashfn[K] hasher, uint nbkts, &K key) -> uint {
|
fn hashr[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint {
|
||||||
ret ((((~ 0u) >>> (sys.rustrt.size_of[uint]() * 8u / 2u))
|
ret ((((~ 0u) >>> (sys.rustrt.size_of[uint]() * 8u / 2u))
|
||||||
& hasher(key)) * 2u + 1u)
|
& hasher(key)) * 2u + 1u)
|
||||||
% nbkts;
|
% nbkts;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash[K](hashfn[K] hasher, uint nbkts, &K key, uint i) -> uint {
|
fn hash[K](&hashfn[K] hasher, uint nbkts, &K key, uint i) -> uint {
|
||||||
ret hashl[K](hasher, nbkts, key) + i * hashr[K](hasher, nbkts, key);
|
ret hashl[K](hasher, nbkts, key) + i * hashr[K](hasher, nbkts, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,30 +65,36 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
|
|||||||
* We attempt to never call this with a full table. If we do, it
|
* We attempt to never call this with a full table. If we do, it
|
||||||
* will fail.
|
* will fail.
|
||||||
*/
|
*/
|
||||||
fn insert_common[K, V](hashfn[K] hasher,
|
fn insert_common[K, V](&hashfn[K] hasher,
|
||||||
|
&eqfn[K] eqer,
|
||||||
vec[mutable bucket[K, V]] bkts,
|
vec[mutable bucket[K, V]] bkts,
|
||||||
uint nbkts,
|
uint nbkts,
|
||||||
&K key,
|
&K key,
|
||||||
&V val)
|
&V val)
|
||||||
|
-> bool
|
||||||
{
|
{
|
||||||
let uint i = 0u;
|
let uint i = 0u;
|
||||||
while (i < nbkts) {
|
while (i < nbkts) {
|
||||||
// FIXME (issue #94): as in find_common()
|
// FIXME (issue #94): as in find_common()
|
||||||
let int j = (hash[K](hasher, nbkts, key, i)) as int;
|
let int j = (hash[K](hasher, nbkts, key, i)) as int;
|
||||||
alt (bkts.(j)) {
|
alt (bkts.(j)) {
|
||||||
case (some[K, V](_, _)) {
|
case (some[K, V](k, _)) {
|
||||||
|
if (eqer(key, k)) {
|
||||||
|
ret false;
|
||||||
|
}
|
||||||
i += 1u;
|
i += 1u;
|
||||||
}
|
}
|
||||||
case (_) {
|
case (_) {
|
||||||
bkts.(j) = some[K, V](key, val);
|
bkts.(j) = some[K, V](key, val);
|
||||||
ret;
|
ret true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fail; // full table
|
fail; // full table
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_common[K, V](hashfn[K] hasher,
|
fn find_common[K, V](&hashfn[K] hasher,
|
||||||
|
&eqfn[K] eqer,
|
||||||
vec[mutable bucket[K, V]] bkts,
|
vec[mutable bucket[K, V]] bkts,
|
||||||
uint nbkts,
|
uint nbkts,
|
||||||
&K key)
|
&K key)
|
||||||
@ -99,29 +105,31 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
|
|||||||
// FIXME (issue #94): Pending bugfix, remove uint coercion.
|
// FIXME (issue #94): Pending bugfix, remove uint coercion.
|
||||||
let int j = (hash[K](hasher, nbkts, key, i)) as int;
|
let int j = (hash[K](hasher, nbkts, key, i)) as int;
|
||||||
alt (bkts.(j)) {
|
alt (bkts.(j)) {
|
||||||
case (some[K, V](_, val)) {
|
case (some[K, V](k, v)) {
|
||||||
ret util.some[V](val);
|
if (eqer(key, k)) {
|
||||||
|
ret util.some[V](v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case (nil[K, V]()) {
|
case (nil[K, V]()) {
|
||||||
ret util.none[V]();
|
ret util.none[V]();
|
||||||
}
|
}
|
||||||
case (deleted[K, V]()) {
|
case (deleted[K, V]()) { }
|
||||||
|
}
|
||||||
i += 1u;
|
i += 1u;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
ret util.none[V]();
|
ret util.none[V]();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn rehash[K, V](hashfn[K] hasher,
|
fn rehash[K, V](&hashfn[K] hasher,
|
||||||
|
&eqfn[K] eqer,
|
||||||
vec[mutable bucket[K, V]] oldbkts, uint noldbkts,
|
vec[mutable bucket[K, V]] oldbkts, uint noldbkts,
|
||||||
vec[mutable bucket[K, V]] newbkts, uint nnewbkts)
|
vec[mutable bucket[K, V]] newbkts, uint nnewbkts)
|
||||||
{
|
{
|
||||||
for (bucket[K, V] b in oldbkts) {
|
for (bucket[K, V] b in oldbkts) {
|
||||||
alt (b) {
|
alt (b) {
|
||||||
case (some[K, V](k, v)) {
|
case (some[K, V](k, v)) {
|
||||||
insert_common[K, V](hasher, newbkts, nnewbkts, k, v);
|
insert_common[K, V](hasher, eqer, newbkts, nnewbkts, k, v);
|
||||||
}
|
}
|
||||||
case (_) { }
|
case (_) { }
|
||||||
}
|
}
|
||||||
@ -144,28 +152,28 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
|
|||||||
check ((nnewbkts as int) > 0);
|
check ((nnewbkts as int) > 0);
|
||||||
|
|
||||||
let vec[mutable bucket[K, V]] newbkts = make_buckets[K, V](nnewbkts);
|
let vec[mutable bucket[K, V]] newbkts = make_buckets[K, V](nnewbkts);
|
||||||
rehash[K, V](hasher, bkts, nbkts, newbkts, nnewbkts);
|
rehash[K, V](hasher, eqer, bkts, nbkts, newbkts, nnewbkts);
|
||||||
}
|
}
|
||||||
insert_common[K, V](hasher, bkts, nbkts, key, val);
|
insert_common[K, V](hasher, eqer, bkts, nbkts, key, val);
|
||||||
nelts += 1u;
|
nelts += 1u;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn contains_key(&K key) -> bool {
|
fn contains_key(&K key) -> bool {
|
||||||
alt (find_common[K, V](hasher, bkts, nbkts, key)) {
|
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
|
||||||
case (util.some[V](_)) { ret true; }
|
case (util.some[V](_)) { ret true; }
|
||||||
case (_) { ret false; }
|
case (_) { ret false; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&K key) -> V {
|
fn get(&K key) -> V {
|
||||||
alt (find_common[K, V](hasher, bkts, nbkts, key)) {
|
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
|
||||||
case (util.some[V](val)) { ret val; }
|
case (util.some[V](val)) { ret val; }
|
||||||
case (_) { fail; }
|
case (_) { fail; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find(&K key) -> util.option[V] {
|
fn find(&K key) -> util.option[V] {
|
||||||
be find_common[K, V](hasher, bkts, nbkts, key);
|
be find_common[K, V](hasher, eqer, bkts, nbkts, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&K key) -> util.option[V] {
|
fn remove(&K key) -> util.option[V] {
|
||||||
@ -191,7 +199,7 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
|
|||||||
|
|
||||||
fn rehash() {
|
fn rehash() {
|
||||||
let vec[mutable bucket[K, V]] newbkts = make_buckets[K, V](nbkts);
|
let vec[mutable bucket[K, V]] newbkts = make_buckets[K, V](nbkts);
|
||||||
rehash[K, V](hasher, bkts, nbkts, newbkts, nbkts);
|
rehash[K, V](hasher, eqer, bkts, nbkts, newbkts, nbkts);
|
||||||
bkts = newbkts;
|
bkts = newbkts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
src/test/run-pass/lib-map.rs
Normal file
16
src/test/run-pass/lib-map.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// -*- rust -*-
|
||||||
|
|
||||||
|
use std;
|
||||||
|
import std.map;
|
||||||
|
|
||||||
|
fn test_simple() {
|
||||||
|
fn eq(&uint x, &uint y) -> bool { ret x == y; }
|
||||||
|
|
||||||
|
let map.hashfn[uint] hasher = std.util.id[uint];
|
||||||
|
let map.eqfn[uint] eqer = eq;
|
||||||
|
let map.hashmap[uint, uint] hm = map.mk_hashmap[uint, uint](hasher, eqer);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
test_simple();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user