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

HashMap temp_init_from_map skips over some elements #1695

Open
tomaskallup opened this issue Dec 17, 2024 · 1 comment · May be fixed by #1696
Open

HashMap temp_init_from_map skips over some elements #1695

tomaskallup opened this issue Dec 17, 2024 · 1 comment · May be fixed by #1696

Comments

@tomaskallup
Copy link
Contributor

Example code:

import std::io;
import std::collections::map;

def Vector2 = int[<2>];

fn uint Vector2.hash(self) => self.x * 31 + self.y;

HashMap(<Vector2, Vector2>) visited_positions;

fn void main() {
  visited_positions.set({1,10}, {0,-1});
  visited_positions.set({1,5}, {0,-1});
  visited_positions.set({1,6}, {0,-1});
  visited_positions.set({1,7}, {0,-1});
  visited_positions.set({3,5}, {0,1});

  visited_positions.@each(;Vector2 pos, Vector2 dir) {
    io::printfn("%s: %s", pos, dir);
  };

  io::printn("Copy:");

  HashMap(<Vector2, Vector2>) visited_positions_copy;
  visited_positions_copy.temp_init_from_map(&visited_positions);
  visited_positions_copy.@each(;Vector2 pos, Vector2 dir) {
    io::printfn("%s: %s", pos, dir);
  };
}

Running this with c3c compile-run test.c3 will result in following output:

Program linked to executable 'test'.
Launching ./test
[<3, 5>]: [<0, 1>]
[<1, 7>]: [<0, -1>]
[<1, 5>]: [<0, -1>]
[<1, 6>]: [<0, -1>]
[<1, 10>]: [<0, -1>]
Copy:
[<3, 5>]: [<0, 1>]
[<1, 5>]: [<0, -1>]
[<1, 6>]: [<0, -1>]
[<1, 10>]: [<0, -1>]

As you can see, [<1, 7>]: [<0, -1>] is missing in the copied HashMap.

@tomaskallup
Copy link
Contributor Author

Managed to get even simpler repro:

import std::io;
import std::collections::map;

def Map = HashMap(<String, usz>);

fn void main() {
  Map map;
  map.temp_init();

  map.set("aa", 1);
  map.set("b", 2);
  map.set("bb", 1);

  Map map_copy;
  map_copy.temp_init_from_map(&map);
  map_copy.@each(;String key, usz val) {
    io::printfn("%s: %s", key, val);
  };
}

tomaskallup added a commit to tomaskallup/c3c that referenced this issue Dec 17, 2024
@tomaskallup tomaskallup linked a pull request Dec 17, 2024 that will close this issue
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 a pull request may close this issue.

1 participant