-
Notifications
You must be signed in to change notification settings - Fork 871
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
bluecrush
wants to merge
1
commit into
killme2008:master
Choose a base branch
from
bluecrush:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/com/googlecode/aviator/runtime/function/seq/AbstractSeqOperationFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/googlecode/aviator/runtime/function/seq/SeqContainsAnyFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) { | ||
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; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/googlecode/aviator/runtime/function/seq/SeqDisjunctionFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/googlecode/aviator/runtime/function/seq/SeqIntersectionFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/googlecode/aviator/runtime/function/seq/SeqIsEqualFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/googlecode/aviator/runtime/function/seq/SeqIsSubSeqFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/googlecode/aviator/runtime/function/seq/SeqSubtractFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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