changed: Set no longer has a default element; `set.get` returns an Optional
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 14 Jun 2023 03:06:25 +0000 (22:06 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 14 Jun 2023 03:06:25 +0000 (22:06 -0500)
core/container/set.onyx

index 942ddd5795b7c197b0083c444cb46951fd9d289d..ab771197a527a651e60fa22f1f9f61a6ec590077 100644 (file)
@@ -19,8 +19,6 @@ Set :: struct (Elem_Type: type_expr) where SetValue(Elem_Type) {
     hashes  : [] i32;
     entries : [..] Entry(Elem_Type);
 
-    default_value: Elem_Type;
-
     Entry :: struct (T: type_expr) {
         next  : i32;
         hash  : u32;
@@ -46,18 +44,17 @@ Set :: struct (Elem_Type: type_expr) where SetValue(Elem_Type) {
     Set :: Set;
 }
 
-make :: ($T: type_expr, default := T.{}, allocator := context.allocator) -> Set(T) {
+make :: ($T: type_expr, allocator := context.allocator) -> Set(T) {
     set : Set(T);
-    init(&set, default=default, allocator=allocator);
+    init(&set, allocator=allocator);
     return set;
 }
 
 #overload
 builtin.__make_overload :: macro (x: &Set, allocator: Allocator) => #this_package.make(x.Elem_Type, allocator = allocator);
 
-init :: (set: &Set($T), default := T.{}, allocator := context.allocator) {
+init :: (set: &Set($T), allocator := context.allocator) {
     set.allocator = allocator;
-    set.default_value = default;
 
     memory.alloc_slice(&set.hashes, 8, allocator=allocator);
     array.fill(set.hashes, -1);
@@ -102,9 +99,9 @@ get_ptr :: (use set: &Set, value: set.Elem_Type) -> &set.Elem_Type {
     return (&entries[lr.entry_index].value) if lr.entry_index >= 0 else null;
 }
 
-get_opt :: (use set: &Set, value: set.Elem_Type) -> Optional(set.Elem_Type) {
+get_opt :: (use set: &Set, value: set.Elem_Type) -> ? set.Elem_Type {
     lr := lookup(set, value);
-    if lr.entry_index >= 0 do return Optional.make(entries[lr.entry_index].value);
+    if lr.entry_index >= 0 do entries[lr.entry_index].value;
 
     return .{};
 }