forked from k06a/boolinq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fab5a78
Showing
70 changed files
with
6,051 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 11.00 | ||
# Visual Studio 2010 | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "boolinq", "boolinq\boolinq.vcxproj", "{65AA3851-4457-48AB-BD08-83250D523D49}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Win32 = Debug|Win32 | ||
Release|Win32 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{65AA3851-4457-48AB-BD08-83250D523D49}.Debug|Win32.ActiveCfg = Debug|Win32 | ||
{65AA3851-4457-48AB-BD08-83250D523D49}.Debug|Win32.Build.0 = Debug|Win32 | ||
{65AA3851-4457-48AB-BD08-83250D523D49}.Release|Win32.ActiveCfg = Release|Win32 | ||
{65AA3851-4457-48AB-BD08-83250D523D49}.Release|Win32.Build.0 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
namespace boolinq | ||
{ | ||
// all(xxx) and all(xxx,lambda) | ||
|
||
template<typename R, typename F> | ||
bool all(R r, F f) | ||
{ | ||
while (!r.empty()) | ||
if (!f(r.popFront())) | ||
return false; | ||
return true; | ||
} | ||
|
||
template<typename R> | ||
bool all(R r) | ||
{ | ||
typedef typename R::value_type value_type; | ||
return all(r,[](const value_type & a)->value_type{return a;}); | ||
} | ||
|
||
// xxx.all() and xxx.all(lambda) | ||
|
||
template<template<typename> class TLinq, typename R> | ||
class All_mixin | ||
{ | ||
public: | ||
bool all() const | ||
{ | ||
return boolinq::all(((TLinq<R>*)this)->r); | ||
} | ||
|
||
template<typename F> | ||
bool all(F f) const | ||
{ | ||
return boolinq::all(((TLinq<R>*)this)->r,f); | ||
} | ||
}; | ||
} | ||
// namespace boolinq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <vector> | ||
#include <string> | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include "IterRange.h" | ||
#include "All.h" | ||
|
||
using namespace boolinq; | ||
|
||
TEST(All, ThreeInts) | ||
{ | ||
std::vector<int> src; | ||
src.push_back(1); | ||
src.push_back(2); | ||
src.push_back(3); | ||
|
||
auto rng = range(src); | ||
|
||
EXPECT_TRUE(all(rng)); | ||
|
||
EXPECT_TRUE(all(rng,[](int a){return a>0;})); | ||
EXPECT_TRUE(all(rng,[](int a){return a<4;})); | ||
EXPECT_TRUE(all(rng,[](int a){return a>0 && a<4;})); | ||
|
||
EXPECT_FALSE(all(rng,[](int a){return a>2;})); | ||
EXPECT_FALSE(all(rng,[](int a){return a==1;})); | ||
EXPECT_FALSE(all(rng,[](int a){return a<3;})); | ||
} | ||
|
||
TEST(All, ThreeIntsSecond) | ||
{ | ||
std::vector<int> src; | ||
src.push_back(0); | ||
src.push_back(1); | ||
src.push_back(2); | ||
|
||
auto rng = range(src); | ||
|
||
EXPECT_FALSE(all(rng)); | ||
|
||
EXPECT_TRUE(all(rng,[](int a){return a>=0;})); | ||
EXPECT_TRUE(all(rng,[](int a){return a<=2;})); | ||
EXPECT_TRUE(all(rng,[](int a){return a>=0 && a<=2;})); | ||
|
||
EXPECT_FALSE(all(rng,[](int a){return a>1;})); | ||
EXPECT_FALSE(all(rng,[](int a){return a==1;})); | ||
EXPECT_FALSE(all(rng,[](int a){return a<2;})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
namespace boolinq | ||
{ | ||
// any(xxx) and any(xxx,lambda) | ||
|
||
template<typename R, typename F> | ||
bool any(R r, F f) | ||
{ | ||
while (!r.empty()) | ||
if (f(r.popFront())) | ||
return true; | ||
return false; | ||
} | ||
|
||
template<typename R> | ||
bool any(R r) | ||
{ | ||
typedef typename R::value_type value_type; | ||
return any(r,[](const value_type & a)->value_type{return a;}); | ||
} | ||
|
||
// xxx.any() and xxx.any(lambda) | ||
|
||
template<template<typename> class TLinq, typename R> | ||
class Any_mixin | ||
{ | ||
public: | ||
bool any() const | ||
{ | ||
return boolinq::any(((TLinq<R>*)this)->r); | ||
} | ||
|
||
template<typename F> | ||
bool any(F f) const | ||
{ | ||
return boolinq::any(((TLinq<R>*)this)->r,f); | ||
} | ||
}; | ||
} | ||
// namespace boolinq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <vector> | ||
#include <string> | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include "IterRange.h" | ||
#include "Any.h" | ||
|
||
using namespace boolinq; | ||
|
||
TEST(Any, ThreeInts) | ||
{ | ||
std::vector<int> src; | ||
src.push_back(1); | ||
src.push_back(2); | ||
src.push_back(3); | ||
|
||
auto rng = range(src); | ||
|
||
EXPECT_TRUE(any(rng)); | ||
|
||
EXPECT_TRUE(any(rng,[](int a){return a==1;})); | ||
EXPECT_TRUE(any(rng,[](int a){return a==2;})); | ||
EXPECT_TRUE(any(rng,[](int a){return a==3;})); | ||
EXPECT_TRUE(any(rng,[](int a){return a>1;})); | ||
EXPECT_TRUE(any(rng,[](int a){return a<3;})); | ||
EXPECT_TRUE(any(rng,[](int a){return a!=2;})); | ||
|
||
EXPECT_FALSE(any(rng,[](int a){return a==0;})); | ||
EXPECT_FALSE(any(rng,[](int a){return a==4;})); | ||
EXPECT_FALSE(any(rng,[](int a){return a<1;})); | ||
EXPECT_FALSE(any(rng,[](int a){return a>3;})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
namespace boolinq | ||
{ | ||
// avg(xxx) and avg(xxx,lambda) | ||
|
||
template<typename R, typename F> | ||
auto avg(R r, F f) -> decltype(f(r.front()) + f(r.back())) | ||
{ | ||
typedef decltype(f(r.front()) + f(r.back())) value_type; | ||
value_type val = value_type(); | ||
int count = 0; | ||
for (; !r.empty(); r.popFront()) | ||
{ | ||
val = val + f(r.front()); | ||
count++; | ||
} | ||
return val/count; | ||
} | ||
|
||
template<typename R> | ||
auto avg(R r) -> decltype(r.front() + r.back()) | ||
{ | ||
typedef decltype(r.front() + r.back()) value_type; | ||
return avg(r,[](const value_type & a)->value_type{return a;}); | ||
} | ||
|
||
// xxx.avg() and xxx.avg(lambda) | ||
|
||
template<template<typename> class TLinq, typename R> | ||
class Avg_mixin | ||
{ | ||
template<typename F, typename TArg> | ||
static auto get_return_type(F * f = NULL, TArg * arg = NULL) | ||
-> decltype((*f)(*arg)); | ||
|
||
public: | ||
//TODO: Invalid return type ... should be (value_type + value_type) | ||
typename R::value_type avg() const | ||
{ | ||
return boolinq::avg(((TLinq<R>*)this)->r); | ||
} | ||
|
||
template<typename F> | ||
auto avg(F f) const -> decltype(get_return_type<F,typename R::value_type>() | ||
+ get_return_type<F,typename R::value_type>()) | ||
{ | ||
return boolinq::avg(((TLinq<R>*)this)->r,f); | ||
} | ||
}; | ||
} | ||
// namespace boolinq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <list> | ||
#include <vector> | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include "IterRange.h" | ||
#include "WhereRange.h" | ||
#include "Avg.h" | ||
|
||
using namespace boolinq; | ||
|
||
TEST(Avg, ThreeInts) | ||
{ | ||
std::vector<int> src; | ||
src.push_back(1); | ||
src.push_back(2); | ||
src.push_back(3); | ||
|
||
auto rng = range(src); | ||
|
||
EXPECT_EQ(2, avg(rng)); | ||
} | ||
|
||
TEST(Avg, FiveInts) | ||
{ | ||
std::vector<int> src; | ||
src.push_back(1); | ||
src.push_back(2); | ||
src.push_back(3); | ||
src.push_back(4); | ||
src.push_back(5); | ||
|
||
auto rng = range(src); | ||
auto dst0 = where(rng, [](int a){return a%2 == 0;}); | ||
auto dst1 = where(rng, [](int a){return a%2 == 1;}); | ||
|
||
EXPECT_EQ(3, avg(dst0)); | ||
EXPECT_EQ(3, avg(dst1)); | ||
} | ||
|
||
TEST(Avg, FiveStringsLen) | ||
{ | ||
std::vector<std::string> src; | ||
src.push_back("hello"); | ||
src.push_back("apple"); | ||
src.push_back("nokia"); | ||
src.push_back("oracle"); | ||
src.push_back("ponny"); | ||
|
||
auto rng = range(src); | ||
|
||
EXPECT_EQ(5, avg(rng,[](const std::string & str){return str.size();})); | ||
} |
Oops, something went wrong.