<commit_before>/**
 * 
 */
package org.geneview.graph.algorithm;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.geneview.graph.EGraphItemProperty;
import org.geneview.graph.IGraphItem;
import org.geneview.graph.algorithm.AGraphVisitorSearch;

/**
 * Breadth First Search Algorithm.
 * 
 * @author <NAME>
 *
 */
public class GraphVisitorSearchBFS 
extends AGraphVisitorSearch
implements IGraphVisitorSearch {

	protected EGraphItemProperty prop;
	
	/**
	 *  
	 */
	protected List < List<IGraphItem> > depthSortedList;
	
	/**
	 * 
	 */
	public GraphVisitorSearchBFS(IGraphItem item, final int iSearchDepth) {
		super(item, iSearchDepth);
	}
	
	private List < List<IGraphItem> > createDepthSortedList( final int iSetDepth ) {
		
		/** create list and initialize it .. */
		List < List<IGraphItem> > resultDepthSortedList = 
			new ArrayList < List<IGraphItem> > (iSetDepth);
		for ( int i=0; i<iSearchDepth; i++ ) {
			depthSortedList.add(i, new ArrayList <IGraphItem> ()); 
		}
		
		return resultDepthSortedList;
	}

	@Override
	public void setSearchDepth(final int iSearchDepth) {
		
		super.setSearchDepth(iSearchDepth);
		
		if (depthSortedList != null) {
			if ( depthSortedList.size() == iSearchDepth) {
				/** List has already the required size */
				return;
			}		
		}
			
		/** create list and initialize it .. */
		depthSortedList = createDepthSortedList(iSearchDepth);
	}

	

	/* (non-Javadoc)
	 * @see org.geneview.graph.algorithm.IGraphVisitorSearch#getSearchResult()
	 */
	public List<IGraphItem> getSearchResult() {
			
		if ( iSearchDepth < 1 ) {
			/** special case, no elements, return empty list */
			return new ArrayList <IGraphItem> (0);
		}
		
		List<IGraphItem> resultList = new ArrayList<IGraphItem>(); 
			
		List<IGraphItem> tmpList = this.itemSource.getAllItemsByProp(prop);
		Iterator<IGraphItem> iterTmpGraphItems = tmpList.iterator();
		while(iterTmpGraphItems.hasNext())
		{
			resultList.add(iterTmpGraphItems.next());
		}
		
		
		if ( iSearchDepth == 1 ) {
			/** special case; only direct adjacent elements, return list */
			depthSortedList.add(0, resultList);
			return resultList;
		}
		
		/** general case: */
					
		List <IGraphItem> currentSourceList = new ArrayList <IGraphItem> (resultList);
		
		/** insert elements with adjacency ==1 */
		depthSortedList.add(0, currentSourceList);
		
		for ( int iCurrentDepthIndex=1; iCurrentDepthIndex< this.iSearchDepth; iCurrentDepthIndex++) {
			
			List <IGraphItem> currentLevel = new ArrayList <IGraphItem> ();
			
			Iterator <IGraphItem> iter = currentSourceList.iterator();
			while ( iter.hasNext() ) {
				Iterator <IGraphItem> iterInner = 
					iter.next().getAllItemsByProp(prop).iterator();
				
				while ( iterInner.hasNext() ) {
					IGraphItem item = iterInner.next();
					if ( ! resultList.contains(item) ) {
						/** add to list of visited nodes .. */
						resultList.add(item);
						/** add to list of nodes of current level .. */
						currentLevel.add(item);
					}
					/** else the item was already visited and will be skipped. */
					
				} // while ( iterInner.hasNext() )
				
			} // while ( iter.hasNext() ) {
			
			/** store current level as result */
			depthSortedList.add(iCurrentDepthIndex, currentLevel);
			currentSourceList = currentLevel;
			
		} // for ( int iCurrentDepthIndex=1; iCurrentDepthIndex< this.iSearchDepth; iCurrentDepthIndex++) {
		
		return resultList;
	}

	public final List<List<IGraphItem>> getSearchResultDepthOrdered() {
		
		List<List<IGraphItem>> resultList = createDepthSortedList(iSearchDepth);
		
		Iterator<List<IGraphItem>> iterDepthLevels = depthSortedList.iterator();
		
		for (int i=0;iterDepthLevels.hasNext();i++) {
			List<IGraphItem> resultListDepthLevels = resultList.get(i);
			Iterator <IGraphItem> iterInnerLoop = iterDepthLevels.next().iterator();
			while ( iterInnerLoop.hasNext() ) {
				resultListDepthLevels.add( iterInnerLoop.next() );
			}
		}
		
		return resultList;
	}
	
	protected final List<List<IGraphItem>> exposeDepthResultList() {
		
		return depthSortedList;
	}
	
	/**
	 * @return the prop
	 */
	public final EGraphItemProperty getProp() {
		return prop;
	}

	/**
	 * @param prop the prop to set
	 */
	public final void setProp(EGraphItemProperty prop) {
		this.prop = prop;
	}

	/*
	 * (non-Javadoc)
	 * @see org.geneview.graph.algorithm.IGraphVisitorSearch#init()
	 */<commit_msg>fixed deep copy bug <commit_after>/**
 * 
 */
package org.geneview.graph.algorithm;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.geneview.graph.EGraphItemProperty;
import org.geneview.graph.IGraphItem;
import org.geneview.graph.algorithm.AGraphVisitorSearch;

/**
 * Breadth First Search Algorithm.
 * 
 * @author <NAME>
 *
 */
public class GraphVisitorSearchBFS 
extends AGraphVisitorSearch
implements IGraphVisitorSearch {

	protected EGraphItemProperty prop;
	
	/**
	 *  
	 */
	protected List < List<IGraphItem> > depthSortedList;
	
	/**
	 * 
	 */
	public GraphVisitorSearchBFS(IGraphItem item, final int iSearchDepth) {
		super(item, iSearchDepth);
	}
	
	private List < List<IGraphItem> > createDepthSortedList( final int iSetDepth ) {
		
		/** create list and initialize it .. */
		List < List<IGraphItem> > resultDepthSortedList = 
			new ArrayList < List<IGraphItem> > (iSetDepth);
		for ( int i=0; i<iSearchDepth; i++ ) {
			resultDepthSortedList.add(i, new ArrayList <IGraphItem> ()); 		
		}
		
		return resultDepthSortedList;
	}

	@Override
	public void setSearchDepth(final int iSearchDepth) {
		
		super.setSearchDepth(iSearchDepth);
		
		if (depthSortedList != null) {
			if ( depthSortedList.size() == iSearchDepth) {
				/** List has already the required size */
				return;
			}		
		}
			
		/** create list and initialize it .. */
		depthSortedList = createDepthSortedList(iSearchDepth);
	}

	

	/* (non-Javadoc)
	 * @see org.geneview.graph.algorithm.IGraphVisitorSearch#getSearchResult()
	 */
	public List<IGraphItem> getSearchResult() {
			
		if ( iSearchDepth < 1 ) {
			/** special case, no elements, return empty list */
			return new ArrayList <IGraphItem> (0);
		}
		
		List<IGraphItem> resultList = new ArrayList<IGraphItem>(); 
			
		List<IGraphItem> tmpList = this.itemSource.getAllItemsByProp(prop);
		Iterator<IGraphItem> iterTmpGraphItems = tmpList.iterator();
		while(iterTmpGraphItems.hasNext())
		{
			resultList.add(iterTmpGraphItems.next());
		}
		
		
		if ( iSearchDepth == 1 ) {
			/** special case; only direct adjacent elements, return list */
			depthSortedList.add(0, resultList);
			return resultList;
		}
		
		/** general case: */
					
		List <IGraphItem> currentSourceList = new ArrayList <IGraphItem> (resultList);
		
		/** insert elements with adjacency ==1 */
		depthSortedList.add(0, currentSourceList);
		
		for ( int iCurrentDepthIndex=1; iCurrentDepthIndex< this.iSearchDepth; iCurrentDepthIndex++) {
			
			List <IGraphItem> currentLevel = new ArrayList <IGraphItem> ();
			
			Iterator <IGraphItem> iter = currentSourceList.iterator();
			while ( iter.hasNext() ) {
				Iterator <IGraphItem> iterInner = 
					iter.next().getAllItemsByProp(prop).iterator();
				
				while ( iterInner.hasNext() ) {
					IGraphItem item = iterInner.next();
					if ( ! resultList.contains(item) ) {
						/** add to list of visited nodes .. */
						resultList.add(item);
						/** add to list of nodes of current level .. */
						currentLevel.add(item);
					}
					/** else the item was already visited and will be skipped. */
					
				} // while ( iterInner.hasNext() )
				
			} // while ( iter.hasNext() ) {
			
			/** store current level as result */
			depthSortedList.add(iCurrentDepthIndex, currentLevel);
			currentSourceList = currentLevel;
			
		} // for ( int iCurrentDepthIndex=1; iCurrentDepthIndex< this.iSearchDepth; iCurrentDepthIndex++) {
		
		return resultList;
	}

	/**
	 * Create a deep copy of teh search result list.
	 * 
	 * @return deep copy of the search result list
	 */
	public final List<List<IGraphItem>> getSearchResultDepthOrdered() {
		
		/* depthSortedList.size() != iSearchDepth */
		List<List<IGraphItem>> resultList = createDepthSortedList(depthSortedList.size());		
		Iterator<List<IGraphItem>> iterRawDataTopLevel = depthSortedList.iterator();
		
		int index=0;
		while (iterRawDataTopLevel.hasNext()) {
			
			/* raw data */
			List <IGraphItem> currentRawDataDepthList = iterRawDataTopLevel.next();
			Iterator <IGraphItem> iterRawDataInnerLoop = currentRawDataDepthList.iterator();

			/* result list, deep copy */
			ArrayList <IGraphItem> resultListDeepCopy = 
				new ArrayList <IGraphItem> (currentRawDataDepthList.size()); 			
						
			while ( iterRawDataInnerLoop.hasNext() ) {
				resultListDeepCopy.add(iterRawDataInnerLoop.next());
			}

			/* set ArrayList <IGraphItem> resultListDeepCopy ==> resultListDeepCopy[index]= resultListDeepCopy */
			resultList.set(index, resultListDeepCopy);			
			index++;
			
		} //while (iterRawDataTopLevel.hasNext()) {
		
		return resultList;
	}
	
	protected final List<List<IGraphItem>> exposeDepthResultList() {
		
		return depthSortedList;
	}
	
	/**
	 * @return the prop
	 */
	public final EGraphItemProperty getProp() {
		return prop;
	}

	/**
	 * @param prop the prop to set
	 */
	public final void setProp(EGraphItemProperty prop) {
		this.prop = prop;
	}

	/*
	 * (non-Javadoc)
	 * @see org.geneview.graph.algorithm.IGraphVisitorSearch#init()
	 */
{'fraction_non_alphanumeric': 0.12126351494594022, 'fraction_numerical': 0.00211999152003392, 'mean_word_length': 3.5491803278688523, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1465831', 'n_tokens_mistral': 3481, 'n_tokens_neox': 2906, 'n_words': 642}