Resources JSONFormatter::parse( const Bytes& value ) { auto json = json::parse( String::to_string( value ) ); if ( json.count( "data" ) == 0 ) { throw domain_error( "Root property must be named 'data'." ); } auto data = json[ "data" ]; Resources resources; if ( data.is_array( ) ) { for ( const auto object : data ) { if ( not object.is_object( ) ) { throw domain_error( "Root array child elements must be objects." ); } resources.push_back( parse_object( object ) ); } } else if ( data.is_object( ) ) { resources.push_back( parse_object( data ) ); } else { throw domain_error( "Root property must be unordered set or object." ); } return resources; }
Point read_point(const Json& json) { if (json.is_array()) { auto coords = json.array_items(); return Point(coords[0].number_value(), coords[1].number_value()); } throw domain_error("Invalid JSON"); }
size_t XmlRpcFunction::parameterCount (size_t synopsis_index) { XmlRpcValue func_synop = mSynopsis.arrayGetItem(synopsis_index); size_t size = func_synop.arraySize(); if (size < 1) throw domain_error("Synopsis contained no items"); return size - 1; }
void ListAsArray::Insert (Object& object) { if (count == array.Length ()) throw domain_error ("list is full"); array [count] = &object; ++count; }
rawmem_idevice (const context& ctx, unsigned image_count = 1) : idevice (ctx), image_count_(image_count) { if (context::unknown_size == ctx_.width ()) { BOOST_THROW_EXCEPTION (domain_error ("cannot handle unknown sizes")); } reset (); }
double grade(double mid_exam, double final_exam, const vector<double>& homework) { if (homework.size() == 0) { throw domain_error("student has no homework."); } double hw_grade = median(homework); return grade(mid_exam, final_exam, hw_grade); }
double angle(const std::vector<Point>& pg){ if(pg.size() < 3) throw domain_error("Can't compute angle with less than 3 points"); else{ vector<Point>::size_type i = pg.size()-1; return angle(pg[i-2],pg[i-1],pg[i]); } }
/** * Reduces the probability of the cell being occupied in the grid with given x * and y indices. Probability of the cell will be decreased by 2, only if * the probability is not already set to the lowest possible value of 1. * * @param x The x index of the cell which will have its probability decreased * @param y The y index of the cell which will have its probability decreased * @throws The index out of bounds domain error will be thrown if one of the * indices will indicate a cell outside of the 50x50 grid */ void Grid::decreaseProbability(int x, int y) { if (x < 0 || x >= COLS_NUMBER || y < 0 || y >= ROWS_NUMBER) { throw domain_error("Index out of bounds!"); } if (rawGrid[x][y] > MIN_PROBABILITY) { rawGrid[x][y] -= 2; } }
double median(vector<double> vec){ typedef vector<double>::size_type vec_size; vec_size size = vec.size(); if(size == 0){ throw domain_error("The size is zero"); } sort(vec.begin(), vec.end()); return size%2 == 0 ? (vec[mid]+vec[mid-1])/2 : vec[mid]; }
T median(vector<T> v) { typedef typename vector<T>::size_type vec_sz; vec_sz size = v.size(); if (size == 0) throw domain_error("median of an empty vector"); sort(v.begin(), v.end()); vec_sz mid = size/2; return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; }
double median(vector<double> vec) { typedef vector<double>::size_type vz; vz size = vec.size(); if (size == 0) { throw domain_error("median of an empty vector"); } sort(vec.begin(), vec.end()); vz mid = size / 2; return size % 2 == 0 ? (vec[mid - 1] + vec[mid]) / 2 : vec[mid]; }
int nrand(const int n) { if (n <= 0 || n > RAND_MAX) { throw domain_error("Argument to nrand is out of range."); } time_t t = time(nullptr); srand(t); const int r = rand() % n; cout << "r = " << r << endl; return r; }
double median(vector<double> v){ typedef vector<double>::size_type vec_sz; sort(v.begin(), v.end()); vec_sz sz=v.size(); if(sz==0) throw domain_error("Median of an empty vector"); vec_sz mid=sz/2; return sz%2?v[mid]:(v[mid]+v[mid-1])/2; }
static double getFloatArg( const char * arg ) { char * endptr; double x = strtod( arg, &endptr ); if ( endptr == arg ) { cout << "Error processing argument: " << arg << endl; throw domain_error( "bad argument" ); } return x; }
T median (In begin, In end) { int size = end - begin; if (size == 0) throw domain_error("median of an empty vector"); sort (begin, end); int mid = size / 2; return size % 2 == 0 ? (*(begin+mid) + *(begin+mid-1)) / 2 : *(begin+mid); }
void ListAsArray::Withdraw (Position const& arg) { Pos const& position = dynamic_cast<Pos const&> (arg); if (count == 0) throw domain_error ("list is empty"); if (&position.list != this || position.offset >= count) throw invalid_argument ("invalid position"); for (unsigned int i = position.offset; i < count-1U; ++i) array [i] = array [i + 1]; --count; }
int nrand(int n) { if(n <= 0 || n > RAND_MAX) throw domain_error("Argument to nrand is out of range"); const int bucket_size = RAND_MAX/n; int r; do r = rand()/bucket_size; while(r >= n); return r; }
T median(In begin,In end) { typedef typename vector<T>::iterator vec_sz; char size = end - begin; if(begin == end) throw domain_error("median of an empty vector"); sort(begin, end); char mid = size / 2;; return size % 2 == 0 ? (*(begin + mid) + *(begin + mid - 1)) / 2 :*(begin + mid); }
// NOTE THAT THE VECTOR PASSED HERE IS PASSED *BY COPY*! And we want that bc we sort! double median(vector<double> vec) { typedef vector<double>::size_type vec_sz; vec_sz count = vec.size(); if(count == 0) throw domain_error("median of an empty vector!"); sort(vec.begin(), vec.end()); vec_sz middle = count / 2; return count % 2 == 0 ? (vec[middle - 1] + vec[middle]) / 2 : vec[middle]; }
vector<Point> read_points(const string& path) { if (ifstream infile{path}) { const string contents{std::istreambuf_iterator<char>(infile), {}}; string error; auto json = Json::parse(contents, error); auto items = json.array_items(); vector<Point> result; transform(items.begin(), items.end(), back_inserter(result), read_point); return result; } throw domain_error("Invalid JSON"); }
void ListAsArray::Withdraw (Object& object) { if (count == 0) throw domain_error ("list is empty"); unsigned int i = 0; while (i < count && array [i] != &object) ++i; if (i == count) throw invalid_argument ("object not found"); for ( ; i < count - 1U; ++i) array [i] = array [i + 1]; --count; }
double median(vector<double> vec){ typedef vector<double>::size_type vec_sz; vec_sz sz=vec.size(); if(sz==0){ throw domain_error("Enter your grades"); } sort(vec.begin(), vec.end()); vec_sz mid=sz/2; if(sz%2) return vec[mid]; return (vec[mid]+vec[mid-1])/2; }
double median(vector<double> vec) { typedef vector<double>::size_type vec_sz; vec_sz size = vec.size(); if (size == 0) throw domain_error("vector is empty"); sort(vec.begin(),vec.end()); vec_sz mid = size / 2; return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid]; }
Resource JSONFormatter::parse_object( const json& value ) const { Resource object; for ( auto iterator = value.begin( ); iterator not_eq value.end( ); iterator++ ) { if ( iterator->is_object( ) ) { throw domain_error( "Child objects are not supported." ); } else if ( iterator->is_array( ) ) { for ( const auto& item : *iterator ) { if ( item.is_object( ) ) { throw domain_error( "Child objects are not supported." ); } else if ( item.is_array( ) ) { throw domain_error( "Child unordered sets are not supported." ); } auto value = to_string( item ); object.insert( make_pair( iterator.key( ), String::to_bytes( value ) ) ); } } else { auto value = to_string( *iterator ); object.insert( make_pair( iterator.key( ), String::to_bytes( value ) ) ); } } return object; }
// compute the median of a `vector<double>' // note that calling this function copies the entire argument `vector' double median(vector<double> vec) { #ifdef _MSC_VER typedef std::vector<double>::size_type vec_sz; #else typedef vector<double>::size_type vec_sz; #endif vec_sz size = vec.size(); if (size == 0) throw domain_error("median of an empty vector"); sort(vec.begin(), vec.end()); vec_sz mid = size/2; return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid]; }
string JSONFormatter::to_string( const json& value ) const { if ( value.is_string( ) ) { return value.get< string >( ); } else if ( value.is_boolean( ) ) { return ( value.get< bool >( ) == true ) ? "true" : "false"; } else if ( value.is_number_integer( ) ) { return ::to_string( value.get< long >( ) ); } else if ( value.is_number_float( ) ) { return ::to_string( value.get< double >( ) ); } throw domain_error( "Data-type not supported." ); }
int nrand(int n) { if(n<=0) throw domain_error("argument to nrand is out of range"); if(n<=RAND_MAX) { int bucket_size=RAND_MAX/n; int r; do r=rand()/bucket_size; while(r>=n); return r; } else { int rate=n/RAND_MAX; int r; do r=nrand(rate)*RAND_MAX + rand(); while(r>=n); return r; } }
void ListAsArray::InsertBefore ( Position const& arg, Object& object) { Pos const& position = dynamic_cast<Pos const&> (arg); #ifdef DPRINT std:: cerr<< " obj "<< object<<" insertBefore: pos.offset = " << position.offset <<std::endl; #endif if (count == array.Length ()) throw domain_error ("list is full"); if (&position.list != this || position.offset >= count+1 ) throw invalid_argument ("invalid position"); unsigned int insertPosition = position.offset ; for (unsigned int i = count; i > insertPosition; --i) array [i] = array [i - 1U]; array [insertPosition] = &object; ++count; }
double grade(double m, double f, const vector<double>& hw){ if(hw.size() == 0){ throw domain_error("no homework"); } return grade(m, f, median(hw)); }
/** * Rethrow an exception of type specified by the dynamic type of * the specified exception, adding the specified line number to * the specified exception's message. * * @param[in] e original exception * @param[in] line line number in Stan source program where * exception originated * @param[in] reader trace of how program was included from files */ inline void rethrow_located(const std::exception& e, int line, const io::program_reader& reader = stan::io::program_reader()) { using std::bad_alloc; // -> exception using std::bad_cast; // -> exception using std::bad_exception; // -> exception using std::bad_typeid; // -> exception using std::ios_base; // ::failure -> exception using std::domain_error; // -> logic_error using std::invalid_argument; // -> logic_error using std::length_error; // -> logic_error using std::out_of_range; // -> logic_error using std::logic_error; // -> exception using std::overflow_error; // -> runtime_error using std::range_error; // -> runtime_error using std::underflow_error; // -> runtime_error using std::runtime_error; // -> exception using std::exception; // create message with trace of includes and location of error std::stringstream o; o << "Exception: " << e.what(); if (line < 1) { o << " Found before start of program."; } else { io::program_reader::trace_t tr = reader.trace(line); o << " (in '" << tr[tr.size() - 1].first << "' at line " << tr[tr.size() - 1].second; for (int i = tr.size() - 1; --i >= 0; ) o << "; included from '" << tr[i].first << "' at line " << tr[i].second; o << ")" << std::endl; } std::string s = o.str(); if (is_type<bad_alloc>(e)) throw located_exception<bad_alloc>(s, "bad_alloc"); if (is_type<bad_cast>(e)) throw located_exception<bad_cast>(s, "bad_cast"); if (is_type<bad_exception>(e)) throw located_exception<bad_exception>(s, "bad_exception"); if (is_type<bad_typeid>(e)) throw located_exception<bad_typeid>(s, "bad_typeid"); if (is_type<domain_error>(e)) throw domain_error(s); if (is_type<invalid_argument>(e)) throw invalid_argument(s); if (is_type<length_error>(e)) throw length_error(s); if (is_type<out_of_range>(e)) throw out_of_range(s); if (is_type<logic_error>(e)) throw logic_error(s); if (is_type<overflow_error>(e)) throw overflow_error(s); if (is_type<range_error>(e)) throw range_error(s); if (is_type<underflow_error>(e)) throw underflow_error(s); if (is_type<runtime_error>(e)) throw runtime_error(s); throw located_exception<exception>(s, "unknown original type"); }