Changeset 2137

Show
Ignore:
Timestamp:
06/30/08 07:23:42 (3 months ago)
Author:
mspiegel
Message:

Implemented verify() method of skip tree data structures. test suite for skip trees is almost complete.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Library/incomplete/SkipTree.fsi

    r2117 r2137  
    4040    randomLevel():ZZ32 
    4141 
     42    (** 
     43     * The verify method will throw an error 
     44     * if this structure does not satisfy the properties 
     45     * of a Skip Tree. 
     46     **)     
     47    verify():() 
     48 
    4249end (* trait SkipTree[\Key,Value\] *) 
    4350 
  • trunk/Library/incomplete/SkipTree.fss

    r2117 r2137  
    4848        level 
    4949    end 
     50     
     51    (** 
     52     * The verify method will throw an error 
     53     * if this structure does not satisfy the properties 
     54     * of a Skip Tree. 
     55     **)     
     56    verify():()     
    5057 
    5158end (* trait SkipTree[\Key,Value\] *) 
     
    6673    end 
    6774     
     75    verify():() = root.verify() 
     76    
    6877end 
    6978 
     
    7786        NonEmptySkipTree[\Key,Value\](root')         
    7887    end 
     88     
     89    verify():() = () 
    7990 
    8091end 
     
    232243        if index >= 0 then index 
    233244        else |index| - 1 end 
     245    end 
     246 
     247    smallestKey():Key = do 
     248        typecase child = children[0] of     
     249            Just[\Node[\Key,Value\]\] => child.get().smallestKey() 
     250            else => keys[0] 
     251        end 
     252    end 
     253     
     254    largestKey():Key = do 
     255        typecase child = children[children.size() - 1] of     
     256            Just[\Node[\Key,Value\]\] => child.get().largestKey() 
     257            else => keys[keys.size() - 1] 
     258        end 
     259    end 
     260 
     261    (** 
     262     * The verify method will throw an error 
     263     * if this structure does not satisfy the properties 
     264     * of a Skip Tree. 
     265     **)         
     266    verify():() = do 
     267        if height <= 0 then 
     268            fail(toString() " : All nodes must have a height that is a positive integer") 
     269        end 
     270        if |keys| =/= |values| then 
     271            fail(toString() " : Size of keys is not equal to size of values") 
     272        end 
     273        if |keys| + 1 =/= |children| then 
     274            fail(toString() " : Size of children is not equal to size of keys plus one") 
     275        end 
     276        for i <- 0 # (keys.size() - 1) do 
     277            if keys[i] >= keys[i + 1] then 
     278                fail(toString() " : Keys " keys[i] " and " keys[i + 1] " are not in sorted order") 
     279            end 
     280        end 
     281        for i <- 0 # keys.size() do         
     282            typecase child = children[i] of     
     283                Just[\Node[\Key,Value\]\] => do 
     284                    if child.get().largestKey() > keys[i] then 
     285                        fail(toString() " : " child.get().largestKey() " > " keys[i]) 
     286                    end 
     287                end 
     288                else => () 
     289            end 
     290            typecase child = children[i + 1] of     
     291                Just[\Node[\Key,Value\]\] => do 
     292                    if child.get().smallestKey() < keys[i] then 
     293                        fail(toString() " : " child.get().smallestKey() " < " keys[i]) 
     294                    end 
     295                end 
     296                else => () 
     297            end             
     298        end 
     299        (* recusively check non-empty children *) 
     300        for i <- 0 # children.size() do 
     301            typecase child = children[i] of     
     302                Just[\Node[\Key,Value\]\] => child.get().verify() 
     303                else => () 
     304            end         
     305        end 
    234306    end 
    235307     
     
    269341  **) 
    270342binarySearch[\T\](list : List[\T\], key : T) : ZZ32 = do 
    271     if list.size() = 1 then (* base case *) 
    272         if key < list[0] then -1 
    273         elif key = list[0] then 0 
    274         else -2 end 
     343    if list.size() <= 8 then (* base case *) 
     344        index:ZZ32 := - list.size() - 1 
     345        label loop 
     346            for i <- seq(0 # list.size()) do 
     347                if list[i] = key then 
     348                    index := i 
     349                    exit loop 
     350                elif list[i] > key then 
     351                    index := -i - 1 
     352                    exit loop                     
     353                end 
     354           end 
     355        end loop 
     356        index 
    275357    else (* recursive step *) 
    276358        split = list.size() DIV 2 
  • trunk/Library/incomplete/SkipTreeTest.fss

    r2132 r2137  
    1515    trademarks of Sun Microsystems, Inc. in the U.S. and other countries. 
    1616 ******************************************************************************) 
     17 
    1718 
    1819component SkipTreeTest 
     
    3839 
    3940    println leaf.insert(5,2,4).toString() 
     41    foo = leaf.insert(5,2,3) 
     42    println foo.toString() 
     43    foo.verify(); 
     44     
    4045    (** tree = SkipTree[\ZZ32,ZZ32\](Just[\Node[\ZZ32,ZZ32\]\](leaf)) *) 
    4146