All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scudata.expression.mfn.sequence.PFind Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20240823
Show newest version
package com.scudata.expression.mfn.sequence;

import com.scudata.array.ArrayUtil;
import com.scudata.array.BoolArray;
import com.scudata.array.ConstArray;
import com.scudata.array.IArray;
import com.scudata.array.IntArray;
import com.scudata.common.MessageManager;
import com.scudata.common.ObjectCache;
import com.scudata.common.RQException;
import com.scudata.dm.Context;
import com.scudata.dm.IndexTable;
import com.scudata.dm.Sequence;
import com.scudata.expression.IParam;
import com.scudata.expression.Relation;
import com.scudata.expression.SequenceFunction;
import com.scudata.resources.EngineMessage;
import com.scudata.util.Variant;

/**
 * ??????ֵ???Ҽ?¼?????
 * A.pfind(k)
 * @author RunQian
 *
 */
public class PFind extends SequenceFunction {
	private Sequence prevSequence;
	private IndexTable indexTable;
	
	/**
	 * ??????ʽ????Ч?ԣ???Ч???׳??쳣
	 */
	public void checkValidity() {
		if (param == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("pfind" + mm.getMessage("function.missingParam"));
		}
	}

	public Object calculate(Context ctx) {
		boolean isSorted = false, isInsertPos = false, isZero = false;;
		if (option != null) {
			if (option.indexOf('b') != -1)isSorted = true;
			if (option.indexOf('s') != -1) {
				isSorted = true;
				isInsertPos = true;
			}
			
			if (option.indexOf('0') != -1)isZero = true;
		}

		Object key;
		if (param.isLeaf()) {
			key = param.getLeafExpression().calculate(ctx);
		} else {
			int count = param.getSubSize();
			Sequence seq = new Sequence(count);
			key = seq;
			for (int i = 0; i < count; ++i) {
				IParam sub = param.getSub(i);
				if (sub == null || !sub.isLeaf()) {
					MessageManager mm = EngineMessage.get();
					throw new RQException("pfind" + mm.getMessage("function.invalidParam"));
				}
				
				seq.add(sub.getLeafExpression().calculate(ctx));
			}
		}

		int index = srcSequence.pfindByKey(key, isSorted);
		if (index <= 0 && !isInsertPos) {
			return isZero ? ObjectCache.getInteger(0) : null;
		} else {
			return ObjectCache.getInteger(index);
		}
	}

	/**
	 * ??????????еĽ??
	 * @param ctx ??????????
	 * @return IArray
	 */
	public IArray calculateAll(Context ctx) {
		IArray leftArray = left.calculateAll(ctx);
		if (leftArray instanceof ConstArray) {
			Object leftValue = ((ConstArray)leftArray).getData();
			if (leftValue instanceof Sequence && param.isLeaf()) {
				Sequence srcSequence = (Sequence)leftValue;
				if (prevSequence != srcSequence) {
					prevSequence = srcSequence;
					indexTable = srcSequence.newIndexTable();
				}
				
				IArray array = param.getLeafExpression().calculateAll(ctx);
				int []index = indexTable.findAllPos(array);
				boolean []signs = null;
				
				if (option == null || option.indexOf('0') == -1) {
					int len = index.length;
					signs = new boolean[len];
					for (int i = 1; i < len; ++i) {
						signs[i] = index[i] < 1;
					}
				}
				
				IntArray result = new IntArray(index, signs, array.size());
				result.setTemporary(true);
				return result;
			}
		}
		
		return calculateAll(leftArray, ctx);
	}

	/**
	 * ????signArray??ȡֵΪsign????
	 * @param ctx
	 * @param signArray ?б?ʶ????
	 * @param sign ??ʶ
	 * @return IArray
	 */
	public IArray calculateAll(Context ctx, IArray signArray, boolean sign) {
		IArray leftArray = left.calculateAll(ctx);
		if (leftArray instanceof ConstArray) {
			Object leftValue = ((ConstArray)leftArray).getData();
			if (leftValue instanceof Sequence && param.isLeaf()) {
				Sequence srcSequence = (Sequence)leftValue;
				if (prevSequence != srcSequence) {
					prevSequence = srcSequence;
					indexTable = srcSequence.newIndexTable();
				}
				
				BoolArray boolArray = ArrayUtil.booleanValue(signArray, sign);
				IArray array = param.getLeafExpression().calculateAll(ctx, boolArray, true);
				int []index = indexTable.findAllPos(array, boolArray);
				boolean []signs = null;
				
				if (option == null || option.indexOf('0') == -1) {
					int len = index.length;
					signs = new boolean[len];
					for (int i = 1; i < len; ++i) {
						signs[i] = index[i] < 1;
					}
				}
				
				IntArray result = new IntArray(index, signs, array.size());
				result.setTemporary(true);
				return result;
			}
		}
		
		return calculateAll(leftArray, ctx, signArray, sign);
	}
	
	/**
	 * ?????߼????????&&???Ҳ????ʽ
	 * @param ctx ??????????
	 * @param leftResult &&??????ʽ?ļ?????
	 * @return BoolArray
	 */
	public BoolArray calculateAnd(Context ctx, IArray leftResult) {
		IArray leftArray = left.calculateAll(ctx);
		if (leftArray instanceof ConstArray) {
			Object leftValue = ((ConstArray)leftArray).getData();
			if (leftValue instanceof Sequence && param.isLeaf()) {
				Sequence srcSequence = (Sequence)leftValue;
				if (prevSequence != srcSequence) {
					prevSequence = srcSequence;
					indexTable = srcSequence.newIndexTable();
				}
				
				BoolArray result = leftResult.isTrue();
				IArray array = param.getLeafExpression().calculateAll(ctx, result, true);
				int []index = indexTable.findAllPos(array, result);

				for (int i = 1, len = index.length; i < len; ++i) {
					if (index[i] < 1) {
						result.set(i, false);
					}
				}
				
				return result;
			}
		}
		
		return calculateAnd(leftArray, ctx, leftResult);
	}
	
	/**
	 * ?жϸ?????ֵ??Χ?Ƿ????㵱ǰ????????ʽ
	 * @param ctx ??????????
	 * @return ȡֵ????Relation. -1??ֵ??Χ??û????????????ֵ??0??ֵ??Χ??????????????ֵ??1??ֵ??Χ??ֵ??????????
	 */
	public int isValueRangeMatch(Context ctx) {
		if (option != null && option.indexOf('k') != -1) {
			return Relation.PARTICALMATCH;
		}
		
		IArray leftArray = left.calculateAll(ctx);
		if (leftArray instanceof ConstArray) {
			Object leftValue = ((ConstArray)leftArray).getData();
			if (leftValue instanceof Sequence && param.isLeaf()) {
				IArray array = param.getLeafExpression().calculateRange(ctx);
				if (array == null) {
					return Relation.PARTICALMATCH;
				}
							
				Sequence srcSequence = (Sequence)leftValue;
				Object minValue = array.get(1);
				Object maxValue = array.get(2);
				
				if (Variant.isEquals(minValue, maxValue)) {
					boolean isSorted = option != null && option.indexOf('b') != -1;
					Object value = srcSequence.findByKey(minValue, isSorted);
					if (Variant.isTrue(value)) {
						return Relation.ALLMATCH;
					} else {
						return Relation.UNMATCH;
					}
				} else {
					return Relation.PARTICALMATCH;
				}
			}
		}

		return Relation.PARTICALMATCH;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy