开发者

Is there a LINQ library for C++? [closed]

开发者 https://www.devze.com 2023-02-04 14:27 出处:网络
Closed. This question does not meet Stack Overflow guidelines开发者_C百科. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines开发者_C百科. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 6 years ago.

Improve this question

Are there any Platform agnostic (not CLI) movements to get LINQ going for C++ in some fashion?

I mean a great part of server frameworks around the world run on flavors of UNIX and having access to LINQ for C++ on UNIX would probably make lots of people happy!


Linq++ by Hong Jiang looks like a good start. Its syntax is much closer to Linq than CLinq's. Linq by pfultz2 looks interesting, as well, but it needs a C++11 compiler.


This is my solution of template C++ LINQ library.
Source code is here: Boolinq
There are a lot of tests on each feature.
I'm working on it right now.

Any comments?
May be advices?

UPDATE: project moved to https://github.com/k06a/boolinq and now have version 2.0 with only 700 lines of source code :)


Microsoft has just announced that they've built LINQ for C and C++. Not yet available, though.

Update 11/06/2012:

Microsoft Open Technologies, Inc. has now released and open-sourced (Apache License 2.0) a number of related libraries, including a LINQ implementation (Ix++), and it's new Reactive Extensions (Rx++) library.


http://cpplinq.codeplex.com/ is a very good implementation.
From the author:
The motivation for CppLinq is that both boolinq and Native-RX seems to be based around the operator"." to compose list functions. The problem is that the "." operator is that it can't be overloaded in C++ which makes it hard to extend these libraries with functions of my own design. To me this is important. CppLinq is based around operator>> which is overloadable thus CppLinq can be made extensible.


You may take a look at PSade.Oven, a strongly boostified library working on STL ranges and providing a lot LINQ like functions.


I have written a small library cppLinq that reimplements IEnumerable<> and its LINQ operators. It is just an experiment; for now it only works on Windows (coroutines are implemented with Win32 fibers), and only builds with the Dev Preview of VS11 (it makes an heavy usage of lambda expressions :-)).

It allows to write code like this:

auto source = IEnumerable<int>::Range(0, 10);

auto it = source->Where([](int val) { return ((val % 2) == 0); })
                ->Select<double>([](int val) -> double { return (val * val); }));

foreach<double>(it, [](double& val){
    printf("%.2f\n", val);
});


Actually if you just want to use Linq for list comprehension, you can use this Linq library. It requires C++11(it will work in MSVC 2010 though) and Boost. With the library you can write linq queries like this:

struct student_t
{
    std::string last_name;
    std::vector<int> scores;
};

std::vector<student_t> students = 
{
    {"Omelchenko", {97, 72, 81, 60}},
    {"O'Donnell", {75, 84, 91, 39}},
    {"Mortensen", {88, 94, 65, 85}},
    {"Garcia", {97, 89, 85, 82}},
    {"Beebe", {35, 72, 91, 70}} 
};

auto scores = LINQ(from(student, students) 
                   from(score, student.scores) 
                   where(score > 90) 
                   select(std::make_pair(student.last_name, score)));

for (auto x : scores)
{
    printf("%s score: %i\n", x.first.c_str(), x.second);
}

Which will output:

Omelchenko score: 97
O'Donnell score: 91
Mortensen score: 94
Garcia score: 97
Beebe score: 91


C++ 0x, or whatever it ends up being called, does have a new keyword called auto which allows for type-inference. And yes, there will be lambda's coming for C++. Also, a quick Google search revealed this, CLinq.


Here is another alternative that is simply a wrapper around boost and stl algorithms, and thus you get most of the performance benefits of those implementations.

It works like this:

std::vector<int> xs;
auto count = from(xs)
   .select([](int x){return x*x;})
   .where([](int x){return x > 16;})
   .count();
auto xs2 = from(xs)
   .select([](int x){return x*x;})
   .to<std::vector<int>>();

Note that some methods return a proxy for empty ranges, e.g.

std::vector<int> xs;
auto max = from(xs)
   .select([](int x){return x*x;})
   .where([](int x){return x > 16;})
   .max()
   .value_or(default_max_value);

Feedback is welcome.


Here is my implemention of c++-linq with c++11(in chinese):

http://www.cnblogs.com/cbscan/archive/2012/10/20/2732773.html

It support features like "deferred query","stack based"(use operator new as little as possible),"copy semantic"(so you can iterate a query multitime after backup it), and so on.

It also support dozens of function including "from, select, where, cast ,range, all, any ,cast, average ,contain, count ,first, last, head, tail ,groupBy ,takeUntil, skipUntil ,max, min ,reduce ,unique, sort, random ,intersect, _union".

I think my code is simple enough to understand and extend by anybody selves.


I don't think C++ has the compiler sugar to handle things such as lambda expressions, so no, that's not going to happen.

0

精彩评论

暂无评论...
验证码 换一张
取 消