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