diff --git a/lib/std/collections/map.c3 b/lib/std/collections/map.c3 index e504c4621..f925b506e 100644 --- a/lib/std/collections/map.c3 +++ b/lib/std/collections/map.c3 @@ -72,6 +72,11 @@ fn bool HashMap.is_empty(&map) @inline return !map.count; } +fn usz HashMap.len(&map) @inline +{ + return map.count; +} + fn Value*! HashMap.get_ref(&map, Key key) { if (!map.count) return SearchResult.MISSING?; @@ -195,7 +200,20 @@ fn Key[] HashMap.key_list(&map, Allocator* using = mem::heap()) return list; } - +macro HashMap.@each(map; @body(key, value)) +{ + if (map.count) + { + foreach (Entry* entry : map.table) + { + while (entry) + { + @body(entry.key, entry.value); + entry = entry.next; + } + } + } +} fn Value[] HashMap.value_tlist(&map) { diff --git a/lib/std/sort/quicksort.c3 b/lib/std/sort/quicksort.c3 index 9245efe08..b5cec1c65 100644 --- a/lib/std/sort/quicksort.c3 +++ b/lib/std/sort/quicksort.c3 @@ -59,6 +59,10 @@ fn isz partition(Type list, isz low, isz high, Comparer cmp) @inline @local $endswitch } while (ok); if (i >= j) return j; - @swap(list[i], list[j]); + $if $checks(list.swap(0, 0)): + list.swap(i, j); + $else + @swap(list[i], list[j]); + $endif } } \ No newline at end of file diff --git a/src/compiler/sema_name_resolution.c b/src/compiler/sema_name_resolution.c index 528f72fb6..0fef77ed8 100644 --- a/src/compiler/sema_name_resolution.c +++ b/src/compiler/sema_name_resolution.c @@ -562,7 +562,7 @@ INLINE Decl *sema_resolve_symbol_common(SemaContext *context, NameResolve *name_ } if (module_with_path) { - sema_error_at(name_resolve->span, "'%s' could be found in %s.", name_resolve->symbol, module_with_path->name->module); + sema_error_at(name_resolve->span, "'%s' could not be found in %s.", name_resolve->symbol, module_with_path->name->module); } else { diff --git a/test/unit/stdlib/collections/list.c3 b/test/unit/stdlib/collections/list.c3 index 1c6ceb74e..686d36a26 100644 --- a/test/unit/stdlib/collections/list.c3 +++ b/test/unit/stdlib/collections/list.c3 @@ -1,4 +1,4 @@ -module listtests @test; +module list_test @test; import std::collections::list; def IntList = List(); diff --git a/test/unit/stdlib/collections/map.c3 b/test/unit/stdlib/collections/map.c3 new file mode 100644 index 000000000..bfb4d82ea --- /dev/null +++ b/test/unit/stdlib/collections/map.c3 @@ -0,0 +1,56 @@ +module map_test @test; +import std::collections::list; +import std::collections::map; +import std::sort; +import std::io; + +def Map = HashMap(); + +struct MapTest +{ + String key; + usz value; +} +def List = List(); + +fn void map() +{ + Map m; + assert(!m.is_initialized()); + m.tinit(); + assert(m.is_initialized()); + assert(m.is_empty()); + assert(m.len() == 0); + + m.set("a", 1); + assert(!m.is_empty()); + assert(m.len() == 1); + m.remove("a"); + assert(m.is_empty()); + + MapTest[] tcases = { {"key1", 0}, {"key2", 1}, {"key3", 2} }; + foreach (tc : tcases) + { + m.set(tc.key, tc.value); + } + assert(m.len() == tcases.len); + foreach (tc : tcases) + { + usz v = m.get(tc.key)!!; + assert(tc.value == v); + } + + List list; + list.tinit(); + m.@each(;String key, usz value) + { + list.push({key, value}); + }; + assert(list.len() == tcases.len); + quicksort(list, fn int (MapTest a, MapTest b) => (int)(a.value - b.value)); + foreach (i, tc : tcases) + { + assert(tc.key == list[i].key); + assert(tc.value == list[i].value); + } +} \ No newline at end of file diff --git a/test/unit/stdlib/sort/quicksort.c3 b/test/unit/stdlib/sort/quicksort.c3 index a010b41ae..175692055 100644 --- a/test/unit/stdlib/sort/quicksort.c3 +++ b/test/unit/stdlib/sort/quicksort.c3 @@ -1,6 +1,7 @@ module sort_test @test; import std::sort; import sort::check; +import std::collections::list; fn void quicksort() { @@ -70,6 +71,17 @@ fn void quicksort_with_lambda() } } +def List = List(); + +fn void quicksort_list() +{ + List list; + list.tinit(); + list.add_array({ 2, 1, 3}); + sort::quicksort(list, &sort::cmp_int_value); + assert(check::int_sort(list.array_view())); +} + module sort::check; fn bool int_sort(int[] list)