| ... | ... | @@ -40,7 +40,7 @@ pub const Node = struct { |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | while (true) { |
| 43 | | var parent = node.get_parent(); |
| 43 | var parent = node.getParent(); |
| 44 | 44 | if (parent) |p| { |
| 45 | 45 | if (node != p.right) |
| 46 | 46 | return p; |
| ... | ... | @@ -61,7 +61,7 @@ pub const Node = struct { |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | while (true) { |
| 64 | | var parent = node.get_parent(); |
| 64 | var parent = node.getParent(); |
| 65 | 65 | if (parent) |p| { |
| 66 | 66 | if (node != p.left) |
| 67 | 67 | return p; |
| ... | ... | @@ -71,23 +71,23 @@ pub const Node = struct { |
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | | pub fn is_root(node: *Node) bool { |
| 75 | | return node.get_parent() == null; |
| 74 | pub fn isRoot(node: *Node) bool { |
| 75 | return node.getParent() == null; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | | fn is_red(node: *Node) bool { |
| 79 | | return node.get_color() == Red; |
| 78 | fn isRed(node: *Node) bool { |
| 79 | return node.getColor() == Red; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | | fn is_black(node: *Node) bool { |
| 83 | | return node.get_color() == Black; |
| 82 | fn isBlack(node: *Node) bool { |
| 83 | return node.getColor() == Black; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | | fn set_parent(node: *Node, parent: ?*Node) void { |
| 86 | fn setParent(node: *Node, parent: ?*Node) void { |
| 87 | 87 | node.parent_and_color = @ptrToInt(parent) | (node.parent_and_color & 1); |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | | fn get_parent(node: *Node) ?*Node { |
| 90 | fn getParent(node: *Node) ?*Node { |
| 91 | 91 | const mask: usize = 1; |
| 92 | 92 | comptime { |
| 93 | 93 | assert(@alignOf(*Node) >= 2); |
| ... | ... | @@ -95,12 +95,12 @@ pub const Node = struct { |
| 95 | 95 | return @intToPtr(*Node, node.parent_and_color & ~mask); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | | fn set_color(node: *Node, color: Color) void { |
| 98 | fn setColor(node: *Node, color: Color) void { |
| 99 | 99 | const mask: usize = 1; |
| 100 | 100 | node.parent_and_color = (node.parent_and_color & ~mask) | @enumToInt(color); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | | fn get_color(node: *Node) Color { |
| 103 | fn getColor(node: *Node) Color { |
| 104 | 104 | return @intToEnum(Color, @intCast(u1, node.parent_and_color & 1)); |
| 105 | 105 | } |
| 106 | 106 | |
| ... | ... | @@ -112,7 +112,7 @@ pub const Node = struct { |
| 112 | 112 | } |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | | fn get_first(nodeconst: *Node) *Node { |
| 115 | fn getFirst(nodeconst: *Node) *Node { |
| 116 | 116 | var node = nodeconst; |
| 117 | 117 | while (node.left) |left| { |
| 118 | 118 | node = left; |
| ... | ... | @@ -120,7 +120,7 @@ pub const Node = struct { |
| 120 | 120 | return node; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | | fn get_last(node: *Node) *Node { |
| 123 | fn getLast(node: *Node) *Node { |
| 124 | 124 | while (node.right) |right| { |
| 125 | 125 | node = right; |
| 126 | 126 | } |
| ... | ... | @@ -161,15 +161,15 @@ pub const Tree = struct { |
| 161 | 161 | var maybe_parent: ?*Node = undefined; |
| 162 | 162 | var is_left: bool = undefined; |
| 163 | 163 | |
| 164 | | maybe_key = do_lookup(node, tree, &maybe_parent, &is_left); |
| 164 | maybe_key = doLookup(node, tree, &maybe_parent, &is_left); |
| 165 | 165 | if (maybe_key) |key| { |
| 166 | 166 | return key; |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | node.left = null; |
| 170 | 170 | node.right = null; |
| 171 | | node.set_color(Red); |
| 172 | | node.set_parent(maybe_parent); |
| 171 | node.setColor(Red); |
| 172 | node.setParent(maybe_parent); |
| 173 | 173 | |
| 174 | 174 | if (maybe_parent) |parent| { |
| 175 | 175 | parent.set_child(node, is_left); |
| ... | ... | @@ -177,58 +177,58 @@ pub const Tree = struct { |
| 177 | 177 | tree.root = node; |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | | while (node.get_parent()) |*parent| { |
| 181 | | if (parent.*.is_black()) |
| 180 | while (node.getParent()) |*parent| { |
| 181 | if (parent.*.isBlack()) |
| 182 | 182 | break; |
| 183 | 183 | // the root is always black |
| 184 | | var grandpa = parent.*.get_parent() orelse unreachable; |
| 184 | var grandpa = parent.*.getParent() orelse unreachable; |
| 185 | 185 | |
| 186 | 186 | if (parent.* == grandpa.left) { |
| 187 | 187 | var maybe_uncle = grandpa.right; |
| 188 | 188 | |
| 189 | 189 | if (maybe_uncle) |uncle| { |
| 190 | | if (uncle.is_black()) |
| 190 | if (uncle.isBlack()) |
| 191 | 191 | break; |
| 192 | 192 | |
| 193 | | parent.*.set_color(Black); |
| 194 | | uncle.set_color(Black); |
| 195 | | grandpa.set_color(Red); |
| 193 | parent.*.setColor(Black); |
| 194 | uncle.setColor(Black); |
| 195 | grandpa.setColor(Red); |
| 196 | 196 | node = grandpa; |
| 197 | 197 | } else { |
| 198 | 198 | if (node == parent.*.right) { |
| 199 | | rotate_left(parent.*, tree); |
| 199 | rotateLeft(parent.*, tree); |
| 200 | 200 | node = parent.*; |
| 201 | | parent.* = node.get_parent().?; // Just rotated |
| 201 | parent.* = node.getParent().?; // Just rotated |
| 202 | 202 | } |
| 203 | | parent.*.set_color(Black); |
| 204 | | grandpa.set_color(Red); |
| 205 | | rotate_right(grandpa, tree); |
| 203 | parent.*.setColor(Black); |
| 204 | grandpa.setColor(Red); |
| 205 | rotateRight(grandpa, tree); |
| 206 | 206 | } |
| 207 | 207 | } else { |
| 208 | 208 | var maybe_uncle = grandpa.left; |
| 209 | 209 | |
| 210 | 210 | if (maybe_uncle) |uncle| { |
| 211 | | if (uncle.is_black()) |
| 211 | if (uncle.isBlack()) |
| 212 | 212 | break; |
| 213 | 213 | |
| 214 | | parent.*.set_color(Black); |
| 215 | | uncle.set_color(Black); |
| 216 | | grandpa.set_color(Red); |
| 214 | parent.*.setColor(Black); |
| 215 | uncle.setColor(Black); |
| 216 | grandpa.setColor(Red); |
| 217 | 217 | node = grandpa; |
| 218 | 218 | } else { |
| 219 | 219 | if (node == parent.*.left) { |
| 220 | | rotate_right(parent.*, tree); |
| 220 | rotateRight(parent.*, tree); |
| 221 | 221 | node = parent.*; |
| 222 | | parent.* = node.get_parent().?; // Just rotated |
| 222 | parent.* = node.getParent().?; // Just rotated |
| 223 | 223 | } |
| 224 | | parent.*.set_color(Black); |
| 225 | | grandpa.set_color(Red); |
| 226 | | rotate_left(grandpa, tree); |
| 224 | parent.*.setColor(Black); |
| 225 | grandpa.setColor(Red); |
| 226 | rotateLeft(grandpa, tree); |
| 227 | 227 | } |
| 228 | 228 | } |
| 229 | 229 | } |
| 230 | 230 | // This was an insert, there is at least one node. |
| 231 | | tree.root.?.set_color(Black); |
| 231 | tree.root.?.setColor(Black); |
| 232 | 232 | return null; |
| 233 | 233 | } |
| 234 | 234 | |
| ... | ... | @@ -236,14 +236,14 @@ pub const Tree = struct { |
| 236 | 236 | var parent: *Node = undefined; |
| 237 | 237 | var is_left: bool = undefined; |
| 238 | 238 | |
| 239 | | return do_lookup(key, tree, &parent, &is_left); |
| 239 | return doLookup(key, tree, &parent, &is_left); |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | pub fn remove(tree: *Tree, nodeconst: *Node) void { |
| 243 | 243 | var node = nodeconst; |
| 244 | 244 | // as this has the same value as node, it is unsafe to access node after newnode |
| 245 | 245 | var newnode: ?*Node = nodeconst; |
| 246 | | var maybe_parent: ?*Node = node.get_parent(); |
| 246 | var maybe_parent: ?*Node = node.getParent(); |
| 247 | 247 | var color: Color = undefined; |
| 248 | 248 | var next: *Node = undefined; |
| 249 | 249 | |
| ... | ... | @@ -253,7 +253,7 @@ pub const Tree = struct { |
| 253 | 253 | parent.set_child(null, parent.left == node); |
| 254 | 254 | } else |
| 255 | 255 | tree.root = null; |
| 256 | | color = node.get_color(); |
| 256 | color = node.getColor(); |
| 257 | 257 | newnode = null; |
| 258 | 258 | } else { |
| 259 | 259 | if (node.left == null) { |
| ... | ... | @@ -261,7 +261,7 @@ pub const Tree = struct { |
| 261 | 261 | } else if (node.right == null) { |
| 262 | 262 | next = node.left.?; // Not both null as per above |
| 263 | 263 | } else |
| 264 | | next = node.right.?.get_first(); // Just checked for null above |
| 264 | next = node.right.?.getFirst(); // Just checked for null above |
| 265 | 265 | |
| 266 | 266 | if (maybe_parent) |parent| { |
| 267 | 267 | parent.set_child(next, parent.left == node); |
| ... | ... | @@ -272,39 +272,39 @@ pub const Tree = struct { |
| 272 | 272 | const left = node.left.?; |
| 273 | 273 | const right = node.right.?; |
| 274 | 274 | |
| 275 | | color = next.get_color(); |
| 276 | | next.set_color(node.get_color()); |
| 275 | color = next.getColor(); |
| 276 | next.setColor(node.getColor()); |
| 277 | 277 | |
| 278 | 278 | next.left = left; |
| 279 | | left.set_parent(next); |
| 279 | left.setParent(next); |
| 280 | 280 | |
| 281 | 281 | if (next != right) { |
| 282 | | var parent = next.get_parent().?; // Was traversed via child node (right/left) |
| 283 | | next.set_parent(node.get_parent()); |
| 282 | var parent = next.getParent().?; // Was traversed via child node (right/left) |
| 283 | next.setParent(node.getParent()); |
| 284 | 284 | |
| 285 | 285 | newnode = next.right; |
| 286 | 286 | parent.left = node; |
| 287 | 287 | |
| 288 | 288 | next.right = right; |
| 289 | | right.set_parent(next); |
| 289 | right.setParent(next); |
| 290 | 290 | } else { |
| 291 | | next.set_parent(maybe_parent); |
| 291 | next.setParent(maybe_parent); |
| 292 | 292 | maybe_parent = next; |
| 293 | 293 | newnode = next.right; |
| 294 | 294 | } |
| 295 | 295 | } else { |
| 296 | | color = node.get_color(); |
| 296 | color = node.getColor(); |
| 297 | 297 | newnode = next; |
| 298 | 298 | } |
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | if (newnode) |n| |
| 302 | | n.set_parent(maybe_parent); |
| 302 | n.setParent(maybe_parent); |
| 303 | 303 | |
| 304 | 304 | if (color == Red) |
| 305 | 305 | return; |
| 306 | 306 | if (newnode) |n| { |
| 307 | | n.set_color(Black); |
| 307 | n.setColor(Black); |
| 308 | 308 | return; |
| 309 | 309 | } |
| 310 | 310 | |
| ... | ... | @@ -314,69 +314,69 @@ pub const Tree = struct { |
| 314 | 314 | if (node == parent.left) { |
| 315 | 315 | var sibling = parent.right.?; // Same number of black nodes. |
| 316 | 316 | |
| 317 | | if (sibling.is_red()) { |
| 318 | | sibling.set_color(Black); |
| 319 | | parent.set_color(Red); |
| 320 | | rotate_left(parent, tree); |
| 317 | if (sibling.isRed()) { |
| 318 | sibling.setColor(Black); |
| 319 | parent.setColor(Red); |
| 320 | rotateLeft(parent, tree); |
| 321 | 321 | sibling = parent.right.?; // Just rotated |
| 322 | 322 | } |
| 323 | | if ((if (sibling.left) |n| n.is_black() else true) and |
| 324 | | (if (sibling.right) |n| n.is_black() else true)) |
| 323 | if ((if (sibling.left) |n| n.isBlack() else true) and |
| 324 | (if (sibling.right) |n| n.isBlack() else true)) |
| 325 | 325 | { |
| 326 | | sibling.set_color(Red); |
| 326 | sibling.setColor(Red); |
| 327 | 327 | node = parent; |
| 328 | | maybe_parent = parent.get_parent(); |
| 328 | maybe_parent = parent.getParent(); |
| 329 | 329 | continue; |
| 330 | 330 | } |
| 331 | | if (if (sibling.right) |n| n.is_black() else true) { |
| 332 | | sibling.left.?.set_color(Black); // Same number of black nodes. |
| 333 | | sibling.set_color(Red); |
| 334 | | rotate_right(sibling, tree); |
| 331 | if (if (sibling.right) |n| n.isBlack() else true) { |
| 332 | sibling.left.?.setColor(Black); // Same number of black nodes. |
| 333 | sibling.setColor(Red); |
| 334 | rotateRight(sibling, tree); |
| 335 | 335 | sibling = parent.right.?; // Just rotated |
| 336 | 336 | } |
| 337 | | sibling.set_color(parent.get_color()); |
| 338 | | parent.set_color(Black); |
| 339 | | sibling.right.?.set_color(Black); // Same number of black nodes. |
| 340 | | rotate_left(parent, tree); |
| 337 | sibling.setColor(parent.getColor()); |
| 338 | parent.setColor(Black); |
| 339 | sibling.right.?.setColor(Black); // Same number of black nodes. |
| 340 | rotateLeft(parent, tree); |
| 341 | 341 | newnode = tree.root; |
| 342 | 342 | break; |
| 343 | 343 | } else { |
| 344 | 344 | var sibling = parent.left.?; // Same number of black nodes. |
| 345 | 345 | |
| 346 | | if (sibling.is_red()) { |
| 347 | | sibling.set_color(Black); |
| 348 | | parent.set_color(Red); |
| 349 | | rotate_right(parent, tree); |
| 346 | if (sibling.isRed()) { |
| 347 | sibling.setColor(Black); |
| 348 | parent.setColor(Red); |
| 349 | rotateRight(parent, tree); |
| 350 | 350 | sibling = parent.left.?; // Just rotated |
| 351 | 351 | } |
| 352 | | if ((if (sibling.left) |n| n.is_black() else true) and |
| 353 | | (if (sibling.right) |n| n.is_black() else true)) |
| 352 | if ((if (sibling.left) |n| n.isBlack() else true) and |
| 353 | (if (sibling.right) |n| n.isBlack() else true)) |
| 354 | 354 | { |
| 355 | | sibling.set_color(Red); |
| 355 | sibling.setColor(Red); |
| 356 | 356 | node = parent; |
| 357 | | maybe_parent = parent.get_parent(); |
| 357 | maybe_parent = parent.getParent(); |
| 358 | 358 | continue; |
| 359 | 359 | } |
| 360 | | if (if (sibling.left) |n| n.is_black() else true) { |
| 361 | | sibling.right.?.set_color(Black); // Same number of black nodes |
| 362 | | sibling.set_color(Red); |
| 363 | | rotate_left(sibling, tree); |
| 360 | if (if (sibling.left) |n| n.isBlack() else true) { |
| 361 | sibling.right.?.setColor(Black); // Same number of black nodes |
| 362 | sibling.setColor(Red); |
| 363 | rotateLeft(sibling, tree); |
| 364 | 364 | sibling = parent.left.?; // Just rotated |
| 365 | 365 | } |
| 366 | | sibling.set_color(parent.get_color()); |
| 367 | | parent.set_color(Black); |
| 368 | | sibling.left.?.set_color(Black); // Same number of black nodes |
| 369 | | rotate_right(parent, tree); |
| 366 | sibling.setColor(parent.getColor()); |
| 367 | parent.setColor(Black); |
| 368 | sibling.left.?.setColor(Black); // Same number of black nodes |
| 369 | rotateRight(parent, tree); |
| 370 | 370 | newnode = tree.root; |
| 371 | 371 | break; |
| 372 | 372 | } |
| 373 | 373 | |
| 374 | | if (node.is_red()) |
| 374 | if (node.isRed()) |
| 375 | 375 | break; |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | if (newnode) |n| |
| 379 | | n.set_color(Black); |
| 379 | n.setColor(Black); |
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | /// This is a shortcut to avoid removing and re-inserting an item with the same key. |
| ... | ... | @@ -386,15 +386,15 @@ pub const Tree = struct { |
| 386 | 386 | // I assume this can get optimized out if the caller already knows. |
| 387 | 387 | if (tree.compareFn(old, new) != mem.Compare.Equal) return ReplaceError.NotEqual; |
| 388 | 388 | |
| 389 | | if (old.get_parent()) |parent| { |
| 389 | if (old.getParent()) |parent| { |
| 390 | 390 | parent.set_child(new, parent.left == old); |
| 391 | 391 | } else |
| 392 | 392 | tree.root = new; |
| 393 | 393 | |
| 394 | 394 | if (old.left) |left| |
| 395 | | left.set_parent(new); |
| 395 | left.setParent(new); |
| 396 | 396 | if (old.right) |right| |
| 397 | | right.set_parent(new); |
| 397 | right.setParent(new); |
| 398 | 398 | |
| 399 | 399 | new.* = old.*; |
| 400 | 400 | } |
| ... | ... | @@ -405,59 +405,59 @@ pub const Tree = struct { |
| 405 | 405 | } |
| 406 | 406 | }; |
| 407 | 407 | |
| 408 | | fn rotate_left(node: *Node, tree: *Tree) void { |
| 408 | fn rotateLeft(node: *Node, tree: *Tree) void { |
| 409 | 409 | var p: *Node = node; |
| 410 | 410 | var q: *Node = node.right orelse unreachable; |
| 411 | 411 | var parent: *Node = undefined; |
| 412 | 412 | |
| 413 | | if (!p.is_root()) { |
| 414 | | parent = p.get_parent().?; |
| 413 | if (!p.isRoot()) { |
| 414 | parent = p.getParent().?; |
| 415 | 415 | if (parent.left == p) { |
| 416 | 416 | parent.left = q; |
| 417 | 417 | } else { |
| 418 | 418 | parent.right = q; |
| 419 | 419 | } |
| 420 | | q.set_parent(parent); |
| 420 | q.setParent(parent); |
| 421 | 421 | } else { |
| 422 | 422 | tree.root = q; |
| 423 | | q.set_parent(null); |
| 423 | q.setParent(null); |
| 424 | 424 | } |
| 425 | | p.set_parent(q); |
| 425 | p.setParent(q); |
| 426 | 426 | |
| 427 | 427 | p.right = q.left; |
| 428 | 428 | if (p.right) |right| { |
| 429 | | right.set_parent(p); |
| 429 | right.setParent(p); |
| 430 | 430 | } |
| 431 | 431 | q.left = p; |
| 432 | 432 | } |
| 433 | 433 | |
| 434 | | fn rotate_right(node: *Node, tree: *Tree) void { |
| 434 | fn rotateRight(node: *Node, tree: *Tree) void { |
| 435 | 435 | var p: *Node = node; |
| 436 | 436 | var q: *Node = node.left orelse unreachable; |
| 437 | 437 | var parent: *Node = undefined; |
| 438 | 438 | |
| 439 | | if (!p.is_root()) { |
| 440 | | parent = p.get_parent().?; |
| 439 | if (!p.isRoot()) { |
| 440 | parent = p.getParent().?; |
| 441 | 441 | if (parent.left == p) { |
| 442 | 442 | parent.left = q; |
| 443 | 443 | } else { |
| 444 | 444 | parent.right = q; |
| 445 | 445 | } |
| 446 | | q.set_parent(parent); |
| 446 | q.setParent(parent); |
| 447 | 447 | } else { |
| 448 | 448 | tree.root = q; |
| 449 | | q.set_parent(null); |
| 449 | q.setParent(null); |
| 450 | 450 | } |
| 451 | | p.set_parent(q); |
| 451 | p.setParent(q); |
| 452 | 452 | |
| 453 | 453 | p.left = q.right; |
| 454 | 454 | if (p.left) |left| { |
| 455 | | left.set_parent(p); |
| 455 | left.setParent(p); |
| 456 | 456 | } |
| 457 | 457 | q.right = p; |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | | fn do_lookup(key: *Node, tree: *Tree, pparent: *?*Node, is_left: *bool) ?*Node { |
| 460 | fn doLookup(key: *Node, tree: *Tree, pparent: *?*Node, is_left: *bool) ?*Node { |
| 461 | 461 | var maybe_node: ?*Node = tree.root; |
| 462 | 462 | |
| 463 | 463 | pparent.* = null; |