source: ip/src/main/java/geniusweb/ip/general/General.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 30.2 KB
Line 
1package geniusweb.ip.general;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileReader;
6import java.io.FileWriter;
7import java.text.DecimalFormat;
8import java.util.Arrays;
9import java.util.Random;
10
11public class General {
12 /**
13 * Return the facorial of n (i.e. returns n!)
14 */
15 public static long factorial(int n) {
16 long n_factorial = 1;
17 for (int i = 1; i <= n; i++)
18 n_factorial = n_factorial * i;
19 return (n_factorial);
20 }
21
22 // ************************************************************************************************
23
24 /**
25 * Return the multiplicity of "element" in the multiset "arrayOfElements"
26 */
27 public static int getMultiplicity(int element, int[] arrayOfElements) {
28 int result = 0;
29 for (int j = 0; j < arrayOfElements.length; j++)
30 if (arrayOfElements[j] == element)
31 result++;
32 return (result);
33 }
34
35 // ************************************************************************************************
36
37 /**
38 * Converts a multiset to an array of integers
39 */
40 public static int[] convertMultisetToArray(ElementOfMultiset[] multiset) {
41 // Count the total number of elements in the multiset
42 int counter = 0;
43 for (int i = 0; i < multiset.length; i++)
44 counter += multiset[i].repetition;
45
46 int[] array = new int[counter];
47 int index = 0;
48 for (int i = 0; i < multiset.length; i++) {
49 for (int j = 0; j < multiset[i].repetition; j++) {
50 array[index] = multiset[i].element;
51 index++;
52 }
53 }
54 return (array);
55 }
56
57 // ************************************************************************************************
58
59 /**
60 * Converts a multiset to an array of integers
61 */
62 public static ElementOfMultiset[] convertArrayToMultiset(int[] array) {
63 int[] underlyingSet = getUnderlyingSet(array);
64
65 ElementOfMultiset[] multiset = new ElementOfMultiset[underlyingSet.length];
66 for (int i = 0; i < multiset.length; i++) {
67 multiset[i] = new ElementOfMultiset(underlyingSet[i], 0);
68 for (int j = 0; j < array.length; j++)
69 if (multiset[i].element == array[j])
70 multiset[i].repetition++;
71 }
72 return (multiset);
73 }
74
75 // ************************************************************************************************
76
77 /**
78 * returns the total number of elements in the multiset
79 */
80 public static int getCardinalityOfMultiset(ElementOfMultiset[] multiset) {
81 if (multiset == null) {
82 return (0);
83 }
84 int counter = 0;
85 for (int i = 0; i < multiset.length; i++) {
86 counter += multiset[i].repetition;
87 }
88 return (counter);
89 }
90
91 // ************************************************************************************************
92
93 /**
94 * returns array minus multiset
95 */
96 public static int[] setMinus(int[] array, ElementOfMultiset[] multiset) {
97 int[] tempArray = General.copyArray(array);
98 for (int i = 0; i < tempArray.length; i++) {
99 for (int j = 0; j < multiset.length; j++) {
100 if ((tempArray[i] == multiset[j].element)
101 && (multiset[j].repetition > 0)) {
102 tempArray[i] = -1;
103 multiset[j].repetition--;
104 break;
105 }
106 }
107 }
108 // count the elements that have not been deleted
109 int counter = 0;
110 for (int i = 0; i < tempArray.length; i++) {
111 if (tempArray[i] > -1) {
112 counter++;
113 }
114 }
115 // Get rid of any elements that have been deleted
116 if (counter == 0) {
117 return (null);
118 } else {
119 int[] result = new int[counter];
120 int index = 0;
121 for (int i = 0; i < tempArray.length; i++)
122 if (tempArray[i] > -1) {
123 result[index] = tempArray[i];
124 index++;
125 }
126 return (result);
127 }
128 }
129
130 // ************************************************************************************************
131
132 /**
133 * returns multiset1 minus multiset2
134 */
135 public static ElementOfMultiset[] setMinus(ElementOfMultiset[] multiset1,
136 ElementOfMultiset[] multiset2) {
137 ElementOfMultiset[] tempMultiset = copyMultiset(multiset1);
138 for (int i = 0; i < tempMultiset.length; i++) {
139 for (int j = 0; j < multiset2.length; j++) {
140 if (tempMultiset[i].element == multiset2[j].element) {
141 tempMultiset[i].repetition -= multiset2[j].repetition;
142 if (tempMultiset[i].repetition < 0)
143 tempMultiset[i].repetition = 0;
144 break;
145 }
146 }
147 }
148 // count the elements that are repeated more than 0 times
149 int counter = 0;
150 for (int i = 0; i < tempMultiset.length; i++) {
151 if (tempMultiset[i].repetition > 0) {
152 counter++;
153 }
154 }
155 // Get rid of any elements that are repeated 0 times, and return the
156 // remaining
157 if (counter == 0) {
158 return (null);
159 } else {
160 ElementOfMultiset[] result = new ElementOfMultiset[counter];
161 int index = 0;
162 for (int i = 0; i < tempMultiset.length; i++)
163 if (tempMultiset[i].repetition > 0) {
164 result[index] = new ElementOfMultiset(
165 tempMultiset[i].element,
166 tempMultiset[i].repetition);
167 index++;
168 }
169 return (result);
170 }
171 }
172
173 // ************************************************************************************************
174
175 /**
176 * Get the underlying set of a multiset of Objects
177 */
178 public static int[] getUnderlyingSet(int[] array) {
179 // Initialization
180 int numOfUniqueElements = 0;
181 int[] uniqueElements = new int[array.length];
182 for (int i = 0; i < uniqueElements.length; i++)
183 uniqueElements[i] = 0;
184
185 // Counting the number of unique elements in the integerPartition
186 for (int i = 0; i < array.length; i++) {
187 boolean weHaveSeenThisElementBefore = false;
188 for (int j = 0; j < numOfUniqueElements; j++) {
189 if (uniqueElements[j] == array[i]) {
190 weHaveSeenThisElementBefore = true;
191 break;
192 }
193 }
194 if (weHaveSeenThisElementBefore == false) {
195 uniqueElements[numOfUniqueElements] = array[i];
196 numOfUniqueElements++;
197 }
198 }
199 // Setting the elements of the underlying set
200 int[] underlyingSet = new int[numOfUniqueElements];
201 for (int i = 0; i < numOfUniqueElements; i++)
202 underlyingSet[i] = uniqueElements[i];
203
204 return (underlyingSet);
205 }
206
207 // ************************************************************************************************
208
209 /**
210 * Removes from "string" any characters that come after "c"
211 */
212 public String trimStringAfterChar(String string, char c) {
213 int last;
214 if (string.indexOf(c) == -1)
215 last = string.length();
216 else
217 last = string.indexOf(c);
218 return (string.substring(0, last));
219 }
220
221 // ************************************************************************************************
222
223 /**
224 * Given a real number, set the number of digits after the decimal point to
225 * be equal to "precision"
226 */
227 public static String setDecimalPrecision(double doubleValue,
228 int precision) {
229 String precisionString = "0.0";
230 for (int i = 2; i <= precision; i++) {
231 precisionString += "#";
232 }
233 return ((new DecimalFormat(precisionString)).format(doubleValue));
234 }
235
236 // ************************************************************************************************
237
238 /**
239 * Compares two array, and considers them identical if they contain the same
240 * elements even if they are ordered differently
241 *
242 * @return "true" if the elements are identical, even if the order of the
243 * elements is different
244 */
245 public static boolean compareTwoArrays_ignoreElementOrder(int[] array1,
246 int[] array2) {
247 // Compare the length
248 if (array1.length != array2.length) {
249 return (false);
250 }
251
252 // sort the arrays
253 int[] sortedArray1 = sortArray(array1, true);
254 int[] sortedArray2 = sortArray(array2, true);
255
256 // Compare the elements of the sorted arrays
257 int length = array1.length;
258 for (int i = 0; i < length; i++) {
259 if (sortedArray1[i] != sortedArray2[i]) {
260 return (false);
261 }
262 }
263 // If we have reached here, then the arrays are identical
264 return (true);
265 }
266
267 // ************************************************************************************************
268
269 /**
270 * Compares two array, and considers them identical if (1) they contain the
271 * same elements and (2) the order of the elements is exactly the same in
272 * each array
273 *
274 * @return "true" if the elements are identical, and the order of the
275 * elements is identical
276 */
277 public static boolean compareTwoArrays_considerElementOrder(int[] array1,
278 int[] array2) {
279 // Compare the length
280 if (array1.length != array2.length) {
281 return (false);
282 }
283
284 // Compare the elements of the sorted arrays
285 int length = array1.length;
286 for (int i = 0; i < length; i++) {
287 if (array1[i] != array2[i]) {
288 return (false);
289 }
290 }
291 // If we have reached here, then the arrays are identical
292 return (true);
293 }
294
295 // ************************************************************************************************
296
297 /**
298 * Randomize the indices (i.e. the locations) of the values within the array
299 */
300 public static void randomizeElementsInArray(double[] array) {
301 // Initialization
302 Random r = new Random();
303 double[] tempArray = new double[array.length];
304 int[] availableIndices = new int[array.length];
305 for (int i = 0; i < availableIndices.length; i++)
306 availableIndices[i] = i;
307
308 // The actual randomization
309 int indexInTemp = 0;
310 for (int max = array.length; max >= 1; max--) {
311 int i = r.nextInt(max); // recall that nextInt(max) returns a random
312 // integer i (0 <= i < max)
313 int chosenIndex = availableIndices[i];
314 tempArray[indexInTemp] = array[chosenIndex];
315 indexInTemp++;
316
317 // Update availableIndices
318 for (int j = i; j <= max - 2; j++)
319 availableIndices[j] = availableIndices[j + 1];
320 }
321 for (int i = 0; i < array.length; i++)
322 array[i] = tempArray[i];
323 }
324
325 // ************************************************************************************************
326
327 /**
328 * return a random number from a normal distribution with mean = "mu" and
329 * standard deviation = "sigma"
330 */
331 public static double getRandomNumberFromGammaDistribution(double k,
332 double theta, Random random) {
333 boolean accept = false;
334 if (k < 1) {
335 // Weibull algorithm
336 double c = (1 / k);
337 double d = ((1 - k) * Math.pow(k, (k / (1 - k))));
338 double u, v, z, e, x;
339 do {
340 u = random.nextDouble();
341 v = random.nextDouble();
342 z = -Math.log(u);
343 e = -Math.log(v);
344 x = Math.pow(z, c);
345 if ((z + e) >= (d + x)) {
346 accept = true;
347 }
348 } while (!accept);
349 return (x * theta);
350 } else {
351 // Cheng's algorithm
352 double b = (k - Math.log(4));
353 double c = (k + Math.sqrt(2 * k - 1));
354 double lam = Math.sqrt(2 * k - 1);
355 double cheng = (1 + Math.log(4.5));
356 double u, v, x, y, z, r;
357 do {
358 u = random.nextDouble();
359 v = random.nextDouble();
360 y = ((1 / lam) * Math.log(v / (1 - v)));
361 x = (k * Math.exp(y));
362 z = (u * v * v);
363 r = (b + (c * y) - x);
364 if ((r >= ((4.5 * z) - cheng)) || (r >= Math.log(z))) {
365 accept = true;
366 }
367 } while (!accept);
368 return (x * theta);
369 }
370 }
371
372 // ************************************************************************************************
373
374 /**
375 * return a random number from a standard normal distribution (with mean =
376 * 0, and standard deviation = 1)
377 */
378 //
379 public static double getRandomNumberFromNormalDistribution(Random random) {
380 double U = random.nextDouble();
381 double V = random.nextDouble();
382 return Math.sin(2 * Math.PI * V) * Math.sqrt((-2 * Math.log(1 - U)));
383 }
384
385 /**
386 * return a random number from a normal distribution with mean = "mu" and
387 * standard deviation = "sigma"
388 */
389 public static double getRandomNumberFromNormalDistribution(double mu,
390 double sigma, Random random) {
391 return mu + sigma * getRandomNumberFromNormalDistribution(random);
392 }
393
394 // ************************************************************************************************
395
396 /**
397 * Uses "selection sort" to sort the elements of the array in ascending
398 * order
399 */
400 public static int[] sortArray(int[] array, boolean ascending) {
401 // Copy the current array into a new array called "sortedArray"
402 int[] sortedArray = new int[array.length];
403 for (int i = 0; i < array.length; i++)
404 sortedArray[i] = array[i];
405
406 if (ascending) // if the required order is ascending
407 {
408 for (int i = sortedArray.length - 1; i >= 0; i--) // start at the
409 // end of the
410 // array
411 {
412 int highestIndex = i; // (1) default value of the highest
413 // element index.
414 for (int j = i; j >= 0; j--) // (2) loop from the end of
415 // unsorted zone to the
416 // beginning of the array.
417 {
418 if (sortedArray[highestIndex] < sortedArray[j]) // compare
419 // current
420 // element
421 // to
422 // highest
423 highestIndex = j; // if it's higher, it becomes the new
424 // highest
425 }
426 // swap the two values
427 int temp = sortedArray[i];
428 sortedArray[i] = sortedArray[highestIndex];
429 sortedArray[highestIndex] = temp;
430 }
431 } else // i.e., if the required order is descending
432 {
433 for (int i = 0; i <= sortedArray.length - 1; i++) // start at the
434 // beginning of
435 // the array
436 {
437 int highestIndex = i; // (1) default value of the highest
438 // element index.
439 for (int j = i; j <= sortedArray.length - 1; j++) // (2) loop
440 // from the
441 // beginning
442 // of
443 // unsorted
444 // zone to
445 // the end
446 // of the
447 // array.
448 {
449 if (sortedArray[highestIndex] < sortedArray[j]) // compare
450 // current
451 // element
452 // to
453 // highest
454 highestIndex = j; // if it's higher, it becomes the new
455 // highest
456 }
457 // swap the two values
458 int temp = sortedArray[i];
459 sortedArray[i] = sortedArray[highestIndex];
460 sortedArray[highestIndex] = temp;
461 }
462 }
463 return (sortedArray);
464 }
465
466 // ************************************************************************************************
467
468 /**
469 * Uses "selection sort" to sort the elements of the array in ascending
470 * order
471 */
472 public static int[][] sortArrayLexicographically(int[][] array,
473 boolean ascending) {
474 // Copy the current array into a new string array which will be the one
475 // we sort
476 String[] stringArray = new String[array.length];
477 for (int i = 0; i < array.length; i++) {
478 stringArray[i] = "";
479 for (int j = 0; j < array[i].length; j++)
480 stringArray[i] += array[i][j];
481 }
482 if (ascending) // if the required order is ascending
483 {
484 for (int i = stringArray.length - 1; i >= 0; i--) // start at the
485 // end of the
486 // array
487 {
488 int highestIndex = i; // (1) default value of the highest
489 // element index.
490 for (int j = i; j >= 0; j--) // (2) loop from the end of
491 // unsorted zone to the
492 // beginning of the array.
493 {
494 if (stringArray[highestIndex].compareTo(stringArray[j]) < 0) // compare
495 // current
496 // element
497 // to
498 // highest
499 highestIndex = j; // if it's higher, it becomes the new
500 // highest
501 }
502 // swap the two values
503 String temp = stringArray[i];
504 stringArray[i] = stringArray[highestIndex];
505 stringArray[highestIndex] = temp;
506 }
507 } else // i.e., if the required order is descending
508 {
509 for (int i = 0; i <= stringArray.length - 1; i++) // start at the
510 // beginning of
511 // the array
512 {
513 int highestIndex = i; // (1) default value of the highest
514 // element index.
515 for (int j = i; j <= stringArray.length - 1; j++) // (2) loop
516 // from the
517 // beginning
518 // of
519 // unsorted
520 // zone to
521 // the end
522 // of the
523 // array.
524 {
525 if (stringArray[highestIndex].compareTo(stringArray[j]) < 0) // compare
526 // current
527 // element
528 // to
529 // highest
530 highestIndex = j; // if it's higher, it becomes the new
531 // highest
532 }
533 // swap the two values
534 String temp = stringArray[i];
535 stringArray[i] = stringArray[highestIndex];
536 stringArray[highestIndex] = temp;
537 }
538 }
539 // convert the sorted string array into an array of numbers
540 int[][] sortedArray = new int[stringArray.length][];
541 for (int i = 0; i < sortedArray.length; i++) {
542 sortedArray[i] = new int[stringArray[i].length()];
543 for (int j = 0; j < sortedArray[i].length; j++)
544 sortedArray[i][j] = (new Integer(stringArray[i].charAt(j)))
545 .intValue();
546 }
547 return (sortedArray);
548 }
549
550 // ************************************************************************************************
551
552 /**
553 * This method removes the given element from the given array. IMPORTANT: if
554 * the array contains repeated elements, then only one of them is removed.
555 */
556 public static int[] removeElementOnceFromArray(int element, int[] array) {
557 // Initialization
558 int lengthOfArray = array.length;
559 int[] newArray = new int[lengthOfArray - 1];
560
561 // main loop...
562 for (int i = 0; i < lengthOfArray; i++) {
563 if (array[i] == element) {
564 int j = i + 1;
565 while (j < lengthOfArray) {
566 newArray[j - 1] = array[j];
567 j++;
568 }
569 break;
570 } else
571 newArray[i] = array[i];
572 }
573 return (newArray);
574 }
575
576 // ************************************************************************************************
577
578 /**
579 * This method removes the given set of elements from the given array.
580 * IMPORTANT: if the array contains repeated elements, then only one of them
581 * is removed.
582 */
583 public static int[] removeElementsOnceFromArray(int[] elements,
584 int[] array) {
585 // Initialization
586 int lengthOfArray = array.length;
587 int numOfElements = elements.length;
588 int[] tempArray = new int[array.length];
589 for (int i = 0; i < array.length; i++)
590 tempArray[i] = array[i];
591
592 for (int k = 0; k < numOfElements; k++) // For every element
593 {
594 int curElement = elements[k]; // This is the current element
595 for (int i = 0; i < lengthOfArray; i++) {
596 if (tempArray[i] == curElement) // if the current element is
597 // found in the array...
598 {
599 int j = i + 1;
600 while (j < lengthOfArray) {
601 tempArray[j - 1] = tempArray[j];
602 j++;
603 }
604 break;
605 }
606 }
607 }
608
609 // Copy the elements from tempArray to newArray
610 int lengthOfNewArray = lengthOfArray - numOfElements;
611 int[] newArray = new int[lengthOfNewArray];
612 for (int i = 0; i < lengthOfNewArray; i++) {
613 newArray[i] = tempArray[i];
614 }
615 return (newArray);
616 }
617
618 // ************************************************************************************************
619
620 /**
621 * Read a text file, and return a string with the contents. Here, you
622 * specify the path of the file, but that is only starting from the work
623 * space, e.g., if the file's name is "result.txt", and it is in a folder
624 * called: "experiments", which is located in the workspace, then
625 * "filePathAndName" would be: "experiments/result.txt"
626 */
627 public static String readFile(String filePathAndName) {
628 try {
629 BufferedReader bufferReader = new BufferedReader(
630 new FileReader(filePathAndName));
631 String string = "";
632 String line;
633 while (true) {
634 line = bufferReader.readLine();
635 if (line == null)
636 break;
637 string += line;
638 }
639 bufferReader.close();
640 return (string);
641 } catch (Exception e) {
642 System.out.println(e);
643 return (null);
644 }
645 }
646
647 // ************************************************************************************************
648
649 /**
650 * merges the first file and the second file into the "mergedFile" The
651 * merged file can have the same name as the first file, or as the second
652 * file, or have a different name. The method will work in all of these
653 * cases.
654 */
655 public static void mergeTwoFiles(String firstFile, String secondFile,
656 String mergedFile) {
657 if (((new String(mergedFile)).compareTo(firstFile) != 0)
658 && ((new String(mergedFile)).compareTo(secondFile) != 0)) {
659 clearFile(mergedFile);
660 }
661 if (((new String(mergedFile)).compareTo(firstFile) == 0)
662 || ((new String(mergedFile)).compareTo(secondFile) != 0)) {
663 try {
664 BufferedReader bufferReader = new BufferedReader(
665 new FileReader(secondFile));
666 String line;
667 while (true) {
668 line = bufferReader.readLine();
669 if (line == null)
670 break;
671 printToFile(mergedFile, line + "\n", false);
672 }
673 bufferReader.close();
674 } catch (Exception e) {
675 System.out.println(e);
676 }
677 }
678 if (((new String(mergedFile)).compareTo(secondFile) == 0)
679 || ((new String(mergedFile)).compareTo(firstFile) != 0)) {
680 try {
681 BufferedReader bufferReader = new BufferedReader(
682 new FileReader(firstFile));
683 String line;
684 while (true) {
685 line = bufferReader.readLine();
686 if (line == null)
687 break;
688 printToFile(mergedFile, line + "\n", false);
689 }
690 bufferReader.close();
691 } catch (Exception e) {
692 System.out.println(e);
693 }
694 }
695 }
696
697 // ************************************************************************************************
698
699 /**
700 * This method writes the given string to the given file. Here, you specify
701 * the path of the file, but that is only starting from the work space,
702 * e.g., if the file's name is "result.txt", and it is in a folder called:
703 * "experiments", which is located in the workspace, then "filePathAndName"
704 * would be: "experiments/result.txt"
705 *
706 * * if there wasn't a file with this name, then it creates one
707 *
708 * * if there was already a file with this name, then it needs to know
709 * whether you want to erase the previous content of the file before adding
710 * the string. This is determined by the parameter "erasePreviousContent"
711 */
712 public static void printToFile(String filePathAndName, String string,
713 boolean erasePreviousContent) {
714 boolean append = !erasePreviousContent;
715 try {
716 FileWriter file = new FileWriter(filePathAndName, append);
717 file.write(string);
718 file.close();
719 } catch (Exception e) {
720 System.out.println(e);
721 }
722 }
723
724 // ************************************************************************************************
725
726 /**
727 * This method creates the given folder. The folder name may also be a path,
728 * in which case it creates all the folders in the path.
729 */
730 public static void createFolder(String filePathAndName) {
731 File folder = new File(filePathAndName);
732 folder.mkdirs();
733 }
734
735 // ************************************************************************************************
736
737 /**
738 * This method creates a new (blank) file. Now if there was a previous file
739 * with the same name, then it replaces it with the new blank file.
740 */
741 public static void clearFile(String filePathAndName) {
742 try {
743 FileWriter file = new FileWriter(filePathAndName);
744 file.write("");
745 file.close();
746 } catch (Exception e) {
747 System.out.println(e);
748 }
749 }
750
751 // ************************************************************************************************
752
753 public static String convertArrayToString(byte[] array) {
754 String tempStr = "[";
755 for (int i = 0; i < array.length - 1; i++)
756 tempStr += (new Byte(array[i]).toString()) + ", ";
757 tempStr += (new Byte(array[array.length - 1]).toString()) + "]";
758 return (tempStr);
759 }
760
761 public static String convertArrayToString(int[] array) {
762 String tempStr = "[";
763 for (int i = 0; i < array.length - 1; i++)
764 tempStr += (new Integer(array[i]).toString()) + ", ";
765 tempStr += (new Integer(array[array.length - 1]).toString()) + "]";
766 return (tempStr);
767 }
768
769 public static String convertArrayToString(long[] array) {
770 String tempStr = "[";
771 for (int i = 0; i < array.length - 1; i++)
772 tempStr += (new Long(array[i]).toString()) + ", ";
773 tempStr += (new Long(array[array.length - 1]).toString()) + "]";
774 return (tempStr);
775 }
776
777 public static String convertArrayToString(double[] array) {
778 String tempStr = "[";
779 for (int i = 0; i < array.length - 1; i++)
780 tempStr += (new Double(array[i]).toString()) + ", ";
781 tempStr += (new Double(array[array.length - 1]).toString()) + "]";
782 return (tempStr);
783 }
784
785 // ************************************************************************************************
786
787 public static String convertArrayToString(final byte[][] array) {
788 String tempStr = "[";
789 for (int i = 0; i < array.length - 1; i++) {
790 tempStr += "[";
791 for (int j = 0; j < array[i].length - 1; j++) {
792 tempStr += (new Byte(array[i][j]).toString()) + ", ";
793 }
794 int j = array[i].length - 1;
795 tempStr += (new Byte(array[i][j]).toString()) + "], ";
796 }
797 int i = array.length - 1;
798 tempStr += "[";
799 for (int j = 0; j < array[i].length - 1; j++) {
800 tempStr += (new Byte(array[i][j]).toString()) + ", ";
801 }
802 int j = array[i].length - 1;
803 tempStr += (new Byte(array[i][j]).toString()) + "]]";
804 return (tempStr);
805 }
806
807 public static String convertArrayToString(final long[][] array) {
808 String tempStr = "[";
809 for (int i = 0; i < array.length - 1; i++) {
810 tempStr += "[";
811 for (int j = 0; j < array[i].length - 1; j++) {
812 tempStr += (new Long(array[i][j]).toString()) + ", ";
813 }
814 int j = array[i].length - 1;
815 tempStr += (new Long(array[i][j]).toString()) + "], ";
816 }
817 int i = array.length - 1;
818 tempStr += "[";
819 for (int j = 0; j < array[i].length - 1; j++) {
820 tempStr += (new Long(array[i][j]).toString()) + ", ";
821 }
822 int j = array[i].length - 1;
823 tempStr += (new Long(array[i][j]).toString()) + "]]";
824 return (tempStr);
825 }
826
827 public static String convertArrayToString(final double[][] array) {
828 String tempStr = "[";
829 for (int i = 0; i < array.length - 1; i++) {
830 tempStr += "[";
831 for (int j = 0; j < array[i].length - 1; j++) {
832 tempStr += (new Double(array[i][j]).toString()) + ", ";
833 }
834 int j = array[i].length - 1;
835 tempStr += (new Double(array[i][j]).toString()) + "], ";
836 }
837 int i = array.length - 1;
838 tempStr += "[";
839 for (int j = 0; j < array[i].length - 1; j++) {
840 tempStr += (new Double(array[i][j]).toString()) + ", ";
841 }
842 int j = array[i].length - 1;
843 tempStr += (new Double(array[i][j]).toString()) + "]]";
844 return (tempStr);
845 }
846
847 // ************************************************************************************************
848
849 public static void printArray(String initialString, final byte[] array) {
850 System.out.print(initialString + convertArrayToString(array));
851 }
852
853 public static void printArray(String initialString, final int[] array) {
854 System.out.print(initialString + convertArrayToString(array));
855 }
856
857 public static void printArray(String initialString, final long[] array) {
858 System.out.print(initialString + convertArrayToString(array));
859 }
860
861 public static void printArray(String initialString, final double[] array) {
862 System.out.print(initialString + convertArrayToString(array));
863 }
864
865 public static void printArray(String initialString, final byte[][] array) {
866 System.out.print(initialString + convertArrayToString(array));
867 }
868
869 public static void printArray(String initialString, final int[][] array) {
870 System.out.print(initialString + Arrays.deepToString(array));
871 }
872
873 public static void printArray(String initialString, final long[][] array) {
874 System.out.print(initialString + convertArrayToString(array));
875 }
876
877 public static void printArray(String initialString,
878 final double[][] array) {
879 System.out.print(initialString + convertArrayToString(array));
880 }
881
882 // ************************************************************************************************
883
884 public static byte[] copyArray(final byte[] array) {
885 if (array == null)
886 return (null);
887 byte[] newArray = new byte[array.length];
888 for (int i = 0; i < array.length; i++)
889 newArray[i] = array[i];
890 return newArray;
891 }
892
893 public static int[] copyArray(final int[] array) {
894 if (array == null)
895 return (null);
896 int[] newArray = new int[array.length];
897 for (int i = 0; i < array.length; i++)
898 newArray[i] = array[i];
899 return newArray;
900 }
901
902 public static long[] copyArray(final long[] array) {
903 if (array == null)
904 return (null);
905 long[] newArray = new long[array.length];
906 for (int i = 0; i < array.length; i++)
907 newArray[i] = array[i];
908 return newArray;
909 }
910
911 public static double[] copyArray(final double[] array) {
912 if (array == null)
913 return (null);
914 double[] newArray = new double[array.length];
915 for (int i = 0; i < array.length; i++)
916 newArray[i] = array[i];
917 return newArray;
918 }
919
920 public static byte[][] copyArray(final byte[][] array) {
921 if (array == null)
922 return (null);
923 byte[][] newArray = new byte[array.length][];
924 for (int i = 0; i < array.length; i++) {
925 newArray[i] = new byte[array[i].length];
926 for (int j = 0; j < array[i].length; j++)
927 newArray[i][j] = array[i][j];
928 }
929 return newArray;
930 }
931
932 public static int[][] copyArray(final int[][] array) {
933 if (array == null)
934 return (null);
935 int[][] newArray = new int[array.length][];
936 for (int i = 0; i < array.length; i++) {
937 newArray[i] = new int[array[i].length];
938 for (int j = 0; j < array[i].length; j++)
939 newArray[i][j] = array[i][j];
940 }
941 return newArray;
942 }
943
944 public static long[][] copyArray(final long[][] array) {
945 if (array == null)
946 return (null);
947 long[][] newArray = new long[array.length][];
948 for (int i = 0; i < array.length; i++) {
949 newArray[i] = new long[array[i].length];
950 for (int j = 0; j < array[i].length; j++)
951 newArray[i][j] = array[i][j];
952 }
953 return newArray;
954 }
955
956 public static double[][] copyArray(final double[][] array) {
957 if (array == null)
958 return (null);
959 double[][] newArray = new double[array.length][];
960 for (int i = 0; i < array.length; i++) {
961 newArray[i] = new double[array[i].length];
962 for (int j = 0; j < array[i].length; j++)
963 newArray[i][j] = array[i][j];
964 }
965 return newArray;
966 }
967
968 // ************************************************************************************************
969
970 /**
971 * Converts from a multiset which is represented concisely to a string
972 */
973 public static String convertMultisetToString(ElementOfMultiset[] multiset) {
974 String tempStr = "[";
975 for (int i = 0; i < multiset.length; i++)
976 for (int j = 0; j < multiset[i].repetition; j++)
977 if ((i == multiset.length - 1)
978 && (j == multiset[i].repetition - 1))
979 tempStr += multiset[i].element;
980 else
981 tempStr += multiset[i].element + ", ";
982 tempStr += "]";
983 return (tempStr);
984 }
985
986 // ************************************************************************************************
987
988 /**
989 * returns a copy of the given multiset
990 */
991 public static ElementOfMultiset[] copyMultiset(
992 ElementOfMultiset[] multiset) {
993 if (multiset == null)
994 return (null);
995
996 ElementOfMultiset[] copy = new ElementOfMultiset[multiset.length];
997 for (int i = 0; i < multiset.length; i++) {
998 copy[i] = new ElementOfMultiset(multiset[i].element,
999 multiset[i].repetition);
1000 }
1001 return (copy);
1002 }
1003}
Note: See TracBrowser for help on using the repository browser.