@shd101wyy/yo 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/out/cjs/index.cjs +5598 -45
- package/out/cjs/yo-cli.cjs +755 -764
- package/out/esm/index.mjs +5595 -42
- package/out/types/src/codegen/async/state-code-gen.d.ts +3 -3
- package/out/types/src/codegen/async/state-machine.d.ts +4 -4
- package/out/types/src/codegen/c/collection.d.ts +1 -1
- package/out/types/src/codegen/codegen-c.d.ts +1 -1
- package/out/types/src/codegen/constants.d.ts +1 -1
- package/out/types/src/codegen/exprs/{and_or.d.ts → and-or.d.ts} +2 -2
- package/out/types/src/codegen/exprs/{array_fns.d.ts → array-fns.d.ts} +2 -2
- package/out/types/src/codegen/exprs/assignment.d.ts +2 -2
- package/out/types/src/codegen/exprs/async.d.ts +2 -2
- package/out/types/src/codegen/exprs/atom.d.ts +2 -2
- package/out/types/src/codegen/exprs/await.d.ts +2 -2
- package/out/types/src/codegen/exprs/begin.d.ts +2 -2
- package/out/types/src/codegen/exprs/binding.d.ts +2 -2
- package/out/types/src/codegen/exprs/closures.d.ts +3 -3
- package/out/types/src/codegen/exprs/comptime-value.d.ts +4 -0
- package/out/types/src/codegen/exprs/cond.d.ts +2 -2
- package/out/types/src/codegen/exprs/consume.d.ts +2 -2
- package/out/types/src/codegen/exprs/{drop_dup.d.ts → drop-dup.d.ts} +4 -4
- package/out/types/src/codegen/exprs/dyn.d.ts +2 -2
- package/out/types/src/codegen/exprs/expr.d.ts +2 -2
- package/out/types/src/codegen/exprs/gc.d.ts +2 -2
- package/out/types/src/codegen/exprs/generation.d.ts +2 -2
- package/out/types/src/codegen/exprs/{initialization_assignment.d.ts → initialization-assignment.d.ts} +2 -2
- package/out/types/src/codegen/exprs/{inline_fns.d.ts → inline-fns.d.ts} +2 -2
- package/out/types/src/codegen/exprs/iso.d.ts +2 -2
- package/out/types/src/codegen/exprs/match.d.ts +2 -2
- package/out/types/src/codegen/exprs/open.d.ts +2 -2
- package/out/types/src/codegen/exprs/{other_fn_call.d.ts → other-fn-call.d.ts} +2 -2
- package/out/types/src/codegen/exprs/panic.d.ts +2 -2
- package/out/types/src/codegen/exprs/parallelism.d.ts +2 -2
- package/out/types/src/codegen/exprs/{property_access.d.ts → property-access.d.ts} +2 -2
- package/out/types/src/codegen/exprs/{ptr_fns.d.ts → ptr-fns.d.ts} +2 -2
- package/out/types/src/codegen/exprs/{rc_fns.d.ts → rc-fns.d.ts} +2 -2
- package/out/types/src/codegen/exprs/recur.d.ts +2 -2
- package/out/types/src/codegen/exprs/return.d.ts +2 -2
- package/out/types/src/codegen/exprs/sizeof.d.ts +2 -2
- package/out/types/src/codegen/exprs/{tuple_fn.d.ts → tuple-fn.d.ts} +2 -2
- package/out/types/src/codegen/exprs/while.d.ts +2 -2
- package/out/types/src/codegen/functions/collection.d.ts +4 -3
- package/out/types/src/codegen/functions/context.d.ts +6 -6
- package/out/types/src/codegen/functions/declarations.d.ts +4 -4
- package/out/types/src/codegen/functions/dyn.d.ts +1 -1
- package/out/types/src/codegen/functions/generation.d.ts +5 -5
- package/out/types/src/codegen/types/collection.d.ts +4 -4
- package/out/types/src/codegen/types/dyn.d.ts +2 -2
- package/out/types/src/codegen/types/generation.d.ts +2 -2
- package/out/types/src/codegen/utils/fixup.d.ts +1 -1
- package/out/types/src/codegen/utils/index.d.ts +6 -6
- package/out/types/src/env.d.ts +34 -5
- package/out/types/src/error.d.ts +1 -1
- package/out/types/src/evaluator/async/await-analysis-types.d.ts +1 -1
- package/out/types/src/evaluator/async/await-analysis.d.ts +1 -1
- package/out/types/src/evaluator/builtins/alignof.d.ts +3 -3
- package/out/types/src/evaluator/builtins/{and_or.d.ts → and-or.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/{array_fns.d.ts → array-fns.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/as.d.ts +3 -3
- package/out/types/src/evaluator/builtins/{async_fns.d.ts → async-fns.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/comptime-assert.d.ts +8 -0
- package/out/types/src/evaluator/builtins/comptime-bool-fns.d.ts +8 -0
- package/out/types/src/evaluator/builtins/comptime-expect-error.d.ts +8 -0
- package/out/types/src/evaluator/builtins/comptime-fn.d.ts +8 -0
- package/out/types/src/evaluator/builtins/comptime-list-fns.d.ts +33 -0
- package/out/types/src/evaluator/builtins/comptime-numeric-fns.d.ts +8 -0
- package/out/types/src/evaluator/builtins/comptime-print.d.ts +8 -0
- package/out/types/src/evaluator/builtins/comptime-string-fns.d.ts +8 -0
- package/out/types/src/evaluator/builtins/consume.d.ts +3 -3
- package/out/types/src/evaluator/builtins/drop.d.ts +3 -3
- package/out/types/src/evaluator/builtins/dup.d.ts +3 -3
- package/out/types/src/evaluator/builtins/{expr_fns.d.ts → expr-fns.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/{future_fns.d.ts → future-fns.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/gc.d.ts +3 -3
- package/out/types/src/evaluator/builtins/gensym.d.ts +3 -3
- package/out/types/src/evaluator/builtins/{impl_constraint.d.ts → impl-constraint.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/{macro_expand.d.ts → macro-expand.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/panic.d.ts +3 -3
- package/out/types/src/evaluator/builtins/process.d.ts +3 -3
- package/out/types/src/evaluator/builtins/{ptr_fns.d.ts → ptr-fns.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/quote.d.ts +3 -3
- package/out/types/src/evaluator/builtins/{rc_fns.d.ts → rc-fns.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/rc.d.ts +3 -3
- package/out/types/src/evaluator/builtins/sizeof.d.ts +3 -3
- package/out/types/src/evaluator/builtins/the.d.ts +3 -3
- package/out/types/src/evaluator/builtins/{type_fns.d.ts → type-fns.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/{va_start.d.ts → va-start.d.ts} +3 -3
- package/out/types/src/evaluator/builtins/{var_fns.d.ts → var-fns.d.ts} +3 -3
- package/out/types/src/evaluator/calls/{array_type.d.ts → array-type.d.ts} +4 -4
- package/out/types/src/evaluator/calls/array.d.ts +5 -5
- package/out/types/src/evaluator/calls/{closure_type.d.ts → closure-type.d.ts} +4 -4
- package/out/types/src/evaluator/calls/comptime-fn.d.ts +19 -0
- package/out/types/src/evaluator/calls/comptime-list-type.d.ts +11 -0
- package/out/types/src/evaluator/calls/{function_type.d.ts → function-type.d.ts} +5 -5
- package/out/types/src/evaluator/calls/function.d.ts +6 -6
- package/out/types/src/evaluator/calls/helper.d.ts +6 -6
- package/out/types/src/evaluator/calls/iso.d.ts +4 -4
- package/out/types/src/evaluator/calls/{module_type.d.ts → module-type.d.ts} +4 -4
- package/out/types/src/evaluator/calls/{numeric_type.d.ts → numeric-type.d.ts} +4 -4
- package/out/types/src/evaluator/calls/{pointer_type.d.ts → pointer-type.d.ts} +4 -4
- package/out/types/src/evaluator/calls/pointer.d.ts +3 -3
- package/out/types/src/evaluator/calls/{trait_type.d.ts → trait-type.d.ts} +4 -4
- package/out/types/src/evaluator/calls/type.d.ts +4 -4
- package/out/types/src/evaluator/context.d.ts +6 -6
- package/out/types/src/evaluator/ctfe/ctfe-analysis.d.ts +5 -5
- package/out/types/src/evaluator/exprs/_expr.d.ts +3 -3
- package/out/types/src/evaluator/exprs/assignment.d.ts +3 -3
- package/out/types/src/evaluator/exprs/begin.d.ts +3 -3
- package/out/types/src/evaluator/exprs/binding.d.ts +3 -3
- package/out/types/src/evaluator/exprs/{c_include.d.ts → c-include.d.ts} +3 -3
- package/out/types/src/evaluator/exprs/cond.d.ts +3 -3
- package/out/types/src/evaluator/exprs/{destructuring_assignment.d.ts → destructuring-assignment.d.ts} +5 -5
- package/out/types/src/evaluator/exprs/expr.d.ts +3 -3
- package/out/types/src/evaluator/exprs/extern.d.ts +3 -3
- package/out/types/src/evaluator/exprs/{identifer_and_operator.d.ts → identifer-and-operator.d.ts} +3 -3
- package/out/types/src/evaluator/exprs/import.d.ts +3 -3
- package/out/types/src/evaluator/exprs/{initialization_assignment.d.ts → initialization-assignment.d.ts} +3 -3
- package/out/types/src/evaluator/exprs/match.d.ts +3 -3
- package/out/types/src/evaluator/exprs/open.d.ts +3 -3
- package/out/types/src/evaluator/exprs/{property_access.d.ts → property-access.d.ts} +3 -3
- package/out/types/src/evaluator/exprs/recur.d.ts +3 -3
- package/out/types/src/evaluator/exprs/runtime.d.ts +3 -3
- package/out/types/src/evaluator/exprs/subtype-of.d.ts +8 -0
- package/out/types/src/evaluator/exprs/test.d.ts +3 -3
- package/out/types/src/evaluator/exprs/typeof.d.ts +3 -3
- package/out/types/src/evaluator/exprs/while.d.ts +3 -3
- package/out/types/src/evaluator/index.d.ts +4 -4
- package/out/types/src/evaluator/trait-checking.d.ts +31 -0
- package/out/types/src/evaluator/types/array.d.ts +3 -3
- package/out/types/src/evaluator/types/closure.d.ts +3 -3
- package/out/types/src/evaluator/types/comptime-list.d.ts +8 -0
- package/out/types/src/evaluator/types/{concrete_trait.d.ts → concrete-trait.d.ts} +3 -3
- package/out/types/src/evaluator/types/dyn.d.ts +3 -3
- package/out/types/src/evaluator/types/enum.d.ts +3 -3
- package/out/types/src/evaluator/types/{expr_synthesizer.d.ts → expr-synthesizer.d.ts} +4 -4
- package/out/types/src/evaluator/types/field.d.ts +4 -4
- package/out/types/src/evaluator/types/{fn_trait.d.ts → fn-trait.d.ts} +3 -3
- package/out/types/src/evaluator/types/function.d.ts +16 -10
- package/out/types/src/evaluator/types/{future_trait.d.ts → future-trait.d.ts} +3 -3
- package/out/types/src/evaluator/types/module.d.ts +4 -4
- package/out/types/src/evaluator/types/newtype.d.ts +3 -3
- package/out/types/src/evaluator/types/object.d.ts +3 -3
- package/out/types/src/evaluator/types/slice.d.ts +3 -3
- package/out/types/src/evaluator/types/struct.d.ts +3 -3
- package/out/types/src/evaluator/types/synthesizer.d.ts +2 -2
- package/out/types/src/evaluator/types/trait.d.ts +4 -4
- package/out/types/src/evaluator/types/tuple.d.ts +4 -4
- package/out/types/src/evaluator/types/union.d.ts +3 -3
- package/out/types/src/evaluator/types/utils.d.ts +84 -5
- package/out/types/src/evaluator/types/validation.d.ts +2 -2
- package/out/types/src/evaluator/utils/closure.d.ts +7 -7
- package/out/types/src/evaluator/utils.d.ts +2 -2
- package/out/types/src/evaluator/values/{anonymous_function.d.ts → anonymous-function.d.ts} +3 -3
- package/out/types/src/evaluator/values/{anonymous_module.d.ts → anonymous-module.d.ts} +5 -5
- package/out/types/src/evaluator/values/{anonymous_struct.d.ts → anonymous-struct.d.ts} +3 -3
- package/out/types/src/evaluator/values/array.d.ts +3 -3
- package/out/types/src/evaluator/values/boolean.d.ts +2 -2
- package/out/types/src/evaluator/values/char.d.ts +2 -2
- package/out/types/src/evaluator/values/{clone_value.d.ts → clone-value.d.ts} +1 -1
- package/out/types/src/evaluator/values/comptime-list.d.ts +8 -0
- package/out/types/src/evaluator/values/dyn.d.ts +3 -3
- package/out/types/src/evaluator/values/float.d.ts +3 -3
- package/out/types/src/evaluator/values/impl.d.ts +5 -5
- package/out/types/src/evaluator/values/integer.d.ts +3 -3
- package/out/types/src/evaluator/values/string.d.ts +2 -2
- package/out/types/src/evaluator/values/tuple.d.ts +5 -5
- package/out/types/src/expr.d.ts +283 -283
- package/out/types/src/function-value.d.ts +6 -6
- package/out/types/src/index.d.ts +3 -0
- package/out/types/src/lexer.d.ts +1 -1
- package/out/types/src/module-manager.d.ts +1 -1
- package/out/types/src/parser.d.ts +2 -2
- package/out/types/src/test-runner.d.ts +1 -1
- package/out/types/src/type-value.d.ts +1 -1
- package/out/types/src/types/compatibility.d.ts +2 -2
- package/out/types/src/types/creators.d.ts +29 -16
- package/out/types/src/types/definitions.d.ts +29 -26
- package/out/types/src/types/env-lookup.d.ts +4 -0
- package/out/types/src/types/guards.d.ts +6 -6
- package/out/types/src/types/hierarchy.d.ts +2 -2
- package/out/types/src/types/tags.d.ts +4 -4
- package/out/types/src/types/utils.d.ts +13 -38
- package/out/types/src/unit-value.d.ts +1 -1
- package/out/types/src/value-tag.d.ts +4 -4
- package/out/types/src/value.d.ts +32 -26
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -1
- package/std/allocator.yo +5 -5
- package/std/collections/array_list.yo +394 -390
- package/std/collections/hash_map.yo +411 -406
- package/std/collections/hash_set.yo +646 -642
- package/std/collections/linked_list.yo +347 -341
- package/std/io.yo +55 -53
- package/std/path.yo +17 -16
- package/std/prelude.yo +1587 -1455
- package/std/string/rune.yo +25 -24
- package/std/string/string.yo +46 -43
- package/std/sync.yo +50 -30
- package/std/thread.yo +6 -5
- package/std/time.yo +1 -1
- package/out/types/src/codegen/exprs/compt_value.d.ts +0 -4
- package/out/types/src/codegen/types/index.d.ts +0 -3
- package/out/types/src/codegen/values/index.d.ts +0 -1
- package/out/types/src/evaluator/builtins/compt_assert.d.ts +0 -8
- package/out/types/src/evaluator/builtins/compt_boolean_fns.d.ts +0 -8
- package/out/types/src/evaluator/builtins/compt_expect_error.d.ts +0 -8
- package/out/types/src/evaluator/builtins/compt_fn.d.ts +0 -8
- package/out/types/src/evaluator/builtins/compt_list_fns.d.ts +0 -33
- package/out/types/src/evaluator/builtins/compt_numeric_fns.d.ts +0 -8
- package/out/types/src/evaluator/builtins/compt_print.d.ts +0 -8
- package/out/types/src/evaluator/builtins/compt_string_fns.d.ts +0 -8
- package/out/types/src/evaluator/calls/compt_function.d.ts +0 -19
- package/out/types/src/evaluator/calls/compt_list_type.d.ts +0 -11
- package/out/types/src/evaluator/exprs/subtype_of.d.ts +0 -21
- package/out/types/src/evaluator/types/compt_list.d.ts +0 -8
- package/out/types/src/evaluator/values/anonymous_trait.d.ts +0 -17
- package/out/types/src/evaluator/values/compt_list.d.ts +0 -8
- package/out/types/src/types/constants.d.ts +0 -5
- package/out/types/src/types/index.d.ts +0 -8
- package/std/allocators/c_allocator.yo +0 -118
- package/std/index.yo +0 -12
|
@@ -54,7 +54,7 @@ MAX_LOAD_FACTOR_DENOMINATOR :: usize(8);
|
|
|
54
54
|
/**
|
|
55
55
|
* Bucket - Internal structure to store key-value pairs
|
|
56
56
|
*/
|
|
57
|
-
Bucket :: (fn(
|
|
57
|
+
Bucket :: (fn(comptime(K): Type, comptime(V): Type) -> comptime(Type))
|
|
58
58
|
struct(
|
|
59
59
|
key : K,
|
|
60
60
|
value : V
|
|
@@ -85,442 +85,447 @@ h1_hash :: (fn(hash: u64, capacity: usize) -> usize)
|
|
|
85
85
|
* - size: Current number of occupied buckets
|
|
86
86
|
*/
|
|
87
87
|
HashMap :: (fn(
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
comptime(K): Type,
|
|
89
|
+
comptime(V): Type,
|
|
90
90
|
where(K <: (Eq(K), Hash))
|
|
91
|
-
) ->
|
|
91
|
+
) -> comptime(Type))
|
|
92
92
|
object(
|
|
93
93
|
ctrl : ?*(u8),
|
|
94
94
|
data : ?*(Bucket(K, V)),
|
|
95
95
|
capacity : usize,
|
|
96
|
-
size : usize
|
|
96
|
+
size : usize
|
|
97
|
+
)
|
|
98
|
+
;
|
|
97
99
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
100
|
+
impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMap(K, V),
|
|
101
|
+
/**
|
|
102
|
+
* Allocate memory for HashMap with given capacity
|
|
103
|
+
* Initializes all control bytes to EMPTY
|
|
104
|
+
*/
|
|
105
|
+
_alloc_with_capacity : (fn(capacity: usize) -> Result(Self, HashMapError))(
|
|
106
|
+
{
|
|
107
|
+
bucket_size :: sizeof(Bucket(K, V));
|
|
108
|
+
ctrl_size := capacity;
|
|
109
|
+
data_size := (capacity * bucket_size);
|
|
110
|
+
|
|
111
|
+
ctrl_result := malloc(ctrl_size);
|
|
112
|
+
match(ctrl_result,
|
|
113
|
+
.None => .Err(.AllocError(error: .OutOfMemory)),
|
|
114
|
+
.Some(ctrl_void_ptr) => {
|
|
115
|
+
ctrl_ptr := *(u8)(ctrl_void_ptr);
|
|
116
|
+
|
|
117
|
+
data_result := malloc(data_size);
|
|
118
|
+
match(data_result,
|
|
119
|
+
.None => {
|
|
120
|
+
free(.Some(*(void)(ctrl_ptr)));
|
|
121
|
+
.Err(.AllocError(error: .OutOfMemory))
|
|
122
|
+
},
|
|
123
|
+
.Some(data_void_ptr) => {
|
|
124
|
+
data_ptr := *(Bucket(K, V))(data_void_ptr);
|
|
125
|
+
|
|
126
|
+
i := usize(0);
|
|
127
|
+
while(i < capacity, i = (i + usize(1)), {
|
|
128
|
+
(ctrl_ptr &+ i).* = CTRL_EMPTY;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
.Ok(Self(
|
|
132
|
+
ctrl: .Some(ctrl_ptr),
|
|
133
|
+
data: .Some(data_ptr),
|
|
134
|
+
capacity: capacity,
|
|
135
|
+
size: usize(0)
|
|
136
|
+
))
|
|
137
|
+
}
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
),
|
|
140
143
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Create a new empty HashMap with default capacity
|
|
146
|
+
*/
|
|
147
|
+
new : (fn() -> Self)({
|
|
148
|
+
result := Self._alloc_with_capacity(DEFAULT_CAPACITY);
|
|
149
|
+
match(result,
|
|
150
|
+
.Ok(map) => map,
|
|
151
|
+
.Err(_) => panic("Failed to allocate HashMap")
|
|
152
|
+
)
|
|
153
|
+
}),
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Create a HashMap with a specific initial capacity
|
|
157
|
+
* Capacity will be rounded up to next power of 2
|
|
158
|
+
*/
|
|
159
|
+
with_capacity : (fn(requested_capacity: usize) -> Self)(
|
|
160
|
+
{
|
|
161
|
+
capacity := cond(
|
|
162
|
+
(requested_capacity < DEFAULT_CAPACITY) => DEFAULT_CAPACITY,
|
|
163
|
+
true => {
|
|
164
|
+
c := requested_capacity;
|
|
165
|
+
c = (c - usize(1));
|
|
166
|
+
c = (c | (c >> usize(1)));
|
|
167
|
+
c = (c | (c >> usize(2)));
|
|
168
|
+
c = (c | (c >> usize(4)));
|
|
169
|
+
c = (c | (c >> usize(8)));
|
|
170
|
+
c = (c | (c >> usize(16)));
|
|
171
|
+
(c + usize(1))
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
result := Self._alloc_with_capacity(capacity);
|
|
146
176
|
match(result,
|
|
147
177
|
.Ok(map) => map,
|
|
148
178
|
.Err(_) => panic("Failed to allocate HashMap")
|
|
149
179
|
)
|
|
150
|
-
}
|
|
180
|
+
}
|
|
181
|
+
),
|
|
151
182
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
c := requested_capacity;
|
|
162
|
-
c = (c - usize(1));
|
|
163
|
-
c = (c | (c >> usize(1)));
|
|
164
|
-
c = (c | (c >> usize(2)));
|
|
165
|
-
c = (c | (c >> usize(4)));
|
|
166
|
-
c = (c | (c >> usize(8)));
|
|
167
|
-
c = (c | (c >> usize(16)));
|
|
168
|
-
(c + usize(1))
|
|
169
|
-
}
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
result := Self._alloc_with_capacity(capacity);
|
|
173
|
-
match(result,
|
|
174
|
-
.Ok(map) => map,
|
|
175
|
-
.Err(_) => panic("Failed to allocate HashMap")
|
|
176
|
-
)
|
|
177
|
-
}
|
|
178
|
-
),
|
|
183
|
+
/**
|
|
184
|
+
* Get ctrl pointer (unwrapped for internal use)
|
|
185
|
+
*/
|
|
186
|
+
_ctrl_ptr : (fn(self: Self) -> *(u8))(
|
|
187
|
+
match(self.ctrl,
|
|
188
|
+
.Some(ptr) => ptr,
|
|
189
|
+
.None => panic("HashMap ctrl pointer is null")
|
|
190
|
+
)
|
|
191
|
+
),
|
|
179
192
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Get data pointer (unwrapped for internal use)
|
|
192
|
-
*/
|
|
193
|
-
_data_ptr :: (fn(self: Self) -> *(Bucket(K, V)))(
|
|
194
|
-
match(self.data,
|
|
195
|
-
.Some(ptr) => ptr,
|
|
196
|
-
.None => panic("HashMap data pointer is null")
|
|
197
|
-
)
|
|
198
|
-
),
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Find bucket index for a given key using quadratic probing
|
|
202
|
-
* Returns Some(index) if key is found, None otherwise
|
|
203
|
-
*/
|
|
204
|
-
_find_bucket :: (fn(self: Self, key: K, hash: u64) -> Option(usize))(
|
|
205
|
-
{
|
|
206
|
-
h2 := h2_hash(hash);
|
|
207
|
-
index := h1_hash(hash, self.capacity);
|
|
208
|
-
probe := usize(0);
|
|
209
|
-
ctrl_ptr := self._ctrl_ptr();
|
|
210
|
-
data_ptr := self._data_ptr();
|
|
211
|
-
|
|
212
|
-
while(probe < self.capacity, probe = (probe + usize(1)), {
|
|
213
|
-
probe_index := ((index + probe) % self.capacity);
|
|
214
|
-
ctrl_byte := (ctrl_ptr &+ probe_index).*;
|
|
215
|
-
|
|
216
|
-
cond(
|
|
217
|
-
(ctrl_byte == CTRL_EMPTY) => {
|
|
218
|
-
return .None;
|
|
219
|
-
},
|
|
220
|
-
(ctrl_byte == h2) => {
|
|
221
|
-
bucket := (data_ptr &+ probe_index).*;
|
|
222
|
-
cond(
|
|
223
|
-
(bucket.key == key) => {
|
|
224
|
-
return .Some(probe_index);
|
|
225
|
-
},
|
|
226
|
-
true => ()
|
|
227
|
-
);
|
|
228
|
-
},
|
|
229
|
-
true => ()
|
|
230
|
-
);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
.None
|
|
234
|
-
}
|
|
235
|
-
),
|
|
193
|
+
/**
|
|
194
|
+
* Get data pointer (unwrapped for internal use)
|
|
195
|
+
*/
|
|
196
|
+
_data_ptr : (fn(self: Self) -> *(Bucket(K, V)))(
|
|
197
|
+
match(self.data,
|
|
198
|
+
.Some(ptr) => ptr,
|
|
199
|
+
.None => panic("HashMap data pointer is null")
|
|
200
|
+
)
|
|
201
|
+
),
|
|
236
202
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
((ctrl_byte == CTRL_EMPTY) || (ctrl_byte == CTRL_DELETED)) => {
|
|
253
|
-
return probe_index;
|
|
254
|
-
},
|
|
255
|
-
true => ()
|
|
256
|
-
);
|
|
257
|
-
});
|
|
203
|
+
/**
|
|
204
|
+
* Find bucket index for a given key using quadratic probing
|
|
205
|
+
* Returns Some(index) if key is found, None otherwise
|
|
206
|
+
*/
|
|
207
|
+
_find_bucket : (fn(self: Self, key: K, hash: u64) -> Option(usize))(
|
|
208
|
+
{
|
|
209
|
+
h2 := h2_hash(hash);
|
|
210
|
+
index := h1_hash(hash, self.capacity);
|
|
211
|
+
probe := usize(0);
|
|
212
|
+
ctrl_ptr := Self._ctrl_ptr(self);
|
|
213
|
+
data_ptr := Self._data_ptr(self);
|
|
214
|
+
|
|
215
|
+
while(probe < self.capacity, probe = (probe + usize(1)), {
|
|
216
|
+
probe_index := ((index + probe) % self.capacity);
|
|
217
|
+
ctrl_byte := (ctrl_ptr &+ probe_index).*;
|
|
258
218
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
*/
|
|
266
|
-
_needs_resize :: ((fn(self: Self) -> bool)
|
|
267
|
-
{
|
|
268
|
-
threshold := ((self.capacity * MAX_LOAD_FACTOR_NUMERATOR) / MAX_LOAD_FACTOR_DENOMINATOR);
|
|
269
|
-
(self.size >= threshold)
|
|
270
|
-
}
|
|
271
|
-
),
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Resize and rehash the HashMap to a new capacity
|
|
275
|
-
*/
|
|
276
|
-
_resize :: (fn(self: Self, new_capacity: usize) -> Result(unit, HashMapError))(
|
|
277
|
-
{
|
|
278
|
-
result := Self._alloc_with_capacity(new_capacity);
|
|
279
|
-
match(result,
|
|
280
|
-
.Err(err) => .Err(err),
|
|
281
|
-
.Ok(new_map) => {
|
|
282
|
-
i := usize(0);
|
|
283
|
-
ctrl_ptr := self._ctrl_ptr();
|
|
284
|
-
data_ptr := self._data_ptr();
|
|
285
|
-
while(i < self.capacity, i = (i + usize(1)), {
|
|
286
|
-
ctrl_byte := (ctrl_ptr &+ i).*;
|
|
287
|
-
cond(
|
|
288
|
-
((ctrl_byte != CTRL_EMPTY) && (ctrl_byte != CTRL_DELETED)) => {
|
|
289
|
-
bucket := (data_ptr &+ i).*;
|
|
290
|
-
hash := (bucket.key).hash();
|
|
291
|
-
h2 := h2_hash(hash);
|
|
292
|
-
new_index := new_map._find_insert_bucket(hash);
|
|
293
|
-
|
|
294
|
-
new_ctrl_ptr := new_map._ctrl_ptr();
|
|
295
|
-
new_data_ptr := new_map._data_ptr();
|
|
296
|
-
(new_ctrl_ptr &+ new_index).* = h2;
|
|
297
|
-
consume((new_data_ptr &+ new_index).* = bucket);
|
|
298
|
-
new_map.size = (new_map.size + usize(1));
|
|
299
|
-
},
|
|
300
|
-
true => ()
|
|
301
|
-
);
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
// Drop old buckets before freeing old data array
|
|
305
|
-
// (bucket := ... above duped them, so we need to drop the old refs)
|
|
219
|
+
cond(
|
|
220
|
+
(ctrl_byte == CTRL_EMPTY) => {
|
|
221
|
+
return .None;
|
|
222
|
+
},
|
|
223
|
+
(ctrl_byte == h2) => {
|
|
224
|
+
bucket := (data_ptr &+ probe_index).*;
|
|
306
225
|
cond(
|
|
307
|
-
(
|
|
308
|
-
|
|
309
|
-
while(j < self.capacity, j = (j + usize(1)), {
|
|
310
|
-
ctrl_byte := (ctrl_ptr &+ j).*;
|
|
311
|
-
cond(
|
|
312
|
-
((ctrl_byte != CTRL_EMPTY) && (ctrl_byte != CTRL_DELETED)) => {
|
|
313
|
-
bucket_ptr := (data_ptr &+ j);
|
|
314
|
-
cond(
|
|
315
|
-
Type.contains_rc_type(K) => unsafe.drop((bucket_ptr.*).key),
|
|
316
|
-
true => ()
|
|
317
|
-
);
|
|
318
|
-
cond(
|
|
319
|
-
Type.contains_rc_type(V) => unsafe.drop((bucket_ptr.*).value),
|
|
320
|
-
true => ()
|
|
321
|
-
);
|
|
322
|
-
},
|
|
323
|
-
true => ()
|
|
324
|
-
);
|
|
325
|
-
});
|
|
226
|
+
(bucket.key == key) => {
|
|
227
|
+
return .Some(probe_index);
|
|
326
228
|
},
|
|
327
229
|
true => ()
|
|
328
230
|
);
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
);
|
|
338
|
-
|
|
339
|
-
self.ctrl = new_map.ctrl;
|
|
340
|
-
self.data = new_map.data;
|
|
341
|
-
self.capacity = new_map.capacity;
|
|
342
|
-
|
|
343
|
-
new_map.ctrl = .None;
|
|
344
|
-
new_map.data = .None;
|
|
345
|
-
|
|
346
|
-
.Ok(())
|
|
347
|
-
}
|
|
348
|
-
)
|
|
349
|
-
}
|
|
350
|
-
),
|
|
231
|
+
},
|
|
232
|
+
true => ()
|
|
233
|
+
);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
.None
|
|
237
|
+
}
|
|
238
|
+
),
|
|
351
239
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
240
|
+
/**
|
|
241
|
+
* Find first available bucket (EMPTY or DELETED) for insertion
|
|
242
|
+
* Returns bucket index using quadratic probing
|
|
243
|
+
*/
|
|
244
|
+
_find_insert_bucket : (fn(self: Self, hash: u64) -> usize)(
|
|
245
|
+
{
|
|
246
|
+
index := h1_hash(hash, self.capacity);
|
|
247
|
+
probe := usize(0);
|
|
248
|
+
ctrl_ptr := Self._ctrl_ptr(self);
|
|
249
|
+
|
|
250
|
+
while(probe < self.capacity, probe = (probe + usize(1)), {
|
|
251
|
+
probe_index := ((index + probe) % self.capacity);
|
|
252
|
+
ctrl_byte := (ctrl_ptr &+ probe_index).*;
|
|
360
253
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
old_bucket := (data_ptr &+ index).*;
|
|
365
|
-
old_value := old_bucket.value;
|
|
366
|
-
new_bucket := Bucket(K, V)(key: key, value: value);
|
|
367
|
-
// Don't use consume here - we want to drop the old bucket at memory
|
|
368
|
-
// Note: old_bucket already duped it, so dropping here decrements to RC=1
|
|
369
|
-
// Then old_bucket goes out of scope and drops again to RC=0
|
|
370
|
-
// But wait - we return old_value which was duped from old_bucket...
|
|
371
|
-
// Actually the Bucket struct contains RC types, so assignment drops old + dups new
|
|
372
|
-
(data_ptr &+ index).* = new_bucket;
|
|
373
|
-
.Ok(.Some(old_value))
|
|
254
|
+
cond(
|
|
255
|
+
((ctrl_byte == CTRL_EMPTY) || (ctrl_byte == CTRL_DELETED)) => {
|
|
256
|
+
return probe_index;
|
|
374
257
|
},
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
258
|
+
true => ()
|
|
259
|
+
);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
panic("HashMap is full - should have resized")
|
|
263
|
+
}
|
|
264
|
+
),
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Check if HashMap needs resizing based on load factor
|
|
268
|
+
*/
|
|
269
|
+
_needs_resize : ((fn(self: Self) -> bool)
|
|
270
|
+
{
|
|
271
|
+
threshold := ((self.capacity * MAX_LOAD_FACTOR_NUMERATOR) / MAX_LOAD_FACTOR_DENOMINATOR);
|
|
272
|
+
(self.size >= threshold)
|
|
273
|
+
}
|
|
274
|
+
),
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Resize and rehash the HashMap to a new capacity
|
|
278
|
+
*/
|
|
279
|
+
_resize : (fn(self: Self, new_capacity: usize) -> Result(unit, HashMapError))(
|
|
280
|
+
{
|
|
281
|
+
result := Self._alloc_with_capacity(new_capacity);
|
|
282
|
+
match(result,
|
|
283
|
+
.Err(err) => .Err(err),
|
|
284
|
+
.Ok(new_map) => {
|
|
285
|
+
i := usize(0);
|
|
286
|
+
ctrl_ptr := Self._ctrl_ptr(self);
|
|
287
|
+
data_ptr := Self._data_ptr(self);
|
|
288
|
+
while(i < self.capacity, i = (i + usize(1)), {
|
|
289
|
+
ctrl_byte := (ctrl_ptr &+ i).*;
|
|
290
|
+
cond(
|
|
291
|
+
((ctrl_byte != CTRL_EMPTY) && (ctrl_byte != CTRL_DELETED)) => {
|
|
292
|
+
bucket := (data_ptr &+ i).*;
|
|
293
|
+
hash := (bucket.key).hash();
|
|
387
294
|
h2 := h2_hash(hash);
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
ctrl_ptr := self._ctrl_ptr();
|
|
391
|
-
data_ptr := self._data_ptr();
|
|
392
|
-
(ctrl_ptr &+ index).* = h2;
|
|
393
|
-
new_bucket := Bucket(K, V)(key: key, value: value);
|
|
394
|
-
consume((data_ptr &+ index).* = new_bucket);
|
|
395
|
-
self.size = (self.size + usize(1));
|
|
295
|
+
new_index := Self._find_insert_bucket(new_map, hash);
|
|
396
296
|
|
|
397
|
-
.
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
297
|
+
new_ctrl_ptr := Self._ctrl_ptr(new_map);
|
|
298
|
+
new_data_ptr := Self._data_ptr(new_map);
|
|
299
|
+
(new_ctrl_ptr &+ new_index).* = h2;
|
|
300
|
+
consume((new_data_ptr &+ new_index).* = bucket);
|
|
301
|
+
new_map.size = (new_map.size + usize(1));
|
|
302
|
+
},
|
|
303
|
+
true => ()
|
|
304
|
+
);
|
|
305
|
+
});
|
|
404
306
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
307
|
+
// Drop old buckets before freeing old data array
|
|
308
|
+
// (bucket := ... above duped them, so we need to drop the old refs)
|
|
309
|
+
cond(
|
|
310
|
+
(Type.contains_rc_type(K) || Type.contains_rc_type(V)) => {
|
|
311
|
+
j := usize(0);
|
|
312
|
+
while(j < self.capacity, j = (j + usize(1)), {
|
|
313
|
+
ctrl_byte := (ctrl_ptr &+ j).*;
|
|
314
|
+
cond(
|
|
315
|
+
((ctrl_byte != CTRL_EMPTY) && (ctrl_byte != CTRL_DELETED)) => {
|
|
316
|
+
bucket_ptr := (data_ptr &+ j);
|
|
317
|
+
cond(
|
|
318
|
+
Type.contains_rc_type(K) => unsafe.drop((bucket_ptr.*).key),
|
|
319
|
+
true => ()
|
|
320
|
+
);
|
|
321
|
+
cond(
|
|
322
|
+
Type.contains_rc_type(V) => unsafe.drop((bucket_ptr.*).value),
|
|
323
|
+
true => ()
|
|
324
|
+
);
|
|
325
|
+
},
|
|
326
|
+
true => ()
|
|
327
|
+
);
|
|
328
|
+
});
|
|
329
|
+
},
|
|
330
|
+
true => ()
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
match(self.ctrl,
|
|
334
|
+
.Some(ptr) => free(.Some(*(void)(ptr))),
|
|
335
|
+
.None => ()
|
|
336
|
+
);
|
|
337
|
+
match(self.data,
|
|
338
|
+
.Some(ptr) => free(.Some(*(void)(ptr))),
|
|
339
|
+
.None => ()
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
self.ctrl = new_map.ctrl;
|
|
343
|
+
self.data = new_map.data;
|
|
344
|
+
self.capacity = new_map.capacity;
|
|
345
|
+
|
|
346
|
+
new_map.ctrl = .None;
|
|
347
|
+
new_map.data = .None;
|
|
348
|
+
|
|
349
|
+
.Ok(())
|
|
350
|
+
}
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
),
|
|
423
354
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
)
|
|
435
|
-
|
|
436
|
-
|
|
355
|
+
/**
|
|
356
|
+
* Insert or update a key-value pair
|
|
357
|
+
* Returns Ok(Some(old_value)) if key existed, Ok(None) if new key
|
|
358
|
+
*/
|
|
359
|
+
set : ((fn(self: Self, key: K, value: V) -> Result(Option(V), HashMapError))
|
|
360
|
+
{
|
|
361
|
+
hash := key.hash();
|
|
362
|
+
bucket_opt := Self._find_bucket(self, key, hash);
|
|
363
|
+
|
|
364
|
+
match(bucket_opt,
|
|
365
|
+
.Some(index) => {
|
|
366
|
+
data_ptr := Self._data_ptr(self);
|
|
367
|
+
old_bucket := (data_ptr &+ index).*;
|
|
368
|
+
old_value := old_bucket.value;
|
|
369
|
+
new_bucket := Bucket(K, V)(key: key, value: value);
|
|
370
|
+
// Don't use consume here - we want to drop the old bucket at memory
|
|
371
|
+
// Note: old_bucket already duped it, so dropping here decrements to RC=1
|
|
372
|
+
// Then old_bucket goes out of scope and drops again to RC=0
|
|
373
|
+
// But wait - we return old_value which was duped from old_bucket...
|
|
374
|
+
// Actually the Bucket struct contains RC types, so assignment drops old + dups new
|
|
375
|
+
(data_ptr &+ index).* = new_bucket;
|
|
376
|
+
.Ok(.Some(old_value))
|
|
377
|
+
},
|
|
378
|
+
.None => {
|
|
379
|
+
resize_check := cond(
|
|
380
|
+
(Self._needs_resize(self)) => {
|
|
381
|
+
new_capacity := (self.capacity * usize(2));
|
|
382
|
+
Self._resize(self, new_capacity)
|
|
383
|
+
},
|
|
384
|
+
true => (Result(unit, HashMapError).Ok(()))
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
match(resize_check,
|
|
388
|
+
.Err(err) => .Err(err),
|
|
389
|
+
.Ok(_) => {
|
|
390
|
+
h2 := h2_hash(hash);
|
|
391
|
+
index := Self._find_insert_bucket(self, hash);
|
|
392
|
+
|
|
393
|
+
ctrl_ptr := Self._ctrl_ptr(self);
|
|
394
|
+
data_ptr := Self._data_ptr(self);
|
|
395
|
+
(ctrl_ptr &+ index).* = h2;
|
|
396
|
+
new_bucket := Bucket(K, V)(key: key, value: value);
|
|
397
|
+
consume((data_ptr &+ index).* = new_bucket);
|
|
398
|
+
self.size = (self.size + usize(1));
|
|
399
|
+
|
|
400
|
+
.Ok(.None)
|
|
401
|
+
}
|
|
402
|
+
)
|
|
403
|
+
}
|
|
404
|
+
)
|
|
405
|
+
}
|
|
406
|
+
),
|
|
437
407
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
unsafe.drop(bucket_ptr.*.key);
|
|
457
|
-
|
|
458
|
-
// Drop the bucket's value extra ref (we duped it above)
|
|
459
|
-
unsafe.drop(bucket_ptr.*.value);
|
|
460
|
-
|
|
461
|
-
(ctrl_ptr &+ index).* = CTRL_DELETED;
|
|
462
|
-
self.size = (self.size - usize(1));
|
|
463
|
-
|
|
464
|
-
.Some(value)
|
|
465
|
-
},
|
|
466
|
-
.None => .None
|
|
467
|
-
)
|
|
468
|
-
}
|
|
469
|
-
),
|
|
408
|
+
/**
|
|
409
|
+
* Get a value by key
|
|
410
|
+
* Returns Some(value) if found, None otherwise
|
|
411
|
+
*/
|
|
412
|
+
get : ((fn(self: Self, key: K) -> Option(V))
|
|
413
|
+
{
|
|
414
|
+
hash := key.hash();
|
|
415
|
+
bucket_opt := Self._find_bucket(self, key, hash);
|
|
416
|
+
match(bucket_opt,
|
|
417
|
+
.Some(index) => {
|
|
418
|
+
data_ptr := Self._data_ptr(self);
|
|
419
|
+
bucket := (data_ptr &+ index).*;
|
|
420
|
+
.Some(bucket.value)
|
|
421
|
+
},
|
|
422
|
+
.None => .None
|
|
423
|
+
)
|
|
424
|
+
}
|
|
425
|
+
),
|
|
470
426
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
427
|
+
/**
|
|
428
|
+
* Check if a key exists in the map
|
|
429
|
+
*/
|
|
430
|
+
has : ((fn(self: Self, key: K) -> bool)
|
|
431
|
+
{
|
|
432
|
+
hash := key.hash();
|
|
433
|
+
bucket_opt := Self._find_bucket(self, key, hash);
|
|
434
|
+
match(bucket_opt,
|
|
435
|
+
.Some(_) => true,
|
|
436
|
+
.None => false
|
|
437
|
+
)
|
|
438
|
+
}
|
|
439
|
+
),
|
|
477
440
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
441
|
+
/**
|
|
442
|
+
* Remove a key-value pair by key
|
|
443
|
+
* Returns Some(value) if the key was found and removed, None otherwise
|
|
444
|
+
*/
|
|
445
|
+
remove : ((fn(self: Self, key: K) -> Option(V))
|
|
446
|
+
{
|
|
447
|
+
hash := key.hash();
|
|
448
|
+
bucket_opt := Self._find_bucket(self, key, hash);
|
|
449
|
+
match(bucket_opt,
|
|
450
|
+
.Some(index) => {
|
|
451
|
+
data_ptr := Self._data_ptr(self);
|
|
452
|
+
ctrl_ptr := Self._ctrl_ptr(self);
|
|
453
|
+
bucket_ptr := (data_ptr &+ index);
|
|
454
|
+
|
|
455
|
+
// Read and dup value for return
|
|
456
|
+
value := bucket_ptr.*.value;
|
|
457
|
+
|
|
458
|
+
// Drop the bucket's key (we're removing it)
|
|
459
|
+
unsafe.drop(bucket_ptr.*.key);
|
|
460
|
+
|
|
461
|
+
// Drop the bucket's value extra ref (we duped it above)
|
|
462
|
+
unsafe.drop(bucket_ptr.*.value);
|
|
463
|
+
|
|
464
|
+
(ctrl_ptr &+ index).* = CTRL_DELETED;
|
|
465
|
+
self.size = (self.size - usize(1));
|
|
466
|
+
|
|
467
|
+
.Some(value)
|
|
468
|
+
},
|
|
469
|
+
.None => .None
|
|
470
|
+
)
|
|
471
|
+
}
|
|
472
|
+
),
|
|
484
473
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
ctrl_byte := (ctrl_ptr &+ i).*;
|
|
499
|
-
cond(
|
|
500
|
-
((ctrl_byte != CTRL_EMPTY) && (ctrl_byte != CTRL_DELETED)) => {
|
|
501
|
-
bucket_ptr := (data_ptr &+ i);
|
|
502
|
-
unsafe.drop(bucket_ptr.*);
|
|
503
|
-
},
|
|
504
|
-
true => ()
|
|
505
|
-
);
|
|
506
|
-
});
|
|
507
|
-
},
|
|
508
|
-
true => ()
|
|
509
|
-
);
|
|
510
|
-
|
|
511
|
-
ctrl_ptr := self._ctrl_ptr();
|
|
512
|
-
i := usize(0);
|
|
513
|
-
while(i < self.capacity, i = (i + usize(1)), {
|
|
514
|
-
(ctrl_ptr &+ i).* = CTRL_EMPTY;
|
|
515
|
-
});
|
|
516
|
-
self.size = usize(0);
|
|
517
|
-
}
|
|
518
|
-
),
|
|
474
|
+
/**
|
|
475
|
+
* Get the number of key-value pairs in the map
|
|
476
|
+
*/
|
|
477
|
+
len : ((fn(self: Self) -> usize)
|
|
478
|
+
self.size
|
|
479
|
+
),
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Check if the map is empty
|
|
483
|
+
*/
|
|
484
|
+
is_empty : ((fn(self: Self) -> bool)
|
|
485
|
+
(self.size == usize(0))
|
|
486
|
+
),
|
|
519
487
|
|
|
488
|
+
/**
|
|
489
|
+
* Clear all key-value pairs
|
|
490
|
+
* Resets all control bytes to EMPTY
|
|
491
|
+
*/
|
|
492
|
+
clear : ((fn(self: Self) -> unit)
|
|
493
|
+
{
|
|
494
|
+
// Drop all occupied buckets before clearing
|
|
495
|
+
cond(
|
|
496
|
+
(Type.contains_rc_type(K) || Type.contains_rc_type(V)) => {
|
|
497
|
+
ctrl_ptr := Self._ctrl_ptr(self);
|
|
498
|
+
data_ptr := Self._data_ptr(self);
|
|
499
|
+
i := usize(0);
|
|
500
|
+
while(i < self.capacity, i = (i + usize(1)), {
|
|
501
|
+
ctrl_byte := (ctrl_ptr &+ i).*;
|
|
502
|
+
cond(
|
|
503
|
+
((ctrl_byte != CTRL_EMPTY) && (ctrl_byte != CTRL_DELETED)) => {
|
|
504
|
+
bucket_ptr := (data_ptr &+ i);
|
|
505
|
+
unsafe.drop(bucket_ptr.*);
|
|
506
|
+
},
|
|
507
|
+
true => ()
|
|
508
|
+
);
|
|
509
|
+
});
|
|
510
|
+
},
|
|
511
|
+
true => ()
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
ctrl_ptr := Self._ctrl_ptr(self);
|
|
515
|
+
i := usize(0);
|
|
516
|
+
while(i < self.capacity, i = (i + usize(1)), {
|
|
517
|
+
(ctrl_ptr &+ i).* = CTRL_EMPTY;
|
|
518
|
+
});
|
|
519
|
+
self.size = usize(0);
|
|
520
|
+
}
|
|
521
|
+
)
|
|
522
|
+
);
|
|
523
|
+
impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMap(K, V),
|
|
524
|
+
Dispose(
|
|
520
525
|
/**
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
dispose
|
|
526
|
+
* RAII destructor - automatically called when HashMap goes out of scope
|
|
527
|
+
*/
|
|
528
|
+
dispose : (fn(self: Self) -> unit)({
|
|
524
529
|
// Drop all occupied buckets before freeing memory
|
|
525
530
|
cond(
|
|
526
531
|
(Type.contains_rc_type(K) || Type.contains_rc_type(V)) => {
|
|
@@ -558,7 +563,7 @@ HashMap :: (fn(
|
|
|
558
563
|
.None => ()
|
|
559
564
|
);
|
|
560
565
|
})
|
|
561
|
-
);
|
|
566
|
+
));
|
|
562
567
|
|
|
563
568
|
export
|
|
564
569
|
HashMap,
|