Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 4
0
void ListAsArray::Insert (Object& object)
{
    if (count == array.Length ())
	throw domain_error ("list is full");
    array [count] = &object;
    ++count;
}
Ejemplo n.º 5
0
 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 ();
 }
Ejemplo n.º 6
0
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);
}
Ejemplo n.º 7
0
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;
    }
}
Ejemplo n.º 9
0
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];
}
Ejemplo n.º 10
0
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];
}
Ejemplo n.º 11
0
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];
}
Ejemplo n.º 12
0
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;
}
Ejemplo n.º 13
0
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;
}
Ejemplo n.º 14
0
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;
}
Ejemplo n.º 15
0
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);
}
Ejemplo n.º 16
0
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;
}
Ejemplo n.º 17
0
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;
}
Ejemplo n.º 18
0
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);
}
Ejemplo n.º 19
0
// 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];
}
Ejemplo n.º 20
0
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");
}
Ejemplo n.º 21
0
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;
}
Ejemplo n.º 22
0
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;
}
Ejemplo n.º 23
0
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];
}
Ejemplo n.º 24
0
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;
}
Ejemplo n.º 25
0
// 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];
}
Ejemplo n.º 26
0
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." );
}
Ejemplo n.º 27
0
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;
    }
}
Ejemplo n.º 28
0
 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;
}
Ejemplo n.º 29
0
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));
}
Ejemplo n.º 30
0
    /**
     * 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");
    }