| 1 |
/* BaitFisher (version 1.2.7) a program for designing DNA target enrichment baits |
| 2 |
* Copyright 2013-2016 by Christoph Mayer |
| 3 |
* |
| 4 |
* This source file is part of the BaitFisher-package. |
| 5 |
* |
| 6 |
* This program is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation, either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with BaitFisher. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* |
| 20 |
* For any enquiries send an Email to Christoph Mayer |
| 21 |
* [email protected] |
| 22 |
* |
| 23 |
* When publishing work that is based on the results please cite: |
| 24 |
* Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design |
| 25 |
* |
| 26 |
*/ |
| 27 |
//----------------------------------------------------------------------------- |
| 28 |
// Author: Christoph Mayer |
| 29 |
// Gropiusweg 13 |
| 30 |
// 44801 Bochum |
| 31 |
// |
| 32 |
// Copyright: Christoph Mayer |
| 33 |
// |
| 34 |
// Description: |
| 35 |
// A fast vector class for objects which do not require their constructors |
| 36 |
// or destructors to be called. |
| 37 |
// This class uses malloc, realloc and free to allocate and |
| 38 |
// free dynamic memory for the "vector". Especially the efficient |
| 39 |
// realloc has no C++ equivalent - the major drawback of new and |
| 40 |
// delete. |
| 41 |
// Disadvantage: No constructors and destructors are called for |
| 42 |
// objects in the vector. This limits is applicability. |
| 43 |
// The fastvector-nd.h uses new and delete and does not use |
| 44 |
// memcpy. It is therefore well suited for all classes. |
| 45 |
// Unfortunately, its less efficient. |
| 46 |
// |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#ifndef FASTVECTOR_H |
| 51 |
#define FASTVECTOR_H |
| 52 |
|
| 53 |
#include <iostream> |
| 54 |
#include <cstdlib> |
| 55 |
#include <cstring> |
| 56 |
#include <cctype> |
| 57 |
// #include <iomanip> |
| 58 |
|
| 59 |
#define _MINBUFFER_CAPACITY 4 |
| 60 |
|
| 61 |
template<typename T> |
| 62 |
class fastvector |
| 63 |
{ |
| 64 |
// Possibly even more efficient: |
| 65 |
// Use pointer _end, _capacity_end instead of _len, _capacity |
| 66 |
|
| 67 |
|
| 68 |
private: |
| 69 |
T *_buf; |
| 70 |
unsigned _len; |
| 71 |
unsigned _capacity; // Capacity of container, i.e number of bytes availible |
| 72 |
// for writing. If a capacity is requested by the user, |
| 73 |
// an additional byte is reserved in order to be able to append |
| 74 |
// the additional \0 which is appended by some function calls. |
| 75 |
// Consequently, the capacity reported to user is _capacity-1. |
| 76 |
protected: |
| 77 |
|
| 78 |
|
| 79 |
public: |
| 80 |
~fastvector () |
| 81 |
{ |
| 82 |
if (_buf) |
| 83 |
// delete [] _buf; |
| 84 |
std::free(_buf); |
| 85 |
} |
| 86 |
|
| 87 |
fastvector ():_buf(NULL), _len(0), _capacity(0) |
| 88 |
{ |
| 89 |
} |
| 90 |
|
| 91 |
fastvector (const T *v, unsigned len) |
| 92 |
{ |
| 93 |
// std::cout << "New fastvector 2" << std::endl; |
| 94 |
_len = len; |
| 95 |
_capacity = len; |
| 96 |
|
| 97 |
if (len) |
| 98 |
{ |
| 99 |
_buf = (T *) malloc(_capacity*sizeof(T)); |
| 100 |
//_buf = new T [_capacity]; |
| 101 |
memcpy(_buf, v, len*sizeof(T)); |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
_buf = NULL; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
fastvector (const T *v_begin, const T *v_end) |
| 110 |
{ |
| 111 |
// std::cout << "New fastvector 3" << std::endl; |
| 112 |
_len = (v_end - v_begin); |
| 113 |
_capacity = _len; |
| 114 |
|
| 115 |
if (_len) |
| 116 |
{ |
| 117 |
_buf = (T *)malloc(_capacity*sizeof(T)); |
| 118 |
// _buf = new T [_capacity]; |
| 119 |
memcpy(_buf, v_begin, _len*sizeof(T)); |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
_buf = NULL; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
fastvector (const fastvector &a):_len(a._len),_capacity(a._len) |
| 128 |
{ |
| 129 |
if (_len) |
| 130 |
{ |
| 131 |
_buf = (T *)malloc(_capacity*sizeof(T)); |
| 132 |
memcpy(_buf, a._buf, _len*sizeof(T)); |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
_buf = NULL; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
void push_back(const T &c) |
| 142 |
{ |
| 143 |
if (_capacity == _len) |
| 144 |
{ |
| 145 |
if (_buf == NULL) |
| 146 |
{ |
| 147 |
_capacity = _MINBUFFER_CAPACITY; |
| 148 |
_buf = (T *)malloc(_capacity*sizeof(T)); |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
T *tmp; |
| 153 |
|
| 154 |
_capacity <<= 1; |
| 155 |
tmp = (T *)realloc(_buf, _capacity*sizeof(T)); |
| 156 |
|
| 157 |
if (tmp == NULL) |
| 158 |
{ |
| 159 |
std::cout << "Program aborded due to failed realloc!" << std::endl; |
| 160 |
exit(0); |
| 161 |
} |
| 162 |
_buf = tmp; |
| 163 |
} |
| 164 |
} |
| 165 |
_buf[_len] = c; |
| 166 |
++_len; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
T *begin() const |
| 171 |
{ |
| 172 |
return _buf; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
T *end() const |
| 177 |
{ |
| 178 |
return _buf+_len; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
T *rbegin() const |
| 183 |
{ |
| 184 |
return _buf + (_len - 1); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
T *rend() const |
| 189 |
{ |
| 190 |
return _buf - 1; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
void assign(const T *v, int len) |
| 195 |
{ |
| 196 |
_len = len; |
| 197 |
reserve(len); |
| 198 |
memcpy(_buf, v, len*sizeof(T)); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
void assign(const T *v_begin, const T *v_end) |
| 203 |
{ |
| 204 |
_len = v_end - v_begin; |
| 205 |
reserve(_len); |
| 206 |
memcpy(_buf, v_begin, _len*sizeof(T)); |
| 207 |
} |
| 208 |
|
| 209 |
void assign(const fastvector &a) |
| 210 |
{ |
| 211 |
_len = a._len; |
| 212 |
reserve(_len); |
| 213 |
memcpy(_buf, a._buf, _len*sizeof(T)); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
void reserve(unsigned s) |
| 218 |
{ |
| 219 |
// std::cout << "reserve" << std::endl; |
| 220 |
if (_capacity < s) |
| 221 |
{ |
| 222 |
if (_buf == NULL) |
| 223 |
{ |
| 224 |
//_buf = new T [s]; |
| 225 |
_buf = (T *)malloc(s*sizeof(T)); |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
T *tmp; |
| 230 |
|
| 231 |
tmp = (T *)realloc(_buf, s*sizeof(T)); |
| 232 |
if (tmp == NULL) |
| 233 |
{ |
| 234 |
std::cout << "Program aborded due to failed realloc!" << std::endl; |
| 235 |
exit(0); |
| 236 |
} |
| 237 |
_buf = tmp; |
| 238 |
} |
| 239 |
_capacity = s; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
bool empty() const |
| 244 |
{ |
| 245 |
return _len == 0; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
unsigned size() const |
| 250 |
{ |
| 251 |
return _len; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
unsigned capacity() const |
| 256 |
{ |
| 257 |
return _capacity; // One char is reserved for the \0 at end of string |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
void clear() |
| 262 |
{ |
| 263 |
// std::cout << "clear" << std::endl; |
| 264 |
_len = 0; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
void reset() |
| 269 |
{ |
| 270 |
// std::cout << "reset" << std::endl; |
| 271 |
if (_buf) |
| 272 |
{ |
| 273 |
// delete [] _buf; |
| 274 |
free(_buf); |
| 275 |
_buf = NULL; |
| 276 |
_capacity = 0; |
| 277 |
_len = 0; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
bool check_pos(unsigned pos) |
| 283 |
{ |
| 284 |
return (pos < _len); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
void set_unckecked(unsigned pos, const T &c) |
| 289 |
{ |
| 290 |
_buf[pos] = c; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
bool set(unsigned pos, const T &c) |
| 295 |
{ |
| 296 |
if ( check_pos(pos) ) |
| 297 |
{ |
| 298 |
_buf[pos] = c; |
| 299 |
return true; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
return false; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
void get_unckecked(unsigned pos, T &c) |
| 309 |
{ |
| 310 |
c = _buf[pos]; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
T get_unckecked(unsigned pos) |
| 315 |
{ |
| 316 |
return _buf[pos]; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
bool get(unsigned pos, T &c) |
| 322 |
{ |
| 323 |
if ( check_pos(pos) ) |
| 324 |
{ |
| 325 |
c = _buf[pos]; |
| 326 |
return true; |
| 327 |
} |
| 328 |
else |
| 329 |
{ |
| 330 |
return false; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
fastvector &operator=(const fastvector &a) |
| 335 |
{ |
| 336 |
_len = a._len; |
| 337 |
if (_len) |
| 338 |
{ |
| 339 |
reserve(_len); |
| 340 |
memcpy(_buf, a._buf, _len*sizeof(T)); |
| 341 |
} |
| 342 |
return *this; |
| 343 |
} |
| 344 |
|
| 345 |
void swap(fastvector &a) |
| 346 |
{ |
| 347 |
T *_tmpbuf = _buf; _buf = a._buf; a._buf = _tmpbuf; |
| 348 |
unsigned _tmplen = _len; _len = a._len; a._len = _tmplen; |
| 349 |
unsigned _tmpcapacity = _capacity; _capacity = a._capacity; a._capacity = _tmpcapacity; |
| 350 |
} |
| 351 |
|
| 352 |
}; |
| 353 |
|
| 354 |
|
| 355 |
#endif |