/* 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, null, 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}; // "jakelib.util.ArrayList.ArrayList" static jchar chars_jakelib2_str_1[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,65,114,114,97,121,76,105,115,116}; // "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}; // "jakelib.util.ArrayList.ensureCapacity" static jchar chars_jakelib2_str_4[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,101,110,115,117,114,101,67,97,112,97,99,105,116,121}; // "" static jchar chars_jakelib2_str_5[] = {0}; // " >= " static jchar chars_jakelib2_str_6[] = {32,62,61,32}; // "jakelib.util.ArrayList.get" static jchar chars_jakelib2_str_7[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,103,101,116}; // "" static jchar chars_jakelib2_str_8[] = {0}; // " < 0" static jchar chars_jakelib2_str_9[] = {32,60,32,48}; // "jakelib.util.ArrayList.get" static jchar chars_jakelib2_str_10[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,103,101,116}; // "" static jchar chars_jakelib2_str_11[] = {0}; // " < 0" static jchar chars_jakelib2_str_12[] = {32,60,32,48}; // "jakelib.util.ArrayList.set" static jchar chars_jakelib2_str_13[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,115,101,116}; // "" static jchar chars_jakelib2_str_14[] = {0}; // " >= " static jchar chars_jakelib2_str_15[] = {32,62,61,32}; // "jakelib.util.ArrayList.set" static jchar chars_jakelib2_str_16[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,115,101,116}; // "" static jchar chars_jakelib2_str_17[] = {0}; // " < 0" static jchar chars_jakelib2_str_18[] = {32,60,32,48}; // "jakelib.util.ArrayList.insert" static jchar chars_jakelib2_str_19[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,105,110,115,101,114,116}; // "" static jchar chars_jakelib2_str_20[] = {0}; // " > " static jchar chars_jakelib2_str_21[] = {32,62,32}; // "jakelib.util.ArrayList.insert" static jchar chars_jakelib2_str_22[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,105,110,115,101,114,116}; // "" static jchar chars_jakelib2_str_23[] = {0}; // " >= " static jchar chars_jakelib2_str_24[] = {32,62,61,32}; // "jakelib.util.ArrayList.remove" static jchar chars_jakelib2_str_25[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,114,101,109,111,118,101}; // "" static jchar chars_jakelib2_str_26[] = {0}; // " < 0" static jchar chars_jakelib2_str_27[] = {32,60,32,48}; // "jakelib.util.ArrayList.remove" static jchar chars_jakelib2_str_28[] = {106,97,107,101,108,105,98,46,117,116,105,108,46,65,114,114,97,121,76,105,115,116,46,114,101,109,111,118,101}; // "[" static jchar chars_jakelib2_str_29[] = {91}; // "null" static jchar chars_jakelib2_str_30[] = {110,117,108,108}; // ", " static jchar chars_jakelib2_str_31[] = {44,32}; // "]" static jchar chars_jakelib2_str_32[] = {93}; #line 1 "util/ArrayList.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: ArrayList.jlc,v 1.11 2006-01-14 11:34:22 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/util/ArrayList.h" #include "jakelib2/util/ArrayListIterator.h" #include "jakelib2/util/Comparator.h" #include using namespace jakelib::lang; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS("jakelib.util.ArrayList", ArrayList, AbstractList); //#pragma javasyntax /*****************************************************************************\ * ArrayList | *****************************************************************************/ ArrayList::ArrayList(int initialCapacity) { if (initialCapacity < 0) throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[0], new jakelib::lang::String(chars_jakelib2_str_0, 0, 18)) ->plus( (jlong) initialCapacity )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[1], new jakelib::lang::String(chars_jakelib2_str_1, 0, 32))))); elementData = (Object**) GC_MALLOC(initialCapacity * sizeof(Object*)); elementCount = 0; capacity = initialCapacity; } /*****************************************************************************\ * ~ArrayList | *****************************************************************************/ ArrayList::~ArrayList() { destroy(); } /*****************************************************************************\ * destroy | *****************************************************************************/ void ArrayList::destroy() { clear(); GC_FREE(elementData); elementData = null; capacity = 0; } /*****************************************************************************\ * add | *****************************************************************************/ jboolean ArrayList::add(Object* object) { ensureCapacity(elementCount + 1); elementData[elementCount++] = object; return true; } /*****************************************************************************\ * addAll | *****************************************************************************/ jboolean ArrayList::addAll(int index, Collection* c) { return AbstractList::addAll(index, c); } jboolean ArrayList::addAll(Collection* c) { return AbstractList::addAll(c); } /*****************************************************************************\ * ensureCapacity | *****************************************************************************/ void ArrayList::ensureCapacity(int minCapacity) { if (minCapacity > capacity) { int newCapacity = 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_ONDEMAND(jakelib2_strings[4], new jakelib::lang::String(chars_jakelib2_str_4, 0, 37))))); elementData = newElementData; capacity = newCapacity; } } /*****************************************************************************\ * trimToSize | *****************************************************************************/ void ArrayList::trimToSize() { if (capacity > elementCount) { Object** newElementData = (Object**) GC_REALLOC(elementData, sizeof(Object*) * elementCount); if (newElementData != null) { elementData = newElementData; capacity = elementCount; } } } /*****************************************************************************\ * get | *****************************************************************************/ Object* ArrayList::get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(JAKELIB_ONDEMAND(jakelib2_strings[5], new jakelib::lang::String(chars_jakelib2_str_5, 0, 0)) ->plus( (jlong) index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[6], new jakelib::lang::String(chars_jakelib2_str_6, 0, 4)) )->plus( (jlong) elementCount )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[7], new jakelib::lang::String(chars_jakelib2_str_7, 0, 26))))); if (index < 0) throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[8], new jakelib::lang::String(chars_jakelib2_str_8, 0, 0)) ->plus((jlong) index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[9], new jakelib::lang::String(chars_jakelib2_str_9, 0, 4)) )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[10], new jakelib::lang::String(chars_jakelib2_str_10, 0, 26))))); return elementData[index]; } /*****************************************************************************\ * size | *****************************************************************************/ int ArrayList::size() { return elementCount; } /*****************************************************************************\ * set | *****************************************************************************/ Object* ArrayList::set(int index, Object* object) { if (index < 0) throw new ArrayIndexOutOfBoundsException(JAKELIB_ONDEMAND(jakelib2_strings[11], new jakelib::lang::String(chars_jakelib2_str_11, 0, 0)) ->plus( (jlong) index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[12], new jakelib::lang::String(chars_jakelib2_str_12, 0, 4)) )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[13], new jakelib::lang::String(chars_jakelib2_str_13, 0, 26))))); if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(JAKELIB_ONDEMAND(jakelib2_strings[14], new jakelib::lang::String(chars_jakelib2_str_14, 0, 0)) ->plus( (jlong) index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[15], new jakelib::lang::String(chars_jakelib2_str_15, 0, 4)) )->plus( (jlong) elementCount )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[16], new jakelib::lang::String(chars_jakelib2_str_16, 0, 26))))); Object* old = elementData[index]; elementData[index] = object; return old; } /*****************************************************************************\ * insert | *****************************************************************************/ void ArrayList::insert(int index, Object* object) { if (index < 0) throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[17], new jakelib::lang::String(chars_jakelib2_str_17, 0, 0)) ->plus( (jlong) index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[18], new jakelib::lang::String(chars_jakelib2_str_18, 0, 4)) )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[19], new jakelib::lang::String(chars_jakelib2_str_19, 0, 29))))); if (index > elementCount) throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[20], new jakelib::lang::String(chars_jakelib2_str_20, 0, 0)) ->plus( (jlong) index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[21], new jakelib::lang::String(chars_jakelib2_str_21, 0, 3)) )->plus( (jlong) elementCount )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[22], new jakelib::lang::String(chars_jakelib2_str_22, 0, 29))))); ensureCapacity(elementCount +1); for (int idx = elementCount; idx > index; idx--) { elementData[idx] = elementData[idx - 1]; } elementData[index] = object; elementCount ++; } /*****************************************************************************\ * clear | *****************************************************************************/ void ArrayList::clear() { memset(elementData, 0, sizeof(Object*) * elementCount); elementCount = 0; } /*****************************************************************************\ * indexOf | *****************************************************************************/ int ArrayList::indexOf(Object* object) { for (int idx = 0; idx < elementCount; idx++) if (elementData[idx] == object || (object != null && object->equals(elementData[idx]))) return idx; return -1; } /*****************************************************************************\ * contains | *****************************************************************************/ jboolean ArrayList::contains(Object* object) { return (indexOf(object) != -1); } /*****************************************************************************\ * removeLastElement | *****************************************************************************/ void ArrayList::removeLastElement() { if (elementCount == 0) return; elementCount --; elementData[elementCount] = null; } /*****************************************************************************\ * remove | *****************************************************************************/ jboolean ArrayList::remove(Object* object) { int idx = indexOf(object); if (idx >= 0) { remove(idx); return true; } return false; } /*****************************************************************************\ * remove | *****************************************************************************/ Object* ArrayList::remove(int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(JAKELIB_ONDEMAND(jakelib2_strings[23], new jakelib::lang::String(chars_jakelib2_str_23, 0, 0)) ->plus( (jlong) index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[24], new jakelib::lang::String(chars_jakelib2_str_24, 0, 4)) )->plus( (jlong) elementCount )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[25], new jakelib::lang::String(chars_jakelib2_str_25, 0, 29))))); } else if (index < 0) { throw new ArrayIndexOutOfBoundsException(JAKELIB_ONDEMAND(jakelib2_strings[26], new jakelib::lang::String(chars_jakelib2_str_26, 0, 0)) ->plus( (jlong) index )->plus( JAKELIB_ONDEMAND(jakelib2_strings[27], new jakelib::lang::String(chars_jakelib2_str_27, 0, 4)) )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[28], new jakelib::lang::String(chars_jakelib2_str_28, 0, 29))))); } int j = elementCount - index - 1; Object* old = elementData[index]; if (j > 0) { for (int idx = index; idx < elementCount - 1; idx++) elementData[idx] = elementData[idx + 1]; } elementCount--; elementData[elementCount] = null; return old; } /*****************************************************************************\ * isEmpty | *****************************************************************************/ jboolean ArrayList::isEmpty() { return (elementCount == 0); } /*****************************************************************************\ * iterator | *****************************************************************************/ Iterator* ArrayList::iterator() { return new ArrayListIterator(this); } /*****************************************************************************\ * qsort | *****************************************************************************/ void ArrayList::qsort(Comparator* comparator) { qsorti(comparator, 0, elementCount -1); } void ArrayList::qsorti(Comparator* comparator, int left, int right) { int part, j; Object* tmp; if (right > left) { /* Watch our bounds */ part = left; j = right - 1; /* Loop to determine partition */ for (;;) { /* Part (partition) towards the right while it's < rightmost */ while (comparator->compare( elementData[part], elementData[right] ) < 0 ) part ++; /* Move j from right-1 to Part */ while (j > part && comparator->compare(elementData[j], elementData[right] ) >= 0) j --; /* If Part and j crossed paths, or j is out of bounds, we got the Part */ if (part >= j || j<0) break; /* Otherwise, swap these lines (here's where data is moved far) */ //Swap( Lines+part, Lines+j); tmp = elementData[part]; elementData[part] = elementData[j]; elementData[j] = tmp; } if ( part != right ) { //Swap( Lines+Part, Lines+Right); tmp = elementData[part]; elementData[part] = elementData[right]; elementData[right] = tmp; } qsorti(comparator, left, part-1 ); /* Sort the left of the partition */ qsorti(comparator, part+1, right ); /* sort the right of the partition */ } } /*****************************************************************************\ * bubbleSort | *****************************************************************************/ void ArrayList::bubbleSort(Comparator* comparator, SortDirection dir) { jboolean finished; int lastElement; Object* a; Object* b; if (dir == ASCENDING) { lastElement = elementCount - 1; do { finished = true; for (int idx = 0; idx < lastElement; idx++) { a = elementData[idx]; b = elementData[idx + 1]; if (comparator->compare(a, b) > 0) { elementData[idx + 1] = a; elementData[idx] = b; finished = false; } } lastElement --; } while (!finished && lastElement > 0); } else { lastElement = 0; do { finished = true; for (int idx = elementCount -1; idx > lastElement; idx--) { a = elementData[idx]; b = elementData[idx - 1]; if (comparator->compare(a, b) > 0) { elementData[idx - 1] = a; elementData[idx] = b; finished = false; } } lastElement ++; } while (!finished && lastElement > 0); } } /*****************************************************************************\ * toString | *****************************************************************************/ String* ArrayList::toString() { StringBuffer buf(JAKELIB_ONDEMAND(jakelib2_strings[29], new jakelib::lang::String(chars_jakelib2_str_29, 0, 1))); for (int idx = 0; idx < elementCount; idx++) { if (elementData[idx] == null) buf.append(JAKELIB_ONDEMAND(jakelib2_strings[30], new jakelib::lang::String(chars_jakelib2_str_30, 0, 4))); else buf.append(elementData[idx]->toString()); if (idx < elementCount -1) buf.append(JAKELIB_ONDEMAND(jakelib2_strings[31], new jakelib::lang::String(chars_jakelib2_str_31, 0, 2))); } buf.append(JAKELIB_ONDEMAND(jakelib2_strings[32], new jakelib::lang::String(chars_jakelib2_str_32, 0, 1))); return buf.toString(); }