void CMaquetteImpl::send(CPacketToSend& packet, bool wait /*= false*/) { const data_t data = packet.encode(); LOG4CPLUS_DEBUG(logger, "Sending " << CPacket::Type::toString(data[0]) << ": " << data.size() << " byte\n" << CUtils::dump(data).c_str()); // Send message to the server // See https://casablanca.codeplex.com/wikipage?title=Web%20Socket&referringTitle=Documentation auto buf = std::make_shared<producer_consumer_buffer<byte>>(); auto task = buf->putn_nocopy(data.data(), data.size()) .then([=](size_t size) { websocket_outgoing_message msg; msg.set_binary_message(buf->create_istream(), size); pplx::task<void>task = m_client->send(msg); task.then([](pplx::task<void> t) { try { t.get(); } catch(const websocket_exception& ex) { LOG4CPLUS_ERROR(logger, "websocket_callback_client::send() failed: " << ex.what()); } }); return task; }); if(wait) task.wait(); if(packet.type() != CPacket::Type::PINGREQ) { m_keepAliveTimer.restart(); } }
static bool fibonacci(data_t const& args, data_t& into) { // unpack arguments if (args.size() != 2) return false; auto f = args[0], l = args[1]; // iterate uint64_t gen0 = 0, gen1 = 1, next = gen0 + gen1; for(auto i = 0u; i <= l; ++i) { std::cout << "fibonacci(" << f << ", " << l << ") at i == " << i << ", next: " << next << "\n"; switch(i) { case 0: if (i>=f) into.push_back(gen0); break; case 1: if (i>=f) into.push_back(gen1); break; default: { next = gen0 + gen1; if (i>=f) into.push_back(next); gen0 = gen1; gen1 = next; break; } } } std::cout << "fibonacci(" << f << ", " << l << ") result: " << karma::format_delimited(karma::auto_, ';', into) << "\n"; // done return true; }
string TH_0x01::makeStringFromData(const data_t& data, const options_t& options) { (void)options; string str; size_t byteCount = data.size(); size_t numCount = (size_t) ((data[0] & 0xFF) + ((data[1] << 8) & 0xFF00)); if (byteCount < 2+TH_0x00::dataByteCount || ((byteCount - 2) % TH_0x00::dataByteCount != 0) || (numCount != (size_t)((byteCount - 2) / TH_0x00::dataByteCount)) || numCount > 999) { std::cerr << "Invalid data array. Needs to contain 2+" + to_string(TH_0x00::dataByteCount) + "*n bytes" << endl; return ""; } str = "{"; for (size_t i = 2, num = 0; i < byteCount; i += TH_0x00::dataByteCount, num++) { str += TH_0x00::makeStringFromData(data_t(data.begin()+i, data.begin()+i+TH_0x00::dataByteCount)); if (num < numCount - 1) // not last num { str += ','; } } str += "}"; return str; }
// fold functor example static bool fun(const char *& acc, const data_t& data, const store_t& store, pos_t begin, pos_t end, bool has_next) { if (data.empty()) return true; acc = data.str(store); std::cout << begin << ":" << end << ":" << has_next << ":" << acc << "\n"; return true; }
// fold functor example static bool fun(const char *& acc, const data_t& data, const store_t& store, pos_t, bool) { if (data.empty()) return true; acc = data.str(store); std::cout << acc << std::endl; return true; }
void random_forest_p(const data_t& train, const vec_data_t& test, preds_t& train_preds, vec_preds_t& test_preds, args_t& args) { int trees = args.trees; int numthreads=args.processors, i, x, t; int fk = args.kfeatures; if (numthreads > trees) numthreads = trees; trees = (trees / numthreads) * numthreads; int trees_per_thread = trees / numthreads; thread** threads = new thread*[numthreads]; vector< vec_preds_t > seg; vector< preds_t > train_seg; for (i=0; i < numthreads; i++) { vec_preds_t p; init_pred_vec(test, p); seg.push_back(p); preds_t tp; init_pred(train, tp); train_seg.push_back(tp); } for (i=0;i<numthreads;i++) threads[i] = new thread(bind(multiple_forest, trees_per_thread, cref(train), cref(test), ref(train_seg[i]), ref(seg[i]), ref(args))); for (i=0;i<numthreads;i++){ threads[i]->join(); delete threads[i]; } fprintf(stderr, "done threading\n"); delete[] threads; // congregate results of the threads for (i = 0; i < numthreads; i++) for (t = 0; t < test.size(); t++) for (int j = 0; j < test[t].size(); j++) test_preds[t][j] += seg[i][t][j]; for (i = 0; i < numthreads; i++) for (int j = 0; j < train.size(); j++) train_preds[j] += train_seg[i][j]; // average results of the trees for (i = 0; i < test.size(); i++) for (int j = 0; j < test[i].size(); j++) test_preds[i][j] /= trees; for (int j = 0; j < train.size(); j++) train_preds[j] /= trees; // write results to file for (i = 0; i < test.size(); i++) { tuple::write_to_file(test_preds[i], test[i], args.test_outs[i]); } }
void session_interface::save_data(data_t const &data,std::string &s) { s.clear(); data_t::const_iterator p; for(p=data.begin(); p!=data.end(); ++p) { packed header(p->first.size(),p->second.exposed,p->second.value.size()); char *ptr=(char *)&header; s.append(ptr,ptr+sizeof(header)); s.append(p->first.begin(),p->first.end()); s.append(p->second.value.begin(),p->second.value.end()); } }
secure_session_t(const data_t& id, const data_t& priv_key, secure_session_callback_interface_t* callbacks): _session(NULL), _res(0){ _callback.get_public_key_for_id=themispp::get_public_key_for_id_callback; _callback.send_data=themispp::send_callback; _callback.receive_data=themispp::receive_callback; _callback.state_changed=NULL; _callback.user_data=callbacks; _session=secure_session_create(&id[0], id.size(), &priv_key[0], priv_key.size(), &_callback); if(!_session) throw themispp::exception_t("Secure Session failde creating"); }
static bool range(data_t const& args, data_t& into) { // unpack arguments if (args.size() != 2) return false; auto f = args[0], l = args[1]; if (l>f) into.reserve(1 + l - f + into.size()); for(; f<=l; ++f) into.push_back(f); // to optimize return true; }
bool bin_index_t::file_node::first(data_t& key,data_t& val) const { open_index_file(); index_t ind; if(!first_item(root_page,ind))return false; key=ind.key; key.resize(key_len); val.resize(ind.data_len); load_data(ind.data_offset,val); return true; }
/////////////////////////////////////////////////////////////////////////////////// // dir_node // bool bin_index_t::dir_node::get(const data_t& key,data_t& val) const { validate_key_len(key); validate_index(); data_t head(key.begin(),key.begin()+parent.dir_key_len); data_t tail(key.begin()+parent.dir_key_len,key.end()); if(!std::binary_search(indexes.begin(),indexes.end(),head,data_less_pr())) return false; validate_sub(head); return sub_node->get(tail,val); }
/** Sets the filter. * The length of the impulse response is arbitrary. It automatically * performs zero padding if necessary and creates the required number of * partitions. It is not very efficient since it calls * \b Convolver::prepare_impulse_response(). It is provided for convenience. * If you have the filter's transfer function in halfcomplex format use * \b Convolver::set_filter_f() instead. * @param filter impulse response of the filter */ void Convolver::set_filter_t(const data_t& filter) { if (filter.empty()) { WARNING("You are trying to use an empty filter."); return; } data_t buffer; // TODO: It would be more efficient to use _fft_plan and _fft_buffer etc. prepare_impulse_response(buffer, &filter[0], filter.size(), _frame_size); set_filter_f(buffer); }
void clever_runner( std::size_t thread_index, std::size_t iteration, atomic_count_t& counter, data_t& data) { fill_data(data.at(thread_index)); if (++counter == data_t::static_size) { compute_send_data(data); ++ iteration; if (iteration == 1000) { // exiting, because 1000 iterations are done tasks_processor::get().stop(); return; } counter = 0; for (std::size_t i = 0; i < data_t::static_size; ++ i) { tasks_processor::get().push_task(boost::bind( clever_runner, i, iteration, boost::ref(counter), boost::ref(data) )); } } }
string TH_0x20::makeStringFromData(const data_t& data, const options_t& options) { (void)options; if (data.size() != dataByteCount) { throw invalid_argument("Empty data array. Needs to contain " + to_string(dataByteCount) + " bytes"); } string coeff = TH_0x00::makeStringFromData(data); string str = ((coeff != "0") ? (coeff + "*π") : ""); // Improve final display str = regex_replace(str, regex("\\+1\\*"), "+"); str = regex_replace(str, regex("\\(1\\*"), "("); str = regex_replace(str, regex("-1\\*"), "-"); str = regex_replace(str, regex("\\(-1\\*"), "(-"); str = regex_replace(str, regex("\\+-"), "-"); // Shouldn't happen - I don't believe the calc generate such files. if (str == "") { str = "0"; } return str; }
bool bin_index_t::dir_node::set(const data_t& key,const data_t& val) { validate_key_len(key); validate_index(); data_t head(key.begin(),key.begin()+parent.dir_key_len); data_t tail(key.begin()+parent.dir_key_len,key.end()); bool already_exists=std::binary_search(indexes.begin(),indexes.end(),head,data_less_pr()); if(!already_exists)create_text_child(head); validate_sub(head); bool r=sub_node->set(tail,val); return r; }
file_offset_t bin_index_t::file_node::append_data(const data_t& res) { open_data_file(); file_offset_t ret=fd->append(res); data_size=ret+res.size(); return ret; }
bool bin_index_t::dir_node::first(data_t& key,data_t& val) const { validate_index(); if(indexes.empty())return false; const data_t& head=indexes.front(); validate_sub(head); data_t tail; if(!sub_node->first(tail,val))return false; key=head; key.insert(key.end(),tail.begin(),tail.end()); return true; }
inline header_t* header()const { if(_data) { return (header_t*)_data.get(); } return NULL; }
string TH_0x1B::makeStringFromData(const data_t& data, const options_t& options) { (void)options; if (data.size() != dataByteCount) { throw invalid_argument("Empty data array. Needs to contain " + to_string(dataByteCount) + " bytes"); } string coeffR = TH_0x00::makeStringFromData(data_t(data.begin(), data.begin() + TH_0x00::dataByteCount)); string coeffI = TH_0x00::makeStringFromData(data_t(data.begin() + TH_0x00::dataByteCount, data.begin() + 2 * TH_0x00::dataByteCount)); string str = dec2frac(atof(coeffR.c_str())) + "+" + dec2frac(atof(coeffI.c_str())) + "i"; str = regex_replace(str, regex("\\+-"), "-"); return str; }
inline body_t body()const { if(_data) { return body_t(_data.get() + sizeof(header_t),header()->size); } return body_t(); }
bool bin_index_t::dir_node::next(data_t& key,data_t& val) const { validate_key_len(key); validate_index(); data_t head(key.begin(),key.begin()+parent.dir_key_len); data_t tail(key.begin()+parent.dir_key_len,key.end()); datas_t::const_iterator it=std::lower_bound(indexes.begin(),indexes.end(),head,data_less_pr()); if(it==indexes.end())return false; if(*it==head) { validate_sub(head); if(sub_node->next(tail,val)) { key=head; key.insert(key.end(),tail.begin(),tail.end()); return true; } } ++it; if(it==indexes.end())return false; head=*it; validate_sub(head); if(!sub_node->first(tail,val))return false; key=head; key.insert(key.end(),tail.begin(),tail.end()); return true; }
void CTCPProfile::Compress(const data_t &data, data_t &output) { size_t outputInSize = output.size(); const iphdr* ip = reinterpret_cast<const iphdr*>(&data[0]); const tcphdr* tcp = reinterpret_cast<const tcphdr*>(ip+ip->ihl*4); UpdateIpIdOffset(ip); if (IR_State == state) { CreateIR(ip, tcp, output); } else { CreateCO(ip, tcp, output); } UpdateIpInformation(ip); AdvanceState(false, false); increaseMsn(); // Append payload // TODO, handle TCP options output.insert(output.end(), data.begin() + sizeof(iphdr) + sizeof(tcphdr), data.end()); ++numberOfPacketsSent; dataSizeCompressed += output.size() - outputInSize; dataSizeUncompressed += data.size(); }
secure_comparator_t(const data_t& shared_secret) : comparator_(NULL) { if (shared_secret.empty()) { throw themispp::exception_t("Secure Comparator must have non-empty shared secret"); } res_.reserve(512); comparator_ = secure_comparator_create(); if (!comparator_) { throw themispp::exception_t("Secure Comparator construction failed"); } themis_status_t status = secure_comparator_append_secret(comparator_, &shared_secret[0], shared_secret.size()); if (THEMIS_SUCCESS != status) { throw themispp::exception_t("Secure Comparator failed to append secret", status); } }
std::string STH_ExactFractionPi::makeStringFromData(const data_t& data, const options_t& options) { if (data.size() != 9) { throw std::invalid_argument("Invalid data array. Needs to contain 9 bytes"); } return dec2frac(stod(STH_FP::makeStringFromData(data, options)), "π"); }
void bin2points(const data_t& bin,points_t& pts) { if((bin.size()%2)!=0) throw std::runtime_error("bin2points(): (bin.size()%2)!=0"); pts.resize(bin.size()/2); for(unsigned i=0;i<pts.size();i++) { point& p=pts[i]; char x=*reinterpret_cast<const unsigned char*>(&bin[i*2]); char y=*reinterpret_cast<const unsigned char*>(&bin[i*2+1]); p.x=x; p.y=y; } }
bool bin_index_t::file_node::next(data_t& key,data_t& val) const { validate_key_len(key); open_index_file(); if(!root_page) return false; index_t ind; align_key(key,ind); if(!next_item(root_page,ind))return false; key=ind.key; key.resize(key_len); val.resize(ind.data_len); load_data(ind.data_offset,val); return true; }
void construct(point &root, data_t dataset){ if (dataset.size() == 0) return; root = new point_t(); vector< double > buff; for (size_t i = 0; i < dataset.at(0).size(); i++){ buff.push_back(variation(dataset, i)); } size_t split = 0; double min; for (size_t i = 0; i < buff.size(); i++){ if (i == 0){ min = buff.at(i); } else{ if (min < buff.at(i)){ min = buff.at(i); split = i; } } } sort(dataset.begin(), dataset.end(), [split](vec_t a, vec_t b){ return a.at(split) < b.at(split); }); size_t middle = dataset.size() / 2; root->split = split; for (size_t i = 0; i < dataset.at(middle).size(); i++){ root->node.push_back(dataset.at(middle).at(i)); } root->dim = root->node.size(); data_t lDataset, rDataset; for (size_t i = 0; i < middle; i++){ lDataset.push_back(dataset.at(i)); } for (size_t i = middle + 1; i < dataset.size(); i++){ rDataset.push_back(dataset.at(i)); } construct(root->left, lDataset); construct(root->right, rDataset); if (root->left) root->left->parent = root; if (root->right) root->right->parent = root; return; }
void bin2points(const data_t& bin,steps_t& pts) { if((bin.size()%3)!=0) throw std::runtime_error("bin2points(): (bin.size()%3)!=0"); pts.resize(bin.size()/3); for(unsigned i=0;i<pts.size();i++) { step_t& p=pts[i]; char x=*reinterpret_cast<const unsigned char*>(&bin[i*3]); char y=*reinterpret_cast<const unsigned char*>(&bin[i*3+1]); p.x=x; p.y=y; p.step=(Step)bin[i*3+2]; } }
bool bin_index_t::file_node::set(const data_t& key,const data_t& val) { validate_key_len(key); open_index_file(); if(!root_page) { root_page=create_page(); append_page(*root_page); save_index_data(0,root_page->page_offset); add_page(root_page); } index_t it; align_key(key,it); if(get_item(it)) { index_t old_i=it; if(it.data_len>=val.size())save_data(it.data_offset,val); else it.data_offset=append_data(val); it.data_len=val.size(); if(old_i.data_offset==it.data_offset&& old_i.data_len==it.data_len) return false; update_page(it); return false; } index_t v; v.key=key; align_key(key,v); v.data_len=val.size(); v.data_offset=append_data(val); add_item(v); return true; }
//min-max splits based on impurity measure bool impurity_splitW_noMiss(data_t data, int& f_split, double& v_split, double imp, boost::numeric::ublas::vector<int>& c_total,args_t& myargs, double (*impurityHandle)(int, boost::numeric::ublas::vector<int>&,double)) { int NF=myargs.features; int num_c=myargs.num_c; double alpha=myargs.alpha; f_split = -1; double min = MY_DBL_MAX, cf; int n = data.size(), i,j ; double imp_l=0.0, imp_r=0.0, imp_m=0.0, imp_max=0.0; //impurity on the left double mind; double v_split_d; for (int f = 1; f < NF; f++) { //for each feature cf=myargs.Costs[f]; sort(data.begin(), data.end(), boost::bind(mysortf, _1,_2, f)); boost::numeric::ublas::vector<int> c_l(num_c,0); //number of examples in each class on the left boost::numeric::ublas::vector<int> c_r(c_total); //number of examples in each class on the right mind = MY_DBL_MAX; //assume no missing data for( i=0;i<n-1;i++){ c_l[data[i]->label]++; c_r[data[i]->label]--; // do not consider splitting here if data is the same as next if (data[i]->features[f] == data[i+1]->features[f]) continue; imp_l=(*impurityHandle)(num_c, c_l, alpha); imp_r=(*impurityHandle)(num_c, c_r, alpha); imp_max=(imp_l < imp_r) ? imp_r: imp_l; if(imp_max<mind){ mind=imp_max; v_split_d = (data[i]->features[f] + data[i+1]->features[f])/2; } } if ((imp-mind>0.0000001) && (cf/(imp-mind) < min)) { min = cf/(imp-mind); f_split = f; v_split = v_split_d; } } return min != MY_DBL_MAX; }