/* * vector.{cc,hh} -- simple array template class * Eddie Kohler * * Copyright (c) 1999-2000 Massachusetts Institute of Technology * Copyright (c) 2001-2006 Eddie Kohler * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, subject to the conditions * listed in the Click LICENSE file. These conditions include: you must * preserve this copyright notice, and you cannot mention the copyright * holders in advertising related to the Software without their permission. * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This * notice is a summary of the Click LICENSE file; the license in that file is * legally binding. */ #include "vector.hh" template Vector::Vector(const Vector &o) : _l(0), _n(0), _cap(0) { *this = o; } template Vector::~Vector() { for (int i = 0; i < _n; i++) _l[i].~T(); delete[] (unsigned char *)_l; } template Vector & Vector::operator=(const Vector &o) { if (&o != this) { for (int i = 0; i < _n; i++) _l[i].~T(); _n = 0; if (reserve(o._n)) { _n = o._n; for (int i = 0; i < _n; i++) new(velt(i)) T(o._l[i]); } } return *this; } template Vector & Vector::assign(size_type n, const T &e) { resize(0, e); resize(n, e); return *this; } template bool Vector::reserve(size_type want) { if (want < 0) want = _cap > 0 ? _cap * 2 : 4; if (want <= _cap) return true; T *new_l = (T *)new unsigned char[sizeof(T) * want]; if (!new_l) return false; for (int i = 0; i < _n; i++) { new(velt(new_l, i)) T(_l[i]); _l[i].~T(); } delete[] (unsigned char *)_l; _l = new_l; _cap = want; return true; } template void Vector::shrink(size_type nn) { if (nn < _n) { for (int i = nn; i < _n; i++) _l[i].~T(); _n = nn; } } template void Vector::resize(size_type nn, const T &e) { if (nn <= _cap || reserve(nn)) { for (int i = nn; i < _n; i++) _l[i].~T(); for (int i = _n; i < nn; i++) new(velt(i)) T(e); _n = nn; } } template void Vector::swap(Vector &o) { T *l = _l; int n = _n; int cap = _cap; _l = o._l; _n = o._n; _cap = o._cap; o._l = l; o._n = n; o._cap = cap; }