Skip to content

Handle map iterator reset for reuse + fix example#6335

Merged
Kelimion merged 4 commits intoodin-lang:masterfrom
Faker-09:handle_map_iterator_reset
Feb 25, 2026
Merged

Handle map iterator reset for reuse + fix example#6335
Kelimion merged 4 commits intoodin-lang:masterfrom
Faker-09:handle_map_iterator_reset

Conversation

@Faker-09
Copy link
Contributor

Reset the handle_map iterators for easy reuse and make the example more compilable.

@Kelimion
Copy link
Member

I propose a slightly different diff to make the example more easily compileable:

  1. Demonstrate is_valid to use h3 and h in the iterator loop.
  2. Add #optional_allocator_error to the dynamic map to mirror the static version's #optional_ok, so neither needs , _ to get rid of ok or err respectively.

Also, your , ok1 doesn't actually use that ok1 anywhere I can see in the diff. Neither are the new err0 and err1, so it wouldn't compile with -vet.

This does. But please do keep the it.index = 0 to make iterating again easier. :-)

diff --git a/core/container/handle_map/doc.odin b/core/container/handle_map/doc.odin
index fe4154774..088ba37d7 100644
--- a/core/container/handle_map/doc.odin
+++ b/core/container/handle_map/doc.odin
@@ -24,10 +24,12 @@ Example:
                hm.remove(&entities, h1)

                h3 := hm.add(&entities, Entity{pos = {6, 7}})
+               assert(hm.is_valid(entities, h3))

                it := hm.iterator_make(&entities)
                for e, h in hm.iterate(&it) {
                        e.pos += {1, 2}
+                       assert(hm.is_valid(entities, h))
                }
        }

@@ -46,10 +48,12 @@ Example:
                hm.remove(&entities, h1)

                h3 := hm.add(&entities, Entity{pos = {6, 7}})
+               assert(hm.is_valid(&entities, h3))

                it := hm.iterator_make(&entities)
                for e, h in hm.iterate(&it) {
                        e.pos += {1, 2}
+                       assert(hm.is_valid(&entities, h))
                }
        }
 */
diff --git a/core/container/handle_map/dynamic_handle_map.odin b/core/container/handle_map/dynamic_handle_map.odin
index 4024ac5b6..bdd9b7daf 100644
--- a/core/container/handle_map/dynamic_handle_map.odin
+++ b/core/container/handle_map/dynamic_handle_map.odin
@@ -31,7 +31,7 @@ dynamic_destroy :: proc(m: ^$D/Dynamic_Handle_Map($T, $Handle_Type)) {
 }

 @(require_results)
-dynamic_add :: proc(m: ^$D/Dynamic_Handle_Map($T, $Handle_Type), item: T, loc := #caller_location) -> (handle: Handle_Type, err: runtime.Allocator_Error) {
+dynamic_add :: proc(m: ^$D/Dynamic_Handle_Map($T, $Handle_Type), item: T, loc := #caller_location) -> (handle: Handle_Type, err: runtime.Allocator_Error) #optional_allocator_error {
        if xar.len(m.unused_items) > 0 {
                i := xar.pop(&m.unused_items)
                ptr := xar.get_ptr_unsafe(&m.items, i)

@Faker-09
Copy link
Contributor Author

I've made those updates. One minor change to move the assert above the access to e.pos. Thanks!

@Kelimion Kelimion merged commit 61d5321 into odin-lang:master Feb 25, 2026
8 checks passed
@Kelimion
Copy link
Member

Squahed and merged. Thanks. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants