Skip to content

Batch filter (solves #136) #150

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

Merged
merged 1 commit into from
Oct 5, 2019
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
51 changes: 48 additions & 3 deletions src/filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,16 +779,61 @@ InternalValue Serialize::Filter(const InternalValue&, RenderContext&)
return InternalValue();
}

Slice::Slice(FilterParams, Slice::Mode)
Slice::Slice(FilterParams params, Slice::Mode mode)
: m_mode{mode}
{

if(m_mode == BatchMode)
{
ParseParams({{"linecount"s, true}, {"fill_with"s, false}}, params);
}
}

InternalValue Slice::Filter(const InternalValue&, RenderContext&)
InternalValue Slice::Filter(const InternalValue& baseVal, RenderContext& context)
{
if(m_mode == BatchMode)
return Batch(baseVal, context);

return InternalValue();
}

InternalValue Slice::Batch(const InternalValue& baseVal, RenderContext& context)
{
auto linecount_value = ConvertToInt(GetArgumentValue("linecount", context));
InternalValue fillWith = GetArgumentValue("fill_with", context);

if(linecount_value <= 0)
return InternalValue();
auto linecount = static_cast<std::size_t>(linecount_value);

bool isConverted = false;
auto list = ConvertToList(baseVal, isConverted);
if (!isConverted)
return InternalValue();

auto elementsCount = list.GetSize().value_or(0);
if(!elementsCount)
return InternalValue();

InternalValueList resultList;
resultList.reserve(linecount);

const auto remainder = elementsCount % linecount;
const auto columns = elementsCount / linecount + (remainder > 0 ? 1 : 0);
for(std::size_t line = 0, idx = 0; line < linecount; ++line)
{
const auto elems = columns - (remainder && line >= remainder ? 1 : 0);
InternalValueList row;
row.reserve(columns);
std::fill_n(std::back_inserter(row), columns, fillWith);

for(std::size_t column = 0; column < elems; ++column)
row[column] = list.GetValueByIndex(idx++);

resultList.push_back(ListAdapter::CreateAdapter(std::move(row)));
}
return ListAdapter::CreateAdapter(std::move(resultList));
}

StringFormat::StringFormat(FilterParams, StringFormat::Mode)
{

Expand Down
4 changes: 4 additions & 0 deletions src/filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ class Slice : public FilterBase
Slice(FilterParams params, Mode mode);

InternalValue Filter(const InternalValue& baseVal, RenderContext& context);
private:
InternalValue Batch(const InternalValue& baseVal, RenderContext& context);

Mode m_mode;
};

class Sort : public FilterBase
Expand Down
19 changes: 19 additions & 0 deletions test/filters_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,22 @@ INSTANTIATE_TEST_CASE_P(Escape, FilterGenericTest, ::testing::Values(
InputOutputPair{"'abcd&><efgh' | escape | pprint", "'abcd&amp;&gt;&lt;efgh'"},
InputOutputPair{"'\\\"\\'' | escape | pprint", "'&#34;&#39;'"}
));

INSTANTIATE_TEST_CASE_P(Batch, FilterGenericTest, ::testing::Values(
InputOutputPair{
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] | batch(linecount=3) | pprint",
"[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, none], [12, 13, 14, 15, 16, none]]"
},
InputOutputPair{
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] | batch(3, 0) | pprint",
"[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 0], [12, 13, 14, 15, 16, 0]]"
},
InputOutputPair{
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] | batch(3, -1) | pprint",
"[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, -1]]"
},
InputOutputPair{"[1, 2, 3] | batch(0) | pprint", "none"},
InputOutputPair{"[1, 2, 3, 4] | batch(2) | pprint", "[[1, 2], [3, 4]]"},
InputOutputPair{"'some string' | batch(0) | pprint", "none"},
InputOutputPair{"[] | batch(0) | pprint", "none"}
));