Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transient-vector): adds initial support for transient vector #60

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ add_library(
src/cpp/jank/runtime/obj/persistent_array_map.cpp
src/cpp/jank/runtime/obj/persistent_hash_map.cpp
src/cpp/jank/runtime/obj/transient_hash_map.cpp
src/cpp/jank/runtime/obj/transient_vector.cpp
src/cpp/jank/runtime/obj/persistent_set.cpp
src/cpp/jank/runtime/obj/persistent_string.cpp
src/cpp/jank/runtime/obj/cons.cpp
Expand Down
6 changes: 6 additions & 0 deletions include/cpp/jank/runtime/erasure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <jank/runtime/obj/persistent_hash_map.hpp>
#include <jank/runtime/obj/persistent_hash_map_sequence.hpp>
#include <jank/runtime/obj/transient_hash_map.hpp>
#include <jank/runtime/obj/transient_vector.hpp>
#include <jank/runtime/obj/iterator.hpp>
#include <jank/runtime/obj/range.hpp>
#include <jank/runtime/obj/jit_function.hpp>
Expand Down Expand Up @@ -179,6 +180,11 @@ namespace jank::runtime
return fn(expect_object<obj::transient_hash_map>(erased), std::forward<Args>(args)...);
}
break;
case object_type::transient_vector:
{
return fn(expect_object<obj::transient_vector>(erased), std::forward<Args>(args)...);
}
break;
case object_type::persistent_set:
{
return fn(expect_object<obj::persistent_set>(erased), std::forward<Args>(args)...);
Expand Down
10 changes: 10 additions & 0 deletions include/cpp/jank/runtime/obj/persistent_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@

namespace jank::runtime
{
namespace obj
{
using transient_vector = static_object<object_type::transient_vector>;
using transient_vector_ptr = native_box<transient_vector>;
}

template <>
struct static_object<object_type::persistent_vector> : gc
{
using transient_type = static_object<object_type::transient_vector>;
using value_type = runtime::detail::native_persistent_vector;

static constexpr bool pointer_free{ false };
Expand Down Expand Up @@ -51,6 +58,9 @@ namespace jank::runtime
/* behavior::consable */
native_box<static_object> cons(object_ptr head) const;

/* behavior::transientable */
obj::transient_vector_ptr to_transient() const;

object base{ object_type::persistent_vector };
value_type data;
option<object_ptr> meta;
Expand Down
63 changes: 63 additions & 0 deletions include/cpp/jank/runtime/obj/transient_vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

namespace jank::runtime
{
template <>
struct static_object<object_type::transient_vector> : gc
{
static constexpr bool pointer_free{ false };

using value_type = detail::native_transient_vector;
using persistent_type = static_object<object_type::persistent_vector>;

static_object() = default;
static_object(static_object &&) noexcept = default;
static_object(static_object const &) = default;
static_object(detail::native_persistent_vector const &d);
static_object(detail::native_persistent_vector &&d);
static_object(value_type &&d);

static native_box<static_object> empty()
{
static auto const ret(make_box<static_object>());
return ret;
}

/* behavior::objectable */
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_hash to_hash() const;

/* behavior::countable */
size_t count() const;

/* behavior::consable_in_place */
native_box<static_object> cons_in_place(object_ptr head);

/* behavior::persistentable */
native_box<persistent_type> to_persistent();
Samy-33 marked this conversation as resolved.
Show resolved Hide resolved

/* behavior::callable */
object_ptr call(object_ptr const) const;

/* behavior::associatively_readable */
object_ptr get(object_ptr const idx) const;
object_ptr get(object_ptr const idx, object_ptr const fallback) const;
object_ptr get_entry(object_ptr const idx) const;
native_bool contains(object_ptr const elem) const;

void assert_active() const;

object base{ object_type::transient_vector };
value_type data;
mutable native_hash hash{};
native_bool active{ true };
};

namespace obj
{
using transient_vector = static_object<object_type::transient_vector>;
using transient_vector_ptr = native_box<transient_vector>;
}
}
1 change: 1 addition & 0 deletions include/cpp/jank/runtime/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace jank::runtime
persistent_hash_map,
persistent_hash_map_sequence,
transient_hash_map,
transient_vector,
persistent_set,
cons,
range,
Expand Down
3 changes: 2 additions & 1 deletion src/cpp/jank/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ namespace jank::evaluate
}
}
}
else if constexpr(std::same_as<T, runtime::obj::persistent_set>)
else if constexpr(std::same_as<T, runtime::obj::persistent_set>
|| std::same_as<T, runtime::obj::transient_vector>)
{
auto const s(expr.arg_exprs.size());
if(s != 1)
Expand Down
3 changes: 1 addition & 2 deletions src/cpp/jank/runtime/behavior/callable.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <iostream>

#include <fmt/core.h>

#include <jank/runtime/behavior/callable.hpp>
Expand Down Expand Up @@ -69,6 +67,7 @@ namespace jank::runtime
else if constexpr(std::same_as<T, obj::persistent_set>
|| std::same_as<T, obj::persistent_hash_map>
|| std::same_as<T, obj::persistent_array_map>
|| std::same_as<T, obj::transient_vector>
|| std::same_as<T, obj::keyword>)
{
return typed_source->call(a1);
Expand Down
6 changes: 6 additions & 0 deletions src/cpp/jank/runtime/obj/persistent_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <jank/runtime/util.hpp>
#include <jank/runtime/obj/native_function_wrapper.hpp>
#include <jank/runtime/obj/persistent_vector.hpp>
#include <jank/runtime/obj/transient_vector.hpp>

namespace jank::runtime
{
Expand Down Expand Up @@ -102,6 +103,11 @@ namespace jank::runtime
return ret;
}

obj::transient_vector_ptr obj::persistent_vector::to_transient() const
{
return make_box<obj::transient_vector>(data);
}

object_ptr obj::persistent_vector::with_meta(object_ptr const m) const
{
auto const meta(behavior::detail::validate_meta(m));
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/jank/runtime/obj/transient_hash_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace jank::runtime
{
obj::transient_hash_map::static_object(runtime::detail::native_persistent_hash_map &&d)
: data{ d.transient() }
: data{ std::move(d).transient() }
{
}

Expand All @@ -15,7 +15,7 @@ namespace jank::runtime
}

obj::transient_hash_map::static_object(runtime::detail::native_transient_hash_map &&d)
: data{ d }
: data{ std::move(d) }
{
}

Expand Down
164 changes: 164 additions & 0 deletions src/cpp/jank/runtime/obj/transient_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
namespace jank::runtime
{
obj::transient_vector::static_object(runtime::detail::native_persistent_vector &&d)
: data{ std::move(d).transient() }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, because of LSP warning about d not getting moved.

{
}

obj::transient_vector::static_object(runtime::detail::native_persistent_vector const &d)
: data{ d.transient() }
{
}

obj::transient_vector::static_object(runtime::detail::native_transient_vector &&d)
: data{ std::move(d) }
{
}

native_bool obj::transient_vector::equal(object const &o) const
{
/* Transient equality, in Clojure, is based solely on identity. */
return &base == &o;
}

native_persistent_string obj::transient_vector::to_string() const
{
fmt::memory_buffer buff;
to_string(buff);
return native_persistent_string{ buff.data(), buff.size() };
}

void obj::transient_vector::to_string(fmt::memory_buffer &buff) const
{
auto inserter(std::back_inserter(buff));
fmt::format_to(inserter, "{}@{}", magic_enum::enum_name(base.type), fmt::ptr(&base));
}

native_hash obj::transient_vector::to_hash() const
{
/* Hash is also based only on identity. Clojure uses default hashCode, which does the same. */
return static_cast<native_hash>(reinterpret_cast<uintptr_t>(this));
}

size_t obj::transient_vector::count() const
{
assert_active();
return data.size();
}

obj::transient_vector_ptr obj::transient_vector::cons_in_place(object_ptr const head)
{
assert_active();
data.push_back(head);
return this;
}

native_box<obj::transient_vector::persistent_type> obj::transient_vector::to_persistent()
{
assert_active();
active = false;
return make_box<obj::persistent_vector>(data.persistent());
}

object_ptr obj::transient_vector::call(object_ptr const idx) const
{
assert_active();
if(idx->type == object_type::integer)
{
auto const i(expect_object<obj::integer>(idx)->data);
if(i < 0 || data.size() <= static_cast<size_t>(i))
{
throw std::runtime_error{
fmt::format("Index out of bound; index = {}, count = {}", i, count())
};
}

return data[i];
}
else
{
throw std::runtime_error{ fmt::format("key must be an integer; found {}",
runtime::detail::to_string(idx)) };
}
}

object_ptr obj::transient_vector::get(object_ptr const idx) const
{
assert_active();
if(idx->type == object_type::integer)
{
auto const i(expect_object<obj::integer>(idx)->data);
if(i < 0 || data.size() <= static_cast<size_t>(i))
{
return obj::nil::nil_const();
}

return data[i];
}
else
{
throw std::runtime_error{ fmt::format("key must be an integer; found {}",
runtime::detail::to_string(idx)) };
}
}

object_ptr obj::transient_vector::get(object_ptr const idx, object_ptr const fallback) const
{
assert_active();
if(idx->type == object_type::integer)
{
auto const i(expect_object<obj::integer>(idx)->data);
if(i < 0 || data.size() <= static_cast<size_t>(i))
{
return fallback;
}

return data[i];
}
else
{
throw std::runtime_error{ fmt::format("key must be an integer; found {}",
runtime::detail::to_string(idx)) };
}
}

object_ptr obj::transient_vector::get_entry(object_ptr const idx) const
{
if(idx->type == object_type::integer)
{
auto const i(expect_object<obj::integer>(idx)->data);
if(i < 0 || data.size() <= static_cast<size_t>(i))
{
return obj::nil::nil_const();
}
/* TODO: Map entry type? */
return make_box<obj::persistent_vector>(idx, data[i]);
}
else
{
throw std::runtime_error{ fmt::format("get_entry on a vector must be an integer; found {}",
runtime::detail::to_string(idx)) };
}
}

native_bool obj::transient_vector::contains(object_ptr const elem) const
{
if(elem->type == object_type::integer)
{
auto const i(expect_object<obj::integer>(elem)->data);
return i >= 0 && static_cast<size_t>(i) < data.size();
}
else
{
return false;
}
}

void obj::transient_vector::assert_active() const
{
if(!active)
{
throw std::runtime_error{ "transient used after it's been made persistent" };
}
}
}
Loading