/maverick/tags/v0_3/src/org/maverickdbms/basic/mvArray.java
# · Java · 287 lines · 192 code · 26 blank · 69 comment · 61 complexity · 0d2da3fcd5a8ea9401159e3a70560c79 MD5 · raw file
- /**
- Copyright (c) 1999-2001 by Robert J Colquhoun, All Rights Reserved
- 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
- */
- package org.maverickdbms.basic;
- import java.util.BitSet;
- /**
- * Class to represent fixed size arrays.
- */
- public class mvArray implements mvVariable {
-
- private Factory factory;
-
- private int[] dimension;
- private mvString[] array;
-
- mvArray(Factory f) {
- factory = f;
- }
- /** Dimensions the array.
- * @param arraysize array containing the dimensions of the array
- */
- public void DIM(mvConstantString[] arraysize) {
- dimension = new int[arraysize.length];
- int size = 1;
- for (int i = 0; i < arraysize.length; i++) {
- dimension[i] = arraysize[i].intValue();
- size *= dimension[i];
- }
- if (array != null && array.length == size + 1) {
- return;
- }
- mvString[] oldarray = array;
- int len = (oldarray != null) ? oldarray.length : 0;
- array = new mvString[size + 1];
- if (array.length > len) {
- for (int i = len; i < size + 1; i++) {
- array[i] = factory.getString();
- }
- } else {
- len = array.length;
- for (int i = len; i < oldarray.length; i++) {
- factory.putString(oldarray[i]);
- }
- }
- if (len > 0) {
- System.arraycopy(oldarray, 0, array, 0, len);
- }
- }
-
- public mvString INMAT(mvString val) {
- if (array != null && array.length > 0 && array[0].ASSIGNED().equals(mvConstantString.ZERO)) {
- int size = 1;
- for (int i = 0; i < dimension.length; i++) {
- size *= dimension[i];
- }
- val.set(size);
- } else {
- val.set(0);
- }
- return val;
- }
- public void reference(mvString[] arr, int offset, int[] dim) {
- int size = 1;
- for (int i = 0; i < dim.length; i++) {
- size *= dim[i];
- }
- array = new mvString[size + 1];
- System.arraycopy(arr, offset, array, 1, size);
- //assume overflow 0th element is not included in reference
- array[0] = factory.getString();
- dimension = dim;
- //XXX need to add a read-only flag to class to avoid factory.putString()
- }
- /**
- * Assigns a string to every value in the array.
- * @param mvs the string to assign to each element
- */
- public void MAT(mvConstantString mvs) {
- int size = size();
- for (int i = 0; i < size; i++) {
- if (array[i] == null) {
- array[i] = factory.getString();
- }
- array[i].set(mvs);
- }
- }
-
- /**
- * Copies the array.
- * @param a the array from which to assign to each element
- */
- public void MAT(mvArray a) {
- int size = size();
- int asize = a.size();
- //XXX if size != asize ?????
- for (int i = 0; i < size; i++) {
- if (a.array[i] != null) {
- if (array[i] == null) {
- array[i] = factory.getString();
- }
- array[i].set(a.array[i]);
- } else {
- if (array[i] != null) {
- factory.putString(array[i]);
- }
- array[i] = null;
- }
- }
- }
- public void MATBUILD(mvString result, mvConstantString start, mvConstantString end, mvConstantString delimiter) {
- int s = start.intValue();
- if (s <= 0) s = 1;
- int e = end.intValue();
- if (e <= 0 || e > array.length) e = array.length;
- //dont include any trailing empty elements in the result
- while (array[e - 1].ASSIGNED().equals(mvConstantString.ZERO)) e--;
- if (e - s > 0) {
- result.set(array[s++]);
- while (s < e) {
- result.append(delimiter);
- result.append(array[s++]);
- }
- } else {
- result.clear();
- }
- }
-
- /**
- * Parse a dynamic array into the mvArray.
- * @param count number of elements in the array
- * @param s the dynamic array to parse
- * @param start the starting position in array
- * @param end the finishing position in array
- * @param delimiter the delimiter to separate elements
- */
- public void MATPARSE(mvString count, mvConstantString s, mvConstantString start, mvConstantString end, mvConstantString delimiter) {
- int begin = start.intValue();
- if (begin < 1) begin = 1;
- int end2 = end.intValue();
- if (end2 < 1 || end2 > array.length) end2 = array.length;
- int index = begin;
- array[0].clear(); //clear any existing overflow
- count.set(0);
- int dlength = delimiter.length();
- int slength = s.length();
- if (dlength > 0) {
- int max = 0;
- int min = 65535;
- for (int i = 0; i < dlength; i++) {
- char c = delimiter.charAt(i);
- if (c > max) max = c;
- if (c < min) min = c;
- }
- BitSet delims = new BitSet(max - min + 1);
- for (int i = 0; i < dlength; i++) {
- delims.set(delimiter.charAt(i) - min);
- }
-
- int sstart = 0;
- for (int i = 0; i < slength; i++) {
- char c = s.charAt(i);
- if (c <= max && c >= min && delims.get(c - min)) {
- if (index >= end2) {
- array[0].set(s, sstart, slength - sstart);
- return;
- }
- array[index++].set(s, sstart, i - sstart);
- if (max > min) {
- array[index].set(c);
- while (i + 1 < slength && s.charAt(i + 1) == c) {
- array[index].append(c);
- i++;
- }
- index++;
- }
- sstart = i + 1;
- }
- }
- if (sstart < slength) {
- if (index >= end2) {
- array[0].set(s, sstart, slength - sstart);
- return;
- }
- array[index++].set(s, sstart, slength - sstart);
- }
- } else {
- for (int i = 0; i < slength; i++) {
- if (index >= end2) {
- array[0].set(s, i, slength - i);
- return;
- }
- array[index++].set(s.charAt(i));
- }
- }
- count.set(index - begin);
- }
- /**
- * Get array element.
- * @param position Array containing position of item
- * @return reference to the array element
- */
- public mvString get(mvConstantString[] position) {
- int index = 0;
- int offset = 1;
- for (int i = position.length - 1; i >= 0 ; i--) {
- index += position[i].intValue() * offset;
- offset *= dimension[i];
- }
- return array[index];
- }
-
- /**
- * Get array element.
- * @param a position of item
- * @return reference to the array element
- */
- public mvString get(mvConstantString a) {
- return array[a.intValue()];
- }
-
- /**
- * Get array element.
- * @param a position of item
- * @return reference to the array element
- */
- public mvString get(mvConstantString a, mvConstantString b) {
- //XXX i am not sure about this...needs some checking...
- int a1 = a.intValue();
- if (a1 > 0) {
- a1 = (a1 - 1) * dimension[1];
- }
- int b1 = (b.intValue() - 1);
- return array[a1 + b1 + 1];
- }
-
- /**
- * Get array element.
- * @param a position of item
- * @return reference to the array element
- */
- public mvString get(mvConstantString a, mvConstantString b, mvConstantString c) {
- return array[a.intValue() * dimension[0] + b.intValue() * dimension[1] + c.intValue() * dimension[2]];
- }
- public int size() {
- int val = 0;
- for (int i = 0; i < dimension.length; i++) val += dimension[i];
- return val;
- }
- /**
- * Access undelying string array
- */
- public mvString[] toStringArray() {
- return array;
- }
- }