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
//! This method defines several access methods for [`AATreeMap`].
use super::{AATreeMap, Entry, KeyValue, OccupiedEntry, VacantEntry};
use crate::node::TraverseStep;
use core::{borrow::Borrow, cmp::Ordering, fmt::Debug};
impl<K, V> AATreeMap<K, V> {
fn kv<Q>(&self, key: &Q) -> Option<&KeyValue<K, V>>
where
K: Ord + Borrow<Q>,
Q: Ord + ?Sized
{
self.root.traverse(
|content| match key.cmp(content.key.borrow()) {
Ordering::Equal => TraverseStep::Value(Some(content)),
Ordering::Less => TraverseStep::Left,
Ordering::Greater => TraverseStep::Right
},
|_, sub| sub
)
}
/// Returns a reference to the value corresponding to the key.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// map.insert(1, "a");
/// assert_eq!(map.get(&1), Some(&"a"));
/// assert_eq!(map.get(&2), None);
/// ```
pub fn get<Q>(&self, key: &Q) -> Option<&V>
where
K: Ord + Borrow<Q>,
Q: Ord + ?Sized
{
self.kv(key).map(|kv| &kv.value)
}
/// Returns a reference to the key and value corresponding to the key.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// map.insert(1, "a");
/// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
/// assert_eq!(map.get_key_value(&2), None);
/// ```
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where
K: Ord + Borrow<Q>,
Q: Ord + ?Sized
{
self.kv(key).map(|kv| kv.as_tuple())
}
fn kv_mut<Q>(&mut self, key: &Q) -> Option<&mut KeyValue<K, V>>
where
K: Ord + Borrow<Q>,
Q: Ord + ?Sized
{
let mut traverse = self.root.traverse_mut()?;
loop {
let cmp: Ordering = traverse.peek().key.borrow().cmp(key);
match cmp {
Ordering::Greater => traverse = traverse.turn_left().ok()?,
Ordering::Less => traverse = traverse.turn_right().ok()?,
Ordering::Equal => return Some(traverse.into_content())
}
}
}
/// Gets the given key's corresponding entry, allowing for in-place manipulation of
/// the entry as well as inserting an entry with that key if it does not exist yet.
///
/// # Example
///
/// ```rust
/// # use aatree::{AATreeMap, map::Entry};
/// let mut map = AATreeMap::new();
/// let entry = map.entry(1);
/// assert!(matches!(entry, Entry::Vacant(_)));
/// entry.or_insert('a');
/// assert_eq!(map.get(&1), Some(&'a'));
///
/// let entry = map.entry(1);
/// assert!(matches!(entry, Entry::Occupied(_)));
/// entry.and_modify(|value| *value = 'b');
/// assert_eq!(map.get(&1), Some(&'b'));
/// ```
#[allow(unsafe_code)]
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
where
K: Ord
{
// This is a known limitation of the borrow checker in Rust:
// https://blog.rust-lang.org/2022/08/05/nll-by-default.html#looking-forward-what-can-we-expect-for-the-borrow-checker-of-the-future
match unsafe { &mut *(self as *mut Self) }.kv_mut(&key) {
Some(kv) => Entry::Occupied(OccupiedEntry { entry: kv }),
None => Entry::Vacant(VacantEntry { key, map: self })
}
}
/// Returns a mutable reference to the value corresponding to the key.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// map.insert(1, "a");
/// assert_eq!(map.get(&1), Some(&"a"));
/// *map.get_mut(&1).unwrap() = "b";
/// assert_eq!(map.get(&1), Some(&"b"));
/// ```
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where
K: Ord + Borrow<Q>,
Q: Ord + ?Sized
{
self.kv_mut(key).map(|kv| &mut kv.value)
}
/// Gets the first entry (that is, with the smallest key) in the map, allowing for
/// in-place manipulation of the entry.
///
/// # Example
///
/// ```rust
/// # use aatree::{AATreeMap, map::Entry};
/// let mut map = AATreeMap::new();
/// let entry = map.first_entry();
/// assert!(entry.is_none());
///
/// map.insert(1, 'a');
/// map.insert(3, 'c');
/// println!("{map:?}");
///
/// let Some(mut entry) = map.first_entry() else { unreachable!() };
/// *entry.get_mut() = 'b';
/// assert_eq!(map.get(&1), Some(&'b'));
/// assert_eq!(map.get(&3), Some(&'c'));
/// ```
pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
where
K: Ord + Debug,
V: Debug
{
let mut traverse = self.root.traverse_mut()?;
while traverse.has_left_child() {
traverse = traverse.turn_left().unwrap();
}
Some(OccupiedEntry {
entry: traverse.into_content()
})
}
/// Returns a reference to the first entry (that is, with the smallest key) in the map.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// assert_eq!(map.first_key_value(), None);
/// map.insert(3, "a");
/// map.insert(1, "b");
/// map.insert(2, "c");
/// assert_eq!(map.first_key_value(), Some((&1, &"b")));
/// ```
pub fn first_key_value(&self) -> Option<(&K, &V)>
where
K: Ord
{
self.root.traverse(
|_| TraverseStep::Left,
|content, sub| sub.or_else(|| Some(content.as_tuple()))
)
}
/// Returns and removes the first entry (that is, with the smallest key) in the map.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// assert_eq!(map.pop_first(), None);
/// map.insert(3, "a");
/// map.insert(1, "b");
/// map.insert(2, "c");
/// assert_eq!(map.pop_first(), Some((1, "b")));
/// assert_eq!(map.pop_first(), Some((2, "c")));
/// assert_eq!(map.pop_first(), Some((3, "a")));
/// assert_eq!(map.pop_first(), None);
/// ```
pub fn pop_first(&mut self) -> Option<(K, V)>
where
K: Ord
{
self.root.remove_successor().map(KeyValue::into_tuple)
}
/// Gets the last entry (that is, with the largest key) in the map, allowing for
/// in-place manipulation of the entry.
///
/// # Example
///
/// ```rust
/// # use aatree::{AATreeMap, map::Entry};
/// let mut map = AATreeMap::new();
/// let entry = map.last_entry();
/// assert!(entry.is_none());
///
/// map.insert(1, 'a');
/// map.insert(3, 'c');
///
/// let Some(mut entry) = map.last_entry() else { unreachable!() };
/// *entry.get_mut() = 'b';
/// assert_eq!(map.get(&1), Some(&'a'));
/// assert_eq!(map.get(&3), Some(&'b'));
/// ```
pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
where
K: Ord
{
let mut traverse = self.root.traverse_mut()?;
while traverse.has_right_child() {
traverse = traverse.turn_right().unwrap();
}
Some(OccupiedEntry {
entry: traverse.into_content()
})
}
/// Returns a reference to the last entry (that is, with the largest key) in the map.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// assert_eq!(map.last_key_value(), None);
/// map.insert(1, "a");
/// map.insert(3, "b");
/// map.insert(2, "c");
/// assert_eq!(map.last_key_value(), Some((&3, &"b")));
/// ```
pub fn last_key_value(&self) -> Option<(&K, &V)>
where
K: Ord
{
self.root.traverse(
|_| TraverseStep::Right,
|content, sub| sub.or_else(|| Some(content.as_tuple()))
)
}
/// Returns and removes the last entry (that is, with the largest key) in the map.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// assert_eq!(map.pop_last(), None);
/// map.insert(1, "a");
/// map.insert(3, "b");
/// map.insert(2, "c");
/// assert_eq!(map.pop_last(), Some((3, "b")));
/// assert_eq!(map.pop_last(), Some((2, "c")));
/// assert_eq!(map.pop_last(), Some((1, "a")));
/// assert_eq!(map.pop_last(), None);
/// ```
pub fn pop_last(&mut self) -> Option<(K, V)>
where
K: Ord
{
self.root.remove_predecessor().map(KeyValue::into_tuple)
}
pub fn pop_largest(&mut self) -> Option<(K, V)>
where
K: Ord
{
self.pop_last()
}
/// Returns a reference to the first entry with a key greater than or equal to `k` in
/// the map.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// assert_eq!(map.first_key_value_at_or_after(&15), None);
/// map.insert(10, "a");
/// map.insert(30, "b");
/// map.insert(20, "c");
/// assert_eq!(map.first_key_value_at_or_after(&15), Some((&20, &"c")));
/// ```
pub fn first_key_value_at_or_after<Q>(&self, k: &Q) -> Option<(&K, &V)>
where
K: Borrow<Q> + Ord,
Q: Ord + ?Sized
{
self.root.traverse(
|content| match content.key.borrow().cmp(k) {
Ordering::Greater => TraverseStep::Left,
Ordering::Less => TraverseStep::Right,
Ordering::Equal => TraverseStep::Value(Some(content.as_tuple()))
},
|content, sub| match sub {
None if content.key.borrow() > k => Some(content.as_tuple()),
sub => sub
}
)
}
/// Returns a mutable reference to the first entry with a key greater than or equal
/// to `k` in the map.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// assert_eq!(map.first_key_value_mut_at_or_after(&15), None);
/// map.insert(10, "a");
/// map.insert(30, "b");
/// map.insert(20, "c");
/// let (_, value) = map.first_key_value_mut_at_or_after(&15).unwrap();
/// assert_eq!(*value, "c");
/// *value = "d";
/// assert_eq!(map.first_key_value_at_or_after(&15), Some((&20, &"d")));
/// ```
pub fn first_key_value_mut_at_or_after<Q>(&mut self, k: &Q) -> Option<(&K, &mut V)>
where
K: Borrow<Q> + Ord,
Q: Ord + ?Sized
{
let mut traverse = self.root.traverse_mut()?;
loop {
match traverse.peek().key.borrow().cmp(k) {
Ordering::Greater
if traverse
.peek_left_child()
.map(|left| left.key.borrow() >= k)
.unwrap_or(false) =>
{
traverse = traverse.turn_left().unwrap()
},
Ordering::Less => traverse = traverse.turn_right().ok()?,
_ => return Some(traverse.into_content().as_tuple_mut())
}
}
}
/// Returns a reference to the last entry with a key smaller than or equal to `k` in
/// the map.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// assert_eq!(map.last_key_value_at_or_before(&25), None);
/// map.insert(10, "a");
/// map.insert(30, "b");
/// map.insert(20, "c");
/// assert_eq!(map.last_key_value_at_or_before(&25), Some((&20, &"c")));
/// ```
pub fn last_key_value_at_or_before<Q>(&self, k: &Q) -> Option<(&K, &V)>
where
K: Borrow<Q> + Ord,
Q: Ord + ?Sized
{
self.root.traverse(
|content| match content.key.borrow().cmp(k) {
Ordering::Greater => TraverseStep::Left,
Ordering::Less => TraverseStep::Right,
Ordering::Equal => TraverseStep::Value(Some(content.as_tuple()))
},
|content, sub| match sub {
None if content.key.borrow() < k => Some(content.as_tuple()),
sub => sub
}
)
}
/// Returns a mutable reference to the last entry with a key smaller than or equal to
/// `k` in the map.
///
/// # Example
///
/// ```rust
/// # use aatree::AATreeMap;
/// let mut map = AATreeMap::new();
/// assert_eq!(map.last_key_value_mut_at_or_before(&25), None);
/// map.insert(10, "a");
/// map.insert(30, "b");
/// map.insert(20, "c");
/// let (_, value) = map.last_key_value_mut_at_or_before(&25).unwrap();
/// assert_eq!(*value, "c");
/// *value = "d";
/// assert_eq!(map.last_key_value_at_or_before(&25), Some((&20, &"d")));
/// ```
pub fn last_key_value_mut_at_or_before<Q>(&mut self, k: &Q) -> Option<(&K, &mut V)>
where
K: Borrow<Q> + Ord + Debug,
V: Debug,
Q: Ord + ?Sized
{
let mut traverse = self.root.traverse_mut()?;
loop {
match traverse.peek().key.borrow().cmp(k) {
Ordering::Greater => traverse = traverse.turn_left().ok()?,
Ordering::Less
if traverse
.peek_right_child()
.map(|right| right.key.borrow() <= k)
.unwrap_or(false) =>
{
traverse = traverse.turn_right().unwrap()
},
_ => return Some(traverse.into_content().as_tuple_mut())
}
}
}
}
#[cfg(test)]
mod tests {
use crate::AATreeMap;
#[test]
fn test_first_key_value() {
let mut map = AATreeMap::new();
map.insert(10, "a");
map.insert(20, "b");
map.insert(30, "c");
map.insert(40, "d");
map.insert(50, "e");
map.insert(60, "f");
map.insert(70, "g");
// The tree now looks like this:
// 40
// / \
// 20 60
// /\ /\
// 10 30 50 70
// To return the correct value for 15, we need to go left once but not twice
let (key, value) = map.first_key_value_at_or_after(&15).unwrap();
assert_eq!(*key, 20);
assert_eq!(*value, "b");
let (key, value) = map.first_key_value_mut_at_or_after(&15).unwrap();
assert_eq!(*key, 20);
assert_eq!(*value, "b");
}
#[test]
fn test_last_key_value() {
let mut map = AATreeMap::new();
map.insert(10, "a");
map.insert(20, "b");
map.insert(30, "c");
map.insert(40, "d");
// The tree now looks like this:
// 20 - 30
// / \
// 10 40
// To return the correct value for 35, we need to go right once but not twice
let (key, value) = map.last_key_value_at_or_before(&35).unwrap();
assert_eq!(*key, 30);
assert_eq!(*value, "c");
let (key, value) = map.last_key_value_mut_at_or_before(&35).unwrap();
assert_eq!(*key, 30);
assert_eq!(*value, "c");
}
}