Search code examples
c++multithreadingboostcomparedeque

Compare in a deque of tuples c++


I have this deque of tuples:

 deque<boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> > deque_wait;

And I want to be able to compare the elements one by one with this other:

deque<boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> > deque_done;

This is because I want to see what nodes are done and then have a thread pick the next on the list and do something with that, the code I have is this:

Trying to do something like this:

 bool tuple_compare(boost::tuple<ppa::Node*, ppa::Node*, 
 ppa::Node*, bool> &tuple_to_check)
 {
  for(int i = 0; i < deque_wait.size(); i++) {

      boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple_compare = deque_wait.at(i);
      ppa::Node *father = boost::get<0>(tuple_compare);
      ppa::Node *son = boost::get<0>(tuple_compare);
      ppa::Node *second_son = boost::get<2>(tuple_compare);

      bool has_seq = boost::get<3>(tuple_compare);

      cout << "checking " << boost::get<1>(tuple_to_check)->get_name() << " "
              << boost::get<2>(tuple_to_check)->get_name() << " " 
              << boost::get<0>(tuple_compare)->get_name() << endl;
  }

  return true;

}

Thread func:

 void wait_function(void)
 {
  try {

  } catch (boost::lock_error &le)
  {
      cout << le.what() << " from " << boost::this_thread::get_id() << endl;
  }

  }

I know that mutex are needed, I have planned those, for now I just need to know how to compare the elements of the deque with the other to see if they fulfill certain conditions and running them, the compare right now is being used with the sort function like:

sort(deque_wait.begin(), deque_wait.end(), tuple_compare);

Because I though that I could use this func to compare the 2 deques, so in summary what I want to do is to compare the first element of deque_done with all the wait and if the conditions are fulfilled change the bool to true, they are different done and wait, the reason I want to check them is because this a tree like structure has a parent and 2 sons so each tuple is that, a parent and 2 sons and I need to see which sons have a sequence to put it ready to do and which are not so to have a thread come and pick it up, it does not matter if another thing like a vector or another deque is needed, so in this case price is not important, thanks. (:


Solution

  • OK, let's give it a try:

    what I want to do is to compare the first element of deque_done with all the wait and if the conditions are fulfilled change the bool to true,

    Calling sort() is not what you need here - it technically compares each element of deque_wait with each other and orders them according to the result. To compare one given value with each deque-element you should try std::find() or std::find_if().

    //Helper functor to compare:
    struct tuple_compare
    {
      //note: maybe it could use some const's
      tuple<...>& value;
      tuple_compare(tuple<...>& to_compare) : value(to_compare) {}
      bool operator()(tuple<...>& second) const
      {
        //do anything to compare the original value with the given parameter
      }
    };
    
    deque<...>::iterator pos = find_if(deque_wait.begin(),deque_wait.end(),tuple_compare(deque_done.front()));
    if(pos!=deque_wait.end())
      //...