Lambda syntax with algorithms - c++11

I read here and also here and examples on cpluplus.com
And I still don't understand how it works
what confuses me at most how lambdas work with _if algorithms like copy_if that they don't reference containers in the body
std::vector<int> foo = {25,15,5,-5,-15};
std::vector<int> bar (foo.size());
// copy only positive numbers:
auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i)
{return !(i<0);} )
doesn't reference vector object foo in the body. so how it performs the desired action?
Additionally, I don't understand what is the difference beetween capturing a variable and passing as parameter
I also tried my own example:
vector<unsigned long>v2(10);
for(unsigned long i=0;i<v2.size();i++)
v2[i]=10-i;
v2.erase(remove_if(v1.begin(), v1.end(),[](unsigned long n) {
return n%2==1; } ),v2.end());//remove odd numbers
It compiles (MVS 2010 with Intel Composer 14) but produces rubbish and assertion error.

If you look at std::copy_if source code, you should see how that works.
Essentially what copy_if does is this:
void copy_if(It1 f1, It1 l1, It2 f2, Pred pred) {
for (; f1 != l1; ++f1) {
if (pred(*f1)) {
*f2 = *f1;
++f2;
}
}
}
It does not know about containers, nor does it have to. The pair of iterators (f1, l1) specify a range where the items are copied from, the iterators contain all the knowledge about how to get to the element. The iterator f2 specifies the starting point of the destination range. If the destination range is not big enough woul will have a buffer overflow bug. The predicate is a function that indicates whether to copy an element or not. It does not have to know anything about the container, it just needs to be able to tell copy_if whether an element being visited should be copied or not.
Difference between capturing a variable and passing as argument should be explained by the following snippets. This lambda
int captured = 42;
auto functor = [captured](int x) { return x%2 == 0; };
essentially means this:
int captured = 42;
struct Blah
{
Blah(int c) : captured_(c) { }
bool operator()(int x) const { return x%2 == 0; }
int captured_;
} functor(captured);

You're erasing from vector v2 using iterators from container v1. This is expected to.give you crap results - it's undefined behaviour.

Related

Frama-C - Get function input value through command line

Analysing the code below on GUI, it is possible check the input values of the function div0.
int div0(int x, int y)
{
return (x/y);
}
int main()
{
int res;
int a = 4;
int b = 2;
res = div0(a,b);
return 0;
}
Is it possible get this value through command line?
The simplest approach in your case is to insert calls to Frama_C_show_each, which is a special Frama-C builtin function that prints the internal Eva state for the given expressions, each time the interpreter passes through the program point. For instance:
int div0(int x, int y)
{
Frama_C_show_each_div0(x, y);
return (x/y);
}
Running frama-c -eva on the modified program will print:
[eva] file.c:3: Frama_C_show_each_div0: {4}, {2}
You can choose the suffix after Frama_C_show_each for each line you want. For instance, if you prefer to print each variable separately:
int div0(int x, int y)
{
Frama_C_show_each_x(x);
Frama_C_show_each_y(y);
return (x/y);
}
Will print instead:
[eva] file.c:3: Frama_C_show_each_x: {4}
[eva] file.c:4: Frama_C_show_each_y: {2}
For a more complex situation, or to avoid modifying the source code, other alternatives are possible, but they may require writing some OCaml code, either to modify Eva directly, or to add e.g. a new abstract domain which will print the expressions. But it's overkill for simple cases.
By the way, if you want your code to still compile normally, simply protect the call to Frama_C_show_each with #ifdef __FRAMAC__ guards:
int div0(int x, int y)
{
#ifdef __FRAMAC__
Frama_C_show_each_div0(x, y);
#endif
return (x/y);
}

STL algorithm for splitting a vector into multiple smaller ones based on lambda

Let's say I have a vector containing a struct with a member describing its target vector.
struct Foo
{
int target;
static const int A = 0;
static const int B = 1;
static const int C = 2;
};
std::vector<Foo> elements;
std::vector<Foo> As;
std::vector<Foo> Bs;
std::vector<Foo> Cs;
std::vector<Foo> others;
Now I want to move each Foo in one of the four other vectors based on the value of Target.
For example
auto elements = std::vector<Foo>{ {Foo::A}, {Foo::A}, {Foo::B} };
Should result in two elements in As, one in Bs and none in Cs or others. Elements should be empty afterwards.
I could as well do it myself, but I wonder if there is an STL algorithm I could use to do its job.
Standard algorithms usually don't operate on multiple output destinations, so it's hard to come up with a suitable solution here when you want to abstract away the destination containers through output iterators. What might come closest is std::copy_if. This could look like
// Help predicate creation:
auto pred = [](int target){ return [target](const Foo& f){ return f.target == target; }; };
std::copy_if(elements.begin(), elements.end(), std::back_inserter(As), pred(Foo::A));
std::copy_if(elements.begin(), elements.end(), std::back_inserter(Bs), pred(Foo::B));
std::copy_if(elements.begin(), elements.end(), std::back_inserter(Cs), pred(Foo::C));
std::copy_if(elements.begin(), elements.end(), std::back_inserter(others),
[](const Foo& f){ return false; /* TODO */ });
elements.clear();
If copying is more expensive than move-construction, you should pass std::make_move_iterator(elements.begin()) and the same for elements.end() to the algorithm. The issue here is that this doesn't scale. std::copy_if linearly traverses the input range, and the above has to do this four times. One traversal can be obtained e.g. like the following.
auto doTheWork = [&As, &Bs, &Cs, &others](const Foo& foo) {
if (foo.target == Foo::A)
As.push_back(foo);
else if (foo.target == Foo::B)
Bs.push_back(foo);
else if (foo.target == Foo::C)
Cs.push_back(foo);
else
others.push_back(foo);
};
std::for_each(elements.begin(), elements.end(), doTheWork);
In this scenario, we have at least employed a standard algorithm, but shifted the logic into a rather ugly lambda. Note that the above lambda will always copy its arguments, it needs some adjustments to properly work with std::move_iterators.
Sometimes, a good old range based for loop is the most readable solution.

Can anybody please describe this portion of code of the Dijkstra algorithm

I can't understand this given portion of the Dijkstra algorithm. I want to understand this portion of code line by line.
The code:
bool operator < (const DATA &p) const { return p.dist > dist; }
I have the basic knowledge of c/c++ code.
bool operator < (const DATA &p) const {
return p.dist > dist;
}
This is less than < operator overloading.
You are passing DATA &p prefixed by const which means p is passed by reference and it can't be modified or altered inside the function.
The function started with const { means there will be no write/modify operation inside the method.
p.dist > dist means after pushing into priority_queue, comparing between two Data will follow this criteria - when Data having smaller dist will be appeared first in the priority queue than Data with longer dist. This sounds contradictory but this is true because priority_queue is by-default a max heap.

Are std::get<> and std::tuple<> slower then raw pointers?

I have an C++11 application where I commonly iterate over several different structure of arrays for various algorithms. Raw CPU performance is important for this app.
The array elements are fundamental types (int, double, ..) or simple struct. The array are typically tens of thousands of elements long. I often need to iterate several arrays at once in a given loop. So typically I would need one pointer for each array of whatever type. So times I need to increment five individual pointers which is verbose.
Based on these answers about tuples,
Why is std::pair faster than std::tuple
C++11 tuple performance
I hoped there was no overhead to using tuples to pack the pointers together into a single object.
I thought it might be nice to implement a cursor like object to assist in iterating, since missing the increment on a particular pointer would be an annoying bug.
auto pts = std::make_tuple(p1, p2, p3...);
allow you to bundle a bunch of variables together in a typesafe way. Then you can implement a variadic template function to increment each pointer in the tuple in a type safe way.
However...
When I measure performance, the tuple version was slower then using raw pointers. But when I look at the generated assembly I see additional mov instructions in the tuple loop increment. Maybe due to the fact the std::get<> returns a reference? I had hoped that would be compiled away...
Am I missing something or are raw pointers just going to beat tuples when used like this? Here is a simple test harness. I threw away the fancy cursor code and just use a std::tuple<> for this test
On my machine, the tuple loop is consistently twice as slow as the raw pointer version for various data sizes.
My system config is Visual C++ 2013 x64 on Windows 8 with a release build. I did try turning on various optimization in Visual Studio such as
Inline Function Expansion : Any Suitable (/Ob2)
but it did not seem to change the time result for my case.
I did need to do two extra things to avoid aggressive optimization by VS
1) I forced the test data array to allocated on the heap, not the stack. That made a big difference when I timed things, possibly due to memory cache effects.
2) I forced a side effect by writing to static variable at the end so the compiler would not just skip my loop.
struct forceHeap
{
__declspec(noinline) int* newData(int M)
{
int* data = new int[M];
return data;
}
};
void timeSumCursor()
{
static int gIntStore;
int maxCount = 20;
int M = 10000000;
// compiler might place array on stack which changes the timing
// int* data = new int[N];
forceHeap fh;
int* data = fh.newData(M);
int *front = data;
int *end = data + M;
int j = 0;
for (int* p = front; p < end; ++p)
{
*p = (++j) % 1000;
}
{
BEGIN_TIMING_BLOCK("raw pointer loop", maxCount);
int* p = front;
int sum = 0;
int* cursor = front;
while (++cursor != end)
{
sum += *cursor;
}
gIntStore = sum;// force a side effect
END_TIMING_BLOCK();
}
printf("%d\n", gIntStore);
{
// just use a simple tuple to show the issue
// rather full blown cursor object
BEGIN_TIMING_BLOCK("tuple loop", maxCount);
int sum = 0;
auto cursor = std::make_tuple(front);
while (++std::get<0>(cursor) != end)
{
sum += *std::get<0>(cursor);
}
gIntStore = sum; // force a side effect
END_TIMING_BLOCK();
}
printf("%d\n", gIntStore);
delete[] data;
}

How can I keep track of character positions after I remove elements from a string?

Let us say I have the following string:
"my ., .,dog. .jumps. , .and..he. .,is., .a. very .,good, .dog"
1234567890123456789012345678901234567890123456789012345678901 <-- char pos
Now, I have written a regular expression to remove certain elements from the string above, in this example, all whitespace, all periods, and all commas.
I am left with the following transformed string:
"mydogjumpsandheisaverygooddog"
Now, I want to construct k-grams of this string. Let us say I were to take 5-grams of the above string, it would look like:
mydog ydogj dogju ogjum gjump jumps umpsa ...
The problem I have is that for each k-gram, I want to keep track of its original character position in the first source text I listed.
So, "mydog", would have a start position of "0" and an end position of "11". However, I have no mapping between the source text and the modified text. So, I have no idea where a particular k-gram starts and ends in relation to the original, unmodified text. This is important to my program to keep track of.
I am creating a list of k-grams like this:
public class Kgram
{
public int start;
public int end;
public int text;
}
where start and end are positions in the source text (top) and the text is that of the k-gram text after the modifications.
Can anyone point me in the right direction for the best way to solve this problem?
Don't use a regular expression 'replace' API to do your replacing. Only use regexps to find the places you want to modify, do the mod yourself, and maintain an offset mapping. One form I've used is an array of ints as big as the original string, storing 'n chars deleted' here values, but there are a host of other possibilities.
The basic data structure here is an array of pairs. Each pair contains an offset and a correction. Depending on time/space tradeoffs, you may prefer to spread the information out over a data structure as large as the original string.
Here's how I would solve this problem in Haskell:
kgramify k string =
let charsWithPos = zip string [1..] -- attach original position to each char
goodCWP = filter (not o isWhitePeriodOrComma o fst) charsWithPos -- drop nasty chars
groups = takeEveryK k goodCWP -- clump remaining chars in groups of size k
posnOfGroup g = (snd (head g), map fst g) -- position of first char with group
in map posnOfGroup groups
In informal English:
Tag each character with its position
Filter out uninteresting (character, position) pairs
Take the remaining list of pairs and group them into a list of lists of length k
For each inner list, take the position of the first character, and pair it with the list of the all the characters (with the positions dropped)
In any functional language like Clean, Haskell, ML, or Scheme, this kind of thing is very easy. In a language with explicit memory allocation (new) or even worse, malloc and free, such a solution would be very tedious.
A C solution, to show that as Norman Ramsey says, it's pretty tedious. It takes the filter as a callback with context, just for kicks, but in your case you could pass 0 as the filterdata and not_wspc as the filter:
int not_wspc(void *, char c) {
if isspace((unsigned char)c) return 0;
if ((c == '.') || (c == ',')) return 0;
return 1;
}
typedef struct {
char c;
int pos;
} charwithpos;
KGram *foo(const char *input, int (*filter)(void *,char), void *filterdata) {
size_t len = strlen(input);
charwithpos *filtered = malloc(len * sizeof(*filtered));
assert(filtered);
// combine Norman's zip and filter steps
charwithpos *current = filtered
for (size_t i = 0; i < len; ++i) {
if (filter(filterdata, input[i])) {
current->c = input[i];
current->pos = i;
++current;
}
}
size_t shortlen = (current - filtered);
// wouldn't normally recommend returning malloced data, but
// illustrates the point.
KGram *result = malloc((shortlen / 5 + 1) * sizeof(*result));
assert(result);
// take each 5 step
KGram *currentgram = result;
current = filtered;
for (size_t i = 0; i < shortlen; ++i) {
currentgram->text[i%5] = current->c;
if ((i % 5) == 0) {
currentgram->start = current->pos;
} else if ((i % 5) == 4) {
currentgram->end = current->pos;
++currentgram;
}
++current;
}
if (shortlen % 5) != 0 {
currentgram->end = filtered[shortlen-1].pos;
currentgram->text[shortlen%5] = 0;
}
free(filtered);
return(result);
}
Or something like that, I can't be actually compiling and testing it. Obviously this has the significant weakness that filtered sees the chars one at a time, which means it cannot apply backtracking algorithms. You could get around it by passing the whole string into the filter, so that if necessary it can do a lot of work on the first call, and store the results to return on all the rest of the calls. But if you need to apply regular-expression-like logic to arbitrary types, then C is probably not the right language to use.
Here's the beginnings of a C++ solution, without even using <functional>. Not sure what Norman saying about languages with new: just because the language has it doesn't mean you have to use it ;-)
template <typename OutputIterator>
struct KGramOutput {
OutputIterator dest;
KGram kgram;
KGramOutput(OutputIterator dest) : dest(dest) {}
void add(char, size_t);
void flush(void);
};
template <typename InputIterator, typename OutputIterator, typename Filter>
void foo(InputIterator first, InputIterator last, OutputIterator dest, Filter filter) {
size_t i = 0;
KGramOutput<OutputIterator> kgram(dest);
while (first != last) {
if (filter(*first)) kgram.add(*first, i);
++first;
++i;
}
kgram.flush();
}
The add and flush functions are a bit tedious, they have to bundle up 5 pairs into a KGram struct, and then do *dest++ = kgram. The user could pass for example a pushback_iterator over a vector<KGram> as the output iterator. Btw the '5' and the 'char' could be template parameters too.
This can be done in a single pass without needing to construct intermediate character-position pairs:
(defclass k-gram ()
((start :reader start :initarg :start)
(end :accessor end)
(text :accessor text)))
(defmethod initialize-instance :after ((k-gram k-gram) &rest initargs &key k)
(declare (ignorable initargs))
(setf (slot-value k-gram 'text) (make-array k :element-type 'character)))
(defun k-gramify (string k ignore-string)
"Builds the list of complete k-grams with positions from the original
text, but with all characters in ignore-string ignored."
(loop
for character across string
for position upfrom 0
with k-grams = ()
do (unless (find character ignore-string)
(push (make-instance 'k-gram :k k :start position) k-grams)
(loop
for k-gram in k-grams
for i upfrom 0 below k
do (setf (aref (text k-gram) i) character
(end k-gram) (1+ position))))
finally (return (nreverse (nthcdr (- k 1) k-grams)))))

Resources