Skip to content

Commit

Permalink
feat: unordered set types
Browse files Browse the repository at this point in the history
  • Loading branch information
PraneethJain committed Jun 10, 2024
1 parent b00e22b commit 2a32d2c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ set(JLCXX_STL_SOURCES
${JLCXX_SOURCE_DIR}/stl_queue.cpp
${JLCXX_SOURCE_DIR}/stl_set.cpp
${JLCXX_SOURCE_DIR}/stl_multiset.cpp
${JLCXX_SOURCE_DIR}/stl_unordered_set.cpp
${JLCXX_SOURCE_DIR}/stl_unordered_multiset.cpp
${JLCXX_SOURCE_DIR}/stl_shared_ptr.cpp
${JLCXX_SOURCE_DIR}/stl_unique_ptr.cpp
${JLCXX_SOURCE_DIR}/stl_weak_ptr.cpp
Expand Down
24 changes: 20 additions & 4 deletions include/jlcxx/stl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <deque>
#include <queue>
#include <set>
#include <unordered_set>

#include "module.hpp"
#include "smart_pointers.hpp"
Expand Down Expand Up @@ -53,6 +54,8 @@ class JLCXX_API StlWrappers
TypeWrapper1 queue;
TypeWrapper1 set;
TypeWrapper1 multiset;
TypeWrapper1 unordered_set;
TypeWrapper1 unordered_multiset;

static void instantiate(Module& mod);
static StlWrappers& instance();
Expand All @@ -70,6 +73,8 @@ void apply_deque(TypeWrapper1& deque);
void apply_queue(TypeWrapper1& queue);
void apply_set(TypeWrapper1& set);
void apply_multiset(TypeWrapper1& multiset);
void apply_unordered_set(TypeWrapper1& unordered_set);
void apply_unordered_multiset(TypeWrapper1& unordered_multiset);
void apply_shared_ptr();
void apply_weak_ptr();
void apply_unique_ptr();
Expand Down Expand Up @@ -249,7 +254,7 @@ struct WrapQueue
}
};

struct WrapSet
struct WrapSetType
{
template<typename TypeWrapperT>
void operator()(TypeWrapperT&& wrapped)
Expand All @@ -269,7 +274,7 @@ struct WrapSet
}
};

struct WrapMultiset
struct WrapMultisetType
{
template<typename TypeWrapperT>
void operator()(TypeWrapperT&& wrapped)
Expand Down Expand Up @@ -321,6 +326,12 @@ template <typename T>
struct container_has_less_than_operator<T, std::enable_if_t<!is_container<T>::value>>
: has_less_than_operator<T> {};

template <typename T, typename = void>
struct is_hashable : std::false_type {};

template <typename T>
struct is_hashable<T, std::void_t<decltype(std::hash<T>{}(std::declval<T>()))>> : std::true_type {};

template<typename T>
inline void apply_stl(jlcxx::Module& mod)
{
Expand All @@ -330,8 +341,13 @@ inline void apply_stl(jlcxx::Module& mod)
TypeWrapper1(mod, StlWrappers::instance().queue).apply<std::queue<T>>(WrapQueue());
if constexpr (container_has_less_than_operator<T>::value)
{
TypeWrapper1(mod, StlWrappers::instance().set).apply<std::set<T>>(WrapSet());
TypeWrapper1(mod, StlWrappers::instance().multiset).apply<std::multiset<T>>(WrapMultiset());
TypeWrapper1(mod, StlWrappers::instance().set).apply<std::set<T>>(WrapSetType());
TypeWrapper1(mod, StlWrappers::instance().multiset).apply<std::multiset<T>>(WrapMultisetType());
}
if constexpr (is_hashable<T>::value)
{
TypeWrapper1(mod, StlWrappers::instance().unordered_set).apply<std::unordered_set<T>>(WrapSetType());
TypeWrapper1(mod, StlWrappers::instance().unordered_multiset).apply<std::unordered_multiset<T>>(WrapMultisetType());
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ JLCXX_API void StlWrappers::instantiate(Module& mod)
apply_queue(m_instance->queue);
apply_set(m_instance->set);
apply_multiset(m_instance->multiset);
apply_unordered_set(m_instance->unordered_set);
apply_unordered_multiset(m_instance->unordered_multiset);
apply_shared_ptr();
apply_weak_ptr();
apply_unique_ptr();
Expand All @@ -49,7 +51,9 @@ JLCXX_API StlWrappers::StlWrappers(Module& stl) :
deque(stl.add_type<Parametric<TypeVar<1>>>("StdDeque", julia_type("AbstractVector"))),
queue(stl.add_type<Parametric<TypeVar<1>>>("StdQueue", julia_type("AbstractVector"))),
set(stl.add_type<Parametric<TypeVar<1>>>("StdSet")),
multiset(stl.add_type<Parametric<TypeVar<1>>>("StdMultiset"))
multiset(stl.add_type<Parametric<TypeVar<1>>>("StdMultiset")),
unordered_set(stl.add_type<Parametric<TypeVar<1>>>("StdUnorderedSet")),
unordered_multiset(stl.add_type<Parametric<TypeVar<1>>>("StdUnorderedMultiset"))
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/stl_multiset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace stl

void apply_multiset(TypeWrapper1& multiset)
{
multiset.apply_combination<std::multiset, stltypes>(stl::WrapMultiset());
multiset.apply_combination<std::multiset, stltypes>(stl::WrapMultisetType());
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/stl_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace stl

void apply_set(TypeWrapper1& set)
{
set.apply_combination<std::set, stltypes>(stl::WrapSet());
set.apply_combination<std::set, stltypes>(stl::WrapSetType());
}

}
Expand Down
16 changes: 16 additions & 0 deletions src/stl_unordered_multiset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "jlcxx/stl.hpp"

namespace jlcxx
{

namespace stl
{

void apply_unordered_multiset(TypeWrapper1& unordered_multiset)
{
unordered_multiset.apply_combination<std::unordered_multiset, stltypes>(stl::WrapMultisetType());
}

}

}
16 changes: 16 additions & 0 deletions src/stl_unordered_set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "jlcxx/stl.hpp"

namespace jlcxx
{

namespace stl
{

void apply_unordered_set(TypeWrapper1& unordered_set)
{
unordered_set.apply_combination<std::unordered_set, stltypes>(stl::WrapSetType());
}

}

}

0 comments on commit 2a32d2c

Please sign in to comment.