1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 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 784 785 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 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
use crate::iter::{ IterAll, IterRangeWith, LeftBiasIter, LeftBiasIterWidth, NodeRightIter, NodeWidth, SkipListIndexRange, SkipListRange, VerticalIter, }; use core::ops::RangeBounds; use rand::prelude::*; use std::cmp::{Ordering, PartialOrd}; use std::fmt; use std::iter::FromIterator; use std::ops::Index; use std::ptr::NonNull; pub mod iter; #[cfg(feature = "serde_support")] mod serde; #[derive(PartialEq, Debug)] enum NodeValue<T> { NegInf, Value(T), PosInf, } impl<T> NodeValue<T> { #[inline] fn get_value(&self) -> &T { match &self { NodeValue::Value(v) => v, _ => unreachable!("Failed to get value! This shouldn't happen."), } } #[inline] fn is_pos_inf(&self) -> bool { match &self { NodeValue::PosInf => true, _ => false, } } } impl<T: PartialEq> PartialEq<T> for NodeValue<T> { #[inline] fn eq(&self, other: &T) -> bool { match self { NodeValue::Value(v) => v == other, _ => false, } } } impl<T: PartialOrd> PartialOrd<NodeValue<T>> for NodeValue<T> { #[inline] fn partial_cmp(&self, other: &NodeValue<T>) -> Option<Ordering> { match (self, other) { (NodeValue::NegInf, _) => Some(Ordering::Less), (_, NodeValue::PosInf) => Some(Ordering::Less), (NodeValue::Value(l), NodeValue::Value(r)) => l.partial_cmp(r), _ => unreachable!(), } } } impl<T: PartialOrd> PartialOrd<T> for NodeValue<T> { #[inline] fn partial_cmp(&self, other: &T) -> Option<Ordering> { match self { NodeValue::NegInf => Some(Ordering::Less), NodeValue::PosInf => Some(Ordering::Greater), NodeValue::Value(v) => v.partial_cmp(other), } } } struct Node<T> { right: Option<NonNull<Node<T>>>, down: Option<NonNull<Node<T>>>, value: NodeValue<T>, width: usize, } impl<T> Node<T> { #[inline] fn nodes_skipped_over(&self) -> usize { self.width - 1 } #[inline] fn clear_right(&mut self) { self.width = 1; unsafe { while let Some(right) = self.right { if right.as_ref().value.is_pos_inf() { break; } let garbage = std::mem::replace(&mut self.right, (*right.as_ptr()).right); drop(Box::from_raw(garbage.unwrap().as_ptr())); } } } } impl<T: fmt::Debug> fmt::Debug for Node<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "Node(")?; writeln!( f, " right: {:?},", self.right .map(|some| format!("{:?}", unsafe { &some.as_ref().value })) )?; writeln!( f, " down: {:?},", self.down .map(|some| format!("{:?}", unsafe { &some.as_ref().value })) )?; writeln!(f, " value: {:?}", self.value)?; writeln!(f, " width: {:?}", self.width)?; write!(f, ")") } } /// Hint that the current value `item` is: /// /// - SmallerThanRange: `item` is strictly smaller than the range. /// - InRange: `item` is in the range. /// - LargerThanRange: `item` is strictly larger than the range. /// /// Used with IterRangeWith, or `range_with` #[derive(Debug)] pub enum RangeHint { SmallerThanRange, InRange, LargerThanRange, } /// `SkipLists` are fast probabilistic data-structures that feature logarithmic time complexity for inserting elements, /// testing element association, removing elements, and finding ranges of elements. /// /// ```rust /// use convenient_skiplist::SkipList; /// /// // Make a new skiplist /// let mut sk = SkipList::new(); /// for i in 0..5usize { /// // Inserts are O(log(n)) on average /// sk.insert(i); /// } /// // You can print the skiplist! /// dbg!(&sk); /// // You can check if the skiplist contains an element, O(log(n)) /// assert!(sk.contains(&0)); /// assert!(!sk.contains(&10)); /// assert!(sk.remove(&0)); // remove is also O(log(n)) /// assert!(sk == sk); // equality checking is O(n) /// let from_vec = SkipList::from(vec![1usize, 2, 3].into_iter()); // From<Vec<T>> is O(nlogn) /// assert_eq!(vec![1, 2, 3], from_vec.iter_all().cloned().collect::<Vec<usize>>()); /// ``` pub struct SkipList<T> { top_left: NonNull<Node<T>>, height: usize, len: usize, _prevent_sync_send: std::marker::PhantomData<*const ()>, } impl<T> Drop for SkipList<T> { fn drop(&mut self) { // Main idea: Start in top left and iterate row by row. let mut curr_left_node = self.top_left.as_ptr(); let mut next_down; let mut curr_node = self.top_left.as_ptr(); unsafe { loop { if let Some(down) = (*curr_left_node).down { next_down = Some(down.as_ptr()); } else { next_down = None; } while let Some(right) = (*curr_node).right { let garbage = std::mem::replace(&mut curr_node, right.as_ptr()); drop(Box::from_raw(garbage)); } drop(Box::from_raw(curr_node)); if let Some(next_down) = next_down { curr_left_node = next_down; curr_node = curr_left_node; } else { break; } } } } } impl<T: Clone + PartialOrd> From<SkipList<T>> for Vec<T> { fn from(sk: SkipList<T>) -> Vec<T> { sk.iter_all().cloned().collect() } } impl<T: Clone + PartialOrd> Clone for SkipList<T> { fn clone(&self) -> Self { SkipList::from(self.iter_all().cloned()) } } impl<T: PartialOrd + Clone> FromIterator<T> for SkipList<T> { fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> SkipList<T> { let mut sk = SkipList::new(); for item in iter { sk.insert(item); } sk } } impl<T: PartialOrd + Clone, I: Iterator<Item = T>> From<I> for SkipList<T> { fn from(iter: I) -> Self { iter.collect() } } impl<T: PartialOrd + Clone> PartialEq for SkipList<T> { fn eq(&self, other: &Self) -> bool { self.len() == other.len() && self.iter_all().zip(other.iter_all()).all(|(l, r)| l == r) } } macro_rules! fmt_node { ($f:expr, $node:expr) => { write!( $f, "{:?}(skipped: {})", $node.as_ref().value, $node.as_ref().nodes_skipped_over() ) }; } impl<T: fmt::Debug> fmt::Debug for SkipList<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "SkipList(wall_height: {}), and table:", self.height)?; unsafe { fmt_node!(f, self.top_left)?; write!(f, " -> ")?; fmt_node!(f, self.top_left.as_ref().right.unwrap())?; writeln!(f)?; let mut curr_down = self.top_left.as_ref().down; while let Some(down) = curr_down { fmt_node!(f, down)?; let mut curr_right = down.as_ref().right; while let Some(right) = curr_right { write!(f, " -> ")?; fmt_node!(f, right)?; curr_right = right.as_ref().right; } curr_down = down.as_ref().down; writeln!(f)?; } } Ok(()) } } impl<T: PartialOrd + Clone> Default for SkipList<T> { #[inline] fn default() -> Self { Self::new() } } impl<T: PartialOrd + Clone> Index<usize> for SkipList<T> { type Output = T; fn index(&self, index: usize) -> &Self::Output { self.at_index(index).expect("index out of bounds!") } } /// Get the level of an item in the skiplist #[inline] fn get_level() -> usize { let mut height = 1; let mut rng = rand::thread_rng(); while rng.gen::<f32>() >= 0.5 { height += 1; } height } impl<T: PartialOrd + Clone> SkipList<T> { /// Make a new, empty SkipList. By default there is three levels. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::new(); /// sk.insert(0usize); /// /// assert!(sk.contains(&0)); /// ``` #[inline] pub fn new() -> SkipList<T> { let mut sk = SkipList { top_left: SkipList::pos_neg_pair(1), height: 1, len: 0, _prevent_sync_send: std::marker::PhantomData, }; sk.add_levels(2); sk } /// add `additional_levels` to the _top_ of the SkipList #[inline] fn add_levels(&mut self, additional_levels: usize) { let mut curr_level = self.top_left; for _ in 0..additional_levels { let mut new_level = SkipList::pos_neg_pair(self.len() + 1); // We're going to insert this `new_level` between curr_level and the row below it. // So it will look like: // | top_left -> top_right // | *new row here* // | *existing row* unsafe { new_level.as_mut().down = curr_level.as_ref().down; curr_level.as_mut().down = Some(new_level); curr_level = new_level; } } self.height += additional_levels as usize; } /// Insert `item` into the `SkipList`. /// /// Returns `true` if the item was actually inserted (i.e. wasn't already in the skiplist) /// and `false` otherwise. /// /// Runs in `O(logn)` time. /// /// # Arguments /// /// * `item` - the item to insert. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::new(); /// sk.insert(0usize); /// /// assert!(sk.contains(&0)); /// ``` #[inline] pub fn insert(&mut self, item: T) -> bool { #[cfg(debug_assertions)] { self.ensure_invariants() } if self.contains(&item) { return false; } let height = get_level(); let additional_height_req: i32 = (height as i32 - self.height as i32) + 1; if additional_height_req > 0 { self.add_levels(additional_height_req as usize); debug_assert!(self.height > height); } #[cfg(debug_assertions)] { self.ensure_invariants() } // Now the skiplist has enough height to actually insert this element. // We'll need to reverse iterate to stitch the required items between. // As self.path_to returns all nodes immediately *left* of where we've inserted, // we just need to insert the nodes after. let mut node_below_me = None; let mut added = 0; let mut total_width = None; for node in self.insert_path(&item).into_iter().rev() { unsafe { (*node.curr_node).width += 1; } // Set total_width from the bottom node. if total_width.is_none() { total_width = Some(node.curr_width); } let total_width = total_width.unwrap(); if added < height { unsafe { // IDEA: We are iterating every node immediately *left* of where we're inserting // an element. This means we can use `total_width`, or the maximum distance // traveled to the right to reach the node to determine node widths relatively. // // eg. We insert 4 into the skiplist below: // -inf -> ... // -inf -> 1 -> ... // -inf -> 1 -> 2 -> ... // -inf -> 1 -> 2 -> 3 -> ... // // Imagine a placeholder where 4 goes. // // eg. We insert 4 into the skiplist below: // -inf -> _ -> ... // -inf -> 1 -> _ -> ... // -inf -> 1 -> 2 -> _ -> ... // -inf -> 1 -> 2 -> 3 -> _ -> ... // // This placeholder has then increased the width of all nodes by 1. // Once we determine height, for every element on the left, // we need to distribute the widths. We can do this // relative to `total_width`: // // 1. -inf -> _ -> ... // 2. -inf -> 1 -> _ -> ... // 3. -inf -> 1 -> 2 -> _ -> ... // 4. -inf -> 1 -> 2 -> 3 -> _ -> ... // ~ ~ ~ ~ // We know how far _right_ we've been, and know that // all areas a '4' goes is going to truncate widths // of the elements to the left. For example, // row element '2' in row 3 is going to report a `node.curr_width` // of 3, so it's new width is (4 - 3) + 1 (i.e. the number of links between it and 4) // // Lastly, we distribute the remaining width after the // truncation above to the new element. let left_node_width = total_width - node.curr_width + 1; let new_node_width = (*node.curr_node).width - left_node_width; (*node.curr_node).width = left_node_width; debug_assert!(total_width + 1 == node.curr_width + left_node_width); let mut new_node = SkipList::make_node(item.clone(), new_node_width); let node: *mut Node<T> = node.curr_node; new_node.as_mut().down = node_below_me; new_node.as_mut().right = (*node).right; (*node).right = Some(new_node); node_below_me = Some(new_node); } added += 1; } } self.len += 1; #[cfg(debug_assertions)] { self.ensure_invariants() } true } /// Test if `item` is in the skiplist. Returns `true` if it's in the skiplist, /// `false` otherwise. /// /// Runs in `O(logn)` time /// /// # Arguments /// /// * `item` - the item we're testing. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::new(); /// sk.insert(0usize); /// /// assert!(sk.contains(&0)); /// ``` #[inline] pub fn contains(&self, item: &T) -> bool { self.iter_left(item).any(|node| unsafe { if let Some(right) = &(*node).right { &right.as_ref().value == item } else { false } }) } /// Remove `item` from the SkipList. /// /// Returns `true` if the item was in the collection to be removed, /// and `false` otherwise. /// /// Runs in `O(logn)` time. /// /// # Arguments /// /// * `item` - the item to remove. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::new(); /// sk.insert(0usize); /// /// let removed = sk.remove(&0); /// assert!(removed); /// ``` pub fn remove(&mut self, item: &T) -> bool { if !self.contains(item) { return false; } for node in self.iter_left(item) { unsafe { (*node).width -= 1; // Invariant: `node` can never be PosInf let right = (*node).right.unwrap(); if &right.as_ref().value != item { continue; } // So the node right of us needs to be removed. (*node).width += right.as_ref().width; let garbage = std::mem::replace(&mut (*node).right, right.as_ref().right); drop(Box::from_raw(garbage.unwrap().as_ptr())); } } self.len -= 1; true } /// Remove and return the item at `index`. /// /// Runs in O(log n) time. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::from(0..5); /// /// assert_eq!(sk.len(), 5); /// assert_eq!(sk.remove_at(1), Some(1)); /// assert_eq!(sk.len(), 4); /// ``` pub fn remove_at(&mut self, index: usize) -> Option<T> { let item = self.at_index(index).cloned(); if let Some(item) = &item { self.remove(item); } item } /// Return the number of elements in the skiplist. /// /// # Example /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::new(); /// /// sk.insert(0); /// assert_eq!(sk.len(), 1); /// /// sk.insert(1); /// assert_eq!(sk.len(), 2); /// ``` #[inline] pub fn len(&self) -> usize { self.len } /// Returns true if the skiplist is empty #[inline] pub fn is_empty(&self) -> bool { self.len == 0 } // TODO // fn remove_range<'a>(&'a mut self, _start: &'a T, _end: &'a T) -> usize { // // Idea: Use iter_left twice to determine the chunk in the middle to remove. // // Hardest part will be cleaning up garbage. :thinking: // todo!() // } /// Find the index of `item` in the `SkipList`. /// /// Runs in `O(logn)` time. /// /// # Arguments /// /// * `item`: the item to find the position of. /// /// # Example /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::new(); /// sk.insert(1); /// sk.insert(2); /// sk.insert(3); /// /// assert_eq!(sk.index_of(&1), Some(0)); /// assert_eq!(sk.index_of(&2), Some(1)); /// assert_eq!(sk.index_of(&3), Some(2)); /// assert_eq!(sk.index_of(&999), None); /// ``` #[inline] pub fn index_of(&self, item: &T) -> Option<usize> { // INVARIANT: path_to is a LeftBiasIterWidth, so there's always a // node right of us. self.path_to(item).last().and_then(|node| { if unsafe { &(*node.curr_node).right.unwrap().as_ref().value } == item { Some(node.curr_width) } else { None } }) } /// Get the item at the index `index `in the `SkipList`. /// /// Runs in `O(logn)` time. /// /// # Arguments /// /// * `index`: the index to get the item at /// /// # Example /// ```rust /// use convenient_skiplist::SkipList; /// let sk = SkipList::from(0..10); /// for i in 0..10 { /// assert_eq!(Some(&i), sk.at_index(i)); /// } /// assert_eq!(None, sk.at_index(11)); /// /// let mut sk = SkipList::new(); /// sk.insert('a'); /// sk.insert('b'); /// sk.insert('c'); /// assert_eq!(Some(&'a'), sk.at_index(0)); /// assert_eq!(Some(&'b'), sk.at_index(1)); /// assert_eq!(Some(&'c'), sk.at_index(2)); /// assert_eq!(None, sk.at_index(3)); /// ``` #[inline] pub fn at_index(&self, index: usize) -> Option<&T> { if index >= self.len() { return None; } unsafe { let mut curr_node = self.top_left.as_ref(); let mut distance_left = index + 1; loop { if distance_left == 0 { return Some(curr_node.value.get_value()); } if curr_node.width <= distance_left { distance_left -= curr_node.width; // INVARIANT: We've checked if `index` < self.len(), // so there's always a `right` curr_node = curr_node.right.unwrap().as_ptr().as_ref().unwrap(); continue; } else if let Some(down) = curr_node.down { curr_node = down.as_ptr().as_ref().unwrap(); } else { unreachable!() } } } } /// Peek at the first item in the skiplist. /// /// Runs in constant time. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::from(0..10); /// /// assert_eq!(Some(&0), sk.peek_first()); /// ``` #[inline] pub fn peek_first(&self) -> Option<&T> { self.at_index(0) } /// Peek at the last item in the skiplist. /// /// Runs in O(log n) time. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::from(0..10); /// /// assert_eq!(Some(&9), sk.peek_last()); /// ``` #[inline] pub fn peek_last(&self) -> Option<&T> { if self.is_empty() { None } else { self.at_index(self.len() - 1) } } /// Pop `count` elements off of the end of the Skiplist. /// /// Runs in O(logn * count) time, O(logn + count) space. /// /// Memory pressure: This is implemented such that the entire /// region of the skiplist is cleaved off at once. So you'll /// see in the worse case (i.e. all towers have maximum height ~ logn) /// count * logn memory deallocations. /// /// Returns an empty `vec` if count == 0. /// /// Will dealloc the whole skiplist if count >= len and start fresh. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::from(0..10); /// /// assert_eq!(Some(&7), sk.at_index(7)); /// assert_eq!(vec![7, 8, 9], sk.pop_max(3)); /// assert_eq!(vec![6], sk.pop_max(1)); /// assert_eq!(vec![4, 5], sk.pop_max(2)); /// assert_eq!(vec![0, 1, 2, 3], sk.pop_max(5)); /// /// let v: Vec<u32> = Vec::new(); /// assert_eq!(v, sk.pop_max(1000)); // empty /// ``` #[inline] pub fn pop_max(&mut self, count: usize) -> Vec<T> { if self.is_empty() || count == 0 { return vec![]; } if count >= self.len() { // let new = SkipList::new(); // let garbage = std::mem::replace(&mut self, &mut new); // drop(garbage); let ret = self.iter_all().cloned().collect(); *self = SkipList::new(); // TODO: Does this drop me? return ret; } let ele_at = self.at_index(self.len() - count).unwrap().clone(); self.len -= count; // IDEA: Calculate widths by adding _backwards_ through the // insert path. let mut frontier = self.insert_path(&ele_at); let last_value = frontier.last_mut().cloned().unwrap(); let mut last_width = last_value.curr_width; let mut ret: Vec<_> = Vec::with_capacity(count); let mut jumped_left = 1; unsafe { ret.extend(NodeRightIter::new( (*last_value.curr_node).right.unwrap().as_ptr(), )); (*last_value.curr_node).clear_right(); } for mut nw in frontier.into_iter().rev().skip(1) { unsafe { // We've jumped right, and now need to update our width field. // Do we need this if-gate? if (*nw.curr_node).value != (*last_value.curr_node).value { jumped_left += last_width - nw.curr_width; last_width = nw.curr_width; } (*nw.curr_node).clear_right(); (*nw.curr_node).width = jumped_left; } } ret } /// Pop the last element off of the skiplist. /// /// Runs in O(logn) time, O(1) space. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::from(0..10); /// /// assert_eq!(Some(9), sk.pop_back()); /// ``` #[inline] pub fn pop_back(&mut self) -> Option<T> { if self.is_empty() { None } else { self.pop_max(1).pop() } } /// Pop the first element off of the skiplist. /// /// Runs in O(logn) time, O(1) space. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::from(0..10); /// /// assert_eq!(Some(0), sk.pop_front()); /// ``` #[inline] pub fn pop_front(&mut self) -> Option<T> { if self.is_empty() { None } else { self.pop_min(1).pop() } } fn iter_vertical(&self) -> impl Iterator<Item = *mut Node<T>> { VerticalIter::new(self.top_left.as_ptr()) } /// Pop `count` elements off of the start of the Skiplist. /// /// Runs in O(logn * count) time, O(count) space. /// /// Memory pressure: This is implemented such that the entire /// region of the skiplist is cleaved off at once. So you'll /// see in the worse case (i.e. all towers have maximum height ~ logn) /// count * logn memory deallocations. /// /// Returns an empty `vec` if count == 0. /// /// Will dealloc the whole skiplist if count >= len and start fresh. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::from(0..10); /// /// assert_eq!(vec![0, 1, 2], sk.pop_min(3)); /// assert_eq!(vec![3], sk.pop_min(1)); /// assert_eq!(vec![4, 5], sk.pop_min(2)); /// assert_eq!(vec![6, 7, 8, 9], sk.pop_max(5)); /// /// let v: Vec<u32> = Vec::new(); /// assert_eq!(v, sk.pop_min(1000)); // empty /// ``` #[inline] pub fn pop_min(&mut self, count: usize) -> Vec<T> { if count == 0 || self.is_empty() { return Vec::with_capacity(0); } if count >= self.len() { let ret = self.iter_all().cloned().collect(); // Tested in valgrind -- this drops old me. *self = SkipList::new(); return ret; } let ele_at = self.at_index(count).unwrap(); // dbg!(ele_at); let mut ret = Vec::with_capacity(count); for (left, row_end) in self.iter_vertical().zip(self.path_to(ele_at)) { // Our path can have the same elements left and right of the // frontier. if std::ptr::eq(left, row_end.curr_node) { unsafe { (*left).width -= count }; continue; } debug_assert!(count >= row_end.curr_width); // Next, we need to unlink the first node after `left`, // and calculate width. // Idea: count is how many elements popped over, curr_width // is how far we've traveled so far. // _ // -inf -> ... // -inf -> 1 -> ... // -inf -> 1 -> 2 -> 3 -> ... // ~ ~ ~ // width_over_removed = count(_) - count(~) = 2 // new_width = Node<1>.width - width_over_removed let width_over_removed = count - row_end.curr_width; let new_width = unsafe { (*row_end.curr_node).width - width_over_removed }; // Now, surgically remove this stretch of nodes. unsafe { let mut start_garbage = (*left).right.unwrap(); (*left).right = (*row_end.curr_node).right; (*left).width = new_width; (*row_end.curr_node).right = None; // We're at the bottom, so lets grab our return values. if start_garbage.as_ref().down.is_none() { let mut curr_node = start_garbage.as_ptr(); loop { ret.push((*curr_node).value.get_value().clone()); curr_node = match (*curr_node).right { Some(right) => right.as_ptr(), None => break, }; } } start_garbage.as_mut().clear_right(); drop(Box::from_raw(start_garbage.as_ptr())); } } self.len -= count; ret } /// Left-Biased iterator towards `item`. /// /// Returns all possible positions *left* where `item` /// is or should be in the skiplist. #[inline] fn iter_left<'a>(&'a self, item: &'a T) -> impl Iterator<Item = *mut Node<T>> + 'a { LeftBiasIter::new(self.top_left.as_ptr(), item) } /// Iterator over all elements in the Skiplist. /// /// This runs in `O(n)` time. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::new(); /// sk.insert(0usize); /// sk.insert(1usize); /// sk.insert(2usize); /// for item in sk.iter_all() { /// println!("{:?}", item); /// } /// ``` #[inline] pub fn iter_all(&self) -> IterAll<T> { unsafe { IterAll::new(self.top_left.as_ref(), self.len) } } /// Iterator over an inclusive range of elements in the SkipList. /// /// This runs in `O(logn + k)`, where k is the width of range. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::new(); /// for item in 0..100 { /// sk.insert(item); /// } /// /// for item in sk.range(&20, &40) { /// println!("{}", item); // First prints 20, then 21, ... and finally 40. /// } /// ``` #[inline] pub fn range<'a>(&'a self, start: &'a T, end: &'a T) -> SkipListRange<'a, T> { SkipListRange::new(unsafe { self.top_left.as_ref() }, start, end) } /// Iterate over a range of indices. /// /// This runs in `O(logn + k)`, where k is the width of range. /// /// This is different than `SkipList::range` as this operates on indices and not values. /// /// # Example /// /// ```rust /// use convenient_skiplist::SkipList; /// let mut sk = SkipList::new(); /// for c in 'a'..'z' { /// sk.insert(c); /// } /// /// for item in sk.index_range(0..5) { /// println!("{}", item); // Prints a, b, c, d, e /// } /// ``` pub fn index_range<R: RangeBounds<usize>>(&self, range: R) -> SkipListIndexRange<'_, R, T> { SkipListIndexRange::new(unsafe { self.top_left.as_ref() }, range) } /// Iterator over an inclusive range of elements in the SkipList, /// as defined by the `inclusive_fn`. /// /// This runs in `O(logn + k)`, where k is the width of range. /// /// As the skiplist is ordered in an ascending way, `inclusive_fn` should be /// structured with the idea in mind that you're going to see the smallest elements /// first. `inclusive_fn` should be designed to extract a *single contiguous /// stretch of elements*. /// /// This iterator will find the smallest element in the range, /// and then return elements until it finds the first element /// larger than the range. /// /// If multiple ranges are desired, you can use `range_with` multiple times, /// and simply use the last element of the previous run as the start of /// the next run. /// /// # Example /// /// ```rust /// use convenient_skiplist::{RangeHint, SkipList}; /// let mut sk = SkipList::new(); /// for item in 0..100 { /// sk.insert(item); /// } /// /// let desired_range = sk.range_with(|&ele| { /// if ele <= 5 { /// RangeHint::SmallerThanRange /// } else if ele <= 30 { /// RangeHint::InRange /// } else { /// RangeHint::LargerThanRange /// } /// }); /// for item in desired_range { /// println!("{}", item); // First prints 6, then 7, ... and finally 30. /// } /// ``` #[inline] pub fn range_with<F>(&self, inclusive_fn: F) -> IterRangeWith<T, F> where F: Fn(&T) -> RangeHint, { IterRangeWith::new(unsafe { self.top_left.as_ref() }, inclusive_fn) } /// Clear (deallocate all entries in) the skiplist. /// /// Returns the number of elements removed (length of bottom row). /// /// # Example /// /// ```rust /// use convenient_skiplist::{RangeHint, SkipList}; /// let mut sk = SkipList::from(0..10); /// assert_eq!(sk.clear(), 10); /// assert_eq!(sk, SkipList::new()); /// /// ``` pub fn clear(&mut self) -> usize { let removed = self.len(); *self = SkipList::new(); removed } #[inline] fn path_to<'a>(&self, item: &'a T) -> LeftBiasIterWidth<'a, T> { LeftBiasIterWidth::new(self.top_left.as_ptr(), item) } #[inline] fn insert_path(&mut self, item: &T) -> Vec<NodeWidth<T>> { self.path_to(item).collect() } fn pos_neg_pair(width: usize) -> NonNull<Node<T>> { let right = Box::new(Node { right: None, down: None, value: NodeValue::PosInf, width: 1, }); unsafe { let left = Box::new(Node { right: Some(NonNull::new_unchecked(Box::into_raw(right))), down: None, value: NodeValue::NegInf, width, }); NonNull::new_unchecked(Box::into_raw(left)) } } fn make_node(value: T, width: usize) -> NonNull<Node<T>> { unsafe { let node = Box::new(Node { right: None, down: None, value: NodeValue::Value(value), width, }); NonNull::new_unchecked(Box::into_raw(node)) } } #[cfg(debug_assertions)] fn ensure_columns_same_value(&self) { let mut left_row = self.top_left; let mut curr_node = self.top_left; unsafe { loop { while let Some(right) = curr_node.as_ref().right { let curr_value = &curr_node.as_ref().value; let mut curr_down = curr_node; while let Some(down) = curr_down.as_ref().down { assert!(&down.as_ref().value == curr_value); curr_down = down; } curr_node = right; } // Now, move a an entire row down. if let Some(down) = left_row.as_ref().down { left_row = down; curr_node = left_row; } else { break; } } } } #[cfg(debug_assertions)] fn ensure_rows_ordered(&self) { let mut left_row = self.top_left; let mut curr_node = self.top_left; unsafe { loop { while let Some(right) = curr_node.as_ref().right { assert!(curr_node.as_ref().value < right.as_ref().value); curr_node = right; } if let Some(down) = left_row.as_ref().down { left_row = down; curr_node = left_row; } else { break; } } } } #[cfg(debug_assertions)] fn ensure_rows_sum_len(&self) { let mut left_row = self.top_left; let mut curr_node = self.top_left; unsafe { loop { let mut curr_sum = 0; while let Some(right) = curr_node.as_ref().right { curr_sum += curr_node.as_ref().width; curr_node = right; } if let Some(down) = left_row.as_ref().down { assert_eq!(self.len(), curr_sum - 1); left_row = down; curr_node = left_row; } else { break; } } } } #[cfg(debug_assertions)] fn ensure_invariants(&self) { unsafe { assert!(self.top_left.as_ref().right.unwrap().as_ref().value == NodeValue::PosInf) } self.ensure_rows_ordered(); self.ensure_columns_same_value(); self.ensure_rows_sum_len(); } } #[cfg(test)] mod tests { use crate::SkipList; use std::collections::HashSet; #[test] fn insert_no_panic() { let mut sl = SkipList::new(); for i in &[10, 30, 50, 5, 0, 3] { sl.insert(*i); assert!(sl.contains(&i)); } #[cfg(debug_assertions)] sl.ensure_invariants(); } #[test] fn test_remove() { let mut sl = SkipList::new(); sl.insert(0usize); assert!(sl.remove(&0)); assert!(!sl.remove(&0)); assert!(!sl.contains(&0)); sl.insert(0); sl.insert(1); sl.insert(2); assert!(sl.remove(&1)); assert!(!sl.contains(&1)); sl.remove(&2); assert!(!sl.contains(&2)); } #[test] fn test_inclusive_range() { let mut sl = SkipList::new(); let values: &[i32] = &[10, 30, 50, 5, 0, 3]; for i in &[10, 30, 50, 5, 0, 3] { sl.insert(*i); assert!(sl.contains(&i)); } let lower = 3; let upper = 30; let v: HashSet<i32> = sl.range(&lower, &upper).cloned().collect(); for expected_value in values.iter().filter(|&&i| lower <= i && i <= upper) { assert!(v.contains(expected_value)); } let right_empty: HashSet<i32> = sl.range(&100, &1000).cloned().collect(); assert!(right_empty.is_empty()); let left_empty: HashSet<i32> = sl.range(&-2, &-1).cloned().collect(); assert!(left_empty.is_empty()); // Excessive range let lower = -10; let upper = 1000; let v: HashSet<i32> = sl.range(&lower, &upper).cloned().collect(); for expected_value in values.iter().filter(|&&i| lower <= i && i <= upper) { assert!(v.contains(expected_value)); } } #[test] fn test_len() { let mut sl = SkipList::new(); assert_eq!(sl.len(), 0); assert!(sl.is_empty()); sl.insert(0); assert_eq!(sl.len(), 1); assert!(!sl.is_empty()); sl.insert(0); assert_eq!(sl.len(), 1); sl.insert(1); assert_eq!(sl.len(), 2); sl.remove(&1); assert_eq!(sl.len(), 1); sl.remove(&1); assert_eq!(sl.len(), 1); sl.remove(&0); assert_eq!(sl.len(), 0); sl.remove(&0); assert_eq!(sl.len(), 0); } #[test] fn test_eq() { let mut s0 = SkipList::new(); let mut s1 = SkipList::new(); assert!(s0 == s1); s0.insert(0); assert!(s0 != s1); s1.insert(1); assert!(s0 != s1); s0.insert(1); s1.insert(0); assert!(s0 == s1); s0.insert(2); s0.insert(3); assert!(s0 != s1); } #[test] fn test_from() { let values = vec![1usize, 2, 3]; let sk = SkipList::from(values.clone().into_iter()); assert_eq!(sk.iter_all().cloned().collect::<Vec<_>>(), values); let values: Vec<usize> = (0..10).collect(); let sk = SkipList::from(0..10); assert_eq!(sk.iter_all().cloned().collect::<Vec<_>>(), values); } #[test] fn test_index_of() { let mut sk = SkipList::new(); sk.insert(1); sk.insert(2); sk.insert(3); assert_eq!(sk.index_of(&1), Some(0)); assert_eq!(sk.index_of(&2), Some(1)); assert_eq!(sk.index_of(&3), Some(2)); assert_eq!(sk.index_of(&999), None); let sk = SkipList::new(); assert_eq!(sk.index_of(&0), None); assert_eq!(sk.index_of(&999), None); } #[test] fn test_at_index() { let sk = SkipList::from(0..10); for i in 0..10 { assert_eq!(Some(&i), sk.at_index(i)); } assert_eq!(None, sk.at_index(11)); let mut sk = SkipList::new(); sk.insert('a'); sk.insert('b'); sk.insert('c'); assert_eq!(Some(&'a'), sk.at_index(0)); assert_eq!(Some(&'b'), sk.at_index(1)); assert_eq!(Some(&'c'), sk.at_index(2)); assert_eq!(None, sk.at_index(3)); assert_eq!('a', sk[0]); assert_eq!('b', sk[1]); assert_eq!('c', sk[2]); } #[test] #[should_panic] fn test_bad_index() { let sk = SkipList::from(0..10); sk[sk.len()]; } #[test] fn test_pop_max() { let mut sk = SkipList::from(0..10); assert_eq!(Some(&7), sk.at_index(7)); assert_eq!(vec![7, 8, 9], sk.pop_max(3)); assert_eq!(vec![6], sk.pop_max(1)); assert_eq!(vec![4, 5], sk.pop_max(2)); assert_eq!(vec![0, 1, 2, 3], sk.pop_max(5)); let mut sk = SkipList::from(0..3); assert_eq!(vec![2], sk.pop_max(1)); let mut sk: SkipList<u32> = SkipList::new(); let v: Vec<u32> = Vec::new(); assert_eq!(v, sk.pop_max(1)); } #[test] fn test_pop_min() { let mut sk = SkipList::from(0..10); assert_eq!(vec![0, 1, 2], sk.pop_min(3)); assert_eq!(vec![3], sk.pop_min(1)); assert_eq!(vec![4, 5], sk.pop_min(2)); assert_eq!(vec![6, 7, 8, 9], sk.pop_min(5)); let v: Vec<u32> = Vec::new(); assert_eq!(v, sk.pop_min(1)); } #[test] fn test_clone() { let sk = SkipList::from(0..30); let clone = sk.clone(); assert_eq!(sk, clone); assert!(!std::ptr::eq(&sk, &clone)); // Empty case let sk = SkipList::from(0..0); let clone = sk.clone(); assert_eq!( sk, clone, "Empty skiplists should clone nicely, {:?} != {:?}", sk, clone ); } #[test] fn test_peek() { let sk = SkipList::from(0..10); assert_eq!(Some(&0), sk.peek_first()); assert_eq!(Some(&9), sk.peek_last()); } #[test] fn test_vec_from() { let sk: SkipList<u32> = SkipList::from(0..4); assert_eq!(vec![0, 1, 2, 3], Vec::from(sk)); } #[test] fn test_more_complex_type() { // A bit of history behind this test: // I tried to avoid cloning by using std::ptr::read // but you double free as you're copying the string struct // and dropping the original. So you end up with double frees. let mut string_sk = SkipList::new(); for c in b'a'..b'z' { string_sk.insert((c as char).to_string()); } string_sk.pop_back(); } }