Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

集合操作:交集、并集、差集、补集、是否子集…… #475

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,18 @@
import com.googlecode.aviator.runtime.function.seq.SeqCollectorRawFunction;
import com.googlecode.aviator.runtime.function.seq.SeqCompsitePredFunFunction;
import com.googlecode.aviator.runtime.function.seq.SeqCompsitePredFunFunction.LogicOp;
import com.googlecode.aviator.runtime.function.seq.SeqContainsAnyFunction;
import com.googlecode.aviator.runtime.function.seq.SeqContainsKeyFunction;
import com.googlecode.aviator.runtime.function.seq.SeqCountFunction;
import com.googlecode.aviator.runtime.function.seq.SeqDisjunctionFunction;
import com.googlecode.aviator.runtime.function.seq.SeqEveryFunction;
import com.googlecode.aviator.runtime.function.seq.SeqFilterFunction;
import com.googlecode.aviator.runtime.function.seq.SeqGetFunction;
import com.googlecode.aviator.runtime.function.seq.SeqIncludeFunction;
import com.googlecode.aviator.runtime.function.seq.SeqIntersectionFunction;
import com.googlecode.aviator.runtime.function.seq.SeqIntoFunction;
import com.googlecode.aviator.runtime.function.seq.SeqIsEqualFunction;
import com.googlecode.aviator.runtime.function.seq.SeqIsSubSeqFunction;
import com.googlecode.aviator.runtime.function.seq.SeqKeysFunction;
import com.googlecode.aviator.runtime.function.seq.SeqMakePredicateFunFunction;
import com.googlecode.aviator.runtime.function.seq.SeqMapEntryFunction;
Expand All @@ -115,6 +120,8 @@
import com.googlecode.aviator.runtime.function.seq.SeqReverseFunction;
import com.googlecode.aviator.runtime.function.seq.SeqSomeFunction;
import com.googlecode.aviator.runtime.function.seq.SeqSortFunction;
import com.googlecode.aviator.runtime.function.seq.SeqSubtractFunction;
import com.googlecode.aviator.runtime.function.seq.SeqUnionFunction;
import com.googlecode.aviator.runtime.function.seq.SeqValsFunction;
import com.googlecode.aviator.runtime.function.seq.SeqZipmapFunction;
import com.googlecode.aviator.runtime.function.string.StringContainsFunction;
Expand Down Expand Up @@ -975,6 +982,14 @@ private void loadSeqFunctions() {
new SeqMakePredicateFunFunction("seq.false", OperatorType.EQ, AviatorBoolean.FALSE));
addFunction(new SeqMakePredicateFunFunction("seq.nil", OperatorType.EQ, AviatorNil.NIL));
addFunction(new SeqMakePredicateFunFunction("seq.exists", OperatorType.NEQ, AviatorNil.NIL));

addFunction(new SeqContainsAnyFunction());
addFunction(new SeqDisjunctionFunction());
addFunction(new SeqIntersectionFunction());
addFunction(new SeqIsEqualFunction());
addFunction(new SeqIsSubSeqFunction());
addFunction(new SeqSubtractFunction());
addFunction(new SeqUnionFunction());
}

private void loadMathFunctions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.googlecode.aviator.runtime.function.seq;

import com.googlecode.aviator.runtime.function.AbstractFunction;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* Base class for min/max function.
*
* @author dennis
*/
public abstract class AbstractSeqOperationFunction extends AbstractFunction {


/**
* Constant to avoid repeated object creation
*/
private static final Integer INTEGER_ONE = 1;

/**
* Gets cardinality map.
*
* @param coll the coll
* @return the cardinality map
*/
public Map<Object, Integer> getCardinalityMap(final Collection<?> coll) {
Map<Object, Integer> count = new HashMap<>(coll.size());
for (Object obj : coll) {
Integer c = count.get(obj);
if (c == null) {
count.put(obj, INTEGER_ONE);
} else {
count.put(obj, c + 1);
}
}
return count;
}

/**
* Gets freq.
*
* @param obj the obj
* @param freqMap the freq map
* @return the freq
*/
public int getFreq(final Object obj, final Map<?, ?> freqMap) {
Integer count = (Integer) freqMap.get(obj);
if (count != null) {
return count;
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.googlecode.aviator.runtime.function.seq;

import com.googlecode.aviator.runtime.type.AviatorBoolean;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.util.Collection;
import java.util.Map;


/**
* Is there an intersection
*
* @author bluecrush
*/
public class SeqContainsAnyFunction extends AbstractSeqOperationFunction {
@Override
public String getName() {
return "seqUtil.containsAny";
}

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
Object seq1 = arg1.getValue(env);
Object seq2 = arg2.getValue(env);
if (!Collection.class.isAssignableFrom(seq1.getClass())) {
throw new IllegalArgumentException("arg1 `" + seq1 + "` it's not a seq.");
}
if (!Collection.class.isAssignableFrom(seq2.getClass())) {
throw new IllegalArgumentException("arg2 `" + seq2 + "` it's not a seq.");
}
Collection<?> coll1 = (Collection<?>) seq1;
Collection<?> coll2 = (Collection<?>) seq2;
if (coll1.size() < coll2.size()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Collections.disjoint can be used here:
https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#disjoint(java.util.Collection,%20java.util.Collection)

return Collections.disjoint(coll1, coll2)? AviatorBoolean.FALSE : AviatorBoolean.TRUE

for (Object o : coll1) {
if (coll2.contains(o)) {
return AviatorBoolean.TRUE;
}
}
} else {
for (Object o : coll2) {
if (coll1.contains(o)) {
return AviatorBoolean.TRUE;
}
}
}
return AviatorBoolean.FALSE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.googlecode.aviator.runtime.function.seq;

import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


/**
*
* Complement of intersection
*
* @author bluecrush
*/
public class SeqDisjunctionFunction extends AbstractSeqOperationFunction {
@Override
public String getName() {
return "seqUtil.disjunction";
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
Object seq1 = arg1.getValue(env);
Object seq2 = arg2.getValue(env);
if (!Collection.class.isAssignableFrom(seq1.getClass())) {
throw new IllegalArgumentException("arg1 `" + seq1 + "` it's not a seq.");
}
if (!Collection.class.isAssignableFrom(seq2.getClass())) {
throw new IllegalArgumentException("arg2 `" + seq2 + "` it's not a seq.");
}
Collection<?> collection1 = (Collection<?>) seq1;
Collection<?> collection2 = (Collection<?>) seq2;
Map<Object,Integer> map1 = getCardinalityMap(collection1);
Map<Object,Integer> map2 = getCardinalityMap(collection2);
Set<Object> set1 = new HashSet<>(collection1);
set1.addAll(collection2);
ArrayList<Object> list = new ArrayList<>();
for (Object obj : set1) {
for (int i = 0, m = ((Math.max(getFreq(obj, map1), getFreq(obj, map2))) - (Math.min(getFreq(obj, map1), getFreq(obj, map2)))); i < m; i++) {
list.add(obj);
}
}
return AviatorRuntimeJavaType.valueOf(list);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.googlecode.aviator.runtime.function.seq;

import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
* intersection
*
* @author bluecrush
*/
public class SeqIntersectionFunction extends AbstractSeqOperationFunction {
@Override
public String getName() {
return "seqUtil.intersection";
}

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
Object seq1 = arg1.getValue(env);
Object seq2 = arg2.getValue(env);
if (!Collection.class.isAssignableFrom(seq1.getClass())) {
throw new IllegalArgumentException("arg1 `" + seq1 + "` it's not a seq.");
}
if (!Collection.class.isAssignableFrom(seq2.getClass())) {
throw new IllegalArgumentException("arg2 `" + seq2 + "` it's not a seq.");
}
Collection<?> collection1 = (Collection<?>) seq1;
Collection<?> collection2 = (Collection<?>) seq2;
Map<?,?> map1 = getCardinalityMap(collection1);
Map<?,?> map2 = getCardinalityMap(collection2);
Set<Object> set1 = new HashSet<>(collection1);
set1.addAll(collection2);
List<Object> list = new ArrayList<>();
for (Object obj : set1) {
for (int i = 0, m = Math.min(getFreq(obj, map1), getFreq(obj, map2)); i < m; i++) {
list.add(obj);
}
}
return AviatorRuntimeJavaType.valueOf(list);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.googlecode.aviator.runtime.function.seq;

import com.googlecode.aviator.runtime.type.AviatorBoolean;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.util.Collection;
import java.util.Map;


/**
* Are the two sets the same
*
* @author bluecrush
*/
public class SeqIsEqualFunction extends AbstractSeqOperationFunction {
@Override
public String getName() {
return "seqUtil.isEqual";
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
Object seq1 = arg1.getValue(env);
Object seq2 = arg2.getValue(env);
if (!Collection.class.isAssignableFrom(seq1.getClass())) {
throw new IllegalArgumentException("arg1 `" + seq1 + "` it's not a seq.");
}
if (!Collection.class.isAssignableFrom(seq2.getClass())) {
throw new IllegalArgumentException("arg2 `" + seq2 + "` it's not a seq.");
}
Collection<?> collection1 = (Collection<?>) seq1;
Collection<?> collection2 = (Collection<?>) seq2;
if(collection1.size() != collection2.size()) {
return AviatorBoolean.FALSE;
} else {
Map<?,?> map1 = getCardinalityMap(collection1);
Map<?,?> map2 = getCardinalityMap(collection2);
if(map1.size() != map2.size()) {
return AviatorBoolean.FALSE;
} else {
for (Object obj : map1.keySet()) {
if (getFreq(obj, map1) != getFreq(obj, map2)) {
return AviatorBoolean.FALSE;
}
}
return AviatorBoolean.TRUE;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.googlecode.aviator.runtime.function.seq;

import com.googlecode.aviator.runtime.type.AviatorBoolean;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.util.Collection;
import java.util.Map;


/**
* Is <arg2/> a subset of <arg1/>
*
* @author bluecrush
*/
public class SeqIsSubSeqFunction extends AbstractSeqOperationFunction {
@Override
public String getName() {
return "seqUtil.isSubSeq";
}

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
Object seq1 = arg1.getValue(env);
Object seq2 = arg2.getValue(env);
if (!Collection.class.isAssignableFrom(seq1.getClass())) {
throw new IllegalArgumentException("arg1 `" + seq1 + "` it's not a seq.");
}
if (!Collection.class.isAssignableFrom(seq2.getClass())) {
throw new IllegalArgumentException("arg2 `" + seq2 + "` it's not a seq.");
}
Collection<?> coll1 = (Collection<?>) seq1;
Collection<?> coll2 = (Collection<?>) seq2;
Map<?,?> map1 = getCardinalityMap(coll1);
Map<?,?> map2 = getCardinalityMap(coll2);
for (Object obj : coll1) {
if (getFreq(obj, map1) > getFreq(obj, map2)) {
return AviatorBoolean.FALSE;
}
}
return AviatorBoolean.TRUE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.googlecode.aviator.runtime.function.seq;

import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
* Subtract
*
* @author bluecrush
*/
public class SeqSubtractFunction extends AbstractSeqOperationFunction {
@Override
public String getName() {
return "seqUtil.subtract";
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
Object seq1 = arg1.getValue(env);
Object seq2 = arg2.getValue(env);
if (!Collection.class.isAssignableFrom(seq1.getClass())) {
throw new IllegalArgumentException("arg1 `" + seq1 + "` it's not a seq.");
}
if (!Collection.class.isAssignableFrom(seq2.getClass())) {
throw new IllegalArgumentException("arg2 `" + seq2 + "` it's not a seq.");
}
Collection<?> collection1 = (Collection<?>) seq1;
Collection<?> collection2 = (Collection<?>) seq2;
List<?> list = new ArrayList<>(collection1);
for (Object o : collection2) {
list.remove(o);
}
return AviatorRuntimeJavaType.valueOf(list);
}
}
Loading