Skip to content

Commit

Permalink
fix(string-seq): considers index for equality check and hash generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Samy-33 committed Jul 15, 2024
1 parent 29f3823 commit 99147da
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ namespace jank
using const_iterator = const_pointer_type;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using difference_type = std::ptrdiff_t;

static constexpr size_type npos{ std::numeric_limits<size_type>::max() };

Expand Down
2 changes: 1 addition & 1 deletion compiler+runtime/include/cpp/jank/runtime/detail/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace jank::runtime::detail
{
bool equal(char const lhs, object_ptr const rhs);
native_bool equal(char const lhs, object_ptr const rhs);
native_bool equal(object_ptr const lhs, object_ptr const rhs);

struct object_ptr_equal
Expand Down
8 changes: 4 additions & 4 deletions compiler+runtime/src/cpp/jank/runtime/detail/object_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ namespace jank::runtime::detail
o);
}

bool equal(char const lhs, object_ptr const rhs)
native_bool equal(char const lhs, object_ptr const rhs)
{
if(!rhs || rhs->type != object_type::character)
{
return false;
}

auto const typed_rhs = expect_object<obj::character>(rhs);
return typed_rhs->to_hash() == static_cast<unsigned int>(lhs);
return typed_rhs->to_hash() == static_cast<native_hash>(lhs);
}

bool equal(object_ptr const lhs, object_ptr const rhs)
native_bool equal(object_ptr const lhs, object_ptr const rhs)
{
if(!lhs)
{
Expand Down Expand Up @@ -75,7 +75,7 @@ namespace std
}

// NOLINTNEXTLINE(bugprone-exception-escape): TODO: Sort this out.
bool equal_to<jank::runtime::object_ptr>::operator()(
jank::native_bool equal_to<jank::runtime::object_ptr>::operator()(
jank::runtime::object_ptr const lhs,
jank::runtime::object_ptr const rhs) const noexcept
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace jank::runtime
/* behavior::objectable */
native_bool obj::persistent_string_sequence::equal(object const &o) const
{
return detail::equal(o, str->data.begin(), str->data.end());
return detail::equal(o, str->data.begin() + index, str->data.end());
}

void obj::persistent_string_sequence::to_string(fmt::memory_buffer &buff) const
Expand All @@ -35,7 +35,7 @@ namespace jank::runtime

native_hash obj::persistent_string_sequence::to_hash() const
{
return hash::ordered(str->data.begin(), str->data.end());
return hash::ordered(str->data.begin() + index, str->data.end());
}

/* behavior::countable */
Expand Down Expand Up @@ -71,7 +71,7 @@ namespace jank::runtime
return nullptr;
}

return jank::make_box<obj::persistent_string_sequence>(str, n);
return make_box<obj::persistent_string_sequence>(str, n);
}

obj::persistent_string_sequence_ptr obj::persistent_string_sequence::next_in_place()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ namespace jank::runtime
/* behavior::objectable */
native_bool obj::persistent_vector_sequence::equal(object const &o) const
{
return detail::equal(o, vec->data.begin(), vec->data.end());
return detail::equal(
o,
vec->data.begin()
+ static_cast<decltype(obj::persistent_vector::data)::difference_type>(index),
vec->data.end());
}

void obj::persistent_vector_sequence::to_string(fmt::memory_buffer &buff) const
Expand Down Expand Up @@ -47,13 +51,16 @@ namespace jank::runtime

native_hash obj::persistent_vector_sequence::to_hash() const
{
return hash::ordered(vec->data.begin(), vec->data.end());
return hash::ordered(
vec->data.begin()
+ static_cast<decltype(obj::persistent_vector::data)::difference_type>(index),
vec->data.end());
}

/* behavior::countable */
size_t obj::persistent_vector_sequence::count() const
{
return vec->data.size();
return vec->data.size() - index;
}

/* behavior::seqable */
Expand All @@ -64,7 +71,7 @@ namespace jank::runtime

obj::persistent_vector_sequence_ptr obj::persistent_vector_sequence::fresh_seq() const
{
return jank::make_box<obj::persistent_vector_sequence>(vec, index);
return make_box<obj::persistent_vector_sequence>(vec, index);
}

/* behavior::sequenceable */
Expand All @@ -83,7 +90,7 @@ namespace jank::runtime
return nullptr;
}

return jank::make_box<obj::persistent_vector_sequence>(vec, n);
return make_box<obj::persistent_vector_sequence>(vec, n);
}

obj::persistent_vector_sequence_ptr obj::persistent_vector_sequence::next_in_place()
Expand Down

0 comments on commit 99147da

Please sign in to comment.