I want to setup matching behavior for Visual Studio and Uncrustify so that instant formatting was not modified later by Uncrustify.
I am almost there, but Uncrustify handles continued multiline expression excessively well. It aligns 2nd and other lines according to the first.
How can I make it put just a normal dumb indent on the lines after the first?
I looked through the full list of options here http://uncrustify.sourceforge.net/default.cfg with no success so far.
Uncrustify:
bool test(int a, int b, int c)
{
return a == 1
&& b == 1
&& c == 1;
}
On the line with b and c there is an undesired alignment.
Visual Studio AutoFormat:
bool test(int a, int b, int c)
{
return a == 1
&& b == 1
&& c == 1;
}
(If you wonder why I need Uncrustify, it's mostly because Uncrustify has indent_namespace_single_indent option and VS does not).
Use this option to archive desirable behavior:
indent_single_after_return = true
Related
Currently if I want to generate an identical list to a previously generated one in Specman e I use:
<'
struct A {
ListA : list of uint;
keep ListA.size() == 5;
keep ListA.sum(it) <= 20;
};
struct B {
ListB : list of uint;
};
extend sys {
A1 : A;
B1 : B;
// Keeps
keep B1.B.size() == read_only(A1.A.size());
keep for each in B1.B {
it == read_only(A1.A[index]);
};
};
'>
Is there a cleaner way to have this generation? A one line keep?
You could say:
keep B1.ListB == A1.ListA.copy();
But using the generator to create an exact copy is very inefficient. In the end, there is nothing to generate...
Instead, use a simple assignment.
extend sys {
...
post_generate is also {
B1.B = A1.A.copy(); // shallow copy OK for uints, use deep_copy() when appropriate
}
}
Depending on other members, you might not even have to generate B1 at all.
Is it possible to translate the R function isTRUE to Rcpp? And if so, how?
The R function:
function (x)
is.logical(x) && length(x) == 1L && !is.na(x) && x
I am struggling with the first and third part. How can I check if the input is of type boolean or logical? And how can I test for NA when the input type is not known?
I know that I can check if the length is 1 with
x.length() == 1 / x.size() == 1
It is already implemented in this header file as
template <bool NA, typename T>
inline bool is_true( const Rcpp::sugar::SingleLogicalResult<NA,T>& x){
return const_cast< Rcpp::sugar::SingleLogicalResult<NA,T>& >(x).is_true() ;
}
That of course references another file ... and all the template-meta-programming is not for the faint of heart. You can also do what you sketch, see this Rcpp Gallery post about dynamic dispatch to see an example of testing for types at run-time.
Below is an example of quicksort. I was wondering how two recursive method call inside quicksort method works i.e in what sequence it'll execute? So to check in what sequence I placed syso after each method (check output).My doubt why this sequence?Thus it depends on any conditions? if so, what condition? It would be helpful if explained the logic in detail.
Thank you in advance :)
void quicksort(int a[], int p, int r)
{
if(p < r)
{
int q;
q = partition(a, p, r);
System.out.println("q:"+q);
quicksort(a, p, q);
System.out.println("1");
quicksort(a, q+1, r);
System.out.println("2");
}
}
int partition(int a[], int p, int r)
{
System.out.println("p:"+p+" r:"+r);
int i, j, pivot, temp;
pivot = a[p];
i = p;
j = r;
while(1)
{
while(a[i] < pivot && a[i] != pivot)
i++;
while(a[j] > pivot && a[j] != pivot)
j--;
if(i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
return j;
}
}
}
Output
p:0 r:7
q:4
p:0 r:4
q:0
1
p:1 r:4
q:1
1
p:2 r:4
q:3
p:2 r:3
q:2
1
2
1
2
2
2
1
p:5 r:7
q:7
p:5 r:7
q:6
p:5 r:6
q:5
1
2
1
2
1
2
2
Would like to know why the gap between method calls?i.e how println(placed after method calls) statement getting executed w/o executing method call?
Yes, it depends on conditions: specifically, the values of p and r on each call. Each instance of the sort will do the two calls in order: none of the execution branches will get to the second call until the first call of that branch is completely done.
You will get a much nicer trace if you put a println at the top of the function that displays the parameter values. You might want to place one after you compute the value of q, as well. Try that, and see whether it tells you the rest of the story.
Okay, you've done the printing ... and you don't see the reason for that gap? When you get to the output line "q:2", you've made five calls to quicksort, and the only progress through that sequence is that you've made it past the "1" print for two of them (you're in the second call). Your current stack looks like this, in terms of p and r:
2, 3
2, 4
0, 4
0, 7
This is the right half of the left half (second quarter) of the array, four levels deep. You now have to finish off those calls, which will get you to the "1" print for the "right-half" ones, the "2" print for each of them.
Looking at it another way, you work to partition the array down to single elements. While you're doing this, you stack up calls for smaller partitions. Any call with at least two elements has to finish off both of its partitions before it returns to the next print.
Once you get to a single-element array, you return right away, and get to print the next "1" or "2". If the other partition is also fully atomized, then you get to the next "1" or "2" without any more partitioning calls.
Halfway through, you get to the point where you've fully atomized the left half of the array; that's when you clear out all the outstanding processing, back up the stack, and do all of that printing. Then you recur down the right half, and get a similar effect.
I think you might have an easier time understanding if you'd give yourself a full trace. Either follow this in your debugger, or modify and add print statements so that (1) you have a unique, easily-read output for every line, rather than 8 lines each of "1" and "2" that you can't tell apart; (2) you can also trace the return from each routine. The objective here is to be able to recognize where you are in the process at each output line.
Yes, it's another programming problem to be able to print out things such as
1.L.R.R
1.1.2
(0,4) L
...
... or whatever format you find readable.
I have to copy certain elements from a std::map into a vector.
It should work like in this loop:
typedef int First;
typedef void* Second;
std::map<First, Second> map;
// fill map
std::vector<Second> mVec;
for (std::map<First, Second>::const_iterator it = map.begin(); it != map.end(); ++it) {
if (it->first % 2 == 0) {
mVec.push_back (it->second);
}
}
Since I'd like to avoid using any functors, but use boost::lambda instead, I tried using std::copy, but can't get it right.
std::copy (map.begin(), map.end(), std::back_inserter(mVec)
bind(&std::map<int, void*>::value_type::first, _1) % 2 == 0);
I'm new to lambda expressions, and I can't figure it out how to use them correctly.
I didn't get any useful results on Google or StackOverflow either.
This question is similar
What you would need in STL would be a transform_if algorithm. Then you would have to write:
transform_if (mymap.begin(), mymap.end(),
back_inserter(myvec),
bind(&std::map<First, Second>::value_type::second, _1) ,
(bind(&std::map<First, Second>::value_type::first, _1) % 2) == 0 );
The code for transform_if is taken from this unrelated question and it is:
template<class InputIterator, class OutputIterator, class UnaryFunction, class Predicate>
OutputIterator transform_if(InputIterator first,
InputIterator last,
OutputIterator result,
UnaryFunction f,
Predicate pred)
{
for (; first != last; ++first)
{
if( pred(*first) )
*result++ = f(*first);
}
return result;
}
I think there is no other way to perform both steps (transform and conditional copy) at once using STL algorithms.
You can use boost range adaptors to achieve that.
using namespace boost::adaptors;
boost::copy( map | filtered( [] (const pair<First,Second> &p)->bool {return p.first % 2 == 0;})
| transformed( [] (const pair<First,Second> &p) {return p.second;}),
std::back_inserter(mVec));
Hey I'm working on a problem where you would print a string vertically using recursion. I know how to do this if i were to use a for loop:
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
but i'm not entirely sure how to do it using recursion. I took care of the base case but i'm not sure how to continue:
if (str == null || str.equals("")) {
return str;
Any help will be greatly appreciated. Thanks!!!
When in doubt, you can always translate iteration directly into recursion:
for (int i = 0; i < str.length(); i++)
System.out.println(str.charAt(i));
...becomes:
public void printVertical(String str, int i) {
if (i < str.length()) {
System.out.println(str.charAt(i));
printVertical(str, i + 1);
}
}
String inputStr = ...
printVertical(inputStr, 0);
Note that there are many ways of doing this that are more elegant. This feels like a homework assignment to me. I would suggest you find one of the subjectively better approaches rather than using my "blind translation" of your loop.
public void printVertString(String str) {
if (str != null && str.length > 0) //1 base condition
{
System.out.println(str.charAt(0)) //2 print first char
printVertString(str.substring(1)) //3 recursive call, but first char is omitted
}
}
What this does:
Check that the string is not empty (the base case). If it is, then there shouldn't be any more recursion and the method just returns without doing anything
Print the first character of the string
Calls itself, but only with the second character onwards
So if you send it a str = "CAT", the following is what happens (indents differentiate the different calls of the same function)
1 str is "CAT" --> not empty
2 print "C"
3 call printVertString "AT"
1 str is "AT" --> not empty
2 print "A"
3 call printVertString "T"
1 str is "T" --> not empty
2 print "T"
3 call printVertString ""
1 str is "" --> EMPTY
return
The key to understanding recursion is understanding recursion. (bah dum bum)
But seriously, you should think about this in terms of making a smaller problem from the problem that you already have. In this example, you have a string of some length; how could you print part of that string and then have a smaller string with which to repeat the process? That's the essential idea of recursion.
Your base case is correct, so one way you could go about it using the Java libraries and your code above:
public String printVertical(String str)
{
if (str == null || str.equals(""))
{
return str;
}
else
{
System.out.println(str.charAt(0));
return printVertical(str.substring(1, str.length);
}
}
Why would you want to do something like this? This is clearly a case where an iterative process would be clearer and easier to implement than a recursive implementation.
There are a few ways to handle this recursively. Since you are working with Java, I would recommend that you look at the String.substring method in the java core library.
This should do the trick:
void foo (final String str) {
if (null != str && str.length > 0) {
System.out.println(str.charAt(0))
foo(str.substring(1))
}
}
The point of recursion is that you have a function that calls itself.