else break; } else {    if (candidate.getGID() < reference.getGID()) {  if (Expression.NO_CONTEXT_ID != contextId)  candidate.addContextNode(contextId, reference); else candidate.copyContext(reference); result.add(candidate); if (iCandidates.hasNext())
else break; } else { if (firstCandidate == null) firstCandidate = candidate;     if (candidate.getGID() < reference.getGID()) {  NodeProxy t = result.get(candidate); if (t == null) { if (Expression.IGNORE_CONTEXT != contextId) { if (Expression.NO_CONTEXT_ID == contextId) { candidate.copyContext(reference); } else { candidate.addContextNode(contextId, reference); } } result.add(candidate); } else if (contextId > Expression.NO_CONTEXT_ID){ t.addContextNode(contextId, reference); } if (iCandidates.hasNext())
	public static NodeSet selectPrecedingSiblings(NodeSet candidates, NodeSet references, int contextId) {		if (references.getLength() == 0 || candidates.getLength() == 0)			return NodeSet.EMPTY_SET;		NodeSet result = new ExtArrayNodeSet();		Iterator iReferences = references.iterator();		Iterator iCandidates = candidates.iterator();        NodeProxy reference = (NodeProxy) iReferences.next();        NodeProxy candidate = (NodeProxy) iCandidates.next();		while (true) {			// first, try to find nodes belonging to the same doc			if (reference.getDocument().getDocId() < candidate.getDocument().getDocId()) {				if (iReferences.hasNext())                    reference = (NodeProxy) iReferences.next();				else					break;			} else if (reference.getDocument().getDocId() > candidate.getDocument().getDocId()) {				if (iCandidates.hasNext())                    candidate = (NodeProxy) iCandidates.next();				else					break;			} else {				// same document: check if the nodes have the same parent                long pa = getParentId(reference.getDocument(), reference.getGID());                long pb = getParentId(candidate.getDocument(), candidate.getGID());				if (pa < pb) {					// wrong parent: proceed					if (iReferences.hasNext())                        reference = (NodeProxy) iReferences.next();					else						break;				} else if (pa > pb) {					// wrong parent: proceed					if (iCandidates.hasNext())                        candidate = (NodeProxy) iCandidates.next();					else						break;				} else {					// found two nodes with the same parent					// now, compare the ids: a node is a following sibling					// if its id is greater than the id of the other node					if (candidate.getGID() < reference.getGID()) {						// found a preceding sibling												if (Expression.NO_CONTEXT_ID != contextId)                            //TODO : add transverse context                            candidate.addContextNode(contextId, reference);						else                            candidate.copyContext(reference);						result.add(candidate);												if (iCandidates.hasNext())                            candidate = (NodeProxy) iCandidates.next();						else							break;					} else if (candidate.getGID() > reference.getGID()) {						// found a following sibling						if (iCandidates.hasNext())                            //TODO : break ?                            candidate = (NodeProxy) iCandidates.next();						else							break;						// equal nodes: proceed with next node					} else {						if (iReferences.hasNext())                            reference = (NodeProxy) iReferences.next();						else							break;					}				}			}		}		return result;	}