aboutsummaryrefslogtreecommitdiff
path: root/src/string_set.cpp
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-07-28 16:01:18 +0200
committerGitHub <noreply@github.com>2022-07-28 16:01:18 +0200
commit674ebe395f1feba70becaac75c4fb9451f74f84c (patch)
tree78589612bd458269904d53de46d0accf4f58a7af /src/string_set.cpp
parent9746e25784c208cf8a7883bcb2e1ac2317117aeb (diff)
parent96eecaab54cea02499e9cf418aa39eb5a9c9ec75 (diff)
Merge branch 'master' into master
Diffstat (limited to 'src/string_set.cpp')
-rw-r--r--src/string_set.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/string_set.cpp b/src/string_set.cpp
index e27145289..746ad9529 100644
--- a/src/string_set.cpp
+++ b/src/string_set.cpp
@@ -13,6 +13,7 @@ struct StringSet {
void string_set_init (StringSet *s, gbAllocator a, isize capacity = 16);
void string_set_destroy(StringSet *s);
void string_set_add (StringSet *s, String const &str);
+bool string_set_update (StringSet *s, String const &str); // returns true if it previously existed
bool string_set_exists (StringSet *s, String const &str);
void string_set_remove (StringSet *s, String const &str);
void string_set_clear (StringSet *s);
@@ -149,6 +150,34 @@ void string_set_add(StringSet *s, String const &str) {
}
}
+bool string_set_update(StringSet *s, String const &str) {
+ bool exists = false;
+ MapIndex index;
+ MapFindResult fr;
+ StringHashKey key = string_hash_string(str);
+ if (s->hashes.count == 0) {
+ string_set_grow(s);
+ }
+ fr = string_set__find(s, key);
+ if (fr.entry_index != MAP_SENTINEL) {
+ index = fr.entry_index;
+ exists = true;
+ } else {
+ index = string_set__add_entry(s, key);
+ if (fr.entry_prev != MAP_SENTINEL) {
+ s->entries[fr.entry_prev].next = index;
+ } else {
+ s->hashes[fr.hash_index] = index;
+ }
+ }
+ s->entries[index].value = str;
+
+ if (string_set__full(s)) {
+ string_set_grow(s);
+ }
+ return exists;
+}
+
void string_set__erase(StringSet *s, MapFindResult fr) {
MapFindResult last;