Browse Source

Added hash table exposure

Also add way to iterate over keys
windows-imgui
Macoy Madson 1 year ago
parent
commit
915768deac
  1. 91
      src/Dictionary.cake

91
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 (* <your Key Value type>) &return size_t)
(def-c-function-alias dict-free hmfree)
;; (dict (* <your Key Value type>))
;; 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 (* <your Key Value type>) item <your Key Value type>
;; &return <your Key Value type>)
(def-c-function-alias dict-set-not-found-default-value hmdefault)
;; (dict (* <your Key Value type>) value <your Value type>
;; &return <your Value type>)
;; Setting
(def-c-function-alias dict-set-key-value hmput)
;; (dict (* <your Key Value type>) key <your Key type> value <your Value type>
;; &return <your Value type>)
;; Requires a key field
(def-c-function-alias dict-set-struct hmputs)
;; (dict (* <your Key Value type>) item <your Key Value type> &return <your Key Value type>)
;; Getting
(def-c-function-alias dict-ptr-at hmgetp_null)
;; (dict (* <your Key Value type>) key <your Key type> &return (* <your Key Value type>))
(def-c-function-alias dict-ptr-or-default-at hmgetp)
;; (dict (* <your Key Value type>) key <your Key type> &return (* <your Key Value type>))
(def-c-function-alias dict-index-at hmgeti)
;; (dict (* <your Key Value type>) key <your Key type> &return ptrdiff_t)
(def-c-function-alias dict-struct-at hmgets)
;; (dict (* <your Key Value type>) key <your Key type> &return <your Key Value type>)
(def-c-function-alias dict-value-at hmget)
;; (dict (* <your Key Value type>) &return <your value type>)
(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")

Loading…
Cancel
Save