--- /dev/null
+/* =============================================================================
+ *
+ * bitmap.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * Ported to Java:
+ * Author:Alokika dash
+ * University of California, Irvine
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+#define NUM_BIT_PER_BYTE (8)
+#define NUM_BIT_PER_WORD (4 * NUM_BIT_PER_BYTE)
+
+public class BitMap {
+ int numBit;
+ int numWord;
+ int[] bits;
+
+ public BitMap() {
+
+ }
+
+ public int DIVIDE_AND_ROUND_UP(int a, int b) {
+ int res1 = a / b;
+ int res2 = a % b;
+ int val = (res2 > 0) ? (1) : (0);
+ return (res1 + val);
+ }
+
+
+ /* =============================================================================
+ * bitmap_alloc
+ * -- Returns null on failure
+ * =============================================================================
+ */
+ public static BitMap bitmap_alloc(int numBit)
+ {
+ BitMap bitmapPtr = new BitMap();
+ if (bitmapPtr == null) {
+ return null;
+ }
+
+ bitmapPtr.numBit = numBit;
+ int numWord = bitmapPtr.DIVIDE_AND_ROUND_UP(numBit, NUM_BIT_PER_WORD);
+ bitmapPtr.numWord = numWord;
+
+ bitmapPtr.bits = new int[numWord];
+ if (bitmapPtr.bits == null) {
+ return null;
+ }
+ for(int i = 0; i < numWord; i++)
+ bitmapPtr.bits[i] = 0;
+
+ return bitmapPtr;
+ }
+
+ /* =============================================================================
+ * bitmap_free
+ * =============================================================================
+ */
+ public void
+ bitmap_free (BitMap bitmapPtr)
+ {
+ bitmapPtr.bits = null;
+ bitmapPtr = null;
+ //free(bitmapPtr.bits);
+ //free(bitmapPtr);
+ }
+
+
+ /* =============================================================================
+ * bitmap_set
+ * -- Sets ith bit to 1
+ * -- Returns TRUE on success, else FALSE
+ * =============================================================================
+ */
+ /*
+ bool_t
+ bitmap_set (bitmap_t* bitmapPtr, int i)
+ {
+ if ((i < 0) || (i >= bitmapPtr.numBit)) {
+ return FALSE;
+ }
+
+ bitmapPtr.bits[i/NUM_BIT_PER_WORD] |= (1UL << (i % NUM_BIT_PER_WORD));
+
+ return TRUE;
+ }
+ */
+
+
+ /* =============================================================================
+ * bitmap_clear
+ * -- Clears ith bit to 0
+ * -- Returns TRUE on success, else FALSE
+ * =============================================================================
+ */
+ /*
+ bool_t
+ bitmap_clear (bitmap_t* bitmapPtr, int i)
+ {
+ if ((i < 0) || (i >= bitmapPtr.numBit)) {
+ return FALSE;
+ }
+
+ bitmapPtr.bits[i/NUM_BIT_PER_WORD] &= ~(1UL << (i % NUM_BIT_PER_WORD));
+
+ return TRUE;
+ }
+ */
+
+
+ /* =============================================================================
+ * bitmap_clearAll
+ * -- Clears all bit to 0
+ * =============================================================================
+ */
+ /*
+ void
+ bitmap_clearAll (bitmap_t* bitmapPtr)
+ {
+ memset(bitmapPtr.bits, 0, (bitmapPtr.numWord * sizeof(uint_t)));
+ }
+ */
+
+
+ /* =============================================================================
+ * bitmap_isClear
+ * -- Returns TRUE if ith bit is clear, else FALSE
+ * =============================================================================
+ */
+ /*
+ bool_t
+ bitmap_isClear (bitmap_t* bitmapPtr, int i)
+ {
+ if ((i >= 0) && (i < bitmapPtr.numBit) &&
+ !(bitmapPtr.bits[i/NUM_BIT_PER_WORD] & (1UL << (i % NUM_BIT_PER_WORD)))) {
+ return TRUE;
+ }
+
+ return FALSE;
+ }
+ */
+
+
+ /* =============================================================================
+ * bitmap_isSet
+ * -- Returns TRUE if ith bit is set, else FALSE
+ * =============================================================================
+ */
+ /*
+ bool_t
+ bitmap_isSet (bitmap_t* bitmapPtr, int i)
+ {
+ if ((i >= 0) && (i < bitmapPtr.numBit) &&
+ (bitmapPtr.bits[i/NUM_BIT_PER_WORD] & (1UL << (i % NUM_BIT_PER_WORD)))) {
+ return TRUE;
+ }
+
+ return FALSE;
+ }
+ */
+
+
+ /* =============================================================================
+ * bitmap_findClear
+ * -- Returns index of first clear bit
+ * -- If start index is negative, will start from beginning
+ * -- If all bits are set, returns -1
+ * =============================================================================
+ */
+ /*
+ int
+ bitmap_findClear (bitmap_t* bitmapPtr, int startIndex)
+ {
+ int i;
+ int numBit = bitmapPtr.numBit;
+ uint_t* bits = bitmapPtr.bits;
+
+ for (i = MAX(startIndex, 0); i < numBit; i++) {
+ if (!(bits[i/NUM_BIT_PER_WORD] & (1UL << (i % NUM_BIT_PER_WORD)))) {
+ return i;
+ }
+ }
+
+ return -1;
+ }
+ */
+
+
+ /* =============================================================================
+ * bitmap_findSet
+ * -- Returns index of first set bit
+ * -- If start index is negative, will start from beginning
+ * -- If all bits are clear, returns -1
+ * =============================================================================
+ */
+ /*
+ int
+ bitmap_findSet (bitmap_t* bitmapPtr, int startIndex)
+ {
+ int i;
+ int numBit = bitmapPtr.numBit;
+ uint_t* bits = bitmapPtr.bits;
+
+ for (i = MAX(startIndex, 0); i < numBit; i++) {
+ if (bits[i/NUM_BIT_PER_WORD] & (1UL << (i % NUM_BIT_PER_WORD))) {
+ return i;
+ }
+ }
+
+ return -1;
+ }
+ */
+
+ /* =============================================================================
+ * bitmap_getNumClear
+ * =============================================================================
+ */
+ /*
+ int
+ bitmap_getNumClear (bitmap_t* bitmapPtr)
+ {
+ int numBit = bitmapPtr.numBit;
+
+ return (numBit - bitmap_getNumSet(bitmapPtr));
+ }
+ */
+
+
+ /* =============================================================================
+ * bitmap_getNumSet
+ * =============================================================================
+ */
+ /*
+ int
+ bitmap_getNumSet (bitmap_t* bitmapPtr)
+ {
+ int i;
+ int numBit = bitmapPtr.numBit;
+ uint_t* bits = bitmapPtr.bits;
+ int count = 0;
+
+ for (i = 0; i < numBit; i++) {
+ if (bits[i/NUM_BIT_PER_WORD] & (1UL << (i % NUM_BIT_PER_WORD))) {
+ count++;
+ }
+ }
+
+ return count;
+ }
+ */
+
+
+ /* =============================================================================
+ * bitmap_copy
+ * =============================================================================
+ */
+ /*
+ void
+ bitmap_copy (bitmap_t* dstPtr, bitmap_t* srcPtr)
+ {
+ assert(dstPtr.numBit == srcPtr.numBit);
+ memcpy(dstPtr.bits, srcPtr.bits, (dstPtr.numWord * sizeof(uint_t)));
+ }
+ */
+
+
+ /* =============================================================================
+ * bitmap_toggleAll
+ * =============================================================================
+ */
+ /*
+ void
+ bitmap_toggleAll (bitmap_t* bitmapPtr)
+ {
+ uint_t* bits = bitmapPtr.bits;
+ int numWord = bitmapPtr.numWord;
+ int w;
+ for (w = 0; w < numWord; w++) {
+ bits[w] ^= (uint_t)(-1L);
+ }
+ }
+ */
+}
+
+/* =============================================================================
+ *
+ * End of bitmap.java
+ *
+ * =============================================================================
+ */
--- /dev/null
+/* =============================================================================
+ *
+ * queue.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * Ported to Java
+ * Author:Alokika Dash
+ * University of California, Irvine
+ *
+ * =============================================================================
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+#define QUEUE_GROWTH_FACTOR 2
+
+public class Queue {
+ int pop; /* points before element to pop */
+ int push;
+ int capacity;
+ Object[] elements;
+
+ public Queue() {
+ }
+
+ /* =============================================================================
+ * queue_alloc
+ * =============================================================================
+ */
+ public static Queue queue_alloc (int initCapacity)
+ {
+ Queue queuePtr = new Queue();
+
+ int capacity = ((initCapacity < 2) ? 2 : initCapacity);
+ queuePtr.elements = new Object[capacity];
+ if (queuePtr.elements == null) {
+ queuePtr = null;
+ return null;
+ }
+ queuePtr.pop = capacity - 1;
+ queuePtr.push = 0;
+ queuePtr.capacity = capacity;
+
+ return queuePtr;
+ }
+
+
+ /* =============================================================================
+ * Pqueue_alloc
+ * =============================================================================
+ */
+ public Queue
+ Pqueue_alloc (int initCapacity)
+ {
+ Queue queuePtr = new Queue();
+
+ int capacity = ((initCapacity < 2) ? 2 : initCapacity);
+ queuePtr.elements = new Object[capacity];
+ if (queuePtr.elements == null) {
+ queuePtr = null;
+ return null;
+ }
+ queuePtr.pop = capacity - 1;
+ queuePtr.push = 0;
+ queuePtr.capacity = capacity;
+
+ return queuePtr;
+ }
+
+
+ /* =============================================================================
+ * TMqueue_alloc
+ * =============================================================================
+ */
+ public Queue TMqueue_alloc (int initCapacity)
+ {
+ Queue queuePtr = new Queue();
+
+ int capacity = ((initCapacity < 2) ? 2 : initCapacity);
+ queuePtr.elements = new Object[capacity];
+ if (queuePtr.elements == null) {
+ queuePtr = null;
+ return null;
+ }
+ queuePtr.pop = capacity - 1;
+ queuePtr.push = 0;
+ queuePtr.capacity = capacity;
+
+ return queuePtr;
+ }
+
+
+ /* =============================================================================
+ * queue_free
+ * =============================================================================
+ */
+ public void
+ queue_free (Queue queuePtr)
+ {
+ queuePtr.elements = null;
+ queuePtr = null;
+ }
+
+
+ /* =============================================================================
+ * Pqueue_free
+ * =============================================================================
+ */
+ public void
+ Pqueue_free (Queue queuePtr)
+ {
+ queuePtr.elements = null;
+ queuePtr = null;
+ }
+
+
+ /* =============================================================================
+ * TMqueue_free
+ * =============================================================================
+ */
+ public void
+ TMqueue_free (TM_ARGDECL Queue* queuePtr)
+ {
+ queuePtr.elements = null;
+ queuePtr = null;
+ }
+
+
+ /* =============================================================================
+ * queue_isEmpty
+ * =============================================================================
+ */
+ public boolean
+ queue_isEmpty (Queue queuePtr)
+ {
+ int pop = queuePtr.pop;
+ int push = queuePtr.push;
+ int capacity = queuePtr.capacity;
+
+ return (((pop + 1) % capacity == push) ? true : false);
+ }
+
+
+ /* =============================================================================
+ * queue_clear
+ * =============================================================================
+ */
+ public void
+ queue_clear (Queue* queuePtr)
+ {
+ queuePtr.pop = queuePtr.capacity - 1;
+ queuePtr.push = 0;
+ }
+
+
+ /* =============================================================================
+ * TMqueue_isEmpty
+ * =============================================================================
+ */
+ public boolean
+ TMqueue_isEmpty (Queue queuePtr)
+ {
+ int pop = queuePtr.pop;
+ int push = queuePtr.push;
+ int capacity = queuePtr.capacity;
+
+ return (((pop + 1) % capacity == push) ? true : false);
+ }
+
+
+ /* =============================================================================
+ * queue_shuffle
+ * =============================================================================
+ */
+ public void
+ queue_shuffle (Queue queuePtr, Random randomPtr)
+ {
+ int pop = queuePtr.pop;
+ int push = queuePtr.push;
+ int capacity = queuePtr.capacity;
+
+ int numElement;
+ if (pop < push) {
+ numElement = push - (pop + 1);
+ } else {
+ numElement = capacity - (pop - push + 1);
+ }
+
+ Object[] elements = queuePtr.elements;
+ int i;
+ int base = pop + 1;
+ for (i = 0; i < numElement; i++) {
+ int r1 = randomPtr.random_generate() % numElement;
+ int r2 = randomPtr.random_generate() % numElement;
+ int i1 = (base + r1) % capacity;
+ int i2 = (base + r2) % capacity;
+ Object tmp = elements[i1];
+ elements[i1] = elements[i2];
+ elements[i2] = tmp;
+ }
+ }
+
+
+ /* =============================================================================
+ * queue_push
+ * =============================================================================
+ */
+ public boolean
+ queue_push (Queue queuePtr, Object dataPtr)
+ {
+ int pop = queuePtr.pop;
+ int push = queuePtr.push;
+ int capacity = queuePtr.capacity;
+
+ if(pop == push) {
+ System.out.println("push == pop in Queue.java");
+ return false;
+ }
+
+ /* Need to resize */
+ int newPush = (push + 1) % capacity;
+ if (newPush == pop) {
+
+ int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
+ Object[] newElements = new Object[newCapacity];
+ if (newElements == null) {
+ return false;
+ }
+
+ int dst = 0;
+ Object[] elements = queuePtr.elements;
+ if (pop < push) {
+ int src;
+ for (src = (pop + 1); src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ } else {
+ int src;
+ for (src = (pop + 1); src < capacity; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ for (src = 0; src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ }
+
+ elements = null;
+ queuePtr.elements = newElements;
+ queuePtr.pop = newCapacity - 1;
+ queuePtr.capacity = newCapacity;
+ push = dst;
+ newPush = push + 1; /* no need modulo */
+ }
+
+ queuePtr.elements[push] = dataPtr;
+ queuePtr.push = newPush;
+
+ return true;
+ }
+
+
+ /* =============================================================================
+ * Pqueue_push
+ * =============================================================================
+ */
+ public boolean
+ Pqueue_push (Queue queuePtr, Object dataPtr)
+ {
+ int pop = queuePtr.pop;
+ int push = queuePtr.push;
+ int capacity = queuePtr.capacity;
+
+ if(pop == push) {
+ System.out.println("push == pop in Queue.java");
+ return false;
+ }
+
+ /* Need to resize */
+ int newPush = (push + 1) % capacity;
+ if (newPush == pop) {
+
+ int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
+ Object[] newElements = new Object[newCapacity];
+ if (newElements == null) {
+ return false;
+ }
+
+ int dst = 0;
+ Object[] elements = queuePtr.elements;
+ if (pop < push) {
+ int src;
+ for (src = (pop + 1); src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ } else {
+ int src;
+ for (src = (pop + 1); src < capacity; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ for (src = 0; src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ }
+
+ elements = null;
+ queuePtr.elements = newElements;
+ queuePtr.pop = newCapacity - 1;
+ queuePtr.capacity = newCapacity;
+ push = dst;
+ newPush = push + 1; /* no need modulo */
+
+ }
+
+ queuePtr.elements[push] = dataPtr;
+ queuePtr.push = newPush;
+
+ return true;
+ }
+
+
+ /* =============================================================================
+ * TMqueue_push
+ * =============================================================================
+ */
+ public boolean
+ TMqueue_push (Queue queuePtr, Object dataPtr)
+ {
+ int pop = (queuePtr.pop);
+ int push = (queuePtr.push);
+ int capacity = (queuePtr.capacity);
+
+ if(pop == push) {
+ System.out.println("push == pop in Queue.java");
+ return false;
+ }
+
+ /* Need to resize */
+ int newPush = (push + 1) % capacity;
+ if (newPush == pop) {
+ int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
+ Object[] newElements = new Object[newCapacity];
+ if (newElements == null) {
+ return false;
+ }
+
+ int dst = 0;
+ Object[] elements = queuePtr.elements;
+ if (pop < push) {
+ int src;
+ for (src = (pop + 1); src < push; src++, dst++) {
+ newElements[dst] = (elements[src]);
+ }
+ } else {
+ int src;
+ for (src = (pop + 1); src < capacity; src++, dst++) {
+ newElements[dst] = (elements[src]);
+ }
+ for (src = 0; src < push; src++, dst++) {
+ newElements[dst] = (elements[src]);
+ }
+ }
+
+ elements = null;
+ queuePtr.elements = newElements;
+ queuePtr.pop = newCapacity - 1;
+ queuePtr.capacity = newCapacity;
+ push = dst;
+ newPush = push + 1; /* no need modulo */
+
+ }
+
+ Object[] elements = queuePtr.elements;
+ elements[push] = dataPtr;
+ queuePtr.push = newPush;
+
+ return true;
+ }
+
+
+ /* =============================================================================
+ * queue_pop
+ * =============================================================================
+ */
+ public Object
+ queue_pop (Queue queuePtr)
+ {
+ int pop = queuePtr.pop;
+ int push = queuePtr.push;
+ int capacity = queuePtr.capacity;
+
+ int newPop = (pop + 1) % capacity;
+ if (newPop == push) {
+ return null;
+ }
+
+ Object dataPtr = queuePtr.elements[newPop];
+ queuePtr.pop = newPop;
+
+ return dataPtr;
+ }
+
+
+ /* =============================================================================
+ * TMqueue_pop
+ * =============================================================================
+ */
+ public Object
+ TMqueue_pop (Queue queuePtr)
+ {
+ int pop = queuePtr.pop;
+ int push = queuePtr.push;
+ int capacity = queuePtr.capacity;
+
+ int newPop = (pop + 1) % capacity;
+ if (newPop == push) {
+ return null;
+ }
+
+ Object[] elements = queuePtr.elements;
+ Object dataPtr = elements[newPop];
+ queuePtr.pop = newPop;
+
+ return dataPtr;
+ }
+}
+/* =============================================================================
+ *
+ * End of queue.java
+ *
+ * =============================================================================
+ */
--- /dev/null
+public class QuickSort {
+
+ public QuickSort() {
+
+ }
+
+ public static void swap (int A[], int x, int y)
+ {
+ int temp = A[x];
+ A[x] = A[y];
+ A[y] = temp;
+ }
+
+ // Reorganizes the given list so all elements less than the first are
+ // before it and all greater elements are after it.
+ public static int partition(int A[], int left, int right)
+ {
+ int i = left - 1;
+ int j = right;
+ while (true) {
+ while (less(a[++i], a[right])) // find item on left to swap
+ ; // a[right] acts as sentinel
+ while (less(a[right], a[--j])) // find item on right to swap
+ if (j == left) break; // don't go out-of-bounds
+ if (i >= j) break; // check if pointers cross
+ swap(a, i, j); // swap two elements into place
+ }
+ swap(a, i, right); // swap with partition element
+ return i;
+ }
+
+ public static void Quicksort(int A[], int f, int l)
+ {
+ if (f >= l) return;
+ int pivot_index = partition(A, f, l);
+ Quicksort(A, f, pivot_index);
+ Quicksort(A, pivot_index+1, l);
+ }
+}
--- /dev/null
+public class Vector_t {
+ int size;
+ int capacity;
+ Object[] elements;
+
+ public Vector_t() {
+
+ }
+
+ /* =============================================================================
+ * Vector_alloc
+ * -- Returns null if failed
+ * =============================================================================
+ */
+ public static Vector_t vector_alloc (int initCapacity) {
+ int capacity = Math.max(initCapacity, 1);
+ Vector_t vectorPtr = new Vector();
+ if(vectorPtr != null) {
+ vectorPtr.size = 0;
+ vectorPtr.capacity = capacity;
+ vectorPtr.elements = new Object[capacity];
+ if(vectorPtr.elements == null)
+ return null;
+ }
+ return vectorPtr;
+ }
+
+ /* =============================================================================
+ * Vector_free
+ * =============================================================================
+ */
+ public void
+ vector_free ()
+ {
+ elements = null;
+ }
+
+ /* =============================================================================
+ * Vector_at
+ * -- Returns null if failed
+ * =============================================================================
+ */
+ public static Object vector_at (Vector_t vectorPtr, int i) {
+ if ((i < 0) || (i >= vectorPtr.size)) {
+ System.out.println("Illegal Vector.element\n");
+ return null;
+ }
+ return (vectorPtr.elements[i]);
+ }
+
+
+ /* =============================================================================
+ * Vector_pushBack
+ * -- Returns false if fail, else true
+ * =============================================================================
+ */
+ public boolean vector_pushBack (Object dataPtr) {
+ if (size == capacity) {
+ int newCapacity = capacity * 2;
+ Object[] newElements = new Object[newCapacity];
+
+ //void** newElements = (void**)malloc(newCapacity * sizeof(void*));
+ if (newElements == null) {
+ return false;
+ }
+ capacity = newCapacity;
+ for (int i = 0; i < size; i++) {
+ newElements[i] = elements[i];
+ }
+ elements = null;
+ elements = newElements;
+ }
+
+ elements[size++] = dataPtr;
+
+ return true;
+ }
+
+ /* =============================================================================
+ * Vector_popBack
+ * -- Returns null if fail, else returns last element
+ * =============================================================================
+ */
+ public Object
+ vector_popBack ()
+ {
+ if (size < 1) {
+ return null;
+ }
+
+ return (elements[--(size)]);
+ }
+
+ /* =============================================================================
+ * Vector_getSize
+ * =============================================================================
+ */
+ public int
+ vector_getSize ()
+ {
+ return (size);
+ }
+
+ /* =============================================================================
+ * Vector_clear
+ * =============================================================================
+ */
+ public void
+ vector_clear ()
+ {
+ size = 0;
+ }
+
+ /* =============================================================================
+ * Vector_sort
+ * =============================================================================
+ */
+ public void
+ vector_sort ()
+ {
+ QuickSort.Quicksort(elements, 0, elements.length - 1);
+ //qsort(elements, size, 4, compare);
+ }
+
+ /* =============================================================================
+ * Vector_copy
+ * =============================================================================
+ */
+ public static boolean
+ vector_copy (Vector_t dstVectorPtr, Vector_t srcVectorPtr)
+ {
+ int dstCapacity = dstVectorPtr.capacity;
+ int srcSize = srcVectorPtr.size;
+ if (dstCapacity < srcSize) {
+ int srcCapacity = srcVectorPtr.capacity;
+ Object[] elements = new Object[srcCapacity];
+
+ if (elements == null) {
+ return false;
+ }
+ dstVectorPtr.elements = null;
+ dstVectorPtr.elements = elements;
+ dstVectorPtr.capacity = srcCapacity;
+ }
+
+ for(int i = 0; i< srcSize; i++) {
+ dstVectorPtr.elements[i] = srcVectorPtr.elements[i];
+ }
+
+ dstVectorPtr.size = srcSize;
+
+ return true;
+ }
+}