Can clion use arrow(->) not dot(.) to access struct member when extracting methods? - refactoring

Original Code:
typedef struct {
int x;
int y;
} Point;
int main() {
Point point = {1, 2};
printf("%d %d\n", point.x, point.y);
return 0;
}
Use refactor to extract a method:
typedef struct {
int x;
int y;
} Point;
void PrintPoint(Point *point)
{
printf("%d %d\n", (*point).x, (*point).y);
}
int main() {
Point point = {1, 2};
PrintPoint(&point);
return 0;
}
But I want the generated PrintPoint function is like this:
void PrintPoint(Point *point)
{
printf("%d %d\n", point->x, point->y);
}
Is there a configuration in CLion to change (*pStru). to pStru-> when extracting a method?

Unfortunately there is no way to configure the behaviour. There is a bug in CLion tracker about your case. https://youtrack.jetbrains.com/issue/CPP-2193
Maybe it's time to fix it.

Related

Whats wrong with my top down knapsack dp approach?

I am not able to figure out what is wrong with my top down knapsack dp approach, its failing testcases on below link, need help.
Question link: https://www.interviewbit.com/problems/0-1-knapsack/
Here is my code:
int fin(int i,int wt,int curprofit,vector<int>&A,vector<int>&B,int C,int n,vector<vector<int>>&dp)
{
if(i==n)
return curprofit;
if(dp[i][wt]!=-1)
return dp[i][wt];
int ret=0;
ret=max(ret,fin(i+1,wt,curprofit,A,B,C,n,dp));
if(wt+B[i]<=C)
{
ret=max(ret,fin(i+1,wt+B[i],curprofit+A[i],A,B,C,n,dp));
}
return dp[i][wt]= ret;
}
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
int n=A.size();
vector<vector<int>>dp(n+1,vector<int>(C+1,-1));
return fin(0,0,0,A,B,C,n,dp);
}
Here's the fix, but I'd suggest you brush up on your recursion knowledge.
Calculate the max profit on the fly, not passing as the parameter. Otherwise, you'll need to put curprofit also in the dp state which will be costly. You may also see the output by removing the dp[][] caching. Just put up a correct recursive solution & memoize it.
int fin(int i,int wt,vector<int>&A,vector<int>&B,int C,int n,vector<vector<int>>&dp)
{
if(i==n)
return 0;
if(dp[i][wt]!=-1)
return dp[i][wt];
int ret=0;
ret=max(ret,fin(i+1,wt,A,B,C,n,dp));
if(wt+B[i]<=C)
{
ret=max(ret,fin(i+1,wt+B[i],A,B,C,n,dp) + A[i]);
}
return dp[i][wt]= ret;
}
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
int n=A.size();
vector<vector<int>>dp(n+1,vector<int>(C+1,-1));
return fin(0,0,A,B,C,n,dp);
}

Document function Transfer to calculate the row of text file

I want to transfer a function to calculate the row of a text file.
The compile can pass but the function can not be transferred. I want to know what happens.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int getLine( const char *filename)
{
ifstream infile(filename,ios::in);
if(!infile){
cout<<"can not open"<<filename<<'\n';
return 0;
}
int count=0;
infile.unsetf(ios::skipws);
char buff[300];
while(infile.getline(buff,300))
count++;
cout<<"the total line:"<<count<<endl;
infile.close();
return 0;
}
int getLineNoEmpty(const char* filename)
{
ifstream infile(filename,ios::in);
if(!infile){
cout<<"can not open"<<filename<<'\n';
return 0;
}
int count=0;
char buff[300];
while(infile.getline(buff,300))
{
if(sizeof(buff)==0)
continue;
else
count++;
}
cout<<"the total line without null string:"<<count<<endl;
return 0;
}
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
int getLine(const char &filename);
int getLineNoEmpty(const char &filename);
return 0;
}
The compile can pass but the function can not be transferred. I want to know what happens about it. It can output the result I want. And I don't know how to
realize the goal of calculating the total line without null string.
Firstly, You are just declaring 2 functions in main() without using them. Change
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
int getLine(const char &filename);
int getLineNoEmpty(const char &filename);
return 0;
}
to
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
getLine(filename);
getLineNoEmpty(filename);
return 0;
}

Run time error in graph

I am implementing Graph for the first time and for that I took this problem from SPOJ.
Took help of geeksforgeeks, applied union find algorithm to find out whether or not graph contains a cycle but I get run time error (SIGSEGV).
Can you please help why is it so?
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct Edge{
int s,d;
};
struct Graph{
int v,e;
struct Edge* edge;
};
struct Graph* create(int v, int e){
struct Graph* graph=(struct Graph*)malloc(sizeof (struct Graph));
graph->v=v;
graph->e=e;
graph->edge=(struct Edge*)malloc(sizeof (struct Edge));
return graph;
};
int Find(int p[],int i)
{
if (p[i] == -1)
return i;
return Find(p, p[i]);
}
void Union(int p[],int i, int j)
{
p[j]=i;
}
bool CheckCycle(struct Graph* graph)
{
int *p=(int*)malloc(graph->v* sizeof (int));
memset(p,-1,graph->v * sizeof (int));
/*for(int i=0;i<graph->v;i++)
cout<<"p"<<i<<"="<<p[i];
cout<<"\n";*/
for(int i=0;i<graph->e;i++)
{
/*cout<<"edge"<<i<<" src="<<graph->edge[i].s<<"\n";
cout<<"edge"<<i<<" dest="<<graph->edge[i].d<<"\n";*/
int x=Find(p,graph->edge[i].s);
int y=Find(p,graph->edge[i].d);
/*cout<<"x="<<x<<" "<<"y="<<y<<"\n";*/
if(x==y)
return true;
Union(p,x,y);
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
int N,M,v1,v2;
cin>>N>>M;
if(M!=(N-1))
cout<<"NO\n";
else{
struct Graph* graph=create(N,M);
for(int i=0;i<M;i++)
{
cin>>v1;
graph->edge[i].s=v1-1;
cin>>v2;
graph->edge[i].d=v2-1;
}
if(CheckCycle(graph))
cout<<"NO\n";
else
cout<<"YES\n";
}
}
One issue is this in your main program:
graph->edge[i].s=v1-1;
You created a single edge. If i is greater than 0, then this is an out-of-bounds access.
Look how you created edge in the create function:
graph->edge=(struct Edge*)malloc(sizeof (struct Edge));
That allocation holds a single edge, not multiple edges. Given how you coded the rest of your program in a C-like fashion, what you probably wanted is this:
graph->edge=(struct Edge*)malloc(sizeof(Edge) * e);
Also, you should strive to not use single-letter variable names. It is hard to read code with e, v, etc. as member variable names. Name those items m_edge, m_vertex or something that is more descriptive.

Does Mac OS X have pthread_spinlock_t type?

I didn't find it in Mac, but almost all Linux os support it..
Any one knows how to port it to mac?
Here is drop in replacement code. You should be able to put this in a header file and drop it in your project.
typedef int pthread_spinlock_t;
int pthread_spin_init(pthread_spinlock_t *lock, int pshared) {
__asm__ __volatile__ ("" ::: "memory");
*lock = 0;
return 0;
}
int pthread_spin_destroy(pthread_spinlock_t *lock) {
return 0;
}
int pthread_spin_lock(pthread_spinlock_t *lock) {
while (1) {
int i;
for (i=0; i < 10000; i++) {
if (__sync_bool_compare_and_swap(lock, 0, 1)) {
return 0;
}
}
sched_yield();
}
}
int pthread_spin_trylock(pthread_spinlock_t *lock) {
if (__sync_bool_compare_and_swap(lock, 0, 1)) {
return 0;
}
return EBUSY;
}
int pthread_spin_unlock(pthread_spinlock_t *lock) {
__asm__ __volatile__ ("" ::: "memory");
*lock = 0;
return 0;
}
See discussion, and Github source
EDIT: Here's a class that works on all OSes that includes a workaround for missing pthread spinlocks on OSX:
class Spinlock
{
private: //private copy-ctor and assignment operator ensure the lock never gets copied, which might cause issues.
Spinlock operator=(const Spinlock & asdf);
Spinlock(const Spinlock & asdf);
#ifdef __APPLE__
OSSpinLock m_lock;
public:
Spinlock()
: m_lock(0)
{}
void lock() {
OSSpinLockLock(&m_lock);
}
bool try_lock() {
return OSSpinLockTry(&m_lock);
}
void unlock() {
OSSpinLockUnlock(&m_lock);
}
#else
pthread_spinlock_t m_lock;
public:
Spinlock() {
pthread_spin_init(&m_lock, 0);
}
void lock() {
pthread_spin_lock(&m_lock);
}
bool try_lock() {
int ret = pthread_spin_trylock(&m_lock);
return ret != 16; //EBUSY == 16, lock is already taken
}
void unlock() {
pthread_spin_unlock(&m_lock);
}
~Spinlock() {
pthread_spin_destroy(&m_lock);
}
#endif
};
Try using OSSpinLock instead. Documentation is here: http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/spinlock.3.html
If the performance of your lock is not critical, pthread_mutex_t can be used as a drop replacement for pthread_spinlock_t, which makes porting easy.
I have used instead (that is natively supported by OS X intel)
pthread_rwlock_t lock;
pthread_rwlock_init
pthread_rwlock_wrlock
pthread_rwlock_unlock
And that works very fine as well

Visual C++ initliazing aggregates inline

In g++ I could do this:
struct s
{
int a, b;
};
void MyFunction(s) { }
int main()
{
MyFunction((s) { 0, 0 });
return 0;
}
In Visual Studio however, it doesn't work. is there any way to make it work or some alternative syntax without making a variable and initializing it (and without adding a constructor to the struct as it will make it non-aggregate and it wouldn't be able to initialize in aggregates)?
My C is a bit rusty, but didn't you have to use struct s unless you typedef it? Something like this:
struct s
{
int a, b;
};
void MyFunction(struct s) { }
int main()
{
MyFunction((struct s) { 0, 0 });
return 0;
}
or
typedef struct s
{
int a, b;
} s_t;
void MyFunction(s_t) { }
int main()
{
MyFunction((s_t) { 0, 0 });
return 0;
}

Resources