Check类中的union,excl,diff,intersect

 

定义一些类,这些类之间有父子关系,如下:

class Father{}
class Son1 extends Father{}
class Son2 extends Father{}

class Top{}
class Middle extends Top{}
class Bottom extends Middle{}

 

 

1、incl()方法

 

源代码如下:

/** Add type set to given type list, unless
 *  it is a subclass of some class  in the list.
 */
public List<Type> incl(Type t, List<Type> ts) {
    List<Type> result;
    // 如果t是ts列表中一些类的子类,则返回这个ts
    if(subset(t,ts)){
        result = ts;
    }else{
        // 如果ts列表中有些类是t的子类,则从ts列表中移除这些子类,
        // 然后追加t后将这个ts列表返回
        List<Type> temp = excl(t, ts);
        result = temp.prepend(t);
    }
    return result;
}

  

 

 

2、excl()方法  

 

源代码如下: 

/** Remove type set from type set list.
 */
// 如果ts列表中有些类是t的子类,则从ts列表中移除这些子类后返回,
// 如果ts列表为空,表示没有可移除的或者说已经全部被移除完了,
// 直接返回ts空列表
public List<Type> excl(Type t, List<Type> ts) {
    if (ts.isEmpty()) {
        return ts;
    } else {
        // 需要清楚知道List<Type>这个类型的实现机制
        List<Type> ts1 = excl(t, ts.tail); // 递归
        // 当ts.head是t的子类时,移除这个ts.head,返回ts.tail
        // 处理后的结果
        if (types.isSubtypeCapture(ts.head, t)) {
            return ts1;
        }
        // 当ts.head没有成为t的子类时,则列表中不需要移除
        // 这个ts.head,直接返回ts
        else if (ts1 == ts.tail) {
            return ts;
        }
        // 当ts.head没有成为t的子类时且ts.tail处理结果也有变动,
        // 则追加ts.head到ts1后返回
        else {
            return ts1.prepend(ts.head);
        }
    }
}

 

  

 

 

3、并集union()方法

源代码如下:

/** Form the union of two type set lists.
 */
public List<Type> union(List<Type> ts1, List<Type> ts2) {
    List<Type> ts = ts1;
    for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) {
        ts = incl(l.head, ts);
    }
    return ts;
}

  

 

4、差集diff()方法 

源代码如下: 

/** Form the difference of two type lists.
 */
// 如果ts1列表有些类是ts2列表中某些类的子类,则从ts1
// 列表中移除这些子类,最后返回ts1中剩余的类
public List<Type> diff(List<Type> ts1, List<Type> ts2) {
    List<Type> ts = ts1;
    for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) {
        ts = excl(l.head, ts);
    }
    return ts;
}

  

 

 

5、交集intersect()方法 

源代码如下: 

/** Form the intersection of two type lists.
 */
// 如果有ts1列表中含有Father类型,而ts2中含有Father的
// 子类Sub1,Sub2 时,最终返回Sub1,Sub2,表示这个
// Father类型能够catch那两个子类型
public List<Type> intersect(List<Type> ts1, List<Type> ts2) { // todo
    List<Type> ts = List.nil();
    for (List<Type> l = ts1; l.nonEmpty(); l = l.tail) {
        if (subset(l.head, ts2)) {
            ts = incl(l.head, ts);
        }
    }
    for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) {
        if (subset(l.head, ts1)) {
            ts = incl(l.head, ts);
        }
    }
    return ts;
}

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/extjs4/p/9452798.html