diff --git a/src/Dictionary.cake b/src/Dictionary.cake index 30c0516..1ba3ea7 100644 --- a/src/Dictionary.cake +++ b/src/Dictionary.cake @@ -63,6 +63,19 @@ (token-splice-rest body tokens))) (return true)) +(defmacro each-key-in-strdict (strdict any index-iterator-name symbol + key-name symbol &rest body any) + (tokenize-push output + (c-for + (var (token-splice index-iterator-name) size_t 0) + ;; We could hoist this out but it should be a quick op anyways + (< (token-splice index-iterator-name) (strdict-length (token-splice strdict))) + (incr (token-splice index-iterator-name)) + (var (token-splice key-name) (* char) + (field (at (token-splice index-name strdict)) key)) + (token-splice-rest body tokens))) + (return true)) + (defmacro each-item-in-strdict (strdict any index-name symbol item-name symbol item-type any &rest body any) @@ -82,6 +95,84 @@ (token-splice-rest body tokens))) (return true)) +;; +;; Hash table with arbitrary key +;; + +(def-c-function-alias dict-length hmlenu) +;; (dict (* ) &return size_t) + +(def-c-function-alias dict-free hmfree) +;; (dict (* )) + +;; These are used for the following functions: +;; - dict-ptr-or-default-at +;; - dict-struct-at +;; - dict-value-at +(def-c-function-alias dict-set-not-found-default hmdefaults) +;; (dict (* ) item +;; &return ) +(def-c-function-alias dict-set-not-found-default-value hmdefault) +;; (dict (* ) value +;; &return ) + +;; Setting +(def-c-function-alias dict-set-key-value hmput) +;; (dict (* ) key value +;; &return ) + +;; Requires a key field +(def-c-function-alias dict-set-struct hmputs) +;; (dict (* ) item &return ) + +;; Getting +(def-c-function-alias dict-ptr-at hmgetp_null) +;; (dict (* ) key &return (* )) +(def-c-function-alias dict-ptr-or-default-at hmgetp) +;; (dict (* ) key &return (* )) +(def-c-function-alias dict-index-at hmgeti) +;; (dict (* ) key &return ptrdiff_t) +(def-c-function-alias dict-struct-at hmgets) +;; (dict (* ) key &return ) +(def-c-function-alias dict-value-at hmget) +;; (dict (* ) &return ) + +(defmacro each-key-in-dict (dict any index-iterator-name symbol + key-name symbol key-type symbol &rest body any) + (tokenize-push output + (c-for + (var (token-splice index-iterator-name) size_t 0) + ;; We could hoist this out but it should be a quick op anyways + (< (token-splice index-iterator-name) (dict-length (token-splice dict))) + (incr (token-splice index-iterator-name)) + (var (token-splice key-name) (token-splice key-type) + (field (at (token-splice index-name dict)) key)) + (token-splice-rest body tokens))) + (return true)) + +(defmacro each-item-in-dict (dict any index-name symbol + item-name symbol item-type any + &rest body any) + (tokenize-push output + (each-in-dict (token-splice dict) (token-splice index-name) + (var (token-splice item-name item-type) (at (token-splice index-name dict))) + (token-splice-rest body tokens))) + (return true)) + +(defmacro each-item-addr-in-dict (dict any index-name symbol + item-name symbol ptr-to-item-type any + &rest body any) + (tokenize-push output + (each-in-dict (token-splice dict) (token-splice index-name) + (var (token-splice item-name ptr-to-item-type) + (addr (at (token-splice index-name dict)))) + (token-splice-rest body tokens))) + (return true)) + +;; +;; Tests +;; + (comptime-cond ('auto-test (c-import "stdio.h")