https://github.com/wertzui123/hashmap
A generic hashmap implementation written in V that allows any kind of key-value-pairs
https://github.com/wertzui123/hashmap
hashmap v vlang
Last synced: about 1 year ago
JSON representation
A generic hashmap implementation written in V that allows any kind of key-value-pairs
- Host: GitHub
- URL: https://github.com/wertzui123/hashmap
- Owner: Wertzui123
- License: mit
- Created: 2022-04-10T15:12:49.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-11-24T15:09:24.000Z (over 2 years ago)
- Last Synced: 2025-01-01T19:12:52.292Z (about 1 year ago)
- Topics: hashmap, v, vlang
- Language: V
- Homepage:
- Size: 21.5 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HashMap
This is a hashmap/hashtable implementation written in V.
Since the standard `map` type in V does not allow for arbitrary key types, you can use this library when you need keys of different types than `string`, `integer`, `float`, `rune`, `enum` or `voidptr`[^1].
Note that both your key and value types must define an `equals` as well as a `hash` method.
[^1]: Those types are allowed in the default V `map` datatype, see https://github.com/vlang/v/blob/a0e7a46be4d468ecf61b0e6cd7c81f11ddbd4233/vlib/v/parser/parse_type.v#L131
## Example
```v
import wertzui123.hashmap
struct Str {
str string
}
pub fn (s1 Str) equals(s2 Str) bool {
return s1.str == s2.str
}
pub fn (s Str) hash() int {
return s.str.hash()
}
fn main() {
mut m := hashmap.new_hashmap[Str, Str]()!
m.set(Str{'Hello'}, Str{'World'})
assert m.contains_key(Str{'Hello'})
assert m.contains_value(Str{'World'})
assert m.get_value(Str{'Hello'})? == Str{'World'}
assert m.get_key(Str{'World'})? == Str{'Hello'}
m.remove(Str{'Hello'})
assert m.len == 0
}
```
## Performance
Since this is my first attempt at writing a hashmap, the performance is probably pretty poor; in particular, removing elements is really slow.
Furthermore, the array under the hoods is scaled by only one element each time it is enlarged, which is very bad if many elements are added (you can provide an `initial_capacity` when creating a hashmap though).
Of course the performance also depends a lot on what hashing algorithm you use for your keys.
## License
This library is licensed under the MIT license (see [LICENSE](LICENSE)).