Skip to content

Commit

Permalink
maybe reserve fix for list comp example
Browse files Browse the repository at this point in the history
  • Loading branch information
ehren committed Aug 27, 2024
1 parent 6871c91 commit e13f3d8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 57 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,37 @@

include <ranges>

defmacro ([x, for (y in z), if (c)], x, y, z, c:
def (maybe_reserve: template<typename:T>,
vec: mut:[T]:ref,
sized: mut:auto:ref:ref:
vec.reserve(std.size(std.forward<decltype(sized)>(sized)))
) : void:requires:requires(std.size(sized))

def (maybe_reserve: template<typename:T>,
vec: mut:[T]:ref,
unsized: mut:auto:ref:ref:
pass
) : void:requires:not requires(std.size(unsized))

defmacro ([x, for (y in z), if (c)], x, y, z, c:
result = gensym()
zz = gensym()

pre_reserve_stmt = if (isinstance(c, EqualsCompareOp) and std.ranges.any_of(
c.args, lambda(a, a.equals(x) or a.equals(y))):
# Don't bother pre-reserving a std.size(z) sized vector for simple searches
# e.g. [x, for (y in z), if (y == something)]
# e.g. [x, for (y in z) if (y == something)]
dont_reserve: Node = quote(pass)
dont_reserve
else:
reserve: Node = quote(if (requires(std.size(unquote(zz))):
unquote(result).reserve(std.size(unquote(zz)))
) : constexpr)
reserve: Node = quote(maybe_reserve(unquote(result), unquote(zz)))
reserve
)

return quote(lambda (:
return quote(lambda[ref] (:

unquote(result): mut = []
unquote(zz): mut:auto:ref:ref = unquote(z)

unquote(pre_reserve_stmt)

for (unquote(y) in unquote(zz):
Expand Down
22 changes: 14 additions & 8 deletions tests/macros_list_comprehension.cth
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# tests/macros_list_comprehension.cth

include <ranges>

defmacro ([x, for (y in z), if (c)], x, y, z, c:
def (maybe_reserve: template<typename:T>,
vec: mut:[T]:ref,
sized: mut:auto:ref:ref:
vec.reserve(std.size(std.forward<decltype(sized)>(sized)))
) : void:requires:requires(std.size(sized))

def (maybe_reserve: template<typename:T>,
vec: mut:[T]:ref,
unsized: mut:auto:ref:ref:
pass
) : void:requires:not requires(std.size(unsized))

defmacro ([x, for (y in z), if (c)], x, y, z, c:
result = gensym()
zz = gensym()

Expand All @@ -14,17 +23,14 @@ defmacro ([x, for (y in z), if (c)], x, y, z, c:
dont_reserve: Node = quote(pass)
dont_reserve
else:
reserve: Node = quote(if (requires(std.size(unquote(zz))):
unquote(result).reserve(std.size(unquote(zz)))
) : constexpr)
reserve: Node = quote(maybe_reserve(unquote(result), unquote(zz)))
reserve
)

return quote(lambda (:
return quote(lambda[ref] (:

unquote(result): mut = []
unquote(zz): mut:auto:ref:ref = unquote(z)

unquote(pre_reserve_stmt)

for (unquote(y) in unquote(zz):
Expand Down
46 changes: 9 additions & 37 deletions tests/macros_list_comprehension.donotedit.autogenerated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@
#include <thread>
#include <optional>

//#include <concepts>
//#include <ranges>
//#include <numeric>


#include "ceto.h"

#include "ceto_checked_array_access.donotedit.autogenerated.h"

#include <ranges>
;
Expand All @@ -27,10 +23,7 @@
const auto l = [&]() {
auto ceto__private__ident__2 { std::vector<std::ranges::range_value_t<decltype(std::ranges::iota_view(0, 10))>>() } ;
auto && ceto__private__ident__3 { std::ranges::iota_view(0, 10) } ;
if constexpr (requires () { std::size(ceto__private__ident__3);
}) {
(*ceto::mad(ceto__private__ident__2)).reserve(std::size(ceto__private__ident__3));
}
maybe_reserve(ceto__private__ident__2, ceto__private__ident__3);
for(const auto& x : ceto__private__ident__3) {
if ((x % 2) == 0) {
(*ceto::mad(ceto__private__ident__2)).push_back(x);
Expand All @@ -45,10 +38,7 @@
auto ceto__private__ident__0 { std::vector<decltype(std::declval<std::ranges::range_value_t<decltype([&]() {
auto ceto__private__ident__2 { std::vector<std::ranges::range_value_t<decltype(std::ranges::iota_view(0, 10))>>() } ;
auto && ceto__private__ident__3 { std::ranges::iota_view(0, 10) } ;
if constexpr (requires () { std::size(ceto__private__ident__3);
}) {
(*ceto::mad(ceto__private__ident__2)).reserve(std::size(ceto__private__ident__3));
}
maybe_reserve(ceto__private__ident__2, ceto__private__ident__3);
for(const auto& x : ceto__private__ident__3) {
if ((x % 2) == 0) {
(*ceto::mad(ceto__private__ident__2)).push_back(x);
Expand All @@ -57,10 +47,7 @@
if constexpr (!std::is_void_v<decltype(ceto__private__ident__2)>) { return ceto__private__ident__2; } else { static_cast<void>(ceto__private__ident__2); };
}())>>()+1)>() } ;
auto && ceto__private__ident__1 { l } ;
if constexpr (requires () { std::size(ceto__private__ident__1);
}) {
(*ceto::mad(ceto__private__ident__0)).reserve(std::size(ceto__private__ident__1));
}
maybe_reserve(ceto__private__ident__0, ceto__private__ident__1);
for(const auto& x : ceto__private__ident__1) {
(*ceto::mad(ceto__private__ident__0)).push_back(x + 1);
}
Expand All @@ -71,10 +58,7 @@
auto ceto__private__ident__0 { std::vector<decltype(std::declval<std::ranges::range_value_t<decltype([&]() {
auto ceto__private__ident__2 { std::vector<std::ranges::range_value_t<decltype(std::ranges::iota_view(0, 10))>>() } ;
auto && ceto__private__ident__3 { std::ranges::iota_view(0, 10) } ;
if constexpr (requires () { std::size(ceto__private__ident__3);
}) {
(*ceto::mad(ceto__private__ident__2)).reserve(std::size(ceto__private__ident__3));
}
maybe_reserve(ceto__private__ident__2, ceto__private__ident__3);
for(const auto& x : ceto__private__ident__3) {
if ((x % 2) == 0) {
(*ceto::mad(ceto__private__ident__2)).push_back(x);
Expand All @@ -83,20 +67,14 @@
if constexpr (!std::is_void_v<decltype(ceto__private__ident__2)>) { return ceto__private__ident__2; } else { static_cast<void>(ceto__private__ident__2); };
}())>>()+1)>() } ;
auto && ceto__private__ident__1 { l } ;
if constexpr (requires () { std::size(ceto__private__ident__1);
}) {
(*ceto::mad(ceto__private__ident__0)).reserve(std::size(ceto__private__ident__1));
}
maybe_reserve(ceto__private__ident__0, ceto__private__ident__1);
for(const auto& x : ceto__private__ident__1) {
(*ceto::mad(ceto__private__ident__0)).push_back(x + 1);
}
if constexpr (!std::is_void_v<decltype(ceto__private__ident__0)>) { return ceto__private__ident__0; } else { static_cast<void>(ceto__private__ident__0); };
}())>>() } ;
auto && ceto__private__ident__5 { l2 } ;
if constexpr (requires () { std::size(ceto__private__ident__5);
}) {
(*ceto::mad(ceto__private__ident__4)).reserve(std::size(ceto__private__ident__5));
}
maybe_reserve(ceto__private__ident__4, ceto__private__ident__5);
for(const auto& x : ceto__private__ident__5) {
if (x > 5) {
(*ceto::mad(ceto__private__ident__4)).push_back(x);
Expand All @@ -110,10 +88,7 @@
auto ceto__private__ident__0 { std::vector<decltype(std::declval<std::ranges::range_value_t<decltype([&]() {
auto ceto__private__ident__2 { std::vector<std::ranges::range_value_t<decltype(std::ranges::iota_view(0, 10))>>() } ;
auto && ceto__private__ident__3 { std::ranges::iota_view(0, 10) } ;
if constexpr (requires () { std::size(ceto__private__ident__3);
}) {
(*ceto::mad(ceto__private__ident__2)).reserve(std::size(ceto__private__ident__3));
}
maybe_reserve(ceto__private__ident__2, ceto__private__ident__3);
for(const auto& x : ceto__private__ident__3) {
if ((x % 2) == 0) {
(*ceto::mad(ceto__private__ident__2)).push_back(x);
Expand All @@ -122,10 +97,7 @@
if constexpr (!std::is_void_v<decltype(ceto__private__ident__2)>) { return ceto__private__ident__2; } else { static_cast<void>(ceto__private__ident__2); };
}())>>()*100)>() } ;
auto && ceto__private__ident__1 { l } ;
if constexpr (requires () { std::size(ceto__private__ident__1);
}) {
(*ceto::mad(ceto__private__ident__0)).reserve(std::size(ceto__private__ident__1));
}
maybe_reserve(ceto__private__ident__0, ceto__private__ident__1);
for(const auto& x : ceto__private__ident__1) {
(*ceto::mad(ceto__private__ident__0)).push_back(x * 100);
}
Expand Down
16 changes: 11 additions & 5 deletions tests/macros_list_comprehension.donotedit.autogenerated.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
#include <thread>
#include <optional>

//#include <concepts>
//#include <ranges>
//#include <numeric>


#include "ceto.h"

#include "ceto_checked_array_access.donotedit.autogenerated.h"

#include <ranges>
;
template<typename T> inline auto maybe_reserve( std::vector<T> & vec, auto && sized) -> void requires (requires () { std::size(sized);
}) {
(*ceto::mad(vec)).reserve(std::size(std::forward<decltype(sized)>(sized)));
}

template<typename T> inline auto maybe_reserve( std::vector<T> & vec, auto && unsized) -> void requires (!requires () { std::size(unsized);
}) {
; // pass
}


;

Expand Down

0 comments on commit e13f3d8

Please sign in to comment.