Facebook
From zcx, 2 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 98
  1. void cv_vector_insert_block(cv_vector hwv, size_t pos, cv_iterator begin, cv_iterator end)
  2. {
  3.     if (cv_vector_empty(hwv))
  4.         CV_Error(STS_NULL_PTR, "cv_vector is a null pointer");
  5.     // 判断 pos 是否越界 < 0 || > cv_size
  6.     if (pos < 0 || pos > hwv->cv_size)
  7.         CV_Error(STS_BAD_ARG, "param is bad");
  8.     // 计算新尺寸
  9.     size_t new_size = hwv->cv_size + ((char*)end - (char*)begin) / hwv->elem_size;
  10.     // 计算并调节新容量
  11.     while (hwv->cv_capacity < new_size)
  12.         hwv->cv_capacity <<= EXPANED_VAL;
  13.     adjust_capacity(hwv, hwv->cv_capacity);
  14.     // pos 是不是末尾元素 = cv_size
  15.     if (pos == hwv->cv_size) {
  16.         memcpy((char*)hwv->data + pos * hwv->elem_size, begin, (char*)end - (char*)begin);
  17.     } else {
  18.         cv_iterator src = cv_vector_index_to_iter(hwv, pos);
  19.         size_t bytes = ((char*)end - (char*)begin);
  20.         cv_iterator dst = (char*)src + bytes;
  21.         memmove(dst, src, bytes);
  22.         memcpy(src, begin, bytes);
  23.     }
  24.     // 更新尺寸
  25.     hwv->cv_size = new_size;
  26. }