Changeset 2137
- Timestamp:
- 06/30/08 07:23:42 (3 months ago)
- Files:
-
- trunk/Library/incomplete/SkipTree.fsi (modified) (1 diff)
- trunk/Library/incomplete/SkipTree.fss (modified) (5 diffs)
- trunk/Library/incomplete/SkipTreeTest.fss (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Library/incomplete/SkipTree.fsi
r2117 r2137 40 40 randomLevel():ZZ32 41 41 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 42 49 end (* trait SkipTree[\Key,Value\] *) 43 50 trunk/Library/incomplete/SkipTree.fss
r2117 r2137 48 48 level 49 49 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():() 50 57 51 58 end (* trait SkipTree[\Key,Value\] *) … … 66 73 end 67 74 75 verify():() = root.verify() 76 68 77 end 69 78 … … 77 86 NonEmptySkipTree[\Key,Value\](root') 78 87 end 88 89 verify():() = () 79 90 80 91 end … … 232 243 if index >= 0 then index 233 244 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 234 306 end 235 307 … … 269 341 **) 270 342 binarySearch[\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 275 357 else (* recursive step *) 276 358 split = list.size() DIV 2 trunk/Library/incomplete/SkipTreeTest.fss
r2132 r2137 15 15 trademarks of Sun Microsystems, Inc. in the U.S. and other countries. 16 16 ******************************************************************************) 17 17 18 18 19 component SkipTreeTest … … 38 39 39 40 println leaf.insert(5,2,4).toString() 41 foo = leaf.insert(5,2,3) 42 println foo.toString() 43 foo.verify(); 44 40 45 (** tree = SkipTree[\ZZ32,ZZ32\](Just[\Node[\ZZ32,ZZ32\]\](leaf)) *) 41 46
