/* Generated automatically by jlpp - do not edit. */ #include static jakelib::lang::String* jakelib2_strings[] = {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}; // "Illegal capacity: " static jchar chars_jakelib2_str_0[] = {73,108,108,101,103,97,108,32,99,97,112,97,99,105,116,121,58,32}; // "Illegal increment: " static jchar chars_jakelib2_str_1[] = {73,108,108,101,103,97,108,32,105,110,99,114,101,109,101,110,116,58,32}; // "Allocating " static jchar chars_jakelib2_str_2[] = {65,108,108,111,99,97,116,105,110,103,32}; // " bytes of memory" static jchar chars_jakelib2_str_3[] = {32,98,121,116,101,115,32,111,102,32,109,101,109,111,114,121}; // "" static jchar chars_jakelib2_str_4[] = {0}; // " >= " static jchar chars_jakelib2_str_5[] = {32,62,61,32}; // "" static jchar chars_jakelib2_str_6[] = {0}; // " < 0" static jchar chars_jakelib2_str_7[] = {32,60,32,48}; // "" static jchar chars_jakelib2_str_8[] = {0}; // " < 0" static jchar chars_jakelib2_str_9[] = {32,60,32,48}; // "" static jchar chars_jakelib2_str_10[] = {0}; // " >= " static jchar chars_jakelib2_str_11[] = {32,62,61,32}; // "" static jchar chars_jakelib2_str_12[] = {0}; // " < 0" static jchar chars_jakelib2_str_13[] = {32,60,32,48}; // "" static jchar chars_jakelib2_str_14[] = {0}; // " >= " static jchar chars_jakelib2_str_15[] = {32,62,61,32}; #line 1 "util/Vector.jlc" // -*- c++ -*- /* * 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: Vector.jlc,v 1.6 2003/09/27 08:10:29 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/lang/System.h" #include "jakelib2/util/Vector.h" #include "jakelib2/lang/Synchronizer.h" #include using namespace jakelib::lang; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS("jakelib.util.Vector", Vector, Object) #ifndef WINOWS32 # include # include #endif /*****************************************************************************\ * Vector | *****************************************************************************/ Vector::Vector() { init(10, 0); } Vector::Vector(int initialCapacity) { init(initialCapacity, 0); } Vector::Vector(int initialCapacity, int capacityIncrement) { init(initialCapacity, capacityIncrement); } Vector::~Vector() { GC_FREE(elementData); } /*****************************************************************************\ * init | *****************************************************************************/ void Vector::init(int initialCapacity, int capacityIncrement) { if (initialCapacity < 0) throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[0], new jakelib::lang::String(chars_jakelib2_str_0, 0, 18)) ->plus( initialCapacity )->plus( JAKELIB_AT2("jakelib.util.Vector.init"))); if (capacityIncrement < 0) throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[1], new jakelib::lang::String(chars_jakelib2_str_1, 0, 19)) ->plus( capacityIncrement )->plus( JAKELIB_AT2("jakelib.util.Vector.init"))); this->capacityIncrement = capacityIncrement; elementData = (Object**) GC_MALLOC(initialCapacity * sizeof(Object*)); elementCount = 0; capacity = initialCapacity; } /*****************************************************************************\ * addElement | *****************************************************************************/ int Vector::addElement(Object* object) { int newIndex; JAKELIB_SYNCHRONIZED(this); ensureCapacity(elementCount + 1); elementData[elementCount++] = object; newIndex = elementCount - 1; return newIndex; } /*****************************************************************************\ * ensureCapacity | *****************************************************************************/ void Vector::ensureCapacity(int minCapacity) { if (minCapacity > capacity) { int newCapacity = (capacityIncrement > 0) ? (capacity + capacityIncrement) : (capacity * 2); if (newCapacity < minCapacity) newCapacity = minCapacity; Object** newElementData = (Object**) GC_REALLOC(elementData, sizeof(Object*) * newCapacity); if (newElementData == null) throw new MemoryException(JAKELIB_ONDEMAND(jakelib2_strings[2], new jakelib::lang::String(chars_jakelib2_str_2, 0, 11)) ->plus( (jlong) (sizeof(Object*) * newCapacity) )->plus( JAKELIB_ONDEMAND(jakelib2_strings[3], new jakelib::lang::String(chars_jakelib2_str_3, 0, 16)) )->plus( JAKELIB_AT2("jakelib.util.Vector.ensureCapacity"))); elementData = newElementData; capacity = newCapacity; } } /*****************************************************************************\ * elementAt | *****************************************************************************/ Object* Vector::elementAt(int index) { JAKELIB_SYNCHRONIZED(this); if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(JAKELIB_ONDEMAND(jakelib2_strings[4], new jakelib::lang::String(chars_jakelib2_str_4, 0, 0)) ->plus( index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[5], new jakelib::lang::String(chars_jakelib2_str_5, 0, 4)) )->plus( elementCount )->plus( JAKELIB_AT2("jakelib.util.Vector.elementAt"))); } if (index < 0) { throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[6], new jakelib::lang::String(chars_jakelib2_str_6, 0, 0)) ->plus( index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[7], new jakelib::lang::String(chars_jakelib2_str_7, 0, 4)) )->plus( JAKELIB_AT2("jakelib.util.Vector.elementAt"))); } return elementData[index]; } /*****************************************************************************\ * size | *****************************************************************************/ int Vector::size() { return elementCount; } /*****************************************************************************\ * set | *****************************************************************************/ void Vector::set(int index, Object* object) { JAKELIB_SYNCHRONIZED(this); if (index < 0) { throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[8], new jakelib::lang::String(chars_jakelib2_str_8, 0, 0)) ->plus( index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[9], new jakelib::lang::String(chars_jakelib2_str_9, 0, 4)) )->plus( JAKELIB_AT2("jakelib.util.Vector.set"))); } ensureCapacity(index +1); elementData[index] = object; } /*****************************************************************************\ * clear | *****************************************************************************/ void Vector::clear() { JAKELIB_SYNCHRONIZED(this); elementCount = 0; } /*****************************************************************************\ * indexOf | *****************************************************************************/ int Vector::indexOf(Object* object) { JAKELIB_SYNCHRONIZED(this); for (int idx = 0; idx < elementCount; idx++) if (elementData[idx] == object) { return idx; } return -1; } /*****************************************************************************\ * contains | *****************************************************************************/ jboolean Vector::contains(Object* object) { return (indexOf(object) != -1); } /*****************************************************************************\ * removeLastElement | *****************************************************************************/ void Vector::removeLastElement() { JAKELIB_SYNCHRONIZED(this); if (elementCount == 0) { return; } elementCount --; elementData[elementCount] = null; } /*****************************************************************************\ * removeElement | *****************************************************************************/ jboolean Vector::removeElement(Object* object) { int i = indexOf(object); if (i >= 0) { removeElementAt(i); return true; } return false; } /*****************************************************************************\ * removeElementAt | *****************************************************************************/ void Vector::removeElementAt(int index) { JAKELIB_SYNCHRONIZED(this); if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(JAKELIB_ONDEMAND(jakelib2_strings[10], new jakelib::lang::String(chars_jakelib2_str_10, 0, 0)) ->plus( index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[11], new jakelib::lang::String(chars_jakelib2_str_11, 0, 4)) )->plus( elementCount )->plus( JAKELIB_AT2("jakelib.util.Vector.removeElementAt"))); } else if (index < 0) { throw new ArrayIndexOutOfBoundsException(JAKELIB_ONDEMAND(jakelib2_strings[12], new jakelib::lang::String(chars_jakelib2_str_12, 0, 0)) ->plus( index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[13], new jakelib::lang::String(chars_jakelib2_str_13, 0, 4)) )->plus( JAKELIB_AT2("jakelib.util.Vector.removeElementAt"))); } int j = elementCount - index - 1; if (j > 0) { for (int idx = index; idx < elementCount - 1; idx++) elementData[idx] = elementData[idx + 1]; } elementCount--; } /*****************************************************************************\ * isEmpty | *****************************************************************************/ jboolean Vector::isEmpty() { return (size() == 0); } /*****************************************************************************\ * toString | *****************************************************************************/ String* Vector::toString() { JAKELIB_SYNCHRONIZED(this); StringBuffer buf("["); for (int idx = 0; idx < elementCount; idx++) { if (elementData[idx] == null) buf.append("null"); else buf.append(elementData[idx]->toString()); if (idx < elementCount -1) buf.append(", "); } buf.append("]"); return buf.toString(); } /*****************************************************************************\ * copyInto | *****************************************************************************/ void Vector::copyInto(Array* a) { if (a == null) throw new NullPointerException(JAKELIB_AT2("jakelib.util.Vector.copyInto")); setSize(a->length()); if (a->length() > elementCount) { throw new ArrayIndexOutOfBoundsException(JAKELIB_ONDEMAND(jakelib2_strings[14], new jakelib::lang::String(chars_jakelib2_str_14, 0, 0)) ->plus( a->length() )->plus( JAKELIB_ONDEMAND(jakelib2_strings[15], new jakelib::lang::String(chars_jakelib2_str_15, 0, 4)) )->plus( elementCount )->plus( JAKELIB_AT2("jakelib.util.Vector.copyInto"))); } memmove(elementData, a->getArray(), a->length() * sizeof(Object*)); } /*****************************************************************************\ * setSize | *****************************************************************************/ void Vector::setSize(jint newSize) { ensureCapacity(newSize); elementCount = newSize; } /*****************************************************************************\ * setElementAt | *****************************************************************************/ void Vector::setElementAt(Object* obj, jint index) { set(index, obj); }