typename boost::enable_if<boost::is_base_of<T, U>, U&>::type emplace_back(Args&&... args) { // the size of the type rounded up to pointer alignment const int object_size = (sizeof(U) + sizeof(*m_storage) - 1) / sizeof(*m_storage); // +1 for the length prefix if (m_size + object_size + header_size > m_capacity) grow_capacity(object_size); uintptr_t* ptr = m_storage + m_size; // length prefix header_t* hdr = reinterpret_cast<header_t*>(ptr); hdr->len = object_size; hdr->move = &move<U>; ptr += header_size; // construct in-place new (ptr) U(std::forward<Args>(args)...); // if we constructed the object without throwing any exception // update counters to indicate the new item is in there ++m_num_items; m_size += header_size + object_size; return *reinterpret_cast<U*>(ptr); }
/** * 申请数组空间 * @param v 动态数组 * @param capacity 要申请的元素数量 * @return 空间分配成功与否 */ bool vector_reserve(vector_t *v, vector_size_t capacity) { if (v) { if (capacity > v->capacity) { v->capacity = grow_capacity(capacity); v->data = realloc(v->data, v->len * v->capacity); return v->data; } return true; } return false; }
void mp_int<A,T>::pow2(typename mp_int<A,T>::size_type b) { grow_capacity(b / digit_bits + 1); // set size_ to where the bit will go size_ = b / digit_bits + 1; // set all bits to zero std::memset(digits_, 0, size_ * sizeof(digit_type)); // put the single bit in its place digits_[b / digit_bits] = digit_type(1) << (b % digit_bits); }