Changeset 2139
- Timestamp:
- 06/30/08 10:43:04 (3 months ago)
- Files:
-
- trunk/Library/incomplete/SkipTree.fss (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Library/incomplete/SkipTree.fss
r2138 r2139 24 24 trait SkipTree[\Key,Value\] 25 25 26 (** Takes a querykey as input and returns either a Just[\Value\]27 * object if the (key,value)pair lives in this map, or28 * returns Nothing[\Value\]otherwise.26 (** Takes a querykey as input and returns either a %Just[\Value\]% 27 * object if the %(key, val)% pair lives in this map, or 28 * returns %Nothing[\Value\]% otherwise. 29 29 *) 30 30 find(querykey : Key) : Maybe[\Value\] 31 31 32 32 (** Takes a newkey and newvalue as input and returns a 33 * new skip tree with the (key, value)pair inserted.33 * new skip tree with the %(key, val)% pair inserted. 34 34 * Currently, if newkey already exists in the tree, 35 * then this operation does not insert (key, newvalue)35 * then this operation does not insert %(key, val)% 36 36 * into the tree. 37 37 *) … … 39 39 40 40 (** 41 * Return a random level >=1 with a negative binomial distribution.41 * Return a random level %>=% 1 with a negative binomial distribution. 42 42 *) 43 43 randomLevel():ZZ32 = do … … 56 56 verify():() 57 57 58 end (* trait SkipTree[\Key,Value\] *) 58 end 59 60 61 62 object EmptySkipTree[\Key,Value\] extends { SkipTree[\Key,Value\] } 63 64 find(querykey : Key) : Maybe[\Value\] = Nothing[\Value\] 65 66 insert(newkey : Key, newvalue : Value) : SkipTree[\Key,Value\] = do 67 newheight = randomLevel() 68 root = LeafNode[\Key,Value\](newkey, newvalue, newheight) 69 NonEmptySkipTree[\Key,Value\](root') 70 end 71 72 verify():() = () 73 74 end 75 59 76 60 77 … … 77 94 end 78 95 79 object EmptySkipTree[\Key,Value\] extends { SkipTree[\Key,Value\] } 80 81 find(querykey : Key) : Maybe[\Value\] = Nothing[\Value\] 82 83 insert(newkey : Key, newvalue : Value) : SkipTree[\Key,Value\] = do 84 newheight = randomLevel() 85 root = LeafNode[\Key,Value\](newkey, newvalue, newheight) 86 NonEmptySkipTree[\Key,Value\](root') 87 end 88 89 verify():() = () 90 91 end 92 93 94 object Filter[\Key\](pivot : Key, lessThanOrEquals : Boolean) 95 96 filter(query : Key) : Boolean = do 97 if lessThanOrEquals then 98 query <= pivot 99 else 100 query > pivot 101 end 102 end 103 104 end 96 97 105 98 106 99 object Node[\Key,Value\](keys : List[\Key\], … … 109 102 height : ZZ32) 110 103 104 111 105 getter toString() : String = keys "" values "" children " h" height 112 106 113 (** Takes a querykey as input and returns either a Just[\Value\] 114 * object if the (key,value) pair lives in this map, or 115 * returns Nothing[\Value\] otherwise. 107 108 (** Takes a querykey as input and returns either a %Just[\Value\]% 109 * object if the %(key,val)% pair lives in this map, or 110 * returns %Nothing[\Value\]% otherwise. 116 111 *) 117 112 find(querykey : Key) : Maybe[\Value\] = do … … 130 125 131 126 (** Takes a newkey and newvalue as input and returns a 132 * new node with the (key, value)pair inserted.127 * new node with the %(key, val)% pair inserted. 133 128 * Currently, if newkey already exists in the tree, 134 * then this operation does not insert (key, newvalue)129 * then this operation does not insert %(key, val)% 135 130 * into the tree. 136 131 *) … … 145 140 end 146 141 147 (* perform the insert operation when self.height > newheight *) 142 143 (** perform the insert operation when %self.height > newheight% *) 148 144 private insertBelowHeight(newkey:Key, newvalue:Value, newheight:ZZ32) : Node[\Key,Value\] = do 149 145 i = insertionPoint(binarySearch[\Key\](keys,newkey)) 150 newchild = typecase child = children[i] of 151 Just[\Node[\Key,Value\]\] => child.get().insert(newkey,newvalue,newheight) 152 else => LeafNode[\Key,Value\](newkey,newvalue,newheight) 146 newchild:Maybe[\Node[\Key,Value\]\] 147 typecase child = children[i] of 148 Just[\Node[\Key,Value\]\] => 149 newchild = child.get().insert(newkey,newvalue,newheight) 150 else => 151 newchild = LeafNode[\Key,Value\](newkey,newvalue,newheight) 153 152 end 154 153 newchildren = children[0 # i] || <| newchild |> || children[(i + 1):] … … 156 155 end 157 156 158 (* perform the insert operation when self.height = newheight *) 157 158 (** perform the insert operation when %self.height = newheight% *) 159 159 private insertEqualHeight(newkey:Key, newvalue:Value) : Node[\Key,Value\] = do 160 160 i = insertionPoint(binarySearch[\Key\](keys,newkey)) … … 162 162 newvalues = values[0#i] || <| newvalue |> || values[i:] 163 163 (newleft, newright) = 164 (filter Child(newkey, children[i], true),165 filter Child(newkey, children[i], false))164 (filter(newkey, children[i], true), 165 filter(newkey, children[i], false)) 166 166 leftchildren = children[0#i] || <|[\Maybe[\Node[\Key,Value\]\]\] newleft |> 167 167 rightchildren = <|[\Maybe[\Node[\Key,Value\]\]\] newright |> || children[(i + 1):] … … 170 170 end 171 171 172 (* perform the insert operation when self.height < newheight *) 172 173 (** perform the insert operation when %self.height < newheight% *) 173 174 private insertAboveHeight(newkey:Key,newvalue:Value,newheight:ZZ32) : Node[\Key,Value\] = do 174 175 newkeys = singleton[\Key\](newkey) 175 176 newvalues = singleton[\Value\](newvalue) 176 (newleft,newright) = (filter Child(newkey, Just[\Node[\Key,Value\]\](self), true),177 filter Child(newkey, Just[\Node[\Key,Value\]\](self), false))177 (newleft,newright) = (filter(newkey, Just[\Node[\Key,Value\]\](self), true), 178 filter(newkey, Just[\Node[\Key,Value\]\](self), false)) 178 179 newchildren = <| newleft, newright |> 179 180 Node[\Key,Value\](newkeys,newvalues,newchildren,newheight) 180 181 end 181 182 182 private filterChild(newkey : Key, 183 target : Maybe[\Node[\Key,Value\]\], 184 lessThanOrEquals : Boolean) 185 : Maybe[\Node[\Key,Value\]\] = do 183 184 private filter(newkey : Key, 185 target : Maybe[\Node[\Key,Value\]\], 186 lessThanOrEquals : Boolean) 187 : Maybe[\Node[\Key,Value\]\] = do 188 filter = Filter[\Key\](newkey, lessThanOrEquals) 186 189 typecase target of 187 190 Just[\Node[\Key,Value\]\] => 188 filter = Filter[\Key\](newkey, lessThanOrEquals)189 191 target.get().applyFilter(filter) 190 192 else => … … 193 195 end 194 196 197 195 198 private applyFilter(filter : Filter[\Key\]) : Maybe[\Node[\Key,Value\]\] = do 196 199 parentIndex = binarySearch[\Key\](keys, filter.pivot) … … 200 203 insertionPoint(parentIndex) 201 204 end 202 filteredChild = typecase child = children[childIndex] of 203 Just[\Node[\Key,Value\]\] => child.get().applyFilter(filter) 204 else => child 205 filteredChild : Maybe[\Node[\Key,Value\]\] 206 typecase child = children[childIndex] of 207 Just[\Node[\Key,Value\]\] => 208 filteredChild = child.get().applyFilter(filter) 209 else => 210 filteredChild = child 205 211 end 206 212 if filter.lessThanOrEquals then … … 211 217 end 212 218 219 213 220 private filterLessThanOrEquals(insertIndex : ZZ32, 214 filteredChild : Maybe[\Node[\Key,Value\]\])215 : Maybe[\Node[\Key,Value\]\] = do221 filteredChild : Maybe[\Node[\Key,Value\]\]) 222 : Maybe[\Node[\Key,Value\]\] = do 216 223 if insertIndex = 0 then 217 224 filteredChild … … 223 230 end 224 231 end 232 225 233 226 234 private filterGreaterThan(insertIndex : ZZ32, 227 filteredChild : Maybe[\Node[\Key,Value\]\])228 : Maybe[\Node[\Key,Value\]\] = do235 filteredChild : Maybe[\Node[\Key,Value\]\]) 236 : Maybe[\Node[\Key,Value\]\] = do 229 237 if insertIndex = keys.size() then 230 238 filteredChild … … 236 244 end 237 245 end 238 239 (* transform the index from binarySearch(list, key) 246 247 248 (** transform the index from %binarySearch[\T\](list, key)% 240 249 * into an insertion point. 241 * )250 **) 242 251 private insertionPoint(index : ZZ32) : ZZ32 = do 243 252 if index >= 0 then index 244 253 else |index| - 1 end 245 254 end 255 246 256 247 257 smallestKey():Key = do … … 251 261 end 252 262 end 263 253 264 254 265 largestKey():Key = do … … 306 317 end 307 318 308 end (** object Node[\Key,Value\] **)319 end 309 320 310 321 LeafNode[\Key,Value\](key : Key, val : Value, height : ZZ32) = do … … 314 325 children = child || child 315 326 Node[\Key,Value\](keys,values,children,height) 316 end (** LeafNode[\Key,Value\](key : Key, val : Value, height : ZZ32) **)327 end 317 328 318 329 … … 337 348 * the index of the first element greater than the key, or list.size() 338 349 * if all elements in the list are less than the specified key. 339 * Note that this guarantees that the return value will be >=0350 * Note that this guarantees that the return value will be %>=% 0 340 351 * if and only if the key is found. 341 352 **) … … 368 379 369 380 370 end (* component SkipTree *) 381 object Filter[\Key\](pivot : Key, lessThanOrEquals : Boolean) 382 383 filter(query : Key) : Boolean = do 384 if lessThanOrEquals then 385 query <= pivot 386 else 387 query > pivot 388 end 389 end 390 391 end 392 393 394 end
