PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/edu/psu/chemxseer/structure/util/IntersectionSet.java

https://github.com/Santa827/Chemxseer_subSearch
Java | 430 lines | 351 code | 15 blank | 64 comment | 101 complexity | 6e95d9ffa0afc0370479eb3a2c2019ed MD5 | raw file
  1. package edu.psu.chemxseer.structure.util;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. // One implementation of set mainly used for fast intersection
  5. // Assumption, all items in IntersectionSet is sorted
  6. public class IntersectionSet{
  7. private int[] items;
  8. private int capacity;
  9. private int size;
  10. /**
  11. * Given two arrays: arrayOne and arrayTwo, and assume that these two arrays are sorted
  12. * Find out what is the intersection set size for the two arrays
  13. * @param arrayOne
  14. * @param arrayTwo
  15. */
  16. public static int getInterSectionSize(int[] arrayOne, int[] arrayTwo){
  17. if(arrayOne == null || arrayTwo == null)
  18. return 0;
  19. else{
  20. int iter = 0, i = 0, j = 0;
  21. // i is index on item, j is index on c
  22. while(i < arrayOne.length && j < arrayTwo.length){
  23. if(arrayOne[i] > arrayTwo[j])
  24. j++;
  25. else if(arrayOne[i]== arrayTwo[j]){
  26. j++;
  27. i++;
  28. iter ++;
  29. }
  30. else {// items[i] < c[j]
  31. i++;
  32. continue;
  33. }
  34. }
  35. return iter;
  36. }
  37. }
  38. /**
  39. * Given two array, firstArray & second array and their boundary
  40. * Return the position of the item on firstArray = item on second array
  41. * @param firstArray
  42. * @param start1
  43. * @param end1
  44. * @param secondArray
  45. * @param start2
  46. * @param end2
  47. * @return
  48. */
  49. public static int[] getInterSectionPosition(int[] firstArray, int start1, int end1,
  50. int[] secondArray, int start2, int end2) {
  51. if(firstArray == null || secondArray == null)
  52. return new int[0];
  53. int[] pos = new int[end1-start1];
  54. int iter = 0, i = start1, j = start2;
  55. // i is index on item, j is index on c
  56. while(i < end1 && j < end2){
  57. if(firstArray[i] > secondArray[j])
  58. j++;
  59. else if(firstArray[i]== secondArray[j]){
  60. pos[iter++] = i;
  61. j++;
  62. i++;
  63. }
  64. else {// items[i] < c[j]
  65. i++;
  66. continue;
  67. }
  68. }
  69. int[] result = new int[iter];
  70. for(int w = 0; w< iter; w++)
  71. result[w] = pos[w];
  72. return result;
  73. }
  74. public IntersectionSet(){
  75. capacity = 0;
  76. size = 0;
  77. }
  78. /**
  79. * Remove all index terms that are larger than size
  80. * @param numOfSets
  81. * @return
  82. */
  83. public void removeLarge(int index){
  84. this.size = index+1;
  85. }
  86. public boolean addAll(int[] c){
  87. if(c == null|| c.length == 0)
  88. return false;
  89. int[] newItems;
  90. capacity = c.length+size;
  91. newItems = new int[capacity];
  92. int iter = 0;
  93. int i = 0;
  94. int j = 0;
  95. while(i < size){
  96. while(j < c.length){
  97. if(items[i]< c[j]){
  98. newItems[iter]=items[i];
  99. iter++;
  100. i++;
  101. break;
  102. }
  103. else if(items[i]==c[j])
  104. j++;
  105. else{
  106. newItems[iter]=c[j];
  107. iter++;
  108. j++;
  109. continue;}
  110. }
  111. if(j == c.length)
  112. break;
  113. }
  114. while(i < size){
  115. newItems[iter] = items[i];
  116. iter++;
  117. i++;
  118. }
  119. while(j < c.length){
  120. newItems[iter]=c[j];
  121. iter++;
  122. j++;
  123. }
  124. items = newItems;
  125. size = iter;
  126. return true;
  127. }
  128. /**
  129. *
  130. * @param c
  131. * @param fromIndex : inclusive
  132. * @param toIndex : exclusive
  133. * @return
  134. */
  135. public boolean addAll(int[] c, int fromIndex, int toIndex){
  136. if(c == null|| c.length == 0 || toIndex-fromIndex <=0)
  137. return false;
  138. int[] newItems;
  139. capacity = toIndex-fromIndex+size;
  140. newItems = new int[capacity];
  141. int iter = 0;
  142. int i = 0;
  143. int j = fromIndex;
  144. while(i < size){
  145. while(j < toIndex){
  146. if(items[i]< c[j]){
  147. newItems[iter]=items[i];
  148. iter++;
  149. i++;
  150. break;
  151. }
  152. else if(items[i]==c[j])
  153. j++;
  154. else{
  155. newItems[iter]=c[j];
  156. iter++;
  157. j++;
  158. continue;}
  159. }
  160. if(j == toIndex)
  161. break;
  162. }
  163. while(i < size){
  164. newItems[iter] = items[i];
  165. iter++;
  166. i++;
  167. }
  168. while(j < toIndex){
  169. newItems[iter]=c[j];
  170. iter++;
  171. j++;
  172. }
  173. items = newItems;
  174. size = iter;
  175. return true;
  176. }
  177. public boolean addAll(List<Integer> c){
  178. if(c == null|| c.size() == 0)
  179. return false;
  180. int[] newItems;
  181. capacity = c.size()+size;
  182. newItems = new int[capacity];
  183. int iter = 0;
  184. int i = 0;
  185. int j = 0;
  186. while(i < size){
  187. while(j < c.size()){
  188. if(items[i]< c.get(j)){
  189. newItems[iter]=items[i];
  190. iter++;
  191. i++;
  192. break;
  193. }
  194. else if(items[i]==c.get(j))
  195. j++;
  196. else{
  197. newItems[iter]=c.get(j);
  198. iter++;
  199. j++;
  200. continue;}
  201. }
  202. if(j == c.size())
  203. break;
  204. }
  205. while(i < size){
  206. newItems[iter] = items[i];
  207. iter++;
  208. i++;
  209. }
  210. while(j < c.size()){
  211. newItems[iter]=c.get(j);
  212. iter++;
  213. j++;
  214. }
  215. items = newItems;
  216. size = iter;
  217. return true;
  218. }
  219. public boolean retainAll(int[] c){
  220. if(c == null || c.length == 0)
  221. return false;
  222. int iter = 0, i = 0, j = 0;
  223. // i is index on item, j is index on c
  224. while(i < size && j < c.length){
  225. if(items[i] > c[j])
  226. j++;
  227. else if(items[i]== c[j]){
  228. items[iter++]=c[j];
  229. j++;
  230. i++;
  231. continue;
  232. }
  233. else {// items[i] < c[j]
  234. i++;
  235. continue;
  236. }
  237. }
  238. size = iter;
  239. // //TEST
  240. // HashSet<Integer> tests = new HashSet<Integer>();
  241. // for(int w = 0; w < size; w++){
  242. // if(tests.contains(items[w]))
  243. // System.out.println("waht");
  244. // else tests.add(items[w]);
  245. // }
  246. // //END OF TEST
  247. return true;
  248. }
  249. /**
  250. *
  251. * @param c
  252. * @param fromIndex: inclusive
  253. * @param toIndex: exclusive
  254. * @return
  255. */
  256. public boolean retainAll(int[] c, int fromIndex, int toIndex){
  257. if(c == null || c.length == 0 || toIndex-fromIndex <=0)
  258. return false;
  259. int iter = 0, i = 0, j = fromIndex;
  260. // i is index on item, j is index on c
  261. while(i < size && j < toIndex){
  262. if(items[i] > c[j])
  263. j++;
  264. else if(items[i]== c[j]){
  265. items[iter++]=c[j];
  266. j++;
  267. i++;
  268. continue;
  269. }
  270. else {// items[i] < c[j]
  271. i++;
  272. continue;
  273. }
  274. }
  275. size = iter;
  276. return true;
  277. }
  278. public boolean retainAll(List<Integer> c){
  279. if(c == null || c.size() == 0)
  280. return false;
  281. int iter = 0, i = 0, j = 0;
  282. // i is index on item, j is index on c
  283. while(i < size && j < c.size()){
  284. if(items[i] > c.get(j))
  285. j++;
  286. else if(items[i]== c.get(j)){
  287. items[iter++]=c.get(j);
  288. j++;
  289. i++;
  290. continue;
  291. }
  292. else {// items[i] < c[j]
  293. i++;
  294. continue;
  295. }
  296. }
  297. size = iter;
  298. return true;
  299. }
  300. public int size(){
  301. return size;
  302. }
  303. public boolean removeAll(int[] c){
  304. if(c == null || c.length == 0)
  305. return false;
  306. int iter = 0, i = 0, j = 0;
  307. while(i < size&&j < c.length){
  308. if(items[i] > c[j])
  309. j++;
  310. else if(items[i] == c[j]){
  311. i++;
  312. j++;
  313. // iter did not update
  314. }
  315. else{
  316. // items[i] < c[j]
  317. items[iter]=items[i];
  318. i++;
  319. iter++;
  320. }
  321. }
  322. while(i < size){
  323. items[iter]=items[i];
  324. i++;
  325. iter++;
  326. }
  327. size = iter;
  328. return true;
  329. }
  330. /**
  331. *
  332. * @param c
  333. * @param fromIndex: inclusive
  334. * @param toIndex: exclusive
  335. * @return
  336. */
  337. public boolean removeAll(int[] c, int fromIndex, int toIndex){
  338. if(c == null || c.length == 0 || toIndex-fromIndex<=0)
  339. return false;
  340. int iter = 0, i = 0, j = fromIndex;
  341. while(i < size&&j < toIndex){
  342. if(items[i] > c[j])
  343. j++;
  344. else if(items[i] == c[j]){
  345. i++;
  346. j++;
  347. // iter did not update
  348. }
  349. else{
  350. // items[i] < c[j]
  351. items[iter]=items[i];
  352. i++;
  353. iter++;
  354. }
  355. }
  356. while(i < size){
  357. items[iter]=items[i];
  358. i++;
  359. iter++;
  360. }
  361. size = iter;
  362. return true;
  363. }
  364. //TODO: if capacity of the IntersectionSet always larger a lot than the size of items
  365. // We do a further optimization of memory by freeing items;
  366. public boolean clear(){
  367. size = 0;
  368. return true;
  369. }
  370. public int[] getItems(){
  371. int[] results = new int[size];
  372. for(int i = 0; i< size; i++)
  373. results[i]=items[i];
  374. return results;
  375. }
  376. public String toString(){
  377. StringBuffer buf = new StringBuffer(4*size);
  378. buf.append(items[0]);
  379. for(int i = 1; i < size; i++){
  380. buf.append(',');
  381. buf.append(items[i]);
  382. }
  383. return buf.toString();
  384. }
  385. public boolean print(){
  386. for(int i = 0; i< size; i++){
  387. System.out.print(items[i]);
  388. System.out.print(' ');
  389. }
  390. System.out.println();
  391. System.out.println("Size: " + size + " Capacity: " + capacity);
  392. return true;
  393. }
  394. public HashSet<Integer> toHashSet(){
  395. HashSet<Integer> results = new HashSet<Integer>();
  396. for(int i = 0; i< size; i++)
  397. results.add(items[i]);
  398. return results;
  399. }
  400. public static int[] getCompleteSet(int[] items, int wholeBound){
  401. int[] results = new int[wholeBound-items.length];
  402. int i = 0,resultsIndex = 0, itemsIndex = 0;
  403. for(; i< wholeBound & itemsIndex< items.length ; i++){
  404. if(i < items[itemsIndex])
  405. results[resultsIndex++] = i;
  406. else if(i == items[itemsIndex])
  407. itemsIndex++;
  408. else if(i > items[itemsIndex])
  409. System.out.println("Illigle Items: not sorted");
  410. }
  411. for(; i < wholeBound; i++, resultsIndex++)
  412. results[resultsIndex] = i;
  413. return results;
  414. }
  415. }