/* * vectorv.cc -- template specialization for Vector * Eddie Kohler * * Copyright (c) 1999-2006 Massachusetts Institute of Technology * * 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. */ #ifdef HAVE_CONFIG_H # include #endif #include #include Vector::Vector(const Vector &o) : _l(0), _n(0), _capacity(0) { *this = o; } Vector::~Vector() { delete[] _l; } Vector & Vector::operator=(const Vector &o) { if (&o != this) { _n = 0; if (reserve(o._n)) { _n = o._n; memcpy(_l, o._l, sizeof(void *) * _n); } } return *this; } Vector & Vector::assign(int n, void *e) { _n = 0; resize(n, e); return *this; } bool Vector::reserve(int want) { if (want < 0) want = (_capacity > 0 ? _capacity * 2 : 4); if (want <= _capacity) return true; void **new_l = new void*[want]; if (!new_l) return false; memcpy(new_l, _l, sizeof(void*) * _n); delete[] _l; _l = new_l; _capacity = want; return true; } Vector::iterator Vector::erase(iterator a, iterator b) { if (b > a) { assert(a >= begin() && b <= end()); memmove(a, b, (end() - b) * sizeof(void*)); _n -= b - a; return a; } else return b; } void Vector::resize(int nn, void *e) { if (nn <= _capacity || reserve(nn)) { for (int i = _n; i < nn; i++) _l[i] = e; _n = nn; } } void Vector::swap(Vector& o) { void **l = _l; int n = _n; int cap = _capacity; _l = o._l; _n = o._n; _capacity = o._capacity; o._l = l; o._n = n; o._capacity = cap; }