From 5b95abb249604e7df9be1d63b1f3dc85b8f5990b Mon Sep 17 00:00:00 2001 From: lemon Date: Sat, 13 Aug 2022 11:04:44 +0200 Subject: set more --- src/set.hff | 18 +++++++++++++++++- src/util.cff | 6 ++++-- 2 files changed, 21 insertions(+), 3 deletions(-) (limited to 'src') 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 { 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); } -- cgit v1.2.3