/*
_____       _    _    Corso   Italia,  178
(_|__   .  (_   |_|_  56125           Pisa
(_|_) |)|(()_)()| |   tel.  +39  050 46380
  |   |               picosoft@picosoft.it

 picocpp - basic objects library for picoSQL 

 Copyright (C) Picosoft s.r.l. 1994-2002

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

# include "pvector.h"
static char rcsid[] = "$Id";
static char rcsidh[] = pvector_h;

PCLASSID(PVector)


static int INCREMENT = 10;

void
PVector::alloc(int n)
{
   if (n > maxItems) {
      register int i;
      PObject **newVec = new PObject*[n];
      for (i = 0; i < maxItems; i++)
         newVec[i] = vec[i];
      for ( ; i < n; i++)
         newVec[i] = (PObject*) 0;
      maxItems = n;
      if (vec != 0)
         delete vec;
      vec = newVec;
   }
}

PVector::PVector ()
{
   numItems = 0;
   maxItems = 0;
   vec = 0;
   alloc (INCREMENT);
}

PVector::~PVector ()
{
   numItems = 0;
   maxItems = 0;
   if (vec != 0)
      delete vec;
   vec = 0;
}

void
PVector::setAt (int index, PObject *obj)
{
   int size = maxItems;
   while (size <= index)
      size += INCREMENT;
   alloc(size);
   vec[index] = obj;
   if (numItems <= index)
      numItems = ++index;
}

int
PVector::add (PObject *obj)
{
   register int i;
   for (i = 0; i < numItems; i++)
      if (this->vec[i] == 0) {
         this->vec[i] = obj;
         return i;
      }
   setAt(i, obj);
   return i;
}

PObject *
PVector::getAt (int index)
{
   if (index < numItems)
      return vec[index];
   else
      return (PObject*) 0;
}

void
PVector::deleteAt (int index)
{
   if (index < numItems)
      vec[index] = (PObject*) 0;
   if (numItems == ++index)
      numItems--;
}
