Skip to content

Commit

Permalink
fix(string-seq): equal(..) and hash(...) now consider all seqs with s…
Browse files Browse the repository at this point in the history
…ame characters
  • Loading branch information
Samy-33 committed Jul 14, 2024
1 parent dc09b77 commit 29f3823
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 33 deletions.
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace jank::hash
uint32_t string(native_persistent_string_view const &input);

uint32_t visit(runtime::object const * const o);
uint32_t visit(char const ch);

uint32_t ordered(runtime::object const * const sequence);
uint32_t unordered(runtime::object const * const sequence);
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/runtime/detail/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace jank::runtime::detail
{
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace jank::runtime
static_object() = default;
static_object(static_object &&) = default;
static_object(static_object const &) = default;
static_object(obj::persistent_string_ptr s);
static_object(obj::persistent_string_ptr s, size_t i);
static_object(obj::persistent_string_ptr const s);
static_object(obj::persistent_string_ptr const s, size_t const i);

/* behavior::objectable */
native_bool equal(object const &) const;
Expand Down
5 changes: 5 additions & 0 deletions compiler+runtime/src/cpp/jank/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ namespace jank::hash
return fmix(h1, 2 * length);
}

uint32_t visit(char const ch)
{
return static_cast<uint32_t>(ch);
}

uint32_t visit(runtime::object const * const o)
{
assert(o);
Expand Down
13 changes: 12 additions & 1 deletion compiler+runtime/src/cpp/jank/runtime/detail/object_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace jank::runtime::detail

void to_string(char const ch, fmt::memory_buffer &buff)
{
make_box(ch)->to_string(buff);
obj::character{ ch }.to_string(buff);
}

void to_string(object_ptr const o, fmt::memory_buffer &buff)
Expand All @@ -35,6 +35,17 @@ namespace jank::runtime::detail
o);
}

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);
}

bool equal(object_ptr const lhs, object_ptr const rhs)
{
if(!lhs)
Expand Down
2 changes: 1 addition & 1 deletion compiler+runtime/src/cpp/jank/runtime/obj/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ namespace jank::runtime

native_hash obj::character::to_hash() const
{
return static_cast<native_hash>(get_char_from_repr(data).unwrap());
return hash::visit(get_char_from_repr(data).unwrap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,40 @@

namespace jank::runtime
{
obj::persistent_string_sequence::static_object(obj::persistent_string_ptr s)
obj::persistent_string_sequence::static_object(obj::persistent_string_ptr const s)
: str{ s }
{
assert(s->count() > 0);
assert(!s->data.empty());
}

obj::persistent_string_sequence::static_object(obj::persistent_string_ptr s, size_t i)
obj::persistent_string_sequence::static_object(obj::persistent_string_ptr const s, size_t const i)
: str{ s }
, index{ i }
{
assert(s->count() > 0);
assert(!s->data.empty() && i < s->data.size());
}

/* behavior::objectable */
native_bool obj::persistent_string_sequence::equal(object const &o) const
{
if(o.type != object_type::persistent_string_sequence)
{
return false;
}
auto const seq(expect_object<obj::persistent_string_sequence>(&o));
return std::equal(str->data.begin() + index,
str->data.end(),
seq->str->data.begin() + seq->index,
seq->str->data.end());
return detail::equal(o, str->data.begin(), str->data.end());
}

void obj::persistent_string_sequence::to_string(fmt::memory_buffer &buff) const
{
return behavior::detail::to_string(
str->data.begin()
+ static_cast<decltype(obj::persistent_string::data)::difference_type>(index),
str->data.end(),
"(",
')',
buff);
return behavior::detail::to_string(str->data.begin() + index, str->data.end(), "(", ')', buff);
}

native_persistent_string obj::persistent_string_sequence::to_string() const
{
fmt::memory_buffer buff;
behavior::detail::to_string(
str->data.begin()
+ static_cast<decltype(obj::persistent_string::data)::difference_type>(index),
str->data.end(),
"(",
')',
buff);
behavior::detail::to_string(str->data.begin() + index, str->data.end(), "(", ')', buff);
return { buff.data(), buff.size() };
}

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

/* behavior::countable */
Expand All @@ -72,7 +52,7 @@ namespace jank::runtime

obj::persistent_string_sequence_ptr obj::persistent_string_sequence::fresh_seq() const
{
return jank::make_box<obj::persistent_string_sequence>(str, index);
return make_box<obj::persistent_string_sequence>(str, index);
}

/* behavior::sequenceable */
Expand Down

0 comments on commit 29f3823

Please sign in to comment.