PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/edu/psu/chemxseer/structure/setcover/maxCoverStatus/Util_IntersectionSet.java

https://github.com/Santa827/Chemxseer_subSearch
Java | 86 lines | 64 code | 5 blank | 17 comment | 30 complexity | 0c1a584495bb624170c8d1c21cef74af MD5 | raw file
  1. package edu.psu.chemxseer.structure.setcover.maxCoverStatus;
  2. public class Util_IntersectionSet {
  3. private Util_IntersectionSet(){
  4. //dummy constructor to make sure that MaxCoverStatus_IntersectionSet is not constructable
  5. }
  6. /**
  7. * Prerequisite: firstArray & secondArray all contain no-negative number
  8. * Return -1, if fistArray-secondArray = empty set
  9. * Return -2, if firstArray-secondArray = set with size >1
  10. * Return n, if firstArray-secondArray = n.
  11. * @param firstArray
  12. * @param seconArray
  13. */
  14. public static int retain(short[] firstArray, int firstArrayBoundary,
  15. short[] secondArray, int secondArrayBoundary, int exceptItemI){
  16. int result = -1;
  17. if(firstArrayBoundary == 0)
  18. return -1; //firstArray is empty
  19. // firstArray is not empty or null
  20. int i = 0, j = 0;
  21. while( i < firstArrayBoundary && j < secondArrayBoundary){
  22. if(firstArray[i] == secondArray[j]){
  23. i ++; j++;
  24. }
  25. // firstArray[i] not in secondArray
  26. else if(firstArray[i] < secondArray[j]){
  27. if(firstArray[i] == exceptItemI){
  28. i++; // continue;
  29. }
  30. else{
  31. if(result!=-1)
  32. return -2; //no single result
  33. else result = firstArray[i];
  34. i++;
  35. }
  36. }
  37. else //firstArray[i] > secondArray[j]
  38. j++;
  39. }
  40. for(; i < firstArrayBoundary; i++){
  41. if(firstArray[i] == exceptItemI)
  42. continue;
  43. if(result!=-1)
  44. return -2; // no single result;
  45. else result = firstArray[i];
  46. }
  47. return result;
  48. }
  49. /**
  50. * Return A-B
  51. * @param A
  52. * @param B
  53. */
  54. public static short[] retain(short[] A, short[] B) {
  55. if(B == null || B.length == 0)
  56. return A;
  57. else if(A == null)
  58. return A;
  59. int iter = 0, i = 0, j = 0;
  60. short[] result = new short[A.length];
  61. // i is index on item, j is index on c
  62. while(i < A.length && j < B.length){
  63. if(A[i] > B[j])
  64. j++;
  65. else if(A[i]== B[j]){
  66. result[iter++]=B[j];
  67. j++;
  68. i++;
  69. continue;
  70. }
  71. else {// items[i] < c[j]
  72. i++;
  73. continue;
  74. }
  75. }
  76. short[] finalResult = new short[iter];
  77. for(int w = 0; w < iter; w++)
  78. finalResult[w] = result[w];
  79. return finalResult;
  80. }
  81. }