aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2022-08-13 11:04:44 +0200
committerlemon <lsof@mailbox.org>2022-08-13 11:04:44 +0200
commit5b95abb249604e7df9be1d63b1f3dc85b8f5990b (patch)
tree5f7065f9bea68e46c925552392541d72dc8de1a6 /src
parentdd4536a8b0dbb770ee1e9e7e492a75157ba0f8e6 (diff)
set more
Diffstat (limited to 'src')
-rw-r--r--src/set.hff18
-rw-r--r--src/util.cff6
2 files changed, 21 insertions, 3 deletions
diff --git a/src/set.hff b/src/set.hff
index acff95b..955eac3 100644
--- a/src/set.hff
+++ b/src/set.hff
@@ -35,12 +35,28 @@ struct Set<T, Traits> {
self.buf->push(Traits:dup(x));
return self.set[i] = &self.buf.dat[self.buf.len - 1];
} else if Traits:eq(*self.set[i], x) {
- fprintf(stderr, "found %s\n", x);
return self.set[i];
}
i = (i + 1) & (self.N - 1);
} while i != i0;
assert(#f, "unreachable");
}
+
+ fn contains(self *Set, x T) bool {
+ let i0 = Traits:hash(x) & (self.N - 1);
+ let i int = i0;
+ do {
+ if self.set[i] == #null {
+ return #f;
+ } else if Traits:eq(*self.set[i], x) {
+ return #t;
+ }
+ i = (i + 1) & (self.N - 1);
+ } while i != i0;
+ assert(#f, "unreachable");
+ }
+ fn put(self *Set, x T) void {
+ self->intern(x);
+ }
}
diff --git a/src/util.cff b/src/util.cff
index 70c80b4..9bb5db9 100644
--- a/src/util.cff
+++ b/src/util.cff
@@ -81,8 +81,10 @@ extern fn fatal(P *Parser, loc Loc, fmt *const u8, ...) void {
extern fn internstr(s *const u8) *const u8 {
static set Set<*const u8, struct {
fn hash(s *const u8) u32 { return fnv1a_s(FNV1A_INI, s); }
- fn eq(a *const u8, b *const u8) bool { return streq(a, b); }
- fn dup(s *const u8) *const u8 { return as(*const u8)xstrdup(s); }
+ fn eq(a *const u8, b *const u8) bool {
+ return a != #null and b != #null and streq(a, b);
+ }
+ fn dup(s *const u8) *const u8 { return xstrdup(s); }
}> = {};
return *set->intern(s);
}