Esempio n. 1
0
 String<Ch,Tr,Capacity>& String<Ch,Tr,Capacity>::operator+=(const Ch* x) {
     size_t n = Tr::length(x);
     if(n + sz > Capacity)
         throw length_error("String::operator+=");
     Tr::copy(s + sz, x, n);
     sz += n;
     s[sz] = 0;
 }
Esempio n. 2
0
 int Pop() {
     if (Empty()) {
         throw length_error("Pop(): empty stack");
     }
     int pop_element = element_with_cached_max_.top().first;
     element_with_cached_max_.pop();
     return pop_element;
 }
 int dequeue() {
   if (!count_) {
     throw length_error("empty queue");
   }
   --count_;
   int ret = data_[head_];
   head_ = (head_ + 1) % data_.size();
   return ret;
 }
 int max() const {
   if (!A_.empty()) {
     return B_.empty() ? A_.max() : std::max(A_.max(), B_.max());
   } else {  // A_.empty() == true.
     if (!B_.empty()) {
       return B_.max();
     }
     throw length_error("empty queue");
   }
 }
 int dequeue() {
   if (B_.empty()) {
     while (!A_.empty()) {
       B_.push(A_.pop());
     }
   }
   if (!B_.empty()) {
     return B_.pop();
   }
   throw length_error("empty queue");
 }
 T pop() {
   if (empty()) {
     throw length_error("empty stack");
   }
   T ret = s_.top();
   s_.pop();
   if (ret == aux_.top().first) {
     --aux_.top().second;
     if (aux_.top().second == 0) {
       aux_.pop();
     }
   }
   return ret;
 }
 int Pop() {
   if (Empty()) {
     throw length_error("Pop(): empty stack");
   }
   int pop_element = element_.top();
   element_.pop();
   const int kCurrentMax = cached_max_with_count_.top().first;
   if (pop_element == kCurrentMax) {
     int& max_frequency = cached_max_with_count_.top.second;
     --max_frequency;
     if (max_frequency == 0) {
       cached_max_with_count_.pop();
     }
   }
   return pop_element;
 }
// @include
const BinaryTreeNode<int>* FindKthNodeBinaryTree(
    const unique_ptr<BinaryTreeNode<int>>& root, int k) {
  const auto* n = root.get();
  while (n) {
    int left_size = n->left ? n->left->size : 0;
    if (left_size < k - 1) {
      k -= (left_size + 1);
      n = n->right.get();
    } else if (left_size == k - 1) {
      return n;
    } else {  // left_size > k - 1.
      n = n->left.get();
    }
  }
  throw length_error("no k-th node in binary tree");
}
// @include
int find_k_th_largest(vector<int> A, int k) {
  int left = 0, right = A.size() - 1;

  while (left <= right) {
    default_random_engine gen((random_device())());
    // Generates random int in [left, right].
    uniform_int_distribution<int> dis(left, right);
    int p = partition(left, right, dis(gen), &A);
    if (p == k - 1) {
      return A[p];
    } else if (p > k - 1) {
      right = p - 1;
    } else {  // p < k - 1.
      left = p + 1;
    }
  }
  // @exclude
  throw length_error("no k-th node in array A");
  // @include
}
Esempio n. 10
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");
    }
Esempio n. 11
0
 int Max() const {
     if (!Empty()) {
         return element_with_cached_max_.top().second;
     }
     throw length_error("Max(): empty stack");
 }
Esempio n. 12
0
 String<Ch,Tr,Capacity>& String<Ch,Tr,Capacity>::operator+=(Ch c) {
     if(sz == Capacity)
         throw length_error("String::operator+=");
     s[sz++] = c;
     s[sz] = 0;
 }
 const T& max() const {
   if (!empty()) {
     return aux_.top().first;
   }
   throw length_error("empty stack");
 }
 int Max() const {
   if (!Empty()) {
     return cached_max_with_count_.top().first;
   }
   throw length_error("Max(): empty stack");
 }