Skip to content

Commit

Permalink
adds conj!, assoc! and dissoc! for transients
Browse files Browse the repository at this point in the history
  • Loading branch information
Samy-33 committed Mar 16, 2024
1 parent 79e9275 commit 78ad135
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/cpp/jank/prelude.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
#include <jank/runtime/behavior/numberable.hpp>
#include <jank/runtime/behavior/nameable.hpp>
#include <jank/runtime/behavior/transientable.hpp>
#include <jank/runtime/behavior/associatively_writable.hpp>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace jank::runtime::behavior
template <typename T>
concept associatively_writable_in_place = requires(T * const t) {
{
t->assoc_in_place(object_ptr{}, object_ptr{})
t->assoc_in_place(object_ptr{}, object_ptr{}), t->dissoc_in_place(object_ptr{})
} -> std::convertible_to<object_ptr>;
};
}
3 changes: 2 additions & 1 deletion include/cpp/jank/runtime/obj/transient_hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ namespace jank::runtime
native_bool contains(object_ptr key) const;

/* behavior::associatively_writable_in_place */
native_box<static_object> assoc_in_place(object_ptr key, object_ptr val);
native_box<static_object> assoc_in_place(object_ptr const key, object_ptr const val);
native_box<static_object> dissoc_in_place(object_ptr const key);

/* behavior::consable_in_place */
native_box<static_object> cons_in_place(object_ptr head);
Expand Down
8 changes: 8 additions & 0 deletions src/cpp/jank/runtime/obj/transient_hash_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ namespace jank::runtime
return this;
}

obj::transient_hash_map_ptr
obj::transient_hash_map::dissoc_in_place(object_ptr const key)
{
assert_active();
data.erase(key);
return this;

Check warning on line 105 in src/cpp/jank/runtime/obj/transient_hash_map.cpp

View check run for this annotation

Codecov / codecov/patch

src/cpp/jank/runtime/obj/transient_hash_map.cpp#L103-L105

Added lines #L103 - L105 were not covered by tests
}

obj::transient_hash_map_ptr obj::transient_hash_map::cons_in_place(object_ptr const head)
{
assert_active();
Expand Down
81 changes: 81 additions & 0 deletions src/jank/clojure/core.jank
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,87 @@
#{ o }#
);"))

(def conj!)
(def conj!
(fn* conj!
([]
(transient []))
([coll]
coll)
([coll x]
(native/raw "__value = visit_object
(
[&](auto const typed_t, auto const head) -> object_ptr
{
using T = typename decltype(typed_t)::value_type;
if constexpr(behavior::persistentable<T>)
{
return typed_t->cons_in_place(head);
}
else
{ throw #{ (ex-info :not-persistentable {:o coll}) }#; }
},
#{ coll }#,
#{ x }#
);
"))))

(def assoc!)
(def assoc!
(fn* assoc!
([coll k v]
(native/raw "__value = visit_object
(
[&](auto const typed_t, auto const key, auto const val) -> object_ptr
{
using T = typename decltype(typed_t)::value_type;
if constexpr(behavior::associatively_writable_in_place<T>)
{
return typed_t->assoc_in_place(key, val);
}
else
{ throw #{ (ex-info :not-associatively_writable_in_place {:o coll}) }#; }
},
#{ coll }#,
#{ k }#,
#{ v }#
);
"))
([coll k v & kvs]
(let* [ret (assoc! coll k v)]
(if kvs
(recur ret (first kvs) (second kvs) (nnext kvs))
ret)))))

(def dissoc!)
(def dissoc!
(fn* dissoc!
([coll k]
(native/raw "__value = visit_object
(
[&](auto const typed_t, auto const key) -> object_ptr
{
using T = typename decltype(typed_t)::value_type;
if constexpr(behavior::associatively_writable_in_place<T>)
{
return typed_t->dissoc_in_place(key);
}
else
{ throw #{ (ex-info :not-associatively_writable_in_place {:o coll}) }#; }
},
#{ coll }#,
#{ k }#
);
"))
([coll k & ks]
(let* [ret (dissoc! coll k)]
(if ks
(recur ret (first ks) (next ks))
ret)))))

; Functions.

(defn- spread [arglist]
Expand Down

0 comments on commit 78ad135

Please sign in to comment.