/* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * 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 * * $Id: ZArrayList.cpp,v 1.2 2002/06/08 10:09:42 florian Exp $ */ #include "../include/jlpplib.h" /*****************************************************************************\ | ZArrayList | \*****************************************************************************/ ZArrayList::ZArrayList(boolean autoDelete) { init(10, 0, autoDelete); } ZArrayList::ZArrayList() { init(10, 0, TRUE); } ZArrayList::ZArrayList(int initialCapacity, boolean autoDelete) { init(initialCapacity, 0, autoDelete); } ZArrayList::ZArrayList(int initialCapacity, int capacityIncrement, boolean autoDelete) { init(initialCapacity, capacityIncrement, autoDelete); } /*****************************************************************************\ | init | \*****************************************************************************/ void ZArrayList::init(int initialCapacity, int capacityIncrement, boolean autoDelete) { classname = "ZArrayList"; if (initialCapacity < 0) throw ZIllegalArgumentException(ZString(0, "Illegal capacity: %u", initialCapacity).getChars()); if (capacityIncrement < 0) throw ZIllegalArgumentException(ZString(0, "Illegal increment: %u", capacityIncrement).getChars()); this->capacityIncrement = capacityIncrement; this->autoDelete = autoDelete; elementData = (ZObject**) malloc(initialCapacity * sizeof(ZObject**)); elementCount = 0; capacity = initialCapacity; } /*****************************************************************************\ | ~ZArrayList | \*****************************************************************************/ ZArrayList::~ZArrayList() { //printf("Deleting ArrayList\n"); clear(); free(elementData); } /*****************************************************************************\ | add | \*****************************************************************************/ int ZArrayList::add(ZObject* object) { ensureCapacity(elementCount + 1); elementData[elementCount++] = object; ZREF(object); return elementCount - 1; } int ZArrayList::add(const char* str) { return add(new ZString(str)); } /*****************************************************************************\ | ensureCapacity | \*****************************************************************************/ void ZArrayList::ensureCapacity(int minCapacity) { if (minCapacity > capacity) { int newCapacity = (capacityIncrement > 0) ? (capacity + capacityIncrement) : (capacity * 2); if (newCapacity < minCapacity) newCapacity = minCapacity; ZObject** newElementData = (ZObject**) realloc(elementData, sizeof(ZObject*) * newCapacity); if (newElementData == NULL) throw ZMemoryException(ZString(0, "Allocating %u bytes of memory.", sizeof(ZObject*) * newCapacity).getChars()); elementData = newElementData; capacity = newCapacity; } } /*****************************************************************************\ | get | \*****************************************************************************/ ZObject* ZArrayList::get(int index) { if (index >= elementCount) throw ZArrayIndexOutOfBoundsException(ZString(0, "ZArrayList.get(%i): %u >= %u", index, index, elementCount).getChars()); if (index < 0) throw ZIllegalArgumentException(ZString(0, "ZArrayList.get(%i): %u < 0", index, index).getChars()); return elementData[index]; } /*****************************************************************************\ | size | \*****************************************************************************/ int ZArrayList::size() { return elementCount; } /*****************************************************************************\ | set | \*****************************************************************************/ void ZArrayList::set(int index, ZObject* object) { if (index < 0) throw ZIllegalArgumentException(ZString(0, "%u < 0", index).getChars()); ensureCapacity(index +1); ZUNREF(elementData[index]); elementData[index] = object; } /*****************************************************************************\ | insert | \*****************************************************************************/ void ZArrayList::insert(int index, ZObject* object) { if (index < 0) throw ZIllegalArgumentException(ZString(0, "ZArrayList.insert(%i, object): %u < 0", index, index).getChars()); if (index > elementCount) throw ZIllegalArgumentException(ZString(0, "ZArrayList.insert(%i, object): %u > %u", index, index, elementCount).getChars()); ensureCapacity(elementCount +1); for (int idx = elementCount; idx > index; idx--) { elementData[idx] = elementData[idx - 1]; } elementData[index] = object; elementCount ++; } /*****************************************************************************\ | clear | \*****************************************************************************/ void ZArrayList::clear() { #ifndef JAKELIB_USE_GC if (autoDelete) { for (int idx = 0; idx < elementCount; idx++) ZUNREF(elementData[idx]); } #endif elementCount = 0; } /*****************************************************************************\ | indexOf | \*****************************************************************************/ int ZArrayList::indexOf(ZObject* object) { int idx; for (idx = 0; idx < elementCount; idx++) if (elementData[idx] == object) return idx; return -1; } /*****************************************************************************\ | contains | \*****************************************************************************/ boolean ZArrayList::contains(ZObject* object) { return (indexOf(object) != -1); } /*****************************************************************************\ | removeLastElement | \*****************************************************************************/ void ZArrayList::removeLastElement() { if (elementCount == 0) return; elementCount --; if (autoDelete) { ZUNREF(elementData[elementCount]); } elementData[elementCount] = NULL; } /*****************************************************************************\ | remove | \*****************************************************************************/ boolean ZArrayList::remove(ZObject* object) { int i = indexOf(object); if (i >= 0) { remove(i); return TRUE; } return FALSE; } /*****************************************************************************\ | remove | \*****************************************************************************/ void ZArrayList::remove(int index) { if (index >= elementCount) { throw ZArrayIndexOutOfBoundsException(ZString(0, "%u >= %u", index, elementCount).getChars()); } else if (index < 0) { throw ZArrayIndexOutOfBoundsException(ZString(0, "%u", index).getChars()); } if (autoDelete) { ZUNREF(elementData[index]); } int j = elementCount - index - 1; if (j > 0) { //memmove(elementData[index], elementData[index + 1], sizeof(void*) * j); for (int idx = index; idx < elementCount - 1; idx++) elementData[idx] = elementData[idx + 1]; } elementCount--; // elementData[elementCount] = NULL; } /*****************************************************************************\ | isEmpty | \*****************************************************************************/ boolean ZArrayList::isEmpty() { return (elementCount == 0); }