void simple_central_tuplespace_test( const std::string& tuplespace_symbol_name, const tuple_type tuple) { examples::simple_central_tuplespace central_tuplespace; if (!central_tuplespace.connect(tuplespace_symbol_name)) { hpx::cerr << "locality " << hpx::get_locality_id() << ": " << "FAIL to connect " << tuplespace_symbol_name << hpx::endl; return; } int ret = central_tuplespace.write(hpx::launch::sync, tuple); hpx::cout << "locality " << hpx::get_locality_id() << ": " << "write_sync "; print_tuple(tuple); hpx::cout << " returns " << ret << hpx::endl; tuple_type partial_tuple; if (tuple.size() > 1) // use second field { partial_tuple.push_back_empty().push_back(*(tuple.begin() + 1)); } else { partial_tuple.push_back(*(tuple.begin())); } tuple_type return_tuple = central_tuplespace.read(hpx::launch::sync, partial_tuple, 0); hpx::cout << "locality " << hpx::get_locality_id() << ": " << "read_sync tuple with "; print_tuple(partial_tuple); hpx::cout << " returns "; print_tuple(return_tuple); hpx::cout << hpx::endl; return_tuple = central_tuplespace.take(hpx::launch::sync, partial_tuple, 0); hpx::cout << "locality " << hpx::get_locality_id() << ": " << "take_sync tuple with "; print_tuple(partial_tuple); hpx::cout << " (1st) returns "; print_tuple(return_tuple); hpx::cout << hpx::endl; return_tuple = central_tuplespace.take(hpx::launch::sync, partial_tuple, 0); hpx::cout << "locality " << hpx::get_locality_id() << ": " << "take_sync tuple with "; print_tuple(partial_tuple); hpx::cout << " (2nd) returns "; print_tuple(return_tuple); hpx::cout << hpx::endl << hpx::flush; }
void print_tuple(const tuple_type& tuple) { if(tuple.empty()) { hpx::cout<<"()"; return; } tuple_type::const_iterator it = tuple.begin(); hpx::cout<<"("<<*it; for(++it; it != tuple.end(); ++it) { hpx::cout<<", "<<*it; } hpx::cout<<")"; }
int insert(const tuple_type& tp) { if(tp.empty()) // empty tuple return -1; // whether size(tp) > size(tuple_fields_) while(tp.size() > tuple_fields_.size()) { tuple_field_container tmp; tuple_fields_.push_back(tmp); } tuple_type::const_iterator it; unsigned int pos; for(it = tp.begin(), pos = 0; it != tp.end(); ++it, ++pos) { tuple_fields_[pos].insert(index_, *it); // insert field } ++index_; // step up the index return 0; }
// put tuple into tuplespace // out function int write(const tuple_type& tp) { if(tp.empty()) { return -1; } { boost::lock_guard<mutex_type> l(mtx_); tuples_.insert(tp); } return 0; }
tuple_type match_and_erase(const tuple_type& tp) { tuple_type result; if(tp.empty()) { return take_random_tuple(); // return random results for an empty tuple } matched_indices_type matched_indices; matched_indices = find_matched_indices(tp); if(!matched_indices.empty()) { // return the tuple at the first index result = take_tuple_at(*(matched_indices.begin())); } return result; }
matched_indices_type find_matched_indices(const tuple_type& tp) const { tuple_type::const_iterator it; unsigned int pos; matched_indices_type matched_indices, empty_set; for(it = tp.begin(), pos = 0; it != tp.end(); ++it, ++pos) { if((*it).empty()) // empty any object { continue; // will match any record } typedef std::pair<tuple_field_container ::field_index_map_const_iterator_type, tuple_field_container::field_index_map_const_iterator_type> equal_range_type; typedef const std::pair<elem_type, index_type> pair_type; equal_range_type found_range = tuple_fields_[pos].field_index_map_.equal_range(*it); if(found_range.first == tuple_fields_[pos].field_index_map_.end()) // no match { return empty_set; // empty } // update index set if(matched_indices.empty()) // not found yet { std::for_each(found_range.first, found_range.second, [&matched_indices](pair_type& p) { matched_indices.insert(p.second); } ); } else { matched_indices_type new_matched_indices; std::for_each(found_range.first, found_range.second, [&new_matched_indices, &matched_indices](pair_type& p) { if(matched_indices.find(p.second) != matched_indices.end()) // found { new_matched_indices.insert(p.second); } } ); if(new_matched_indices.empty()) // no common index { return empty_set; } else { matched_indices = new_matched_indices; } } } return matched_indices; }