samedi 24 avril 2021

this is a program to insert data in binary tree but time limit exceed

this is a code to insert data into a binary tree but it exceed time limit , input would be as follows
First line contains two integers, T and X, number of nodes in the tree and value of the root.
Each detail of node contains two lines. First lines contains a string and second line contains an integer, which denotes the path of the node and the value of the node respectively.
String consists of only L or R. L denotes left child and R denotes right child
5 1
L
2
R
3
LL
4
LR
5

 #include<bits/stdc++.h>

using namespace std;

class Node{
    public:
    int x;
    Node*left,*right;
    Node(int data){
        x=data;
        left=NULL;
        right=NULL;
    }
};

void insert(Node*root,string s,int data)
{
    if(root==NULL) return;
     if(s[0]=='L')
     {
         root->left->x=data;
         root=root->left;
         return;
     }
     if(s[0]=='R'){
     root->right->x=data;
      root=root->right;
      return;
     }
     else
     insert(root,s.substr(1),data);
}

void preorder(Node*root){
    cout<<root->x<<" ";
    preorder(root->left);
    preorder(root->right);
}

 
int main()
{
    int n,x;
    cin>>n>>x;
    Node*root=new Node(x);
    while(n--)
    {
        string s; cin>>s;
        int data; cin>>data;
        insert(root,s,data);
    }
    preorder(root);

}

C++ implementation Do something until the thread is running

I have an implementation where I need to communicate over a network while a job is running.

The way I am doing is that, the main thread creates a seperate thread for doing the job. Now until the seperate thread is running, the main thread will continue to send packets over the network. Once the second thread finishes, it should join back to the main thread and terminate.

I've been trying to search how to do "Once the second thread finishes" part but couldn't get any clean implementation. I am hoping of something like thread.has_finished() boolean function, to know whether I should call thread.join() or not.

Any ideas?

(Library being used is standard c++ thread library)

Difference between the windows installation of minGW64 and the MSYS2 shell installed version of minGW64

I have been using the minGW64 compiler with Visual studio code on windows for some time. However, there are certain limitations to it when it comes to certain libraries. The absence of a make tool and the inability to execute a config script while compiling a c++ program being the main ones. I came to know of MSYS, a tool provides a UNIX environment that allows us to do certain things that are forbidden by the windows terminal. I installed it but I saw that I need to install mingw64 again, from within the terminal to use it with MSYS2.

My question is, is there no way I can use my earlier mingw copy with this installation of MSYS2? Is there some difference between the mingw64 that comes with MSYS2 and the standard installation?

Why is a template base class' public member types hidden by default?

template<typename T>
struct A
{
    using U = T;
};

template<typename T>
struct B : A<T>
{
    B(typename A<T>::U) {} // okay
    B(U) {} // error: unknown type name 'U'
};

int main()
{
    return typename B<int>::U{}; // okay
}

Why is a template base class' public member types hidden by default?

To make all threads coherent, Should I call atomic_thread_fence at all threads?

To make all threads coherent, Should I call atomic_thread_fence at all threads???

I'm making My own JobSystem.
I wanna all threads release their cache at a time. So i decided to use atomic_thread_fence.
But i have a question, Do I need to call atomic_thread_fence from all threads???

I'm on x64

vendredi 23 avril 2021

C++ variable list Initialization

So here are piece of 2 code

#1

int i=0, j=0;

for (i=0,j=0; i<=5; i++, j++)
    {
        cout<<i<<"  "<<j<<endl;
    }

#2

int i{0}, j{0};

for (i{0},j{0}; i<=5; i++, j++)
    {
        cout<<i<<"  "<<j<<endl;
    }

Build Messages

1st code allows me to replace the value inside for loop that was stored in i and j. But in the code #2 (in which I used c++11 list initialization) it shows some errors. But this works :

int i{0}, j{0};

for (i,j; i<=5; i++, j++)
    {
        cout<<i<<"  "<<j<<endl;
    }

Why can't I replace them inside loop? What is the correct way for #2 and how should I Proceed?

jeudi 22 avril 2021

How do I call generic variable's method when I specify T must be based of some class in C++?

class Base {
 public:
   void A() {}
};

class Derived: public Base {
  public:
   void B() {}
};

template <typename T, std::enable_if<std::is_base_of<Base, T>::value, int>::type = 0>
class SomeClass {
 public:
   T t;
   // I want to call method A() with variable 't'.
   void invoke() { t.A(); }
};

auto some = new someClass<Derived>();
// I want to call method B() when the 'T' specify to 'Derived'.
some->t.B();

How do I call the A() method when the 'T' must be based of class 'Base', and call the B() method when the 'T' in the class 'someClass' specify to 'Derived'?