Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lib] Added HashMap.remove to Map.v3 #321

Merged
merged 8 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/util/Map.v3
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,50 @@ class HashMap<K, V> extends PartialMap<K, V> {
}
}
}
// Remove {key} from this map. Return true if {key} existed.
def remove(key: K) -> bool {
var c = cache;
if (c == null) return false; // empty map
var found = false;

// search table
if (table != null) {
for (i < table.length) {
linxuanm marked this conversation as resolved.
Show resolved Hide resolved
if (table[i] == null) continue;
linxuanm marked this conversation as resolved.
Show resolved Hide resolved
if (table[i].key == key || equals(table[i].key, key)) {
table[i] = table[i].next;
found = true;
break;
}
for (b = table[i]; b.next != null; b = b.next) {
if (b.next.key == key || equals(b.next.key, key)) {
b.next = b.next.next;
found = true;
break;
}
}
if (found) break;
}
}

// if cache hit, clear cache and set it to any bucket
linxuanm marked this conversation as resolved.
Show resolved Hide resolved
if (c.key == key || equals(c.key, key)) {
found = true;
// set cache to any bucket
// XXX: better cache refill?
cache = try_find_bucket();
}

return found;
}
private def try_find_bucket() -> Bucket<K, V> {
if (table != null) {
for (b in table) {
if (b != null) return b;
}
}
return null;
}
private def insert(bucket: Bucket<K, V>) {
var hashval = dohash(bucket.key);
bucket.next = table[hashval];
Expand Down
125 changes: 125 additions & 0 deletions test/lib/MapTest.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2023 Virgil authors. All rights reserved.
// See LICENSE for details of Apache 2.0 license.

def T = LibTests.register("Map", _, _);
def X = [
T("get", test_get),
T("set", test_set),
T("has", test_has),
T("apply", test_apply),
T("remove", test_remove),
()
];

class Counter {
var x = 0;
def add<K, V>(a: K, b: V) { x++; }
def reset() { x = 0; }
}

def int_map<V>() -> HashMap<int, V> {
return HashMap<int, V>.new(int.!, int.==);
}

def int2str(v: int) -> string {
return StringBuilder.new().putd(v).extract();
}

def size<K, V>(m: HashMap<K, V>) -> int {
var c = Counter.new();
m.apply(c.add);
return c.x;
}

def test_get(t: LibTest) {
var c = Counter.new();
var i_s = int_map<string>();
var s_s = Strings.newMap<string>();
for (i < 100) {
var s = int2str(i);
i_s[i] = s;
s_s[s] = s;
}
for (i < 100) {
linxuanm marked this conversation as resolved.
Show resolved Hide resolved
var s = int2str(i);
t.assert_string(i_s[i], s);
t.assert_string(s_s[s], s);
}
}

def test_set(t: LibTest) {
var i_s = int_map<string>();
var s_s = Strings.newMap<string>();
for (i < 100) {
var s = int2str(i);
i_s[i] = s;
s_s[s] = s;
}
for (i < 100) {
var s = int2str(i);
t.assert_string(i_s[i], s);
t.assert_string(s_s[s], s);
}
t.asserteq(size(i_s), 100);
t.asserteq(size(s_s), 100);
for (i < 100) {
var s = int2str(i);
i_s[i] = "foo";
s_s[s] = "bar";
}
for (i < 100) {
var s = int2str(i);
t.assert_string(i_s[i], "foo");
t.assert_string(s_s[s], "bar");
}
t.asserteq(size(i_s), 100);
t.asserteq(size(s_s), 100);
}

def test_has(t: LibTest) {
var i_s = int_map<string>();
for (i < 100) i_s[i] = int2str(i);
for (i < 100) t.assert(i_s.has(i));
for (i = 100; i < 200; i++) t.assert(!i_s.has(i));
}

def test_apply(t: LibTest) {
var i_s = int_map<Counter>();
for (i < 100) i_s[i] = Counter.new();
i_s.apply(inc_counter);
for (i < 100) t.asserteq(i_s[i].x, i);
}

def inc_counter(k: int, c: Counter) { c.x += k; }

def test_remove(t: LibTest) {
var i_i = int_map<int>();
i_i[12] = 24;
t.assert(i_i.remove(12));
t.assert(!i_i.remove(12));
t.assert(!i_i.has(12));
t.asserteq(size(i_i), 0);
t.asserteq(i_i[12], 0);

i_i[1] = 10;
i_i[2] = 20;
t.assert(!i_i.remove(0));
t.assert(i_i.remove(1));
t.assert(!i_i.remove(1));
t.assert(!i_i.has(1));
t.assert(i_i.has(2));
t.asserteq(size(i_i), 1);
t.assert(i_i.remove(2));
t.assert(!i_i.has(2));
t.asserteq(size(i_i), 0);

i_i[1] = 0;
i_i[2] = 4;
t.assert(i_i.has(1));
// t.assert(i_i.remove(2));

// for (i < 100) i_i[i] = i * 2;
// for (i < 100) t.assert(i_i.remove(i));
// for (i = 100; i < 200; i++) t.assert(!i_i.remove(i));
// t.asserteq(size(i_i), 0);
}
Loading