/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

  1. /**
  2. Copyright (c) 1999-2001 by Robert J Colquhoun, All Rights Reserved
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. package org.maverickdbms.basic;
  16. import java.util.BitSet;
  17. /**
  18. * Class to represent fixed size arrays.
  19. */
  20. public class mvArray implements mvVariable {
  21. private Factory factory;
  22. private int[] dimension;
  23. private mvString[] array;
  24. mvArray(Factory f) {
  25. factory = f;
  26. }
  27. /** Dimensions the array.
  28. * @param arraysize array containing the dimensions of the array
  29. */
  30. public void DIM(mvConstantString[] arraysize) {
  31. dimension = new int[arraysize.length];
  32. int size = 1;
  33. for (int i = 0; i < arraysize.length; i++) {
  34. dimension[i] = arraysize[i].intValue();
  35. size *= dimension[i];
  36. }
  37. if (array != null && array.length == size + 1) {
  38. return;
  39. }
  40. mvString[] oldarray = array;
  41. int len = (oldarray != null) ? oldarray.length : 0;
  42. array = new mvString[size + 1];
  43. if (array.length > len) {
  44. for (int i = len; i < size + 1; i++) {
  45. array[i] = factory.getString();
  46. }
  47. } else {
  48. len = array.length;
  49. for (int i = len; i < oldarray.length; i++) {
  50. factory.putString(oldarray[i]);
  51. }
  52. }
  53. if (len > 0) {
  54. System.arraycopy(oldarray, 0, array, 0, len);
  55. }
  56. }
  57. public mvString INMAT(mvString val) {
  58. if (array != null && array.length > 0 && array[0].ASSIGNED().equals(mvConstantString.ZERO)) {
  59. int size = 1;
  60. for (int i = 0; i < dimension.length; i++) {
  61. size *= dimension[i];
  62. }
  63. val.set(size);
  64. } else {
  65. val.set(0);
  66. }
  67. return val;
  68. }
  69. public void reference(mvString[] arr, int offset, int[] dim) {
  70. int size = 1;
  71. for (int i = 0; i < dim.length; i++) {
  72. size *= dim[i];
  73. }
  74. array = new mvString[size + 1];
  75. System.arraycopy(arr, offset, array, 1, size);
  76. //assume overflow 0th element is not included in reference
  77. array[0] = factory.getString();
  78. dimension = dim;
  79. //XXX need to add a read-only flag to class to avoid factory.putString()
  80. }
  81. /**
  82. * Assigns a string to every value in the array.
  83. * @param mvs the string to assign to each element
  84. */
  85. public void MAT(mvConstantString mvs) {
  86. int size = size();
  87. for (int i = 0; i < size; i++) {
  88. if (array[i] == null) {
  89. array[i] = factory.getString();
  90. }
  91. array[i].set(mvs);
  92. }
  93. }
  94. /**
  95. * Copies the array.
  96. * @param a the array from which to assign to each element
  97. */
  98. public void MAT(mvArray a) {
  99. int size = size();
  100. int asize = a.size();
  101. //XXX if size != asize ?????
  102. for (int i = 0; i < size; i++) {
  103. if (a.array[i] != null) {
  104. if (array[i] == null) {
  105. array[i] = factory.getString();
  106. }
  107. array[i].set(a.array[i]);
  108. } else {
  109. if (array[i] != null) {
  110. factory.putString(array[i]);
  111. }
  112. array[i] = null;
  113. }
  114. }
  115. }
  116. public void MATBUILD(mvString result, mvConstantString start, mvConstantString end, mvConstantString delimiter) {
  117. int s = start.intValue();
  118. if (s <= 0) s = 1;
  119. int e = end.intValue();
  120. if (e <= 0 || e > array.length) e = array.length;
  121. //dont include any trailing empty elements in the result
  122. while (array[e - 1].ASSIGNED().equals(mvConstantString.ZERO)) e--;
  123. if (e - s > 0) {
  124. result.set(array[s++]);
  125. while (s < e) {
  126. result.append(delimiter);
  127. result.append(array[s++]);
  128. }
  129. } else {
  130. result.clear();
  131. }
  132. }
  133. /**
  134. * Parse a dynamic array into the mvArray.
  135. * @param count number of elements in the array
  136. * @param s the dynamic array to parse
  137. * @param start the starting position in array
  138. * @param end the finishing position in array
  139. * @param delimiter the delimiter to separate elements
  140. */
  141. public void MATPARSE(mvString count, mvConstantString s, mvConstantString start, mvConstantString end, mvConstantString delimiter) {
  142. int begin = start.intValue();
  143. if (begin < 1) begin = 1;
  144. int end2 = end.intValue();
  145. if (end2 < 1 || end2 > array.length) end2 = array.length;
  146. int index = begin;
  147. array[0].clear(); //clear any existing overflow
  148. count.set(0);
  149. int dlength = delimiter.length();
  150. int slength = s.length();
  151. if (dlength > 0) {
  152. int max = 0;
  153. int min = 65535;
  154. for (int i = 0; i < dlength; i++) {
  155. char c = delimiter.charAt(i);
  156. if (c > max) max = c;
  157. if (c < min) min = c;
  158. }
  159. BitSet delims = new BitSet(max - min + 1);
  160. for (int i = 0; i < dlength; i++) {
  161. delims.set(delimiter.charAt(i) - min);
  162. }
  163. int sstart = 0;
  164. for (int i = 0; i < slength; i++) {
  165. char c = s.charAt(i);
  166. if (c <= max && c >= min && delims.get(c - min)) {
  167. if (index >= end2) {
  168. array[0].set(s, sstart, slength - sstart);
  169. return;
  170. }
  171. array[index++].set(s, sstart, i - sstart);
  172. if (max > min) {
  173. array[index].set(c);
  174. while (i + 1 < slength && s.charAt(i + 1) == c) {
  175. array[index].append(c);
  176. i++;
  177. }
  178. index++;
  179. }
  180. sstart = i + 1;
  181. }
  182. }
  183. if (sstart < slength) {
  184. if (index >= end2) {
  185. array[0].set(s, sstart, slength - sstart);
  186. return;
  187. }
  188. array[index++].set(s, sstart, slength - sstart);
  189. }
  190. } else {
  191. for (int i = 0; i < slength; i++) {
  192. if (index >= end2) {
  193. array[0].set(s, i, slength - i);
  194. return;
  195. }
  196. array[index++].set(s.charAt(i));
  197. }
  198. }
  199. count.set(index - begin);
  200. }
  201. /**
  202. * Get array element.
  203. * @param position Array containing position of item
  204. * @return reference to the array element
  205. */
  206. public mvString get(mvConstantString[] position) {
  207. int index = 0;
  208. int offset = 1;
  209. for (int i = position.length - 1; i >= 0 ; i--) {
  210. index += position[i].intValue() * offset;
  211. offset *= dimension[i];
  212. }
  213. return array[index];
  214. }
  215. /**
  216. * Get array element.
  217. * @param a position of item
  218. * @return reference to the array element
  219. */
  220. public mvString get(mvConstantString a) {
  221. return array[a.intValue()];
  222. }
  223. /**
  224. * Get array element.
  225. * @param a position of item
  226. * @return reference to the array element
  227. */
  228. public mvString get(mvConstantString a, mvConstantString b) {
  229. //XXX i am not sure about this...needs some checking...
  230. int a1 = a.intValue();
  231. if (a1 > 0) {
  232. a1 = (a1 - 1) * dimension[1];
  233. }
  234. int b1 = (b.intValue() - 1);
  235. return array[a1 + b1 + 1];
  236. }
  237. /**
  238. * Get array element.
  239. * @param a position of item
  240. * @return reference to the array element
  241. */
  242. public mvString get(mvConstantString a, mvConstantString b, mvConstantString c) {
  243. return array[a.intValue() * dimension[0] + b.intValue() * dimension[1] + c.intValue() * dimension[2]];
  244. }
  245. public int size() {
  246. int val = 0;
  247. for (int i = 0; i < dimension.length; i++) val += dimension[i];
  248. return val;
  249. }
  250. /**
  251. * Access undelying string array
  252. */
  253. public mvString[] toStringArray() {
  254. return array;
  255. }
  256. }