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