insert \p n uninitialized elements before \p i 'th element. * * You must not! use realloc, memcpy or memmove, because some data element points inside itself, and therefore you * always need to copy all elements by hand. */
| 118 | * always need to copy all elements by hand. |
| 119 | */ |
| 120 | void insert(int i, int n) |
| 121 | { |
| 122 | assert(i <= num); |
| 123 | assert(num >= 0); |
| 124 | if (n > 0) |
| 125 | { |
| 126 | T* newdata = 0; |
| 127 | int k; |
| 128 | |
| 129 | spx_alloc(newdata, num + n); |
| 130 | assert(newdata != 0); |
| 131 | |
| 132 | // copy front segment to new array |
| 133 | for( k = 0; k < i; ++k ) |
| 134 | { |
| 135 | new (&(newdata[k])) T(); |
| 136 | newdata[k] = data[k]; |
| 137 | data[k].~T(); |
| 138 | } |
| 139 | |
| 140 | // call constructor for new elements |
| 141 | for( ; k < i+n; ++k ) |
| 142 | new (&(newdata[k])) T(); |
| 143 | |
| 144 | // copy rear segment to new array |
| 145 | for( k = i; k < num; ++k ) |
| 146 | { |
| 147 | new (&(newdata[n + k])) T(); |
| 148 | newdata[n + k] = data[k]; |
| 149 | data[k].~T(); |
| 150 | } |
| 151 | |
| 152 | if( data ) |
| 153 | spx_free(data); |
| 154 | data = newdata; |
| 155 | num += n; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /// insert \p n elements from \p p_array before \p i 'th element. |
| 160 | void insert(int i, int n, const T* p_array) |
no test coverage detected