Commit 33d0d995 authored by Anton Bukov's avatar Anton Bukov

Add append and prepend methods

parent 0fedea17
......@@ -89,6 +89,7 @@ int DenisUniqueContactCount =
- `skip(count)`, `skipWhile(predicate)`, `skipWhile_i(predicate)`
- `orderBy()`, `orderBy(transform)`
- `distinct()`, `distinct(transform)`
- `append(items)`, `prepend(items)`
- `concat(linq)`
- `reverse()`
- `cast<T>()`
......
......@@ -152,6 +152,51 @@ namespace boolinq {
return skipWhile_i([predicate](T value, int /*i*/) { return predicate(value); });
}
template<typename SS, typename TT>
struct LinqValueIndex {
Linq<SS, TT> linq;
std::vector<TT> values;
int index;
};
template<typename ... Types>
Linq<LinqValueIndex<S, T>, T> append(Types ... newValues) const
{
return Linq<LinqValueIndex<S, T>, T>(
{*this, { newValues... }, -1},
[](LinqValueIndex<S, T> &tuple) {
if (tuple.index == -1) {
try {
return tuple.linq.next();
}
catch (LinqEndException &) {
tuple.index = 0;
}
}
if (tuple.index < tuple.values.size()) {
return tuple.values[tuple.index++];
}
throw LinqEndException();
}
);
}
template<typename ... Types>
Linq<LinqValueIndex<S, T>, T> prepend(Types ... newValues) const
{
return Linq<LinqValueIndex<S, T>, T>(
{*this, { newValues... }, 0},
[](LinqValueIndex<S, T> &tuple) {
if (tuple.index < tuple.values.size()) {
return tuple.values[tuple.index++];
}
return tuple.linq.next();
}
);
}
template<typename F, typename _TRet = typename std::result_of<F(T, int)>::type>
Linq<LinqIndex<S, T>, _TRet> select_i(F apply) const
{
......
#include <vector>
#include <string>
#include <gtest/gtest.h>
#include "CommonTests.h"
#include "boolinq.h"
using namespace boolinq;
TEST(Append, ThreePlusOne)
{
std::vector<int> src = {1,2,3};
auto rng = from(src).append(4);
int ans[] = {1,2,3,4};
CheckRangeEqArray(rng, ans);
}
TEST(Append, ThreePlusTwo)
{
std::vector<int> src = {1,2,3};
auto rng = from(src).append(4, 5);
int ans[] = {1,2,3,4,5};
CheckRangeEqArray(rng, ans);
}
TEST(Append, ZeroPlusTwo)
{
std::vector<int> src;
auto rng = from(src).append(7,8);
int ans[] = {7,8};
CheckRangeEqArray(rng, ans);
}
\ No newline at end of file
......@@ -23,6 +23,7 @@ SET (
${PROJECT_SOURCE_DIR}/test/AllTest.cpp
${PROJECT_SOURCE_DIR}/test/AnyTest.cpp
${PROJECT_SOURCE_DIR}/test/AvgTest.cpp
${PROJECT_SOURCE_DIR}/test/AppendTest.cpp
${PROJECT_SOURCE_DIR}/test/BitsTest.cpp
${PROJECT_SOURCE_DIR}/test/BytesTest.cpp
${PROJECT_SOURCE_DIR}/test/ConcatTest.cpp
......@@ -38,6 +39,7 @@ SET (
${PROJECT_SOURCE_DIR}/test/MaxTest.cpp
${PROJECT_SOURCE_DIR}/test/MinTest.cpp
${PROJECT_SOURCE_DIR}/test/OrderByTest.cpp
${PROJECT_SOURCE_DIR}/test/PrependTest.cpp
${PROJECT_SOURCE_DIR}/test/ReverseTest.cpp
${PROJECT_SOURCE_DIR}/test/SelectTest.cpp
${PROJECT_SOURCE_DIR}/test/SkipTest.cpp
......
#include <vector>
#include <string>
#include <gtest/gtest.h>
#include "CommonTests.h"
#include "boolinq.h"
using namespace boolinq;
TEST(Prepend, ThreePlusOne)
{
std::vector<int> src = {1,2,3};
auto rng = from(src).prepend(4);
int ans[] = {4,1,2,3};
CheckRangeEqArray(rng, ans);
}
TEST(Prepend, ThreePlusTwo)
{
std::vector<int> src = {1,2,3};
auto rng = from(src).prepend(4, 5);
int ans[] = {4,5,1,2,3};
CheckRangeEqArray(rng, ans);
}
TEST(Prepend, ZeroPlusTwo)
{
std::vector<int> src;
auto rng = from(src).prepend(7,8);
int ans[] = {7,8};
CheckRangeEqArray(rng, ans);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment