PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/Santa827/Chemxseer_subSearch
Java | 368 lines | 252 code | 8 blank | 108 comment | 60 complexity | f92b2fade55b8c591ee118bb198cc081 MD5 | raw file
  1. package edu.psu.chemxseer.structure.util;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. /**
  6. * An extension to the "set" classes
  7. * @author dayuyuan
  8. *
  9. * @param <T>
  10. */
  11. public class SelfImplementSet <T extends Comparable<T>>{
  12. private ArrayList<T> items;
  13. private int capacity;
  14. private int size;
  15. public SelfImplementSet(){
  16. capacity = 0;
  17. size = 0;
  18. }
  19. /**
  20. * Remove all index terms that are larger than size
  21. * @param numOfSets
  22. * @return
  23. */
  24. public void removeLarge(int index){
  25. this.size = index+1;
  26. }
  27. public boolean addAll(T[] c){
  28. if(c == null|| c.length == 0)
  29. return false;
  30. capacity = c.length+size;
  31. ArrayList<T> newItems = new ArrayList<T>(items.size());
  32. int iter = 0;
  33. int i = 0;
  34. int j = 0;
  35. while(i < size){
  36. while(j < c.length){
  37. if(items.get(i).compareTo(c[j]) <0){
  38. newItems.add(items.get(i));
  39. iter++;
  40. i++;
  41. break;
  42. }
  43. else if(items.get(i).compareTo(c[j])==0)
  44. j++;
  45. else{
  46. newItems.add(c[j]);
  47. iter++;
  48. j++;
  49. continue;}
  50. }
  51. if(j == c.length)
  52. break;
  53. }
  54. while(i < size){
  55. newItems.add(items.get(i));
  56. iter++;
  57. i++;
  58. }
  59. while(j < c.length){
  60. newItems.add(c[j]);
  61. iter++;
  62. j++;
  63. }
  64. items = newItems;
  65. size = iter;
  66. return true;
  67. }
  68. // /**
  69. // *
  70. // * @param c
  71. // * @param fromIndex : inclusive
  72. // * @param toIndex : exclusive
  73. // * @return
  74. // */
  75. // public boolean addAll(T[] c, int fromIndex, int toIndex){
  76. // if(c == null|| c.length == 0 || toIndex-fromIndex <=0)
  77. // return false;
  78. // this.onElement = c[fromIndex];
  79. // capacity = toIndex-fromIndex+size;
  80. // T[] newItems = (T[]) Array.newInstance(this.onElement.getClass(), c.length);
  81. // int iter = 0;
  82. // int i = 0;
  83. // int j = fromIndex;
  84. // while(i < size){
  85. // while(j < toIndex){
  86. // if(items[i].compareTo(c[j]) < 0){
  87. // newItems[iter]=items[i];
  88. // iter++;
  89. // i++;
  90. // break;
  91. // }
  92. // else if(items[i].compareTo(c[j])==0)
  93. // j++;
  94. // else{
  95. // newItems[iter]=c[j];
  96. // iter++;
  97. // j++;
  98. // continue;}
  99. // }
  100. // if(j == toIndex)
  101. // break;
  102. // }
  103. // while(i < size){
  104. // newItems[iter] = items[i];
  105. // iter++;
  106. // i++;
  107. // }
  108. // while(j < toIndex){
  109. // newItems[iter]=c[j];
  110. // iter++;
  111. // j++;
  112. // }
  113. // items = newItems;
  114. // size = iter;
  115. // return true;
  116. // }
  117. //
  118. public boolean addAll(List<T> c){
  119. if(c == null|| c.size() == 0)
  120. return false;
  121. capacity = c.size()+size;
  122. ArrayList<T> newItems = new ArrayList<T>();
  123. int iter = 0;
  124. int i = 0;
  125. int j = 0;
  126. while(i < size){
  127. while(j < c.size()){
  128. if(items.get(i).compareTo(c.get(j))<0){
  129. newItems.add(items.get(i));
  130. iter++;
  131. i++;
  132. break;
  133. }
  134. else if(items.get(i).compareTo(c.get(j))==0)
  135. j++;
  136. else{
  137. newItems.add(c.get(j));
  138. iter++;
  139. j++;
  140. continue;}
  141. }
  142. if(j == c.size())
  143. break;
  144. }
  145. while(i < size){
  146. newItems.add(items.get(i));
  147. iter++;
  148. i++;
  149. }
  150. while(j < c.size()){
  151. newItems.add(c.get(j));
  152. iter++;
  153. j++;
  154. }
  155. items = newItems;
  156. size = iter;
  157. return true;
  158. }
  159. public boolean retainAll(T[] c){
  160. if(c == null || c.length == 0)
  161. return false;
  162. int iter = 0, i = 0, j = 0;
  163. // i is index on item, j is index on c
  164. while(i < size && j < c.length){
  165. if(items.get(i).compareTo(c[j]) > 0)
  166. j++;
  167. else if(items.get(i).compareTo(c[j])==0){
  168. items.add(iter++, c[j]);
  169. j++;
  170. i++;
  171. continue;
  172. }
  173. else {// items[i] < c[j]
  174. i++;
  175. continue;
  176. }
  177. }
  178. size = iter;
  179. return true;
  180. }
  181. // /**
  182. // *
  183. // * @param c
  184. // * @param fromIndex: inclusive
  185. // * @param toIndex: exclusive
  186. // * @return
  187. // */
  188. // public boolean retainAll(T[] c, int fromIndex, int toIndex){
  189. // if(c == null || c.length == 0 || toIndex-fromIndex <=0)
  190. // return false;
  191. // int iter = 0, i = 0, j = fromIndex;
  192. // // i is index on item, j is index on c
  193. // while(i < size && j < toIndex){
  194. // if(items[i].compareTo(c[j])>0)
  195. // j++;
  196. // else if(items[i].compareTo(c[j])==0){
  197. // items[iter++]=c[j];
  198. // j++;
  199. // i++;
  200. // continue;
  201. // }
  202. // else {// items[i] < c[j]
  203. // i++;
  204. // continue;
  205. // }
  206. // }
  207. // size = iter;
  208. // return true;
  209. // }
  210. //
  211. public boolean retainAll(List<T> c){
  212. if(c == null || c.size() == 0)
  213. return false;
  214. int iter = 0, i = 0, j = 0;
  215. // i is index on item, j is index on c
  216. while(i < size && j < c.size()){
  217. if(items.get(i).compareTo(c.get(j)) >0)
  218. j++;
  219. else if(items.get(i).compareTo(c.get(j))==0){
  220. items.set(iter++,c.get(j));
  221. j++;
  222. i++;
  223. continue;
  224. }
  225. else {// items[i] < c[j]
  226. i++;
  227. continue;
  228. }
  229. }
  230. size = iter;
  231. return true;
  232. }
  233. public int size(){
  234. return size;
  235. }
  236. public boolean removeAll(T[] c){
  237. if(c == null || c.length == 0)
  238. return false;
  239. int iter = 0, i = 0, j = 0;
  240. while(i < size&&j < c.length){
  241. if(items.get(i).compareTo(c[j])>0)
  242. j++;
  243. else if(items.get(i).compareTo(c[j])==0){
  244. i++;
  245. j++;
  246. // iter did not update
  247. }
  248. else{
  249. // items[i] < c[j]
  250. items.set(iter, items.get(i));
  251. i++;
  252. iter++;
  253. }
  254. }
  255. while(i < size){
  256. items.set(iter, items.get(i));
  257. i++;
  258. iter++;
  259. }
  260. size = iter;
  261. return true;
  262. }
  263. public boolean removeAll(List<T> c){
  264. if(c == null || c.size() == 0)
  265. return false;
  266. int iter = 0, i = 0, j = 0;
  267. while(i < size&&j < c.size()){
  268. if(items.get(i).compareTo(c.get(j))>0)
  269. j++;
  270. else if(items.get(i).compareTo(c.get(j))==0){
  271. i++;
  272. j++;
  273. // iter did not update
  274. }
  275. else{
  276. // items[i] < c[j]
  277. items.set(iter, items.get(i));
  278. i++;
  279. iter++;
  280. }
  281. }
  282. while(i < size){
  283. items.set(iter, items.get(i));
  284. i++;
  285. iter++;
  286. }
  287. size = iter;
  288. return true;
  289. }
  290. /**
  291. *
  292. * @param c
  293. * @param fromIndex: inclusive
  294. * @param toIndex: exclusive
  295. * @returnInteger
  296. */
  297. public boolean removeAll(T[] c, int fromIndex, int toIndex){
  298. if(c == null || c.length == 0 || toIndex-fromIndex<=0)
  299. return false;
  300. int iter = 0, i = 0, j = fromIndex;
  301. while(i < size&&j < toIndex){
  302. if(items.get(i).compareTo(c[j])>0)
  303. j++;
  304. else if(items.get(i).compareTo(c[j])==0){
  305. i++;
  306. j++;
  307. // iter did not update
  308. }
  309. else{
  310. // items[i] < c[j]
  311. items.set(iter, items.get(i));
  312. i++;
  313. iter++;
  314. }
  315. }
  316. while(i < size){
  317. items.set(iter, items.get(i));
  318. i++;
  319. iter++;
  320. }
  321. size = iter;
  322. return true;
  323. }
  324. //TODO: if capacity of the IntersectionSet always larger a lot than the size of items
  325. // We do a further optimization of memory by freeing items;
  326. public boolean clear(){
  327. size = 0;
  328. return true;
  329. }
  330. public ArrayList<T> getItems(){
  331. ArrayList<T> results = new ArrayList<T>();
  332. for(int i = 0; i< size; i++)
  333. results.add(items.get(i));
  334. return results;
  335. }
  336. public String toString(){
  337. StringBuffer buf = new StringBuffer(4*size);
  338. buf.append(items.get(0));
  339. for(int i = 1; i < size; i++){
  340. buf.append(',');
  341. buf.append(items.get(i).toString());
  342. }
  343. return buf.toString();
  344. }
  345. public boolean print(){
  346. for(int i = 0; i< size; i++){
  347. System.out.print(items.get(i).toString());
  348. System.out.print(' ');
  349. }
  350. System.out.println();
  351. System.out.println("Size: " + size + " Capacity: " + capacity);
  352. return true;
  353. }
  354. public HashSet<T> toHashSet(){
  355. HashSet<T> results = new HashSet<T>();
  356. for(int i = 0; i< size; i++)
  357. results.add(items.get(i));
  358. return results;
  359. }
  360. }