Skip to content

Commit

Permalink
runtime: fix map lookup does not set to default value if record is no…
Browse files Browse the repository at this point in the history
…t exist (#129)
  • Loading branch information
mertcandav committed Jan 16, 2025
1 parent 7eb0aa3 commit 6478089
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion std/runtime/map.jule
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,17 @@ impl _Map {
}

// Returns the |v| mapped by |k| if one exists.
// Sets |v| if found and pointer not nil.
// Sets |v| if pointer is not nil. If not found, sets to default value of Val.
// Same as the |ok| variable which reports whether |v| exist.
unsafe fn lookup(mut &self, mut k: Key, mut v: *Val, mut ok: *bool) {
if self == nil || self.groups == nil {
if ok != nil {
*ok = false
}
if v != nil {
let mut def: Val
*v = def
}
ret
}
hi, lo := splitHash(self.hash(k))
Expand All @@ -217,13 +221,18 @@ impl _Map {
if ok != nil {
*ok = false
}
if v != nil {
let mut def: Val
*v = def
}
ret
}
g += 1 // linear probing
if g >= u32(len(self.groups)) {
g = 0
}
}
panic("unreachable")
}

// Returns value of key if exist, otherwise returns default value of value type.
Expand Down Expand Up @@ -270,6 +279,7 @@ impl _Map {
g = 0
}
}
panic("unreachable")
}

// Attempts to remove |k|, returns true successful.
Expand Down Expand Up @@ -318,6 +328,7 @@ impl _Map {
g = 0
}
}
panic("unreachable")
}

// Removes all elements from the Map.
Expand Down

0 comments on commit 6478089

Please sign in to comment.