void OutResult(const std::string& nameFile, const std::vector<std::string>& listFriday13) { std::ofstream outputFile; outputFile.open(nameFile); std::ostream_iterator<std::string> out_it(outputFile, "\n"); std::copy(listFriday13.begin(), listFriday13.end(), out_it); }
// Remove outlines that are a tiny fraction in either width or height // of the word height. void Textord::clean_small_noise_from_words(ROW *row) { WERD_IT word_it(row->word_list()); for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) { WERD* word = word_it.data(); int min_size = static_cast<int>( textord_noise_hfract * word->bounding_box().height() + 0.5); C_BLOB_IT blob_it(word->cblob_list()); for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) { C_BLOB* blob = blob_it.data(); C_OUTLINE_IT out_it(blob->out_list()); for (out_it.mark_cycle_pt(); !out_it.cycled_list(); out_it.forward()) { C_OUTLINE* outline = out_it.data(); outline->RemoveSmallRecursive(min_size, &out_it); } if (blob->out_list()->empty()) { delete blob_it.extract(); } } if (word->cblob_list()->empty()) { if (!word_it.at_last()) { // The next word is no longer a fuzzy non space if it was before, // since the word before is about to be deleted. WERD* next_word = word_it.data_relative(1); if (next_word->flag(W_FUZZY_NON)) { next_word->set_flag(W_FUZZY_NON, false); } } delete word_it.extract(); } } }
// -------------------------------------------------------- std::vector<std::string> IdSliceParser::parse(const std::string& aStringSlice) { namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; std::string::const_iterator lBegin(aStringSlice.begin()), lEnd(aStringSlice.end()); IdSliceGrammar lGrammar; std::vector<std::string> lIds; bool success = qi::phrase_parse ( lBegin , lEnd , lGrammar , ascii::space , lIds ); if ( success ) { // Throw if slice not fully parsed if ( lBegin != lEnd ) { std::ostringstream msg; std::ostream_iterator<char> out_it (msg,""); msg << "Slice parsing stopped at position " << std::distance(aStringSlice.begin(), lBegin) << " of string '" << aStringSlice << "'"; throw SliceParsingFailed(msg.str()); } } else { // TODO: Should throw here return std::vector<std::string>(); } return lIds; }
int main(int argc, char** argv) { //Generator obj(4); std::deque<int> mydeque(5); std::generate(mydeque.begin(), mydeque.end(), Generator(4)); std::ostream_iterator<int> out_it (std::cout, "; "); copy (mydeque.begin(), mydeque.end(), out_it); std::cout << std::endl; std::vector<int> s1(5); copy(mydeque.begin(), mydeque.end(), s1.begin()); copy (s1.begin(), s1.end(), out_it); std::cout << std::endl; mydeque.clear(); std::vector<int> s2(10); std::generate(s2.begin(), s2.end(), Generator(2)); copy (s2.begin(), s2.end(), out_it); std::cout << std::endl; std::vector<int> s3; //generate_n std::back_insert_iterator<std::vector<int> > it (s3); generate_n(it, 6, Generator(3)); copy (s3.begin(), s3.end(), out_it); std::cout << std::endl; std::cout << "s1 contenu dans s2 : " << (includes(s1.begin(), s1.end(), s2.begin(), s2.end()) ? "true" : "false") << std::endl; return 0; }
void LZ::writeTokens() { deque<triple> temp = tokens; cout << N << "," << L << "," << S << endl; vector<bool>Nb = get_bits(N,8); vector<bool>Lb = get_bits(L,8); vector<bool>Sb = get_bits(S,8); //ouput NLS as bytes vector<bool>output; output.reserve(24); output.insert(output.end(), Nb.begin(), Nb.end()); output.insert(output.end(), Lb.begin(), Lb.end()); output.insert(output.end(), Sb.begin(), Sb.end()); std::ostream_iterator<bool>out_it(std::cerr); copy(output.begin(), output.end(), out_it); while (temp.size() > 0) { writeToken(temp.front()); temp.pop_front(); } triple endToken; endToken.length = 0; endToken.offset = 0; endToken.s = ""; writeToken(endToken); }
std::vector<int> cmim () { int featuresEnd = m_numFeatures-1; int outputFeature = featuresEnd; std::vector<double> partialScores (featuresEnd, 0.0); std::vector<int> lastPicked (featuresEnd, 0); std::vector<int> nonAlreadyUpdated (featuresEnd, 0); for (int n = 0; n < featuresEnd; ++n) { partialScores.at (n) = I_joint_xy (n, outputFeature); } for (int k = 0; k < featuresEnd; ++k) { double score = 0.0; for (int n = 0; n < featuresEnd; ++n) { while (partialScores.at (n) > score && lastPicked.at (n) < k-1) { lastPicked.at (n) += 1; partialScores.at (n) = std::min (partialScores.at (n), I_xy_given_z (outputFeature, n, nonAlreadyUpdated.at(lastPicked.at (n)))); } if (partialScores.at (n) > score) { score = partialScores.at (n); nonAlreadyUpdated.at (k) = n; } } } std::ostream_iterator<double> out_it (std::cout, " , "); std::copy (partialScores.begin (), partialScores.end (), out_it); return nonAlreadyUpdated; }
int main(int argc, char** argv) { (void) argc; (void) argv; std::set<boost::shared_ptr<int> > ints; std::set<IntStorage> storages; for (int i = 0; i < 10; i++) { ints.insert(boost::shared_ptr<int>(new int(i))); storages.insert(i); } int* someInt = new int(42); std::ostream_iterator<boost::shared_ptr<int> > out_it (std::cout, ", "); std::copy(ints.begin(), ints.end(), out_it); std::cout << std::endl; std::ostream_iterator<IntStorage> s_out_it(std::cout, ", "); std::copy(storages.begin(), storages.end(), s_out_it); //std::for_each( ints.begin(), ints.end(), boost::bind(&std::ostream::operator<<, std::cout) ); std::cout << std::endl; //ints.erase(ints.end()); std::remove_if(ints.begin(), ints.end(), SameObject<int>(someInt)); //ints.erase( ints.find( 5 ) ); //std::copy(ints.begin(), ints.end(), out_it); //std::cout << std::endl; return 0; }
int test_iterator_18() { // std::ostreambuf_iterator: 输出流缓存迭代器 std::string mystring("Some text here...\n"); std::ostreambuf_iterator<char> out_it(std::cout); // stdout iterator std::copy(mystring.begin(), mystring.end(), out_it); // Some text here... return 0; }
int main(void) { mylib::date dates[] = {mylib::date(1912,6,23), mylib::date(1941,9,9), mylib::date(1943,2,4), mylib::date(1931,10,12), mylib::date(1926,8,27)}; size_t ndates = sizeof dates / sizeof dates[0]; bubble_sort(dates, ndates); std::ostream_iterator<mylib::date> out_it(std::cout,"\n"); std::copy(dates, dates + ndates, out_it); }
int test_iterator_19() { // std::ostream_iterator: 顺序写入输出流的输出迭代器 std::vector<int> myvector; for (int i = 1; i<10; ++i) myvector.push_back(i * 10); std::ostream_iterator<int> out_it(std::cout, ", "); std::copy(myvector.begin(), myvector.end(), out_it); // 10, 20, 30, 40, 50, 60, 70, 80, 90, return 0; }
int main(int argc, char *argv[]) { numb::Forward_list<int> FL; for (int i = 0; i < 50; ++i) FL.push_front(i); std::ostream_iterator<int> out_it(std::cout, " "); std::cout << "Forwardlist:\n"; numb::Copy(FL.begin(), FL.end(), out_it); return 0; }
void LZ::writeToken(triple token) { //if it is a literal token, write out the //code, strlen, and literal string vector<bool>len;//L vector<bool>offset;//N vector<bool>strlen;//S vector<bool>chars(0); vector<bool>output; std::ostream_iterator<bool>out_it(std::cerr); if (token.length==0) { if (token.offset == 0) { len = get_bits(token.length, L); offset = get_bits(token.offset, S); output.insert(output.end(), len.begin(), len.end()); output.insert(output.end(), offset.begin(), offset.end()); goto endif;//I am so sorry for this goto } //write out L 0's len = get_bits(token.length,L); //write out offset(the number of chars) in S bits offset = get_bits(token.offset, S); //write out the chars as bytes for (int i = 0; i < token.s.length(); ++i) { vector<bool>character = get_bits(token.s[i], 8); chars.insert(chars.end(), character.begin(), character.end()); } output.insert(output.end(), len.begin(), len.end()); output.insert(output.end(), offset.begin(), offset.end()); output.insert(output.end(), chars.begin(), chars.end()); } //if it is a reference, write out //matchlen,offset else { //write out length in L bits len = get_bits(token.length, L); //write out offset in N bits offset = get_bits(token.offset, N); output.insert(output.end(), len.begin(), len.end()); output.insert(output.end(), offset.begin(), offset.end()); } endif: copy(output.begin(), output.end(), out_it); }
ProxyImplDevice::ProxyImplDevice(IBase *pimpl) : ProxyBase<IBase>(pimpl) , frameStatsStream_("frame_stats.txt") , oReleased(true) , disableVBLocks_(false) , currentFrame_(0) { static vector<string> names = boost::assign::list_of("drawUps")("texLocks")("vbLocks")("ibLocks")("shaderConsts")("Total"); std::ostream_iterator<string> out_it (frameStatsStream_, "; "); std::copy(names.begin(), names.end(), out_it); frameStatsStream_ << endl; init(); }
void ProxyImplDevice::dumpFrameStats() { assert(!frameStats_.empty()); size_t n = frameStats_.size(); FrameStats avg; avg.drawUps = std::accumulate(frameStats_.begin(), frameStats_.end(), 0, [](size_t init, FrameStats const &s) { return init + s.drawUps ; }) / n / 1024; avg.texLocks = std::accumulate(frameStats_.begin(), frameStats_.end(), 0, [](size_t init, FrameStats const &s) { return init + s.texLocks ; }) / n / 1024; avg.vbLocks = std::accumulate(frameStats_.begin(), frameStats_.end(), 0, [](size_t init, FrameStats const &s) { return init + s.vbLocks ; }) / n / 1024; avg.ibLocks = std::accumulate(frameStats_.begin(), frameStats_.end(), 0, [](size_t init, FrameStats const &s) { return init + s.ibLocks ; }) / n / 1024; avg.shaderConsts = std::accumulate(frameStats_.begin(), frameStats_.end(), 0, [](size_t init, FrameStats const &s) { return init + s.shaderConsts ; }) / n / 1024; size_t total = avg.drawUps + avg.texLocks + avg.vbLocks + avg.ibLocks + avg.shaderConsts; vector<size_t> values = boost::assign::list_of(avg.drawUps)(avg.texLocks)(avg.vbLocks)(avg.ibLocks)(avg.shaderConsts)(total); std::ostream_iterator<size_t> out_it (frameStatsStream_, "; "); std::copy(values.begin(), values.end(), out_it); frameStatsStream_ << endl; frameStatsStream_.flush(); }
int main() { Solution sol; std::vector<int> candidates = {7, 3, 2, 6}; std::vector<int> targets = {7, 10, 1, 2}; std::cout << "candidates are: "; std::ostream_iterator<int> out_it(std::cout, ", "); std::copy(candidates.begin(), candidates.end(), out_it); std::cout << std::endl << std::endl; for (auto const& target : targets) { std::cout << "target: " << target << std::endl; auto res = sol.combinationSum(candidates, target); for (auto const& val : res) { std::cout << " res: "; std::copy(val.begin(), val.end(), out_it); std::cout << std::endl; } } return 0; }
//------------------------------------------------------------------------------ int main(int argc, char** argv) { if(argc < 6) { std::cerr << "usage: " << argv[0] << " <platform name> <device type = default | cpu | gpu " "| acc | all> <device num> <OpenCL source file path>" " <kernel name>" << std::endl; exit(EXIT_FAILURE); } std::string platformName = argv[ 1 ]; std::string deviceType = argv[2]; int deviceNum = atoi(argv[3]); log_thread_count("\n\nstart"); #ifdef _OPENMP { // const int CHUNKSIZE = 100; // const int N = 1000; // int i, chunk; // float a[N], b[N], c[N]; // for (i=0; i < N; i++) // a[i] = b[i] = i * 1.0; // chunk = CHUNKSIZE; // printf("%d", omp_get_max_threads()); #pragma omp parallel //shared(a,b,c,chunk) private(i) { if(omp_get_thread_num() == 0) { log_thread_count("OMP PARALLEL"); } // printf("%d", omp_get_max_threads()); // #pragma omp for schedule(dynamic,chunk) nowait // for (i=0; i < N; i++) { // c[i] = a[i] + b[i]; // } } #pragma omp barrier } #endif //1)create context cl_context ctx = create_cl_context(platformName, deviceType, deviceNum); std::cout << "OpenCL context created" << std::endl; log_thread_count("CL context created"); //2)load kernel source const std::string programSource = load_text(argv[4]); std::cout << "OpenCL source code loaded" << std::endl; const char* src = programSource.c_str(); const size_t sourceLength = programSource.length(); //3)build program and create kernel cl_int status; cl_program program = clCreateProgramWithSource(ctx, //context 1, //number of strings &src, //source &sourceLength, // size &status); // status check_cl_error(status, "clCreateProgramWithSource"); cl_device_id deviceID; //only a single device was selected // retrieve actual device id from context status = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, sizeof(cl_device_id), &deviceID, 0); check_cl_error(status, "clGetContextInfo"); cl_int buildStatus = clBuildProgram(program, //program 1, //number of devices &deviceID, //array of device ids 0, //program options as passed on //the command line to regualar //compilers e.g. -DUSE_DOUBLE 0, 0); //log output if any char buffer[0x10000] = ""; size_t len = 0; status = clGetProgramBuildInfo(program, deviceID, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len); check_cl_error(status, "clBuildProgramInfo"); if(len > 1) std::cout << "Build output: " << buffer << std::endl; check_cl_error(buildStatus, "clBuildProgram"); std::cout << "Built OpenCL program" << std::endl; const char* kernelName = argv[5]; cl_kernel kernel = clCreateKernel(program, kernelName, &status); check_cl_error(status, "clCreateKernel"); //4)allocate output buffer on OpenCL device typedef float real_t; const size_t ARRAY_LENGTH = 16; const size_t ARRAY_BYTE_LENGTH = ARRAY_LENGTH * sizeof(real_t); cl_mem outputCLBuffer = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, ARRAY_BYTE_LENGTH, 0, &status); check_cl_error(status, "clCreateBuffer"); //5)create command queue cl_command_queue commands = clCreateCommandQueue(ctx, deviceID, 0, &status); check_cl_error(status, "clCreateCommandQueue"); //6)set kernel parameters const real_t value = real_t(3.14); //first parameter: output array status = clSetKernelArg(kernel, //kernel 0, //parameter id sizeof(cl_mem), //size of parameter &outputCLBuffer); //pointer to parameter check_cl_error(status, "clSetKernelArg(0)"); //second parameter: value to assign to each array element status = clSetKernelArg(kernel, //kernel 1, //parameter id sizeof(real_t), //size of parameter &value); //pointer to parameter check_cl_error(status, "clSetKernelArg(1)"); //7)setup kernel launch configuration //total number of threads == number of array elements const size_t globalWorkSize[1] = {ARRAY_LENGTH}; //number of per-workgroup local threads const size_t localWorkSize[1] = {1}; //8)launch kernel status = clEnqueueNDRangeKernel(commands, //queue kernel, //kernel 1, //number of dimensions for work-items 0, //global work offset globalWorkSize, //total number of threads localWorkSize, //threads per workgroup 0, //number of events that need to //complete before kernel executed 0, //list of events that need to complete //before kernel executed 0); //event object identifying this //particular kernel execution instance check_cl_error(status, "clEnqueueNDRangeKernel"); log_thread_count("kernel launched"); std::cout << "Lunched OpenCL kernel - setting all array elements to " << value << std::endl; //9)read back and print results std::vector< real_t > hostArray(ARRAY_LENGTH, real_t(0)); status = clEnqueueReadBuffer(commands, outputCLBuffer, CL_TRUE, //blocking read 0, //offset ARRAY_BYTE_LENGTH, //byte size of data &hostArray[0], //destination buffer in host // memory 0, //number of events that need to //complete before transfer executed 0, //list of events that need to complete //before transfer executed 0); //event identifying this specific operation check_cl_error(status, "clEnqueueReadBuffer"); log_thread_count("device -> host transfer"); std::cout << "Output array: " << std::endl; std::ostream_iterator<real_t> out_it(std::cout, " "); std::copy(hostArray.begin(), hostArray.end(), out_it); std::cout << std::endl; //10)release resources check_cl_error(clReleaseMemObject(outputCLBuffer), "clReleaseMemObject"); check_cl_error(clReleaseCommandQueue(commands),"clReleaseCommandQueue"); check_cl_error(clReleaseKernel(kernel), "clReleaseKernel"); check_cl_error(clReleaseProgram(program), "clReleaseProgram"); check_cl_error(clReleaseContext(ctx), "clReleaseContext"); std::cout << "Released OpenCL resources" << std::endl; log_thread_count("released resources"); print_thread_count(threadlog.begin(), threadlog.end()); std::cout << std::endl; return 0; }