double bisection(double (*func)(double), double x_l , double x_r){ //make sure x's always bracket root if(bracketed((*func)(x_l), (*func)(x_r))){ //return when four digits or precision are recieved if(fabs(x_r - x_l) < (1/pow(10.0,4.0))){return (x_l + x_r)/2.0; } double x_n = (x_r + x_l)/2.0; //reccur if(bracketed( (*func)(x_l), (*func)(x_n) )){ bisection((*func), x_l, x_n); }else{ bisection((*func), x_n, x_r); } }else{ printf("Something went wrong no longer bracketed\n"); return 0.0; } }
void gen_aux(const Grammar& g, const std::string& word, std::list<std::string>& ret) { if(!bracketed(word)) { ret.push_back(word); } else { Grammar::const_iterator it = g.find(word); if(it == g.end()) throw std::logic_error("empty rule"); const Rule_collection& c = it->second; const Rule& r = c[nrand(c.size())]; for(Rule::const_iterator i = r.begin(); i != r.end(); ++i) gen_aux(g, *i, ret); } }
void gen_aux (const Grammar &g, const std::string &start, std::vector<std::string> &result) { TokenStack lifo; expand_aux (g, start, lifo); while (!lifo.empty()) { const std::string curr_word = lifo.top(); lifo.pop(); if (!bracketed (curr_word)) { result.push_back (curr_word); } else { expand_aux (g, curr_word, lifo); } } }
void gen_aux (const Grammar &g, const std::string &word, std::vector<std::string> &result) { if (!bracketed (word)) { result.push_back (word); } else { Grammar::const_iterator it = g.find (word); if (g.end() == it) throw std::logic_error ("empty grammar rule"); const RuleCollection &rc = it->second; const Rule &r = rc[nrand(rc.size())]; for (Rule::const_iterator r_it = r.begin(); r_it != r.end(); ++r_it) gen_aux (g, *r_it, result); } }
void gen_aux(const Grammar& g, const std::string& word, std::vector<std::string>& ret) { if (!bracketed(word)) { ret.push_back(word); } else { Grammar::const_iterator it = g.find(word); if (it == g.end()) { std::logic_error("Rule is empty"); } // get rule collection from grammar const Rule_collection& c = it->second; // select a new rule from collection at random const Rule& r = c[nrand(c.size())]; // expand rule for (Rule::const_iterator i = r.begin(); i != r.end(); ++i) { gen_aux(g, *i, ret); } } }
static Bitmap read_bmp_or_throw(const FilePath& filePath){ BinaryReader in(filePath); if (!in.good()){ throw ReadBmpError(error_open_file_read(filePath)); } auto bitmapFileHeader = read_struct_or_throw_bmp<BitmapFileHeader>(in); if (bitmapFileHeader.fileType != BITMAP_SIGNATURE){ throw ReadBmpError(error_bitmap_signature(bitmapFileHeader.fileType)); } auto bitmapInfoHeader = read_struct_or_throw_bmp<BitmapInfoHeader>(in); if (invalid_header_length(bitmapInfoHeader.headerLen)){ throw ReadBmpError(error_truncated_bmp_header(bitmapInfoHeader.headerLen)); } if (bitmapInfoHeader.compression != Compression::BI_RGB){ throw ReadBmpError(error_compression(bitmapInfoHeader.compression)); } if (bitmapInfoHeader.colorPlanes != 1){ throw ReadBmpError(error_color_planes(bitmapInfoHeader.colorPlanes)); } auto bmpSize = get_size_and_order(bitmapInfoHeader); if (bitmapInfoHeader.paletteColors != 0){ if (bitmapInfoHeader.bitsPerPixel != 8){ // Fixme: Maybe support? throw ReadBmpError("Palette for non 8-bpp bitmaps unsupported by Faint."); } auto colorList = or_throw(read_color_table(in, bitmapInfoHeader.paletteColors), "Failed reading color table."); in.seekg(bitmapFileHeader.dataOffset); auto alphaMap = or_throw(read_8bipp_BI_RGB(in, bmpSize), "Failed reading 8-bits-per-pixel data"); return bitmap_from_indexed_colors(alphaMap, colorList); } else{ // No palette in.seekg(bitmapFileHeader.dataOffset); if (bitmapInfoHeader.bitsPerPixel == 8){ auto alphaMap = or_throw(read_8bipp_BI_RGB(in, bmpSize), "Failed reading 8-bits-per-pixel data"); return bitmap_from_indexed_colors(alphaMap, grayscale_color_table()); } else if (bitmapInfoHeader.bitsPerPixel == 24){ return or_throw(read_24bipp_BI_RGB(in, bmpSize), "Failed reading 24-bits-per-pixel data."); } else if (bitmapInfoHeader.bitsPerPixel == 32){ return or_throw(read_32bipp_BI_RGB(in, bmpSize), "Failed reading 32-bits-per-pixel data."); } } throw ReadBmpError(Sentence("Unsupported bits-per-pixel", bracketed(str_int(bitmapInfoHeader.bitsPerPixel)))); }