| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use crate::{alloc::Flags, bindings, container_of, error::Result, prelude::*};
|
| 10 | use core::{
|
| 11 | cmp::{Ord, Ordering},
|
| 12 | marker::PhantomData,
|
| 13 | mem::MaybeUninit,
|
| 14 | ptr::{addr_of_mut, from_mut, NonNull},
|
| 15 | };
|
| 16 |
|
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | pub struct RBTree<K, V> {
|
| 172 | root: bindings::rb_root,
|
| 173 | _p: PhantomData<Node<K, V>>,
|
| 174 | }
|
| 175 |
|
| 176 | |
| 177 | |
| 178 | unsafe impl<K: Send, V: Send> Send for RBTree<K, V> {}
|
| 179 |
|
| 180 | |
| 181 | |
| 182 | unsafe impl<K: Sync, V: Sync> Sync for RBTree<K, V> {}
|
| 183 |
|
| 184 | impl<K, V> RBTree<K, V> {
|
| 185 | |
| 186 | pub fn new() -> Self {
|
| 187 | Self {
|
| 188 | |
| 189 | root: bindings::rb_root::default(),
|
| 190 | _p: PhantomData,
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | |
| 195 | #[inline]
|
| 196 | pub fn is_empty(&self) -> bool {
|
| 197 | self.root.rb_node.is_null()
|
| 198 | }
|
| 199 |
|
| 200 | |
| 201 | pub fn iter(&self) -> Iter<'_, K, V> {
|
| 202 | Iter {
|
| 203 | _tree: PhantomData,
|
| 204 | |
| 205 | |
| 206 | |
| 207 | iter_raw: IterRaw {
|
| 208 | |
| 209 | next: unsafe { bindings::rb_first(&self.root) },
|
| 210 | _phantom: PhantomData,
|
| 211 | },
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | |
| 216 | pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
|
| 217 | IterMut {
|
| 218 | _tree: PhantomData,
|
| 219 | |
| 220 | |
| 221 | |
| 222 | iter_raw: IterRaw {
|
| 223 | |
| 224 | next: unsafe { bindings::rb_first(from_mut(&mut self.root)) },
|
| 225 | _phantom: PhantomData,
|
| 226 | },
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | |
| 231 | pub fn keys(&self) -> impl Iterator<Item = &'_ K> {
|
| 232 | self.iter().map(|(k, _)| k)
|
| 233 | }
|
| 234 |
|
| 235 | |
| 236 | pub fn values(&self) -> impl Iterator<Item = &'_ V> {
|
| 237 | self.iter().map(|(_, v)| v)
|
| 238 | }
|
| 239 |
|
| 240 | |
| 241 | pub fn values_mut(&mut self) -> impl Iterator<Item = &'_ mut V> {
|
| 242 | self.iter_mut().map(|(_, v)| v)
|
| 243 | }
|
| 244 |
|
| 245 | |
| 246 | pub fn cursor_front_mut(&mut self) -> Option<CursorMut<'_, K, V>> {
|
| 247 | let root = addr_of_mut!(self.root);
|
| 248 | |
| 249 | let current = unsafe { bindings::rb_first(root) };
|
| 250 | NonNull::new(current).map(|current| {
|
| 251 | |
| 252 | |
| 253 | CursorMut {
|
| 254 | current,
|
| 255 | tree: self,
|
| 256 | }
|
| 257 | })
|
| 258 | }
|
| 259 |
|
| 260 | |
| 261 | pub fn cursor_front(&self) -> Option<Cursor<'_, K, V>> {
|
| 262 | let root = &raw const self.root;
|
| 263 | |
| 264 | let current = unsafe { bindings::rb_first(root) };
|
| 265 | NonNull::new(current).map(|current| {
|
| 266 | |
| 267 | |
| 268 | Cursor {
|
| 269 | current,
|
| 270 | _tree: PhantomData,
|
| 271 | }
|
| 272 | })
|
| 273 | }
|
| 274 |
|
| 275 | |
| 276 | pub fn cursor_back_mut(&mut self) -> Option<CursorMut<'_, K, V>> {
|
| 277 | let root = addr_of_mut!(self.root);
|
| 278 | |
| 279 | let current = unsafe { bindings::rb_last(root) };
|
| 280 | NonNull::new(current).map(|current| {
|
| 281 | |
| 282 | |
| 283 | CursorMut {
|
| 284 | current,
|
| 285 | tree: self,
|
| 286 | }
|
| 287 | })
|
| 288 | }
|
| 289 |
|
| 290 | |
| 291 | pub fn cursor_back(&self) -> Option<Cursor<'_, K, V>> {
|
| 292 | let root = &raw const self.root;
|
| 293 | |
| 294 | let current = unsafe { bindings::rb_last(root) };
|
| 295 | NonNull::new(current).map(|current| {
|
| 296 | |
| 297 | |
| 298 | Cursor {
|
| 299 | current,
|
| 300 | _tree: PhantomData,
|
| 301 | }
|
| 302 | })
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | impl<K, V> RBTree<K, V>
|
| 307 | where
|
| 308 | K: Ord,
|
| 309 | {
|
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | pub fn try_create_and_insert(
|
| 317 | &mut self,
|
| 318 | key: K,
|
| 319 | value: V,
|
| 320 | flags: Flags,
|
| 321 | ) -> Result<Option<RBTreeNode<K, V>>> {
|
| 322 | Ok(self.insert(RBTreeNode::new(key, value, flags)?))
|
| 323 | }
|
| 324 |
|
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | pub fn insert(&mut self, node: RBTreeNode<K, V>) -> Option<RBTreeNode<K, V>> {
|
| 332 | match self.raw_entry(&node.node.key) {
|
| 333 | RawEntry::Occupied(entry) => Some(entry.replace(node)),
|
| 334 | RawEntry::Vacant(entry) => {
|
| 335 | entry.insert(node);
|
| 336 | None
|
| 337 | }
|
| 338 | }
|
| 339 | }
|
| 340 |
|
| 341 | fn raw_entry(&mut self, key: &K) -> RawEntry<'_, K, V> {
|
| 342 | let raw_self: *mut RBTree<K, V> = self;
|
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | let mut parent = core::ptr::null_mut();
|
| 362 | let mut child_field_of_parent: &mut *mut bindings::rb_node =
|
| 363 | |
| 364 | unsafe { &mut (*raw_self).root.rb_node };
|
| 365 | while !(*child_field_of_parent).is_null() {
|
| 366 | let curr = *child_field_of_parent;
|
| 367 | |
| 368 | let node = unsafe { container_of!(curr, Node<K, V>, links) };
|
| 369 |
|
| 370 | |
| 371 | match key.cmp(unsafe { &(*node).key }) {
|
| 372 | |
| 373 | Ordering::Less => child_field_of_parent = unsafe { &mut (*curr).rb_left },
|
| 374 | |
| 375 | Ordering::Greater => child_field_of_parent = unsafe { &mut (*curr).rb_right },
|
| 376 | Ordering::Equal => {
|
| 377 | return RawEntry::Occupied(OccupiedEntry {
|
| 378 | rbtree: self,
|
| 379 | node_links: curr,
|
| 380 | })
|
| 381 | }
|
| 382 | }
|
| 383 | parent = curr;
|
| 384 | }
|
| 385 |
|
| 386 | RawEntry::Vacant(RawVacantEntry {
|
| 387 | rbtree: raw_self,
|
| 388 | parent,
|
| 389 | child_field_of_parent,
|
| 390 | _phantom: PhantomData,
|
| 391 | })
|
| 392 | }
|
| 393 |
|
| 394 | |
| 395 | pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
|
| 396 | match self.raw_entry(&key) {
|
| 397 | RawEntry::Occupied(entry) => Entry::Occupied(entry),
|
| 398 | RawEntry::Vacant(entry) => Entry::Vacant(VacantEntry { raw: entry, key }),
|
| 399 | }
|
| 400 | }
|
| 401 |
|
| 402 | |
| 403 | pub fn find_mut(&mut self, key: &K) -> Option<OccupiedEntry<'_, K, V>> {
|
| 404 | match self.raw_entry(key) {
|
| 405 | RawEntry::Occupied(entry) => Some(entry),
|
| 406 | RawEntry::Vacant(_entry) => None,
|
| 407 | }
|
| 408 | }
|
| 409 |
|
| 410 | |
| 411 | pub fn get(&self, key: &K) -> Option<&V> {
|
| 412 | let mut node = self.root.rb_node;
|
| 413 | while !node.is_null() {
|
| 414 | |
| 415 | |
| 416 | let this = unsafe { container_of!(node, Node<K, V>, links) };
|
| 417 |
|
| 418 | |
| 419 | let this_ref = unsafe { &*this };
|
| 420 |
|
| 421 | |
| 422 | let node_ref = unsafe { &*node };
|
| 423 |
|
| 424 | node = match key.cmp(&this_ref.key) {
|
| 425 | Ordering::Less => node_ref.rb_left,
|
| 426 | Ordering::Greater => node_ref.rb_right,
|
| 427 | Ordering::Equal => return Some(&this_ref.value),
|
| 428 | }
|
| 429 | }
|
| 430 | None
|
| 431 | }
|
| 432 |
|
| 433 | |
| 434 | pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
|
| 435 | self.find_mut(key).map(|node| node.into_mut())
|
| 436 | }
|
| 437 |
|
| 438 | |
| 439 | |
| 440 | |
| 441 | pub fn remove_node(&mut self, key: &K) -> Option<RBTreeNode<K, V>> {
|
| 442 | self.find_mut(key).map(OccupiedEntry::remove_node)
|
| 443 | }
|
| 444 |
|
| 445 | |
| 446 | |
| 447 | |
| 448 | pub fn remove(&mut self, key: &K) -> Option<V> {
|
| 449 | self.find_mut(key).map(OccupiedEntry::remove)
|
| 450 | }
|
| 451 |
|
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | pub fn cursor_lower_bound_mut(&mut self, key: &K) -> Option<CursorMut<'_, K, V>>
|
| 458 | where
|
| 459 | K: Ord,
|
| 460 | {
|
| 461 | let best = self.find_best_match(key)?;
|
| 462 |
|
| 463 | NonNull::new(best.as_ptr()).map(|current| {
|
| 464 | |
| 465 | |
| 466 | CursorMut {
|
| 467 | current,
|
| 468 | tree: self,
|
| 469 | }
|
| 470 | })
|
| 471 | }
|
| 472 |
|
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | pub fn cursor_lower_bound(&self, key: &K) -> Option<Cursor<'_, K, V>>
|
| 479 | where
|
| 480 | K: Ord,
|
| 481 | {
|
| 482 | let best = self.find_best_match(key)?;
|
| 483 |
|
| 484 | NonNull::new(best.as_ptr()).map(|current| {
|
| 485 | |
| 486 | |
| 487 | Cursor {
|
| 488 | current,
|
| 489 | _tree: PhantomData,
|
| 490 | }
|
| 491 | })
|
| 492 | }
|
| 493 |
|
| 494 | fn find_best_match(&self, key: &K) -> Option<NonNull<bindings::rb_node>> {
|
| 495 | let mut node = self.root.rb_node;
|
| 496 | let mut best_key: Option<&K> = None;
|
| 497 | let mut best_links: Option<NonNull<bindings::rb_node>> = None;
|
| 498 | while !node.is_null() {
|
| 499 | |
| 500 | |
| 501 | let this = unsafe { container_of!(node, Node<K, V>, links) };
|
| 502 | |
| 503 | let this_key = unsafe { &(*this).key };
|
| 504 |
|
| 505 | |
| 506 | let node_ref = unsafe { &*node };
|
| 507 |
|
| 508 | match key.cmp(this_key) {
|
| 509 | Ordering::Equal => {
|
| 510 | |
| 511 | best_links = Some(unsafe { NonNull::new_unchecked(&mut (*this).links) });
|
| 512 | break;
|
| 513 | }
|
| 514 | Ordering::Greater => {
|
| 515 | node = node_ref.rb_right;
|
| 516 | }
|
| 517 | Ordering::Less => {
|
| 518 | let is_better_match = match best_key {
|
| 519 | None => true,
|
| 520 | Some(best) => best > this_key,
|
| 521 | };
|
| 522 | if is_better_match {
|
| 523 | best_key = Some(this_key);
|
| 524 | |
| 525 | best_links = Some(unsafe { NonNull::new_unchecked(&mut (*this).links) });
|
| 526 | }
|
| 527 | node = node_ref.rb_left;
|
| 528 | }
|
| 529 | };
|
| 530 | }
|
| 531 | best_links
|
| 532 | }
|
| 533 | }
|
| 534 |
|
| 535 | impl<K, V> Default for RBTree<K, V> {
|
| 536 | fn default() -> Self {
|
| 537 | Self::new()
|
| 538 | }
|
| 539 | }
|
| 540 |
|
| 541 | impl<K, V> Drop for RBTree<K, V> {
|
| 542 | fn drop(&mut self) {
|
| 543 | |
| 544 | let mut next = unsafe { bindings::rb_first_postorder(&self.root) };
|
| 545 |
|
| 546 | |
| 547 | while !next.is_null() {
|
| 548 | |
| 549 | let this = unsafe { container_of!(next, Node<K, V>, links) };
|
| 550 |
|
| 551 | |
| 552 | |
| 553 | next = unsafe { bindings::rb_next_postorder(next) };
|
| 554 |
|
| 555 | |
| 556 | |
| 557 |
|
| 558 | |
| 559 | unsafe { drop(KBox::from_raw(this)) };
|
| 560 | }
|
| 561 | }
|
| 562 | }
|
| 563 |
|
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | pub struct CursorMut<'a, K, V> {
|
| 784 | tree: &'a mut RBTree<K, V>,
|
| 785 | current: NonNull<bindings::rb_node>,
|
| 786 | }
|
| 787 |
|
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | pub struct Cursor<'a, K, V> {
|
| 815 | _tree: PhantomData<&'a RBTree<K, V>>,
|
| 816 | current: NonNull<bindings::rb_node>,
|
| 817 | }
|
| 818 |
|
| 819 | |
| 820 | |
| 821 | unsafe impl<'a, K: Sync, V: Sync> Send for Cursor<'a, K, V> {}
|
| 822 |
|
| 823 | |
| 824 | |
| 825 | unsafe impl<'a, K: Sync, V: Sync> Sync for Cursor<'a, K, V> {}
|
| 826 |
|
| 827 | impl<'a, K, V> Cursor<'a, K, V> {
|
| 828 | |
| 829 | pub fn current(&self) -> (&K, &V) {
|
| 830 | |
| 831 | |
| 832 | |
| 833 | unsafe { Self::to_key_value(self.current) }
|
| 834 | }
|
| 835 |
|
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | unsafe fn to_key_value<'b>(node: NonNull<bindings::rb_node>) -> (&'b K, &'b V) {
|
| 841 | |
| 842 | |
| 843 | let this = unsafe { container_of!(node.as_ptr(), Node<K, V>, links) };
|
| 844 | |
| 845 | |
| 846 | let k = unsafe { &(*this).key };
|
| 847 | |
| 848 | |
| 849 | let v = unsafe { &(*this).value };
|
| 850 | (k, v)
|
| 851 | }
|
| 852 |
|
| 853 | |
| 854 | pub fn peek_prev(&self) -> Option<(&K, &V)> {
|
| 855 | self.peek(Direction::Prev)
|
| 856 | }
|
| 857 |
|
| 858 | |
| 859 | pub fn peek_next(&self) -> Option<(&K, &V)> {
|
| 860 | self.peek(Direction::Next)
|
| 861 | }
|
| 862 |
|
| 863 | fn peek(&self, direction: Direction) -> Option<(&K, &V)> {
|
| 864 | self.get_neighbor_raw(direction).map(|neighbor| {
|
| 865 | |
| 866 | |
| 867 | |
| 868 | unsafe { Self::to_key_value(neighbor) }
|
| 869 | })
|
| 870 | }
|
| 871 |
|
| 872 | fn get_neighbor_raw(&self, direction: Direction) -> Option<NonNull<bindings::rb_node>> {
|
| 873 | |
| 874 | let neighbor = unsafe {
|
| 875 | match direction {
|
| 876 | Direction::Prev => bindings::rb_prev(self.current.as_ptr()),
|
| 877 | Direction::Next => bindings::rb_next(self.current.as_ptr()),
|
| 878 | }
|
| 879 | };
|
| 880 |
|
| 881 | NonNull::new(neighbor)
|
| 882 | }
|
| 883 | }
|
| 884 |
|
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | unsafe impl<'a, K: Send, V: Send> Send for CursorMut<'a, K, V> {}
|
| 891 |
|
| 892 | |
| 893 | |
| 894 | unsafe impl<'a, K: Sync, V: Sync> Sync for CursorMut<'a, K, V> {}
|
| 895 |
|
| 896 | impl<'a, K, V> CursorMut<'a, K, V> {
|
| 897 | |
| 898 | pub fn current(&self) -> (&K, &V) {
|
| 899 | |
| 900 | |
| 901 | |
| 902 | unsafe { Self::to_key_value(self.current) }
|
| 903 | }
|
| 904 |
|
| 905 | |
| 906 | pub fn current_mut(&mut self) -> (&K, &mut V) {
|
| 907 | |
| 908 | |
| 909 | |
| 910 | unsafe { Self::to_key_value_mut(self.current) }
|
| 911 | }
|
| 912 |
|
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | pub fn remove_current(self) -> (Option<Self>, RBTreeNode<K, V>) {
|
| 919 | let prev = self.get_neighbor_raw(Direction::Prev);
|
| 920 | let next = self.get_neighbor_raw(Direction::Next);
|
| 921 | |
| 922 | |
| 923 | let this = unsafe { container_of!(self.current.as_ptr(), Node<K, V>, links) };
|
| 924 | |
| 925 | let node = unsafe { KBox::from_raw(this) };
|
| 926 | let node = RBTreeNode { node };
|
| 927 | |
| 928 | |
| 929 | unsafe { bindings::rb_erase(&mut (*this).links, addr_of_mut!(self.tree.root)) };
|
| 930 |
|
| 931 | |
| 932 | |
| 933 | let cursor = next.or(prev).map(|current| Self {
|
| 934 | current,
|
| 935 | tree: self.tree,
|
| 936 | });
|
| 937 |
|
| 938 | (cursor, node)
|
| 939 | }
|
| 940 |
|
| 941 | |
| 942 | pub fn remove_prev(&mut self) -> Option<RBTreeNode<K, V>> {
|
| 943 | self.remove_neighbor(Direction::Prev)
|
| 944 | }
|
| 945 |
|
| 946 | |
| 947 | pub fn remove_next(&mut self) -> Option<RBTreeNode<K, V>> {
|
| 948 | self.remove_neighbor(Direction::Next)
|
| 949 | }
|
| 950 |
|
| 951 | fn remove_neighbor(&mut self, direction: Direction) -> Option<RBTreeNode<K, V>> {
|
| 952 | if let Some(neighbor) = self.get_neighbor_raw(direction) {
|
| 953 | let neighbor = neighbor.as_ptr();
|
| 954 | |
| 955 | |
| 956 | unsafe { bindings::rb_erase(neighbor, addr_of_mut!(self.tree.root)) };
|
| 957 | |
| 958 | |
| 959 | let this = unsafe { container_of!(neighbor, Node<K, V>, links) };
|
| 960 | |
| 961 | let node = unsafe { KBox::from_raw(this) };
|
| 962 | return Some(RBTreeNode { node });
|
| 963 | }
|
| 964 | None
|
| 965 | }
|
| 966 |
|
| 967 | |
| 968 | pub fn move_prev(self) -> Option<Self> {
|
| 969 | self.mv(Direction::Prev)
|
| 970 | }
|
| 971 |
|
| 972 | |
| 973 | pub fn move_next(self) -> Option<Self> {
|
| 974 | self.mv(Direction::Next)
|
| 975 | }
|
| 976 |
|
| 977 | fn mv(self, direction: Direction) -> Option<Self> {
|
| 978 | |
| 979 | |
| 980 | self.get_neighbor_raw(direction).map(|neighbor| Self {
|
| 981 | tree: self.tree,
|
| 982 | current: neighbor,
|
| 983 | })
|
| 984 | }
|
| 985 |
|
| 986 | |
| 987 | pub fn peek_prev(&self) -> Option<(&K, &V)> {
|
| 988 | self.peek(Direction::Prev)
|
| 989 | }
|
| 990 |
|
| 991 | |
| 992 | pub fn peek_next(&self) -> Option<(&K, &V)> {
|
| 993 | self.peek(Direction::Next)
|
| 994 | }
|
| 995 |
|
| 996 | fn peek(&self, direction: Direction) -> Option<(&K, &V)> {
|
| 997 | self.get_neighbor_raw(direction).map(|neighbor| {
|
| 998 | |
| 999 | |
| 1000 | |
| 1001 | unsafe { Self::to_key_value(neighbor) }
|
| 1002 | })
|
| 1003 | }
|
| 1004 |
|
| 1005 | |
| 1006 | pub fn peek_prev_mut(&mut self) -> Option<(&K, &mut V)> {
|
| 1007 | self.peek_mut(Direction::Prev)
|
| 1008 | }
|
| 1009 |
|
| 1010 | |
| 1011 | pub fn peek_next_mut(&mut self) -> Option<(&K, &mut V)> {
|
| 1012 | self.peek_mut(Direction::Next)
|
| 1013 | }
|
| 1014 |
|
| 1015 | fn peek_mut(&mut self, direction: Direction) -> Option<(&K, &mut V)> {
|
| 1016 | self.get_neighbor_raw(direction).map(|neighbor| {
|
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | unsafe { Self::to_key_value_mut(neighbor) }
|
| 1021 | })
|
| 1022 | }
|
| 1023 |
|
| 1024 | fn get_neighbor_raw(&self, direction: Direction) -> Option<NonNull<bindings::rb_node>> {
|
| 1025 | |
| 1026 | let neighbor = unsafe {
|
| 1027 | match direction {
|
| 1028 | Direction::Prev => bindings::rb_prev(self.current.as_ptr()),
|
| 1029 | Direction::Next => bindings::rb_next(self.current.as_ptr()),
|
| 1030 | }
|
| 1031 | };
|
| 1032 |
|
| 1033 | NonNull::new(neighbor)
|
| 1034 | }
|
| 1035 |
|
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | unsafe fn to_key_value<'b>(node: NonNull<bindings::rb_node>) -> (&'b K, &'b V) {
|
| 1041 | |
| 1042 | let (k, v) = unsafe { Self::to_key_value_raw(node) };
|
| 1043 | |
| 1044 | (k, unsafe { &*v })
|
| 1045 | }
|
| 1046 |
|
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | unsafe fn to_key_value_mut<'b>(node: NonNull<bindings::rb_node>) -> (&'b K, &'b mut V) {
|
| 1052 | |
| 1053 | let (k, v) = unsafe { Self::to_key_value_raw(node) };
|
| 1054 | |
| 1055 | (k, unsafe { &mut *v })
|
| 1056 | }
|
| 1057 |
|
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | unsafe fn to_key_value_raw<'b>(node: NonNull<bindings::rb_node>) -> (&'b K, *mut V) {
|
| 1063 | |
| 1064 | |
| 1065 | let this = unsafe { container_of!(node.as_ptr(), Node<K, V>, links) };
|
| 1066 | |
| 1067 | |
| 1068 | let k = unsafe { &(*this).key };
|
| 1069 | |
| 1070 | |
| 1071 | let v = unsafe { addr_of_mut!((*this).value) };
|
| 1072 | (k, v)
|
| 1073 | }
|
| 1074 | }
|
| 1075 |
|
| 1076 | |
| 1077 | enum Direction {
|
| 1078 | |
| 1079 | Prev,
|
| 1080 | |
| 1081 | Next,
|
| 1082 | }
|
| 1083 |
|
| 1084 | impl<'a, K, V> IntoIterator for &'a RBTree<K, V> {
|
| 1085 | type Item = (&'a K, &'a V);
|
| 1086 | type IntoIter = Iter<'a, K, V>;
|
| 1087 |
|
| 1088 | fn into_iter(self) -> Self::IntoIter {
|
| 1089 | self.iter()
|
| 1090 | }
|
| 1091 | }
|
| 1092 |
|
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | pub struct Iter<'a, K, V> {
|
| 1097 | _tree: PhantomData<&'a RBTree<K, V>>,
|
| 1098 | iter_raw: IterRaw<K, V>,
|
| 1099 | }
|
| 1100 |
|
| 1101 | |
| 1102 | |
| 1103 | unsafe impl<'a, K: Sync, V: Sync> Send for Iter<'a, K, V> {}
|
| 1104 |
|
| 1105 | |
| 1106 | |
| 1107 | unsafe impl<'a, K: Sync, V: Sync> Sync for Iter<'a, K, V> {}
|
| 1108 |
|
| 1109 | impl<'a, K, V> Iterator for Iter<'a, K, V> {
|
| 1110 | type Item = (&'a K, &'a V);
|
| 1111 |
|
| 1112 | fn next(&mut self) -> Option<Self::Item> {
|
| 1113 | |
| 1114 | self.iter_raw.next().map(|(k, v)| unsafe { (&*k, &*v) })
|
| 1115 | }
|
| 1116 | }
|
| 1117 |
|
| 1118 | impl<'a, K, V> IntoIterator for &'a mut RBTree<K, V> {
|
| 1119 | type Item = (&'a K, &'a mut V);
|
| 1120 | type IntoIter = IterMut<'a, K, V>;
|
| 1121 |
|
| 1122 | fn into_iter(self) -> Self::IntoIter {
|
| 1123 | self.iter_mut()
|
| 1124 | }
|
| 1125 | }
|
| 1126 |
|
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | pub struct IterMut<'a, K, V> {
|
| 1131 | _tree: PhantomData<&'a mut RBTree<K, V>>,
|
| 1132 | iter_raw: IterRaw<K, V>,
|
| 1133 | }
|
| 1134 |
|
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | unsafe impl<'a, K: Send, V: Send> Send for IterMut<'a, K, V> {}
|
| 1139 |
|
| 1140 | |
| 1141 | |
| 1142 | unsafe impl<'a, K: Sync, V: Sync> Sync for IterMut<'a, K, V> {}
|
| 1143 |
|
| 1144 | impl<'a, K, V> Iterator for IterMut<'a, K, V> {
|
| 1145 | type Item = (&'a K, &'a mut V);
|
| 1146 |
|
| 1147 | fn next(&mut self) -> Option<Self::Item> {
|
| 1148 | self.iter_raw.next().map(|(k, v)|
|
| 1149 | |
| 1150 | unsafe { (&*k, &mut *v) })
|
| 1151 | }
|
| 1152 | }
|
| 1153 |
|
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | struct IterRaw<K, V> {
|
| 1160 | next: *mut bindings::rb_node,
|
| 1161 | _phantom: PhantomData<fn() -> (K, V)>,
|
| 1162 | }
|
| 1163 |
|
| 1164 | impl<K, V> Iterator for IterRaw<K, V> {
|
| 1165 | type Item = (*mut K, *mut V);
|
| 1166 |
|
| 1167 | fn next(&mut self) -> Option<Self::Item> {
|
| 1168 | if self.next.is_null() {
|
| 1169 | return None;
|
| 1170 | }
|
| 1171 |
|
| 1172 | |
| 1173 | |
| 1174 | let cur = unsafe { container_of!(self.next, Node<K, V>, links) };
|
| 1175 |
|
| 1176 | |
| 1177 | self.next = unsafe { bindings::rb_next(self.next) };
|
| 1178 |
|
| 1179 | |
| 1180 | Some(unsafe { (addr_of_mut!((*cur).key), addr_of_mut!((*cur).value)) })
|
| 1181 | }
|
| 1182 | }
|
| 1183 |
|
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | pub struct RBTreeNodeReservation<K, V> {
|
| 1190 | node: KBox<MaybeUninit<Node<K, V>>>,
|
| 1191 | }
|
| 1192 |
|
| 1193 | impl<K, V> RBTreeNodeReservation<K, V> {
|
| 1194 | |
| 1195 | |
| 1196 | pub fn new(flags: Flags) -> Result<RBTreeNodeReservation<K, V>> {
|
| 1197 | Ok(RBTreeNodeReservation {
|
| 1198 | node: KBox::new_uninit(flags)?,
|
| 1199 | })
|
| 1200 | }
|
| 1201 | }
|
| 1202 |
|
| 1203 | |
| 1204 | |
| 1205 | unsafe impl<K, V> Send for RBTreeNodeReservation<K, V> {}
|
| 1206 |
|
| 1207 | |
| 1208 | unsafe impl<K, V> Sync for RBTreeNodeReservation<K, V> {}
|
| 1209 |
|
| 1210 | impl<K, V> RBTreeNodeReservation<K, V> {
|
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | pub fn into_node(self, key: K, value: V) -> RBTreeNode<K, V> {
|
| 1215 | let node = KBox::write(
|
| 1216 | self.node,
|
| 1217 | Node {
|
| 1218 | key,
|
| 1219 | value,
|
| 1220 | links: bindings::rb_node::default(),
|
| 1221 | },
|
| 1222 | );
|
| 1223 | RBTreeNode { node }
|
| 1224 | }
|
| 1225 | }
|
| 1226 |
|
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | pub struct RBTreeNode<K, V> {
|
| 1232 | node: KBox<Node<K, V>>,
|
| 1233 | }
|
| 1234 |
|
| 1235 | impl<K, V> RBTreeNode<K, V> {
|
| 1236 | |
| 1237 | |
| 1238 | pub fn new(key: K, value: V, flags: Flags) -> Result<RBTreeNode<K, V>> {
|
| 1239 | Ok(RBTreeNodeReservation::new(flags)?.into_node(key, value))
|
| 1240 | }
|
| 1241 |
|
| 1242 | |
| 1243 | pub fn to_key_value(self) -> (K, V) {
|
| 1244 | let node = KBox::into_inner(self.node);
|
| 1245 |
|
| 1246 | (node.key, node.value)
|
| 1247 | }
|
| 1248 | }
|
| 1249 |
|
| 1250 | |
| 1251 | |
| 1252 | unsafe impl<K: Send, V: Send> Send for RBTreeNode<K, V> {}
|
| 1253 |
|
| 1254 | |
| 1255 | |
| 1256 | unsafe impl<K: Sync, V: Sync> Sync for RBTreeNode<K, V> {}
|
| 1257 |
|
| 1258 | impl<K, V> RBTreeNode<K, V> {
|
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | pub fn into_reservation(self) -> RBTreeNodeReservation<K, V> {
|
| 1267 | RBTreeNodeReservation {
|
| 1268 | node: KBox::drop_contents(self.node),
|
| 1269 | }
|
| 1270 | }
|
| 1271 | }
|
| 1272 |
|
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | pub enum Entry<'a, K, V> {
|
| 1279 | |
| 1280 | Vacant(VacantEntry<'a, K, V>),
|
| 1281 | |
| 1282 | Occupied(OccupiedEntry<'a, K, V>),
|
| 1283 | }
|
| 1284 |
|
| 1285 | |
| 1286 | enum RawEntry<'a, K, V> {
|
| 1287 | Vacant(RawVacantEntry<'a, K, V>),
|
| 1288 | Occupied(OccupiedEntry<'a, K, V>),
|
| 1289 | }
|
| 1290 |
|
| 1291 | |
| 1292 | pub struct VacantEntry<'a, K, V> {
|
| 1293 | key: K,
|
| 1294 | raw: RawVacantEntry<'a, K, V>,
|
| 1295 | }
|
| 1296 |
|
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | struct RawVacantEntry<'a, K, V> {
|
| 1304 | rbtree: *mut RBTree<K, V>,
|
| 1305 | |
| 1306 | parent: *mut bindings::rb_node,
|
| 1307 | |
| 1308 | |
| 1309 | child_field_of_parent: *mut *mut bindings::rb_node,
|
| 1310 | _phantom: PhantomData<&'a mut RBTree<K, V>>,
|
| 1311 | }
|
| 1312 |
|
| 1313 | impl<'a, K, V> RawVacantEntry<'a, K, V> {
|
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | fn insert(self, node: RBTreeNode<K, V>) -> &'a mut V {
|
| 1319 | let node = KBox::into_raw(node.node);
|
| 1320 |
|
| 1321 | |
| 1322 | |
| 1323 | let node_links = unsafe { addr_of_mut!((*node).links) };
|
| 1324 |
|
| 1325 | |
| 1326 | |
| 1327 | |
| 1328 | unsafe { bindings::rb_link_node(node_links, self.parent, self.child_field_of_parent) };
|
| 1329 |
|
| 1330 | |
| 1331 | unsafe { bindings::rb_insert_color(node_links, addr_of_mut!((*self.rbtree).root)) };
|
| 1332 |
|
| 1333 | |
| 1334 | unsafe { &mut (*node).value }
|
| 1335 | }
|
| 1336 | }
|
| 1337 |
|
| 1338 | impl<'a, K, V> VacantEntry<'a, K, V> {
|
| 1339 | |
| 1340 | pub fn insert(self, value: V, reservation: RBTreeNodeReservation<K, V>) -> &'a mut V {
|
| 1341 | self.raw.insert(reservation.into_node(self.key, value))
|
| 1342 | }
|
| 1343 | }
|
| 1344 |
|
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | pub struct OccupiedEntry<'a, K, V> {
|
| 1350 | rbtree: &'a mut RBTree<K, V>,
|
| 1351 | |
| 1352 | node_links: *mut bindings::rb_node,
|
| 1353 | }
|
| 1354 |
|
| 1355 | impl<'a, K, V> OccupiedEntry<'a, K, V> {
|
| 1356 | |
| 1357 | pub fn get(&self) -> &V {
|
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | unsafe { &(*container_of!(self.node_links, Node<K, V>, links)).value }
|
| 1362 | }
|
| 1363 |
|
| 1364 | |
| 1365 | pub fn get_mut(&mut self) -> &mut V {
|
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | unsafe { &mut (*(container_of!(self.node_links, Node<K, V>, links))).value }
|
| 1370 | }
|
| 1371 |
|
| 1372 | |
| 1373 | |
| 1374 | |
| 1375 | pub fn into_mut(self) -> &'a mut V {
|
| 1376 | |
| 1377 | |
| 1378 | |
| 1379 | unsafe { &mut (*(container_of!(self.node_links, Node<K, V>, links))).value }
|
| 1380 | }
|
| 1381 |
|
| 1382 | |
| 1383 | pub fn remove_node(self) -> RBTreeNode<K, V> {
|
| 1384 | |
| 1385 | unsafe { bindings::rb_erase(self.node_links, &mut self.rbtree.root) };
|
| 1386 |
|
| 1387 | |
| 1388 | |
| 1389 | RBTreeNode {
|
| 1390 | |
| 1391 | |
| 1392 | node: unsafe { KBox::from_raw(container_of!(self.node_links, Node<K, V>, links)) },
|
| 1393 | }
|
| 1394 | }
|
| 1395 |
|
| 1396 | |
| 1397 | pub fn remove(self) -> V {
|
| 1398 | let rb_node = self.remove_node();
|
| 1399 | let node = KBox::into_inner(rb_node.node);
|
| 1400 |
|
| 1401 | node.value
|
| 1402 | }
|
| 1403 |
|
| 1404 | |
| 1405 | |
| 1406 | |
| 1407 | fn replace(self, node: RBTreeNode<K, V>) -> RBTreeNode<K, V> {
|
| 1408 | let node = KBox::into_raw(node.node);
|
| 1409 |
|
| 1410 | |
| 1411 | |
| 1412 | let new_node_links = unsafe { addr_of_mut!((*node).links) };
|
| 1413 |
|
| 1414 | |
| 1415 | |
| 1416 | unsafe {
|
| 1417 | bindings::rb_replace_node(self.node_links, new_node_links, &mut self.rbtree.root)
|
| 1418 | };
|
| 1419 |
|
| 1420 | |
| 1421 | |
| 1422 | |
| 1423 | let old_node = unsafe { KBox::from_raw(container_of!(self.node_links, Node<K, V>, links)) };
|
| 1424 |
|
| 1425 | RBTreeNode { node: old_node }
|
| 1426 | }
|
| 1427 | }
|
| 1428 |
|
| 1429 | struct Node<K, V> {
|
| 1430 | links: bindings::rb_node,
|
| 1431 | key: K,
|
| 1432 | value: V,
|
| 1433 | }
|