Show
Ignore:
Timestamp:
11/06/09 14:37:52 (3 weeks ago)
Author:
jmaessen
Message:

[compiler, tests] Some refactoring on TreapAndTest. Fixed #362 and
added a test for it. Some misc cleanups on AbstractMethodChecker
that didn't make it into the last commit [Still looking to fix this to
avoid O(n2) behavior].

Location:
trunk/ProjectFortress/compiler_tests
Files:
2 added
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/ProjectFortress/compiler_tests/TreapAndTest.fss

    r4317 r4318  
     1(******************************************************************************* 
     2    Copyright 2009 Sun Microsystems, Inc., 
     3    4150 Network Circle, Santa Clara, California 95054, U.S.A. 
     4    All rights reserved. 
     5 
     6    U.S. Government Rights - Commercial software. 
     7    Government users are subject to the Sun Microsystems, Inc. standard 
     8    license agreement and applicable provisions of the FAR and its supplements. 
     9 
     10    Use is subject to license terms. 
     11 
     12    This distribution may include materials developed by third parties. 
     13 
     14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered 
     15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries. 
     16 ******************************************************************************) 
     17 
    118export Executable 
    219 
     
    5572 
    5673 
    57  
    58  
    59 Min_HK : ZZ32 = -1 
     74Min_W : ZZ32 = -1 
    6075 
    6176(* We organize the treap as a max-treap; a min-treap would work just as well. *) 
     
    6782    getter min(): Treap = Treap(root.min) 
    6883    getter max(): Treap = Treap(root.max) 
     84    (* Union and intersection are left-biased *) 
    6985    opr UNION(self, other: Treap): Treap = Treap(root.combine(UnionOp, other.root)) 
    7086    opr INTERSECTION(self, other: Treap): Treap = Treap(root.combine(IntersectionOp, other.root)) 
     
    7389    opr SYMDIFF(self, other: Treap): Treap = 
    7490        Treap(root.combine(SymdiffOp, other.root)) 
     91    containsKey(key:ZZ32): Boolean = NOT root.nodeWithKey(key).isEmpty 
    7592    replace(key:ZZ32, val:String): Treap = singleton(key,val) UNION self 
    7693    add(key:ZZ32, val:String): Treap = self UNION singleton(key,val) 
    77     remove(key:ZZ32): Treap = self DIFFERENCE Treap(Leaf1(Min_HK,key,"")) 
     94    remove(key:ZZ32): Treap = self DIFFERENCE Treap(Leaf1(Min_W,key,"")) 
    7895    lookup(key:ZZ32, defaultValue:String): String = 
    7996        root.nodeWithKey(key).rootValue(defaultValue) 
     
    88105trait TreapNode comprises { NonEmpty, Empty } 
    89106    getter isEmpty(): Boolean 
    90     getter hk(): ZZ32 
     107    getter w(): ZZ32 
    91108    getter min(): TreapNode 
    92109    getter max(): TreapNode 
     
    121138object Empty extends TreapNode 
    122139    getter isEmpty(): Boolean = true 
    123     getter hk(): ZZ32 = Min_HK 
     140    getter w(): ZZ32 = Min_W 
    124141    getter min(): TreapNode = self 
    125142    getter max(): TreapNode = self 
     
    140157    joinNE(l: NonEmpty): NonEmpty = l 
    141158 
    142     combine(c: CombiningOp, r: TreapNode): TreapNode = c.leftEmpty(r) 
    143     combineNE(l: NonEmpty, c: CombiningOp): TreapNode = c.rightEmpty(l) 
    144  
    145     combineRootL(lr: Leaf1, c: CombiningOp): TreapNode = c.rightEmpty(lr) 
    146     combineRootR(c: CombiningOp, rr: Leaf1): TreapNode = c.leftEmpty(rr) 
     159    combine(c: CombiningOp, r: TreapNode): TreapNode = c.rightAlone(r) 
     160    combineNE(l: NonEmpty, c: CombiningOp): TreapNode = c.leftAlone(l) 
     161 
     162    combineRootL(lr: Leaf1, c: CombiningOp): TreapNode = c.leftAlone(lr) 
     163    combineRootR(c: CombiningOp, rr: Leaf1): TreapNode = c.rightAlone(rr) 
    147164 
    148165    rootKey(defaultKey: ZZ32): ZZ32 = defaultKey 
     
    151168 
    152169trait NonEmpty extends TreapNode comprises { Leaf1, Node } 
     170    getter isEmpty(): Boolean = false 
    153171    getter k(): ZZ32 
    154172    getter v(): String 
    155173    getter root(): Leaf1 
     174 
     175    join(r: TreapNode): TreapNode = r.joinNE(self) 
    156176    (* Join this treap (on left) to r (on right); 
    157        assumes hk > r.hk *) 
     177       assumes w > r.w *) 
    158178    joinNEH(r: NonEmpty): NonEmpty 
    159179 
     180    combine(c: CombiningOp, r: TreapNode): TreapNode = 
     181        r.combineNE(self,c) 
    160182    combineNEH(c: CombiningOp, r: NonEmpty): TreapNode 
    161 end 
    162  
    163 object Leaf1(hk0: ZZ32, k0: ZZ32, v0: String) extends NonEmpty 
    164     getter isEmpty(): Boolean = false 
     183 
     184    combineRootL(lr: Leaf1, c: CombiningOp): TreapNode = c.combine(lr,self.root) 
     185    combineRootR(c: CombiningOp, rr: Leaf1): TreapNode = c.combine(self.root,rr) 
     186 
     187    rootKey(defaultKey: ZZ32): ZZ32 = self.k 
     188    rootValue(defaultValue: String): String = self.v 
     189end 
     190 
     191object Leaf1(w0: ZZ32, k0: ZZ32, v0: String) extends NonEmpty 
    165192    getter k(): ZZ32 = k0 
    166193    getter v(): String = v0 
    167194    getter root(): Leaf1 = self 
    168     getter hk(): ZZ32 = hk0 
     195    getter w(): ZZ32 = w0 
    169196    getter min(): TreapNode = self 
    170197    getter max(): TreapNode = self 
     
    172199    mkString(withParens: Boolean): String = 
    173200        if withParens then 
    174             "<" hk0.asString ">" self.asString 
     201            "<" w0.asString ">" self.asString 
    175202        else 
    176203            self.asString 
     
    197224        if key < k0 then self else Empty end 
    198225 
    199     join(r: TreapNode): TreapNode = r.joinNE(self) 
    200226    joinNE(l: NonEmpty): NonEmpty = 
    201         if hk0 > l.hk then 
    202             Node(l, hk0, k0, v0, Empty) 
     227        if w0 > l.w then 
     228            Node(l, w0, k0, v0, Empty) 
    203229        else 
    204230            l.joinNEH(self) 
    205231        end 
    206232    joinNEH(r: NonEmpty): NonEmpty = 
    207         Node(Empty, hk0, k0, v0, r) 
    208  
    209     combine(c: CombiningOp, r: TreapNode): TreapNode = 
    210         r.combineNE(self,c) 
     233        Node(Empty, w0, k0, v0, r) 
     234 
    211235    combineNE(l: NonEmpty, c: CombiningOp): TreapNode = 
    212         if hk0 < l.hk then 
     236        if w0 > l.w then 
     237            (lt, m, rt) = (l.splitL(k0), l.nodeWithKey(k0), l.splitR(k0)) 
     238            c.leftAlone(lt).join(m.combineRootR(c, self)).join(c.leftAlone(rt)) 
     239        else 
    213240            l.combineNEH(c,self) 
    214         else 
    215             (lt, m, rt) = (l.splitL(k0), l.nodeWithKey(k0), l.splitR(k0)) 
    216             c.rightEmpty(lt).join(m.combineRootR(c, self)).join(c.rightEmpty(rt)) 
    217241        end 
    218242    combineNEH(c: CombiningOp, r: NonEmpty): TreapNode = do 
    219243            (lt, m, rt) = (r.splitL(k0), r.nodeWithKey(k0), r.splitR(k0)) 
    220             c.leftEmpty(lt).join(m.combineRootL(self, c)).join(c.leftEmpty(rt)) 
    221         end 
    222  
    223     combineRootL(lr: Leaf1, c: CombiningOp): TreapNode = c.combine(lr,self) 
    224     combineRootR(c: CombiningOp, rr: Leaf1): TreapNode = c.combine(self,rr) 
    225  
    226     rootKey(defaultKey: ZZ32): ZZ32 = k0 
    227     rootValue(defaultValue: String): String = v0 
     244            c.rightAlone(lt).join(m.combineRootL(self, c)).join(c.rightAlone(rt)) 
     245        end 
    228246end 
    229247 
     
    231249    Leaf1(randomZZ32(2147483647), key, val) 
    232250 
    233 object Node(left: TreapNode, hk0: ZZ32, k0: ZZ32, v0: String, right: TreapNode) 
     251object Node(left: TreapNode, w0: ZZ32, k0: ZZ32, v0: String, right: TreapNode) 
    234252        extends NonEmpty 
    235     getter isEmpty(): Boolean = false 
    236253    getter k(): ZZ32 = k0 
    237254    getter v(): String = v0 
    238     getter root(): Leaf1 = Leaf1(hk0, k0, v0) 
    239     getter hk(): ZZ32 = hk0 
     255    getter root(): Leaf1 = Leaf1(w0, k0, v0) 
     256    getter w(): ZZ32 = w0 
    240257    getter min(): TreapNode = 
    241258        if left.isEmpty then self.root else left.min end 
     
    247264        (l,r) = (left.mkString(withParens), right.mkString(withParens)) 
    248265        if withParens then 
    249             "(" l ") <" hk0.asString ">" mid " (" r ")" 
     266            "(" l ") <" w0.asString ">" mid " (" r ")" 
    250267        else 
    251268            lh = if l.isEmpty then mid else l " " mid end 
     
    258275        if key < k0 then 
    259276            (l, match, r) = left.split(key) 
    260             (l, match, node(r, hk0, k0, v0, right)) 
     277            (l, match, node(r, w0, k0, v0, right)) 
    261278        elif key > k0 then 
    262279            (l, match, r) = right.split(key) 
    263             (node(left, hk0, k0, v0, l), match, r) 
     280            (node(left, w0, k0, v0, l), match, r) 
    264281        else 
    265282            (left, self, right) 
     
    271288            left.splitL(key) 
    272289        elif key > k0 then 
    273             Node(left, hk0, k0, v0, right.splitL(key)) 
     290            Node(left, w0, k0, v0, right.splitL(key)) 
    274291        else 
    275292            left 
     
    287304    splitR(key:ZZ32): TreapNode = 
    288305        if key < k0 then 
    289             Node(left.splitR(key), hk0, k0, v0, right) 
     306            Node(left.splitR(key), w0, k0, v0, right) 
    290307        elif key > k0 then 
    291308            right.splitR(key) 
     
    294311        end 
    295312 
    296     join(r: TreapNode): TreapNode = r.joinNE(self) 
    297313    joinNE(l: NonEmpty): NonEmpty = 
    298         if hk0 > l.hk then 
    299             Node(l.join(left), hk0, k0, v0, right) 
     314        if w0 > l.w then 
     315            Node(l.join(left), w0, k0, v0, right) 
    300316        else 
    301317            l.joinNEH(self) 
    302318        end 
    303319    joinNEH(r: NonEmpty): NonEmpty = 
    304         Node(left, hk0, k0, v0, right.join(r)) 
    305  
    306     combine(c: CombiningOp, r: TreapNode): TreapNode = 
    307         r.combineNE(self,c) 
     320        Node(left, w0, k0, v0, right.join(r)) 
     321 
    308322    combineNE(l: NonEmpty, c: CombiningOp): TreapNode = 
    309         if hk0 < l.hk then 
    310             l.combineNEH(c,self) 
    311         else 
     323        if w0 > l.w then 
    312324            (lt, m, rt) = (l.splitL(k0), l.nodeWithKey(k0), l.splitR(k0)) 
    313325            lt.combine(c,left).join(m.combineRootR(c, self.root)).join(rt.combine(c,right)) 
     326        else 
     327            l.combineNEH(c,self) 
    314328        end 
    315329    combineNEH(c: CombiningOp, r: NonEmpty): TreapNode = do 
     
    317331            left.combine(c,lt).join(m.combineRootL(self.root, c)).join(right.combine(c,rt)) 
    318332        end 
    319  
    320     combineRootL(lr: Leaf1, c: CombiningOp): TreapNode = c.combine(lr,self.root) 
    321     combineRootR(c: CombiningOp, rr: Leaf1): TreapNode = c.combine(self.root,rr) 
    322  
    323     rootKey(defaultKey: ZZ32): ZZ32 = k0 
    324     rootValue(defaultValue: String): String = v0 
    325333end 
    326334 
    327335trait CombiningOp 
    328     leftEmpty(right: TreapNode): TreapNode 
    329     rightEmpty(left: TreapNode): TreapNode 
     336    rightAlone(right: TreapNode): TreapNode 
     337    leftAlone(left: TreapNode): TreapNode 
    330338    combine(leftArg: Leaf1, rightArg: Leaf1): TreapNode 
    331339end 
    332340 
    333341object UnionOp extends CombiningOp 
    334     leftEmpty(right: TreapNode): TreapNode = right 
    335     rightEmpty(left: TreapNode): TreapNode = left 
     342    rightAlone(right: TreapNode): TreapNode = right 
     343    leftAlone(left: TreapNode): TreapNode = left 
    336344    combine(leftArg: Leaf1, rightArg: Leaf1): TreapNode = leftArg 
    337345end 
    338346 
    339347object IntersectionOp extends CombiningOp 
    340     leftEmpty(right: TreapNode): TreapNode = Empty 
    341     rightEmpty(left: TreapNode): TreapNode = Empty 
     348    rightAlone(right: TreapNode): TreapNode = Empty 
     349    leftAlone(left: TreapNode): TreapNode = Empty 
    342350    combine(leftArg: Leaf1, rightArg: Leaf1): TreapNode = leftArg 
    343351end 
    344352 
    345353object DifferenceOp extends CombiningOp 
    346     leftEmpty(right: TreapNode): TreapNode = Empty 
    347     rightEmpty(left: TreapNode): TreapNode = left 
     354    rightAlone(right: TreapNode): TreapNode = Empty 
     355    leftAlone(left: TreapNode): TreapNode = left 
    348356    combine(leftArg: Leaf1, rightArg: Leaf1): TreapNode = Empty 
    349357end 
    350358 
    351359object SymdiffOp extends CombiningOp 
    352     leftEmpty(right: TreapNode): TreapNode = right 
    353     rightEmpty(left: TreapNode): TreapNode = left 
     360    rightAlone(right: TreapNode): TreapNode = right 
     361    leftAlone(left: TreapNode): TreapNode = left 
    354362    combine(leftArg: Leaf1, rightArg: Leaf1): TreapNode = Empty 
    355363end