Skip to content

Commit

Permalink
adopt the same slot macro mechanics as Map
Browse files Browse the repository at this point in the history
One day you just know we will have some callbacks on Sets...
  • Loading branch information
vygr committed Oct 26, 2024
1 parent fb2533e commit 90f52a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
24 changes: 12 additions & 12 deletions lib/collections/fset.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
(env-push)

(defmacro slot ()
`(defq x (get :buckets this)
b (% (hash key) (get :num_buckets this))
e (find key (elem-get x b))))
`(defq _x (get :buckets this)
_b (% (hash key) (get :num_buckets this))
_e (find key (elem-get _x _b))))

(defclass Fset (&optional num_buckets) (Set)
; (Fset [num_buckets]) -> fset
Expand All @@ -19,30 +19,30 @@

(defmethod :find (key)
; (. fset :find key) -> :nil | key
(if (slot) (elem-get (elem-get x b) e)))
(if (slot) (elem-get (elem-get _x _b) _e)))

(defmethod :insert (key)
; (. fset :insert key) -> fset
(if (slot) :nil (push (elem-get x b) key))
(if (slot) :nil (push (elem-get _x _b) key))
this)

(defmethod :inserted (key)
; (. fset :inserted key) -> :nil | fset
(cond
((slot) :nil)
((push (elem-get x b) key) this)))
((push (elem-get _x _b) key) this)))

(defmethod :intern (key)
; (. fset :intern key) -> key
(cond
((slot) (elem-get (elem-get x b) e))
((push (elem-get x b) key) key)))
((slot) (elem-get (elem-get _x _b) _e))
((push (elem-get _x _b) key) key)))

(defmethod :erase (key)
; (. fset :erase key) -> fset
(when (slot)
(elem-set (defq b (elem-get x b)) e (last b))
(pop b))
(elem-set (defq _b (elem-get _x _b)) _e (last _b))
(pop _b))
this)

(defmethod_ :each (_fnc)
Expand Down Expand Up @@ -82,8 +82,8 @@
(defmethod :intersect (that)
; (. fset :intersect fset) -> fset
(unless (eql this that)
(each (# (elem-set b (!) (filter-array (# (. that :find %0)) %0)))
(defq b (get :buckets this))))
(each (# (elem-set _b (!) (filter-array (# (. that :find %0)) %0)))
(defq _b (get :buckets this))))
this)

(defmethod :not_intersect (that)
Expand Down
24 changes: 12 additions & 12 deletions lib/collections/xset.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
(env-push)

(defmacro slot ()
`(defq x (get :buckets this)
b (% ((get :hash_fnc this) key) (get :num_buckets this))
e (some (# (if ((get :cmp_fnc this) %0 key) (!))) (elem-get x b))))
`(defq _x (get :buckets this)
_b (% ((get :hash_fnc this) key) (get :num_buckets this))
_e (some (# (if ((get :cmp_fnc this) %0 key) (!))) (elem-get _x _b))))

(defclass Xset (&optional num_buckets cmp_fnc hash_fnc) (Set)
; (Xset [num_buckets cmp_fnc hash_fnc]) -> xset
Expand All @@ -20,30 +20,30 @@

(defmethod :find (key)
; (. xset :find key) -> :nil | key
(if (slot) (elem-get (elem-get x b) e)))
(if (slot) (elem-get (elem-get _x _b) _e)))

(defmethod :insert (key)
; (. xset :insert key) -> xset
(if (slot) :nil (push (elem-get x b) key))
(if (slot) :nil (push (elem-get _x _b) key))
this)

(defmethod :inserted (key)
; (. xset :inserted key) -> :nil | xset
(cond
((slot) this)
((push (elem-get x b) key) :nil)))
((push (elem-get _x _b) key) :nil)))

(defmethod :intern (key)
; (. xset :intern key) -> key
(cond
((slot) (elem-get (elem-get x b) e))
((push (elem-get x b) key) key)))
((slot) (elem-get (elem-get _x _b) _e))
((push (elem-get _x _b) key) key)))

(defmethod :erase (key)
; (. xset :erase key) -> xset
(when (slot)
(elem-set (defq b (elem-get x b)) e (last b))
(pop b))
(elem-set (defq _b (elem-get _x _b)) _e (last _b))
(pop _b))
this)

(defmethod_ :each (_fnc)
Expand Down Expand Up @@ -83,8 +83,8 @@
(defmethod :intersect (that)
; (. xset :intersect xset) -> xset
(unless (eql this that)
(each (# (elem-set b (!) (filter-array (# (. that :find %0)) %0)))
(defq b (get :buckets this))))
(each (# (elem-set _b (!) (filter-array (# (. that :find %0)) %0)))
(defq _b (get :buckets this))))
this)

(defmethod :not_intersect (that)
Expand Down

0 comments on commit 90f52a9

Please sign in to comment.