📄 expstdop.java
字号:
} public Value eval(EvalContext ctx, Value[] args, Type resultType) { CollectionValue coll = (CollectionValue) args[0]; Iterator collIter = coll.iterator(); while (collIter.hasNext() ) { Value elem = (Value) collIter.next(); if (elem.isUndefined() ) return new UndefinedValue(resultType); } if (coll.isBag() ) return ((BagValue) coll).flatten(); else if (coll.isSet() ) return ((SetValue) coll).flatten(); else if (coll.isSequence() ) return ((SequenceValue) coll).flatten(); else throw new RuntimeException("Unexpected collection type `" + coll.type() + "'."); }}// --------------------------------------------------------// // Set operations.//// --------------------------------------------------------/* union : Set(T1) x Set(T2) -> Set(T1), with T2 <= T1 */final class Op_set_union extends OpGeneric { public String name() { return "union"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isSet() && params[1].isSet() ) { SetType set1 = (SetType) params[0]; SetType set2 = (SetType) params[1]; if (set2.elemType().isSubtypeOf(set1.elemType()) ) return set1; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { SetValue set1 = (SetValue) args[0]; SetValue set2 = (SetValue) args[1]; return set1.union(set2); }}// --------------------------------------------------------/* union : Set(T1) x Bag(T2) -> Bag(T1), with T2 <= T1 */final class Op_set_union_bag extends OpGeneric { public String name() { return "union"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isSet() && params[1].isBag() ) { SetType set = (SetType) params[0]; BagType bag = (BagType) params[1]; if (bag.elemType().isSubtypeOf(set.elemType()) ) return TypeFactory.mkBag(set.elemType()); } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { SetValue set = (SetValue) args[0]; BagValue bag = (BagValue) args[1]; return set.union(bag); }}// --------------------------------------------------------/* intersection : Set(T1) x Set(T2) -> Set(T1), with T2 <= T1 */final class Op_set_intersection extends OpGeneric { public String name() { return "intersection"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isSet() && params[1].isSet() ) { SetType set1 = (SetType) params[0]; SetType set2 = (SetType) params[1]; if (set2.elemType().isSubtypeOf(set1.elemType()) ) return set1; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { SetValue set1 = (SetValue) args[0]; SetValue set2 = (SetValue) args[1]; return set1.intersection(set2); }}// --------------------------------------------------------/* intersection : Set(T1) x Bag(T2) -> Set(T1), with T2 <= T1 */final class Op_set_intersection_bag extends OpGeneric { public String name() { return "intersection"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isSet() && params[1].isBag() ) { SetType set = (SetType) params[0]; BagType bag = (BagType) params[1]; if (bag.elemType().isSubtypeOf(set.elemType()) ) return set; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { SetValue set = (SetValue) args[0]; BagValue bag = (BagValue) args[1]; return set.intersection(bag); }}// --------------------------------------------------------/* - : Set(T1) x Set(T2) -> Set(T1), with T2 <= T1 */final class Op_set_difference extends OpGeneric { public String name() { return "-"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return true; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isSet() && params[1].isSet() ) { SetType set1 = (SetType) params[0]; SetType set2 = (SetType) params[1]; if (set2.elemType().isSubtypeOf(set1.elemType()) ) return set1; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { SetValue set1 = (SetValue) args[0]; SetValue set2 = (SetValue) args[1]; return set1.difference(set2); }}// --------------------------------------------------------/* including : Set(T1) x T2 -> Set(T1), with T2 <= T1 */final class Op_set_including extends OpGeneric { public String name() { return "including"; } public int kind() { return SPECIAL; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isSet() ) { SetType set1 = (SetType) params[0]; if (params[1].isSubtypeOf(set1.elemType()) ) return set1; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { if (args[0].isUndefined() ) return new UndefinedValue(resultType); SetValue set1 = (SetValue) args[0]; return set1.including(args[1]); }}// --------------------------------------------------------/* excluding : Set(T1) x T2 -> Set(T1), with T2 <= T1 */final class Op_set_excluding extends OpGeneric { public String name() { return "excluding"; } public int kind() { return SPECIAL; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isSet() ) { SetType set1 = (SetType) params[0]; if (params[1].isSubtypeOf(set1.elemType()) ) return set1; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { if (args[0].isUndefined() ) return new UndefinedValue(resultType); SetValue set1 = (SetValue) args[0]; return set1.excluding(args[1]); }}// --------------------------------------------------------/* symmetricDifference : Set(T1) x Set(T2) -> Set(T1) with T2 <= T1 */final class Op_set_symmetricDifference extends OpGeneric { public String name() { return "symmetricDifference"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isSet() && params[1].isSet() ) { SetType set1 = (SetType) params[0]; SetType set2 = (SetType) params[1]; if (set2.elemType().isSubtypeOf(set1.elemType()) ) return set1; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { SetValue set1 = (SetValue) args[0]; SetValue set2 = (SetValue) args[1]; return set1.symmetricDifference(set2); }}// --------------------------------------------------------/* asSequence : Set(T) -> Sequence(T) */final class Op_set_asSequence extends OpGeneric { public String name() { return "asSequence"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 1 && params[0].isSet() ) { SetType set = (SetType) params[0]; return TypeFactory.mkSequence(set.elemType()); } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { SetValue set = (SetValue) args[0]; return set.asSequence(); }}// --------------------------------------------------------/* asBag : Set(T) -> Bag(T) */final class Op_set_asBag extends OpGeneric { public String name() { return "asBag"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 1 && params[0].isSet() ) { SetType set = (SetType) params[0]; return TypeFactory.mkBag(set.elemType()); } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { SetValue set = (SetValue) args[0]; return set.asBag(); }}// --------------------------------------------------------//// Bag operations.//// --------------------------------------------------------/* union : Bag(T1) x Bag(T2) -> Bag(T1), with T2 <= T1 */final class Op_bag_union extends OpGeneric { public String name() { return "union"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isBag() && params[1].isBag() ) { BagType bag1 = (BagType) params[0]; BagType bag2 = (BagType) params[1]; if (bag2.elemType().isSubtypeOf(bag1.elemType()) ) return bag1; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { BagValue bag1 = (BagValue) args[0]; BagValue bag2 = (BagValue) args[1]; return bag1.union(bag2); }}// --------------------------------------------------------/* union : Bag(T1) x Set(T2) -> Bag(T1), with T2 <= T1 */final class Op_bag_union_set extends OpGeneric { public String name() { return "union"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isBag() && params[1].isSet() ) { BagType bag = (BagType) params[0]; SetType set = (SetType) params[1]; if (set.elemType().isSubtypeOf(bag.elemType()) ) return bag; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { BagValue bag = (BagValue) args[0]; SetValue set = (SetValue) args[1]; return bag.union(set.asBag()); }}// --------------------------------------------------------/* intersection : Bag(T1) x Bag(T2) -> Bag(T1), with T2 <= T1 */final class Op_bag_intersection extends OpGeneric { public String name() { return "intersection"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isBag() && params[1].isBag() ) { BagType bag1 = (BagType) params[0]; BagType bag2 = (BagType) params[1]; if (bag2.elemType().isSubtypeOf(bag1.elemType()) ) return bag1; } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { BagValue bag1 = (BagValue) args[0]; BagValue bag2 = (BagValue) args[1]; return bag1.intersection(bag2); }}// --------------------------------------------------------/* intersection : Bag(T1) x Set(T2) -> Set(T1), with T2 <= T1 */final class Op_bag_intersection_set extends OpGeneric { public String name() { return "intersection"; } public int kind() { return OPERATION; } public boolean isInfixOrPrefix() { return false; } public Type matches(Type params[]) { if (params.length == 2 && params[0].isBag() && params[1].isSet() ) { BagType bag = (BagType) params[0]; SetType set = (SetType) params[1]; if (set.elemType().isSubtypeOf(bag.elemType()) ) return TypeFactory.mkSet(bag.elemType()); } return null; } public Value eval(EvalContext ctx, Value[] args, Type resultType) { BagValue bag = (BagValue)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -