vector.h 19.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
#ifndef GIAC_VECTOR_H
#define GIAC_VECTOR_H
// Simple vector implementation for giac
// Incomplete (allocation is handled by new/delete)
// compatible with alias_ref_vecteur and without all VISUALC++ checks
#include <string.h>
// #include <iostream>
// define IMMEDIATE_VECTOR if you want to use imvector
#define immvector_max 1 << 30 

namespace std {

  // inline void swapptr(void * & a,void * & b){ register void * c=a; a=b; b=c; }
  inline unsigned _abs(int i){ return i>=0?(i==immvector_max?0:i):-i;}
  inline int nextpow2(int n){
    if (n>=16){
      return n>=64?n:(n>32?64:32);
    }
    else {
      return n>8?16:(n>4?8:4);
    }
  }

  template<class _Tp> class giac_reverse_pointer {
    _Tp * ptr;
  public:
  giac_reverse_pointer(): ptr(0) {}
  giac_reverse_pointer(_Tp *p): ptr(p) {}
  giac_reverse_pointer(const giac_reverse_pointer & p): ptr(p.ptr) {}
    giac_reverse_pointer operator +(int n){ return ptr-n; }
    giac_reverse_pointer operator -(int n){ return ptr+n; }
    giac_reverse_pointer & operator ++(){ --ptr; return *this; }
    giac_reverse_pointer & operator --(){ ++ptr; return *this; }
    _Tp & operator *() { return *ptr; }
    const _Tp operator *() const { return *ptr; }
    bool operator ==(const giac_reverse_pointer & other) const { return ptr==other.ptr; }
    bool operator !=(const giac_reverse_pointer & other) const { return ptr!=other.ptr; }
    int operator -(const giac_reverse_pointer & other) const { return int(other.ptr-ptr); }
  };

  // define IMMEDIATE_VECTOR to an integer>=1 for non-dynamical small vectors
#if defined(IMMEDIATE_VECTOR) 

#define _begin_immediate_vect _ptr[0]
#define _endalloc_immediate_vect _ptr[1]
  template<typename _Tp> class imvector{
    // private members
    int _taille; // <=0 for immediate, >0 for allocated, immvector_max for empty allocated
    union {
      _Tp *_ptr[2]; // ptr[0]==begin_immediate_vect, ptr[1]==endalloc_immediate_vect
      int _tab[IMMEDIATE_VECTOR];
    };
    // private allocation methods
    void _zero_tab(){
      for (unsigned i=0;i<IMMEDIATE_VECTOR;i++) 
	_tab[i]=0; 
    }
    void _free_tab(){
      for (unsigned i=0;i<(sizeof(int)*IMMEDIATE_VECTOR)/sizeof(_Tp);++i){
	_Tp * Tptr = (_Tp *) _tab;
	*(Tptr+i)=_Tp();
      }
    }
    void _destroy(){
      if (_taille>0){ 
	if (_begin_immediate_vect) {
	  // std::cerr << "delete " << _taille << endl;
	  delete [] _begin_immediate_vect; 
	}
      }
      else
	_free_tab();
    }
    void _realloc(unsigned n){
      if (n<=(sizeof(int)*IMMEDIATE_VECTOR)/sizeof(_Tp)){
	if (_taille!=immvector_max){
	  for (int i=n;i<_taille;i++){
	    *(_begin_immediate_vect+i)=_Tp();
	  }
	}
	return;
      }
      if (_taille<=0){ // dyn alloc the vector
	_taille=(_taille?-_taille:immvector_max);
	n=nextpow2(n);
	_Tp * _newbegin = new _Tp[n];
	if (_taille<immvector_max){
	  for (int i=0;i<_taille;++i){
	    _newbegin[i] = ((_Tp *)_tab)[i];
	  }
	}
	_free_tab();
	_begin_immediate_vect=_newbegin;
	_endalloc_immediate_vect=_begin_immediate_vect+n;
	return;
      }
      if ( _endalloc_immediate_vect-_begin_immediate_vect>=int(n) )
	return;
      n=nextpow2(n);
      _Tp * _newbegin = new _Tp[n];
      _Tp * _end_immediate_vect = _begin_immediate_vect+(_taille==immvector_max?0:_taille);
      for (_Tp * ptr=_begin_immediate_vect;ptr!=_end_immediate_vect;++ptr,++_newbegin){
	*_newbegin = *ptr;
      }
      _newbegin -= (_taille==immvector_max?0:_taille);
      if (_begin_immediate_vect)
	delete [] _begin_immediate_vect;
      _begin_immediate_vect=_newbegin;
      _endalloc_immediate_vect=_begin_immediate_vect+n;
    }
    void _alloc(unsigned n){
      _zero_tab();
      if (n<=(sizeof(int)*IMMEDIATE_VECTOR)/sizeof(_Tp)){
	_taille=-int(n);
      }
      else {
	_taille=(n?n:immvector_max);
	n=nextpow2(n);
	_begin_immediate_vect = new _Tp[n];
	_endalloc_immediate_vect=_begin_immediate_vect+n;
      }
    }
    void _alloc_fill(const _Tp * b,const _Tp * e){
      unsigned n=unsigned(e-b);
      if (n<=(sizeof(int)*IMMEDIATE_VECTOR)/sizeof(_Tp)){
	_zero_tab();
	_taille = -int(n);
	for (unsigned i=0;i<n;++b,++i){
	  ((_Tp *) _tab)[i]=*b;
	}
      }
      else {
	_alloc(n); 
	unsigned i=0;
	for (_Tp * ptr=_begin_immediate_vect;i<n;++ptr,++b,++i){
	  *ptr = *b;
	}
      }
    }
  public:
    typedef _Tp value_type;
    typedef _Tp * pointer;
    typedef const _Tp * const_iterator;
    typedef pointer iterator;
    typedef giac_reverse_pointer<_Tp> reverse_iterator;
    typedef giac_reverse_pointer<const _Tp> const_reverse_iterator;
    
    imvector():_taille(0) { 
      _zero_tab();
    }
    ~imvector() { 
      _destroy();
    }
    imvector(size_t n_,const _Tp & value=_Tp()){
      unsigned n=unsigned(n_);
      _alloc(n); 
      _Tp * _end_immediate_vect=_taille>0?_begin_immediate_vect:(_Tp *)_tab;
      for (unsigned i=0;i<n;++_end_immediate_vect,++i){
	*_end_immediate_vect =value;
      }
    }
    imvector(const const_iterator & b,const const_iterator & e){
      _alloc_fill(b,e);
    }
    imvector(const imvector<_Tp> & w){
      const _Tp * ptr=w.begin();
      _alloc_fill(ptr,ptr+_abs(w._taille));
    }
    imvector<_Tp> & operator = (const imvector<_Tp> & w){
      if (this==&w)
	return *this;
      _Tp copie[IMMEDIATE_VECTOR];
      _Tp * wbeg;
      unsigned n=_abs(w._taille),i=0;
#ifdef NO_WORKAROUND
      if (w._taille<0){
	wbeg=copie;
	for (unsigned j=0;j<n;++j)
	  copie[j]=((_Tp *)w._tab)[j];
      }
      else {
	wbeg=w._begin_immediate_vect;
      }
#else
      // the loop should be inside the test below
      // we keep it outside as a workaround to keep a copy of the source when doing
      // something like v=*v.front()._VECTptr; where v=imvector<gen>
      for (unsigned j=0;j<n && j<IMMEDIATE_VECTOR;++j)
	copie[j]=w[j];
      if (w._taille<0){
	wbeg=copie;
      }
      else {
	wbeg=w._begin_immediate_vect;
      }
#endif
      _realloc(n);
      _taille=_taille<=0?-int(n):(n?n:immvector_max);
      _Tp * _end_immediate_vect=begin();
      for (const _Tp * ptr=wbeg;i<n;++ptr,++_end_immediate_vect,++i){
	*_end_immediate_vect = *ptr;
      }
      return *this;
    }
    iterator begin(){ return _taille>0?_begin_immediate_vect:((_Tp *) _tab); }
    iterator end(){ return _taille>0?_begin_immediate_vect+_abs(_taille):((_Tp *) _tab)-_taille;}
    const_iterator begin() const { return _taille>0?_begin_immediate_vect:((_Tp *) _tab); }
    const_iterator end() const { return _taille>0?_begin_immediate_vect+_abs(_taille):((_Tp *) _tab)-_taille;}
    reverse_iterator rbegin(){ return _taille>0?_begin_immediate_vect+_abs(_taille)-1:((_Tp *) _tab)-_taille-1; }
    reverse_iterator rend(){ return _taille>0?_begin_immediate_vect-1:((_Tp *) _tab)-1;}
    const_reverse_iterator rbegin() const { return _taille>0?_begin_immediate_vect+_abs(_taille)-1:((_Tp *) _tab)-_taille-1; }
    const_reverse_iterator rend() const { return _taille>0?_begin_immediate_vect-1:((_Tp *) _tab)-1;}
    size_t size() const { return _abs(_taille); }
    size_t capacity() const { return _taille<0?(sizeof(int)*IMMEDIATE_VECTOR)/sizeof(_Tp):_endalloc_immediate_vect-_begin_immediate_vect;}
    _Tp & front() { return *begin(); }
    _Tp & back() { return *rbegin(); }
    _Tp & operator [](size_t i) { return *(begin()+i); }
    const _Tp & front() const { return *begin(); }
    const _Tp & back() const { return *rbegin(); }
    const _Tp & operator [](size_t i) const { return *(begin()+i); }
    void push_back(const _Tp & p0){ 
      if (_taille<=0){ 
	if (unsigned(-_taille)<(sizeof(int)*IMMEDIATE_VECTOR)/sizeof(_Tp)){
	  ((_Tp *) _tab)[-_taille]=p0;
	  --_taille;
	  return;
	}
	// create a copy since p0 may be scratched 
	// if p0 is a vector element and the vector is realloced
	_Tp p(p0); 
	_realloc(_taille?-2*_taille:1);
	if (_taille==immvector_max){
	  *(_begin_immediate_vect)=p;
	  _taille=1;
	}
	else {
	  *(_begin_immediate_vect+_taille)=p;
	  ++_taille;
	}
	return;
      }
      if (_taille==immvector_max) 
	_taille=0;
      if (_endalloc_immediate_vect==_begin_immediate_vect+_taille){
	_Tp p(p0); 
	_realloc(_taille?2*_taille:1);
	*(_begin_immediate_vect+_taille)=p;
      }
      else
	*(_begin_immediate_vect+_taille)=p0;
      ++_taille;
    }
    _Tp pop_back(){ 
      if (_taille<=0){
	if (_taille) ++_taille;
	_Tp res=*( ((_Tp *) _tab) -_taille);
	*(((_Tp *) _tab)-_taille)=_Tp();
	return res;
      }
      --_taille; 
      if (_taille){
	_Tp res=*(_begin_immediate_vect+_taille); 
	*(_begin_immediate_vect+_taille)=_Tp();
	return res;
      }
      _Tp res=*_begin_immediate_vect;
      delete [] _begin_immediate_vect;
      _zero_tab();
      return res;
    }
    void clear(){ 
      if (_taille>0 &&_begin_immediate_vect){
	if (_taille!=immvector_max){
	  for (int i=0;i<_taille;++i)
	    *(_begin_immediate_vect+i)=_Tp();
	  _taille=immvector_max;
	}
      }
      else {
	if (_taille<0)
	  _free_tab();
	_taille=0;
      }
    }
    bool empty() const { return _taille==0 || _taille==immvector_max; }
    void reserve(size_t n){ if (_abs(_taille)<n) _realloc(int(n)); }
    void resize(size_t n_){ 
      unsigned n=unsigned(n_);
      if (_taille!=immvector_max && _abs(_taille)>=n) {
	// clear elements from _begin()+n to _end()
	_Tp * ptr = begin()+n;
	for (;ptr!=end();++ptr)
	  *ptr=_Tp();
	_taille=_taille>0?(n?n:immvector_max):-int(n);
      }
      else {
	unsigned prev=_taille==immvector_max?0:_abs(_taille);
	_realloc(n);
	if (_taille<=0) _taille=-int(n); else _taille=n?n:immvector_max;
      }
    }
    void resize(size_t n_,const _Tp &value){ 
      unsigned n=unsigned(n_);
      if (_taille!=immvector_max && _abs(_taille)>=n) {
	// clear elements from _begin()+n to _end()
	_Tp * ptr = begin()+n;
	for (;ptr!=end();++ptr)
	  *ptr=value;
	_taille=_taille>0?(n?n:immvector_max):-int(n);
      }
      else {
	unsigned prev=_taille==immvector_max?0:_abs(_taille);
	_realloc(n);
	_Tp * ptr=begin()+prev;
	for (unsigned i=prev;i<n;++ptr,++i){
	  *ptr=value;
	}
	if (_taille<=0) _taille=-int(n); else _taille=n?n:immvector_max;
      }
    }
    void erase(_Tp * b,_Tp * e){
      unsigned decal=unsigned(e-b);
      if (!decal || _taille==0 || _taille==immvector_max)
	return;
      if (decal>=_abs(_taille)){
	clear();
	return;
      }
      _Tp * _end_immediate_vect=end();
      for (_Tp * ptr=e;ptr!=_end_immediate_vect;++ptr){
	*(ptr-decal)=*ptr;
	*ptr = _Tp();
      }
      if (_taille<0) { 
	_taille += decal; 
      }
      else {
	_taille -= decal;
	if (!_taille) _taille=immvector_max;
      }
    }
    void erase(_Tp * b){
      erase(b,b+1);
    }
    _Tp * insert(_Tp * b, const _Tp& x ){
      if (_taille==0){
	push_back(x);
	return begin();
      }
      if (_taille<=0){
	if (unsigned(-_taille)<(sizeof(int)*IMMEDIATE_VECTOR)/sizeof(_Tp)){
	  --_taille;
	  for (_Tp * ptr=((_Tp *) _tab)-_taille-1;ptr!=b;--ptr){
	    *ptr=*(ptr-1);
	  }
	  *b=x;
	  return b;
	}
	int pos=int(b-((_Tp *) _tab));
	_realloc(_abs(_taille)?2*(-_taille):1);
	b=_begin_immediate_vect+pos;
      }
      if (int(_abs(_taille))==_endalloc_immediate_vect-_begin_immediate_vect){
	int pos=int(b-_begin_immediate_vect);
	_realloc(_abs(_taille)?2*_taille:1);
	b=_begin_immediate_vect+pos;
      }
      if (_taille==immvector_max) _taille=1; else ++_taille;
      for (_Tp * ptr=_begin_immediate_vect+_abs(_taille)-1;ptr!=b;--ptr){
	*ptr=*(ptr-1);
      }
      *b=x;
      return b;
    }
    void insert(_Tp * b, unsigned k,const _Tp& x ){
      if (_taille<=0){
	int pos=int(b-((_Tp *) _tab));
	_realloc(k+IMMEDIATE_VECTOR);
	b=_begin_immediate_vect+pos;
      }
      if (_endalloc_immediate_vect-_begin_immediate_vect<int(_abs(_taille)+k)){
	unsigned pos=unsigned(b-_begin_immediate_vect);
	_realloc(_abs(_taille)?2*_taille:1);
	b=_begin_immediate_vect+pos;
      }
      if (_taille==immvector_max) _taille=k; else _taille+=k;
      for (_Tp * ptr=_begin_immediate_vect+_abs(_taille)-k;ptr!=b;){
	--ptr;
	*(ptr+k)=*ptr;
      }
      for (unsigned j=0;j<k;++b,++j){
	*b=x;
      }
    }
    void swap(imvector<_Tp> & w){
      char ch[sizeof(imvector<_Tp>)];
      memcpy((void *)ch,(void *)&w,sizeof(imvector<_Tp>));
      memcpy(&w,this,sizeof(imvector<_Tp>));
      memcpy(this,(void *)ch,sizeof(imvector<_Tp>));
    }
    void assign(size_t n_,const _Tp & value=_Tp()){
      unsigned n=unsigned(n_);
      _realloc(n);
      _taille=_taille>0?(n?n:immvector_max):-n;
      unsigned i=0;
      for (_Tp * ptr=_begin_immediate_vect;i<n;++ptr,++i){
	*ptr=value;
      }
    }
    void assign(const _Tp * b,const _Tp * e){
      unsigned n=unsigned(e-b);
      _realloc(n);
      _taille=_taille>0?(n?n:immvector_max):-n;
      for (_Tp * ptr=_begin_immediate_vect;b!=e;++b,++ptr){
	*ptr=*b;
      }
    }
    unsigned max_size() const {
      return (1 << 30) -1;
    }
    static _Tp &OutOfBoundsDefault() { static _Tp value; value = _Tp(); return value; }
    _Tp & at(size_t n){
      if (n>_abs(_taille))
	return OutOfBoundsDefault();
      return *(begin()+n);
    }
    const _Tp at(size_t n) const {
      if (n>_abs(_taille))
	return OutOfBoundsDefault();
      return *(begin()+n);
    }
  };

  template<typename _Tp>
    inline bool operator==(const imvector<_Tp>& __x, const imvector<_Tp>& __y){ 
    if (__x.size() != __y.size())
      return false;
    const _Tp * xend=__x.end();
    for (const _Tp * xptr=__x.begin(), * yptr=__y.begin();xptr!=xend;++yptr,++xptr){
      if (*xptr!=*yptr)
	return false;
    }
    return true;
  }

  template<typename _Tp>
    inline bool operator < (const imvector<_Tp>& __x, const imvector<_Tp>& __y){ 
    if (__x.size() != __y.size())
      return __x.size()<__y.size();
    const _Tp * xend=__x.end();
    for (const _Tp * xptr=__x.begin(), * yptr=__y.begin();xptr!=xend;++yptr,++xptr){
      if (*xptr!=*yptr)
	return *xptr<*yptr;
    }
    return false;
  }

  template<typename _Tp>
    void swap(imvector<_Tp>& __x,imvector<_Tp>& __y){
    __x.swap(__y);
  }
#else // IMMEDIATE_VECTOR

#define imvector vector

#endif // IMMEDIATE_VECTOR
} // namespace std

#ifndef GIAC_VECTOR
#include <vector>
#else

namespace std {
  template<typename _Tp> class vector{
    // private members
    _Tp * _begin,*_end,*_endalloc;
    // private allocation methods
    void _realloc(unsigned n){
      if (_endalloc-_begin>=int(n))
	return;
      unsigned old=unsigned(_end-_begin);
      _Tp * _newbegin = new _Tp[n];
      for (_Tp * ptr=_begin;ptr!=_end;++ptr,++_newbegin){
	*_newbegin = *ptr;
      }
      _newbegin -= old;
      if (_begin)
	delete [] _begin;
      _begin=_newbegin;
      _end=_begin+old;
      _endalloc=_begin+n;
    }
    void _alloc(unsigned n){
      _end=_begin = new _Tp[n];
      _endalloc=_begin+n;
    }
    void _alloc_fill(const _Tp * b,const _Tp * e){
      unsigned n=unsigned(e-b);
      _alloc(n); 
      for (_Tp * ptr=_begin;ptr!=_endalloc;++ptr,++b){
	*ptr = *b;
      }
      _end = _begin+ n;
    }
  public:
    typedef _Tp value_type;
    typedef _Tp * pointer;
    typedef const _Tp * const_iterator;
    typedef pointer iterator;
    typedef giac_reverse_pointer<_Tp> reverse_iterator;
    typedef giac_reverse_pointer<const _Tp> const_reverse_iterator;
    
  vector():_begin(0),_end(0),_endalloc(0) {}
    ~vector() { if (_begin) delete [] _begin; }
    vector(size_t n_,const _Tp & value=_Tp()){
      unsigned n=unsigned(n_);
      _alloc(n); 
      for (;_end!=_endalloc;++_end){
	*_end =value;
      }
    }
    vector(const const_iterator & b,const const_iterator & e){
      _alloc_fill(b,e);
    }
    vector(const vector<_Tp> & w){
      _alloc_fill(w._begin,w._end);
    }
    vector<_Tp> & operator = (const vector<_Tp> & w){
      if (this==&w)
	return *this;
      unsigned n=unsigned(w._end-w._begin);
      _realloc(n);
      _end=_begin;
      for (_Tp * ptr=w._begin;ptr!=w._end;++_end,++ptr){
	*_end = *ptr;
      }
      return *this;
    }
    iterator begin(){ return _begin; }
    iterator end(){ return _end;}
    const_iterator begin() const { return _begin; }
    const_iterator end() const { return _end;}
    reverse_iterator rbegin(){ return _end-1; }
    reverse_iterator rend(){ return _begin-1;}
    const_reverse_iterator rbegin() const { return _end-1; }
    const_reverse_iterator rend() const { return _begin-1;}
    size_t size() const { return _end-_begin; }
    size_t capacity() const { return _endalloc-_begin;}
    _Tp & front() { return *_begin; }
    _Tp & back() { return *(_end-1); }
    _Tp & operator [](size_t i) { return *(_begin+i); }
    const _Tp & front() const { return *_begin; }
    const _Tp & back() const { return *(_end-1); }
    const _Tp & operator [](size_t i) const { return *(_begin+i); }
    void push_back(const _Tp & p){ 
      if (_endalloc==_end){
	unsigned n = unsigned(_end-_begin);
	_realloc(n?2*n:2);
      } 
      *_end=p; 
      ++_end; 
    }
    _Tp pop_back(){ --_end; return *_end; }
    void clear(){ _end=_begin;}
    bool empty() const { return _end==_begin; }
    void reserve(size_t n){ if (_endalloc-_begin<int(n)) _realloc(int(n)); }
    void resize(size_t n_,const _Tp &value=_Tp()){ 
      int n=int(n_);
      if (_end-_begin>=n) _end=_begin+n;
      else {
	_realloc(n);
	for (;_end!=_endalloc;++_end){
	  *_end=value;
	}
      }
    }
    void erase(_Tp * b,_Tp * e){
      unsigned decal=unsigned(e-b);
      if (!decal)
	return;
      for (_Tp * ptr=e;ptr!=_end;++ptr){
	*(ptr-decal)=*ptr;
      }
      _end -= decal;
    }
    void erase(_Tp * b){
      erase(b,b+1);
    }
    _Tp * insert(_Tp * b, const _Tp& x ){
      if (_endalloc==_end){
	unsigned pos=unsigned(b-_begin);
	unsigned n = unsigned(_end-_begin);
	_realloc(n?2*n:2);
	b=_begin+pos;
      }
      ++_end;
      for (_Tp * ptr=_end-1;ptr!=b;--ptr){
	*ptr=*(ptr-1);
      }
      *b=x;
      return b;
    }
    void insert(_Tp * b, unsigned k,const _Tp& x ){
      if (_endalloc<_end+k){
	unsigned pos=unsigned(b-_begin);
	unsigned n = unsigned(_end-_begin);
	_realloc(n>k?2*n:n+k);
	b=_begin+pos;
      }
      _end += k ;
      for (_Tp * ptr=_end-k;ptr!=b;){
	--ptr;
	*(ptr+k)=*ptr;
      }
      for (unsigned j=0;j<k;++b,++j){
	*b=x;
      }
    }
    void swap(vector<_Tp> & w){
      _Tp * tmp=_begin; _begin=w._begin; w._begin=tmp;
      tmp=_end; _end=w._end; w._end=tmp;
      tmp=_endalloc; _endalloc=w._endalloc; w._endalloc=tmp;
    }
    void assign(size_t n_,const _Tp & value=_Tp()){
      unsigned n=unsigned(n_);
      _realloc(n);
      _end = _begin +n;
      for (_Tp * ptr=_begin;ptr!=_end;++ptr){
	*ptr=value;
      }
    }
    void assign(const _Tp * b,const _Tp * e){
      unsigned n=unsigned(e-b);
      _realloc(n);
      _end = _begin +n;
	for (_Tp * ptr=_begin;b!=e;++b,++ptr){
	*ptr=*b;
      }
    }
    unsigned max_size() const {
      return 1 << 30;
    }
    _Tp & at(size_t n){
      if (n>_end-_begin)
	return _Tp(); // should be defined somewhere else
      return *(_begin+n);
    }
    const _Tp at(size_t n) const {
      if (n>_end-_begin)
	return _Tp(); // should be defined somewhere else
      return *(_begin+n);
    }
  };

  template<typename _Tp>
    inline bool operator==(const vector<_Tp>& __x, const vector<_Tp>& __y){ 
    if (__x.size() != __y.size())
      return false;
    for (const _Tp * xptr=__x.begin(), * yptr=__y.begin();xptr!=__x.end();++yptr,++xptr){
      if (*xptr!=*yptr)
	return false;
    }
    return true;
  }

  // template<typename _Tp> inline bool operator!=(const vector<_Tp>& __x, const vector<_Tp>& __y){ return !(__x==__y); }

  template<typename _Tp>
    inline bool operator < (const vector<_Tp>& __x, const vector<_Tp>& __y){ 
    if (__x.size() != __y.size())
      return __x.size()<__y.size();
    for (const _Tp * xptr=__x.begin(), * yptr=__y.begin();xptr!=__x.end();++yptr,++xptr){
      if (*xptr!=*yptr)
	return *xptr<*yptr;
    }
    return false;
  }

  template<typename _Tp>
    inline bool operator > (const _Tp & __x, const _Tp & __y){
    return __y<__x;
  }

  template<typename _Tp>
    inline bool operator >= (const _Tp & __x, const _Tp & __y){
    return !(__x<__y);
  }

  template<typename _Tp>
    inline bool operator <= (const _Tp & __x, const _Tp & __y){
    return !(__y<__x);
  }

}

#endif

#endif // GIAC_VECTOR_H