|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use std::borrow::Borrow; |
| 12 | +use std::cmp::Ordering; |
| 13 | +use std::convert::From; |
| 14 | +use std::mem; |
| 15 | +use std::ops::{RangeBounds, Bound, Index, IndexMut}; |
| 16 | + |
| 17 | +/// `SortedMap` is a data structure with similar characteristics as BTreeMap but |
| 18 | +/// slightly different trade-offs: lookup, inseration, and removal are O(log(N)) |
| 19 | +/// and elements can be iterated in order cheaply. |
| 20 | +/// |
| 21 | +/// `SortedMap` can be faster than a `BTreeMap` for small sizes (<50) since it |
| 22 | +/// stores data in a more compact way. It also supports accessing contiguous |
| 23 | +/// ranges of elements as a slice, and slices of already sorted elements can be |
| 24 | +/// inserted efficiently. |
| 25 | +#[derive(Clone, PartialEq, Eq, Hash, Default, Debug, RustcEncodable, RustcDecodable)] |
| 26 | +pub struct SortedMap<K: Ord, V> { |
| 27 | + data: Vec<(K,V)> |
| 28 | +} |
| 29 | + |
| 30 | +impl<K: Ord, V> SortedMap<K, V> { |
| 31 | + |
| 32 | + #[inline] |
| 33 | + pub fn new() -> SortedMap<K, V> { |
| 34 | + SortedMap { |
| 35 | + data: vec![] |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// Construct a `SortedMap` from a presorted set of elements. This is faster |
| 40 | + /// than creating an empty map and then inserting the elements individually. |
| 41 | + /// |
| 42 | + /// It is up to the caller to make sure that the elements are sorted by key |
| 43 | + /// and that there are no duplicates. |
| 44 | + #[inline] |
| 45 | + pub fn from_presorted_elements(elements: Vec<(K, V)>) -> SortedMap<K, V> |
| 46 | + { |
| 47 | + debug_assert!(elements.windows(2).all(|w| w[0].0 < w[1].0)); |
| 48 | + |
| 49 | + SortedMap { |
| 50 | + data: elements |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + #[inline] |
| 55 | + pub fn insert(&mut self, key: K, mut value: V) -> Option<V> { |
| 56 | + match self.lookup_index_for(&key) { |
| 57 | + Ok(index) => { |
| 58 | + let mut slot = unsafe { |
| 59 | + self.data.get_unchecked_mut(index) |
| 60 | + }; |
| 61 | + mem::swap(&mut slot.1, &mut value); |
| 62 | + Some(value) |
| 63 | + } |
| 64 | + Err(index) => { |
| 65 | + self.data.insert(index, (key, value)); |
| 66 | + None |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + #[inline] |
| 72 | + pub fn remove(&mut self, key: &K) -> Option<V> { |
| 73 | + match self.lookup_index_for(key) { |
| 74 | + Ok(index) => { |
| 75 | + Some(self.data.remove(index).1) |
| 76 | + } |
| 77 | + Err(_) => { |
| 78 | + None |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + #[inline] |
| 84 | + pub fn get(&self, key: &K) -> Option<&V> { |
| 85 | + match self.lookup_index_for(key) { |
| 86 | + Ok(index) => { |
| 87 | + unsafe { |
| 88 | + Some(&self.data.get_unchecked(index).1) |
| 89 | + } |
| 90 | + } |
| 91 | + Err(_) => { |
| 92 | + None |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + #[inline] |
| 98 | + pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { |
| 99 | + match self.lookup_index_for(key) { |
| 100 | + Ok(index) => { |
| 101 | + unsafe { |
| 102 | + Some(&mut self.data.get_unchecked_mut(index).1) |
| 103 | + } |
| 104 | + } |
| 105 | + Err(_) => { |
| 106 | + None |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + #[inline] |
| 112 | + pub fn clear(&mut self) { |
| 113 | + self.data.clear(); |
| 114 | + } |
| 115 | + |
| 116 | + /// Iterate over elements, sorted by key |
| 117 | + #[inline] |
| 118 | + pub fn iter(&self) -> ::std::slice::Iter<(K, V)> { |
| 119 | + self.data.iter() |
| 120 | + } |
| 121 | + |
| 122 | + /// Iterate over the keys, sorted |
| 123 | + #[inline] |
| 124 | + pub fn keys(&self) -> impl Iterator<Item=&K> + ExactSizeIterator { |
| 125 | + self.data.iter().map(|&(ref k, _)| k) |
| 126 | + } |
| 127 | + |
| 128 | + /// Iterate over values, sorted by key |
| 129 | + #[inline] |
| 130 | + pub fn values(&self) -> impl Iterator<Item=&V> + ExactSizeIterator { |
| 131 | + self.data.iter().map(|&(_, ref v)| v) |
| 132 | + } |
| 133 | + |
| 134 | + #[inline] |
| 135 | + pub fn len(&self) -> usize { |
| 136 | + self.data.len() |
| 137 | + } |
| 138 | + |
| 139 | + #[inline] |
| 140 | + pub fn range<R>(&self, range: R) -> &[(K, V)] |
| 141 | + where R: RangeBounds<K> |
| 142 | + { |
| 143 | + let (start, end) = self.range_slice_indices(range); |
| 144 | + (&self.data[start .. end]) |
| 145 | + } |
| 146 | + |
| 147 | + #[inline] |
| 148 | + pub fn remove_range<R>(&mut self, range: R) |
| 149 | + where R: RangeBounds<K> |
| 150 | + { |
| 151 | + let (start, end) = self.range_slice_indices(range); |
| 152 | + self.data.splice(start .. end, ::std::iter::empty()); |
| 153 | + } |
| 154 | + |
| 155 | + /// Mutate all keys with the given function `f`. This mutation must not |
| 156 | + /// change the sort-order of keys. |
| 157 | + #[inline] |
| 158 | + pub fn offset_keys<F>(&mut self, f: F) |
| 159 | + where F: Fn(&mut K) |
| 160 | + { |
| 161 | + self.data.iter_mut().map(|&mut (ref mut k, _)| k).for_each(f); |
| 162 | + } |
| 163 | + |
| 164 | + /// Inserts a presorted range of elements into the map. If the range can be |
| 165 | + /// inserted as a whole in between to existing elements of the map, this |
| 166 | + /// will be faster than inserting the elements individually. |
| 167 | + /// |
| 168 | + /// It is up to the caller to make sure that the elements are sorted by key |
| 169 | + /// and that there are no duplicates. |
| 170 | + #[inline] |
| 171 | + pub fn insert_presorted(&mut self, mut elements: Vec<(K, V)>) { |
| 172 | + if elements.is_empty() { |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + debug_assert!(elements.windows(2).all(|w| w[0].0 < w[1].0)); |
| 177 | + |
| 178 | + let start_index = self.lookup_index_for(&elements[0].0); |
| 179 | + |
| 180 | + let drain = match start_index { |
| 181 | + Ok(index) => { |
| 182 | + let mut drain = elements.drain(..); |
| 183 | + self.data[index] = drain.next().unwrap(); |
| 184 | + drain |
| 185 | + } |
| 186 | + Err(index) => { |
| 187 | + if index == self.data.len() || |
| 188 | + elements.last().unwrap().0 < self.data[index].0 { |
| 189 | + // We can copy the whole range without having to mix with |
| 190 | + // existing elements. |
| 191 | + self.data.splice(index .. index, elements.drain(..)); |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + let mut drain = elements.drain(..); |
| 196 | + self.data.insert(index, drain.next().unwrap()); |
| 197 | + drain |
| 198 | + } |
| 199 | + }; |
| 200 | + |
| 201 | + // Insert the rest |
| 202 | + for (k, v) in drain { |
| 203 | + self.insert(k, v); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /// Looks up the key in `self.data` via `slice::binary_search()`. |
| 208 | + #[inline(always)] |
| 209 | + fn lookup_index_for(&self, key: &K) -> Result<usize, usize> { |
| 210 | + self.data.binary_search_by(|&(ref x, _)| x.cmp(key)) |
| 211 | + } |
| 212 | + |
| 213 | + #[inline] |
| 214 | + fn range_slice_indices<R>(&self, range: R) -> (usize, usize) |
| 215 | + where R: RangeBounds<K> |
| 216 | + { |
| 217 | + let start = match range.start() { |
| 218 | + Bound::Included(ref k) => { |
| 219 | + match self.lookup_index_for(k) { |
| 220 | + Ok(index) | Err(index) => index |
| 221 | + } |
| 222 | + } |
| 223 | + Bound::Excluded(ref k) => { |
| 224 | + match self.lookup_index_for(k) { |
| 225 | + Ok(index) => index + 1, |
| 226 | + Err(index) => index, |
| 227 | + } |
| 228 | + } |
| 229 | + Bound::Unbounded => 0, |
| 230 | + }; |
| 231 | + |
| 232 | + let end = match range.end() { |
| 233 | + Bound::Included(ref k) => { |
| 234 | + match self.lookup_index_for(k) { |
| 235 | + Ok(index) => index + 1, |
| 236 | + Err(index) => index, |
| 237 | + } |
| 238 | + } |
| 239 | + Bound::Excluded(ref k) => { |
| 240 | + match self.lookup_index_for(k) { |
| 241 | + Ok(index) | Err(index) => index, |
| 242 | + } |
| 243 | + } |
| 244 | + Bound::Unbounded => self.data.len(), |
| 245 | + }; |
| 246 | + |
| 247 | + (start, end) |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +impl<K: Ord, V> IntoIterator for SortedMap<K, V> { |
| 252 | + type Item = (K, V); |
| 253 | + type IntoIter = ::std::vec::IntoIter<(K, V)>; |
| 254 | + fn into_iter(self) -> Self::IntoIter { |
| 255 | + self.data.into_iter() |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +impl<K: Ord, V, Q: Borrow<K>> Index<Q> for SortedMap<K, V> { |
| 260 | + type Output = V; |
| 261 | + fn index(&self, index: Q) -> &Self::Output { |
| 262 | + let k: &K = index.borrow(); |
| 263 | + self.get(k).unwrap() |
| 264 | + } |
| 265 | +} |
| 266 | + |
| 267 | +impl<K: Ord, V, Q: Borrow<K>> IndexMut<Q> for SortedMap<K, V> { |
| 268 | + fn index_mut(&mut self, index: Q) -> &mut Self::Output { |
| 269 | + let k: &K = index.borrow(); |
| 270 | + self.get_mut(k).unwrap() |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +impl<K: Ord, V, I: Iterator<Item=(K, V)>> From<I> for SortedMap<K, V> { |
| 275 | + fn from(data: I) -> Self { |
| 276 | + let mut data: Vec<(K, V)> = data.collect(); |
| 277 | + data.sort_unstable_by(|&(ref k1, _), &(ref k2, _)| k1.cmp(k2)); |
| 278 | + data.dedup_by(|&mut (ref k1, _), &mut (ref k2, _)| { |
| 279 | + k1.cmp(k2) == Ordering::Equal |
| 280 | + }); |
| 281 | + SortedMap { |
| 282 | + data |
| 283 | + } |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +#[cfg(test)] |
| 288 | +mod tests { |
| 289 | + use super::SortedMap; |
| 290 | + |
| 291 | + #[test] |
| 292 | + fn test_insert_and_iter() { |
| 293 | + let mut map = SortedMap::new(); |
| 294 | + let mut expected = Vec::new(); |
| 295 | + |
| 296 | + for x in 0 .. 100 { |
| 297 | + assert_eq!(map.iter().cloned().collect::<Vec<_>>(), expected); |
| 298 | + |
| 299 | + let x = 1000 - x * 2; |
| 300 | + map.insert(x, x); |
| 301 | + expected.insert(0, (x, x)); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + #[test] |
| 306 | + fn test_get_and_index() { |
| 307 | + let mut map = SortedMap::new(); |
| 308 | + let mut expected = Vec::new(); |
| 309 | + |
| 310 | + for x in 0 .. 100 { |
| 311 | + let x = 1000 - x; |
| 312 | + if x & 1 == 0 { |
| 313 | + map.insert(x, x); |
| 314 | + } |
| 315 | + expected.push(x); |
| 316 | + } |
| 317 | + |
| 318 | + for mut x in expected { |
| 319 | + if x & 1 == 0 { |
| 320 | + assert_eq!(map.get(&x), Some(&x)); |
| 321 | + assert_eq!(map.get_mut(&x), Some(&mut x)); |
| 322 | + assert_eq!(map[&x], x); |
| 323 | + assert_eq!(&mut map[&x], &mut x); |
| 324 | + } else { |
| 325 | + assert_eq!(map.get(&x), None); |
| 326 | + assert_eq!(map.get_mut(&x), None); |
| 327 | + } |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + #[test] |
| 332 | + fn test_range() { |
| 333 | + let mut map = SortedMap::new(); |
| 334 | + map.insert(1, 1); |
| 335 | + map.insert(3, 3); |
| 336 | + map.insert(6, 6); |
| 337 | + map.insert(9, 9); |
| 338 | + |
| 339 | + let keys = |s: &[(_, _)]| { |
| 340 | + s.into_iter().map(|e| e.0).collect::<Vec<u32>>() |
| 341 | + }; |
| 342 | + |
| 343 | + for start in 0 .. 11 { |
| 344 | + for end in 0 .. 11 { |
| 345 | + if end < start { |
| 346 | + continue |
| 347 | + } |
| 348 | + |
| 349 | + let mut expected = vec![1, 3, 6, 9]; |
| 350 | + expected.retain(|&x| x >= start && x < end); |
| 351 | + |
| 352 | + assert_eq!(keys(map.range(start..end)), expected, "range = {}..{}", start, end); |
| 353 | + } |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | + |
| 358 | + #[test] |
| 359 | + fn test_offset_keys() { |
| 360 | + let mut map = SortedMap::new(); |
| 361 | + map.insert(1, 1); |
| 362 | + map.insert(3, 3); |
| 363 | + map.insert(6, 6); |
| 364 | + |
| 365 | + map.offset_keys(|k| *k += 1); |
| 366 | + |
| 367 | + let mut expected = SortedMap::new(); |
| 368 | + expected.insert(2, 1); |
| 369 | + expected.insert(4, 3); |
| 370 | + expected.insert(7, 6); |
| 371 | + |
| 372 | + assert_eq!(map, expected); |
| 373 | + } |
| 374 | + |
| 375 | + fn keys(s: SortedMap<u32, u32>) -> Vec<u32> { |
| 376 | + s.into_iter().map(|(k, _)| k).collect::<Vec<u32>>() |
| 377 | + } |
| 378 | + |
| 379 | + fn elements(s: SortedMap<u32, u32>) -> Vec<(u32, u32)> { |
| 380 | + s.into_iter().collect::<Vec<(u32, u32)>>() |
| 381 | + } |
| 382 | + |
| 383 | + #[test] |
| 384 | + fn test_remove_range() { |
| 385 | + let mut map = SortedMap::new(); |
| 386 | + map.insert(1, 1); |
| 387 | + map.insert(3, 3); |
| 388 | + map.insert(6, 6); |
| 389 | + map.insert(9, 9); |
| 390 | + |
| 391 | + for start in 0 .. 11 { |
| 392 | + for end in 0 .. 11 { |
| 393 | + if end < start { |
| 394 | + continue |
| 395 | + } |
| 396 | + |
| 397 | + let mut expected = vec![1, 3, 6, 9]; |
| 398 | + expected.retain(|&x| x < start || x >= end); |
| 399 | + |
| 400 | + let mut map = map.clone(); |
| 401 | + map.remove_range(start .. end); |
| 402 | + |
| 403 | + assert_eq!(keys(map), expected, "range = {}..{}", start, end); |
| 404 | + } |
| 405 | + } |
| 406 | + } |
| 407 | + |
| 408 | + #[test] |
| 409 | + fn test_remove() { |
| 410 | + let mut map = SortedMap::new(); |
| 411 | + let mut expected = Vec::new(); |
| 412 | + |
| 413 | + for x in 0..10 { |
| 414 | + map.insert(x, x); |
| 415 | + expected.push((x, x)); |
| 416 | + } |
| 417 | + |
| 418 | + for x in 0 .. 10 { |
| 419 | + let mut map = map.clone(); |
| 420 | + let mut expected = expected.clone(); |
| 421 | + |
| 422 | + assert_eq!(map.remove(&x), Some(x)); |
| 423 | + expected.remove(x as usize); |
| 424 | + |
| 425 | + assert_eq!(map.iter().cloned().collect::<Vec<_>>(), expected); |
| 426 | + } |
| 427 | + } |
| 428 | + |
| 429 | + #[test] |
| 430 | + fn test_insert_presorted_non_overlapping() { |
| 431 | + let mut map = SortedMap::new(); |
| 432 | + map.insert(2, 0); |
| 433 | + map.insert(8, 0); |
| 434 | + |
| 435 | + map.insert_presorted(vec![(3, 0), (7, 0)]); |
| 436 | + |
| 437 | + let expected = vec![2, 3, 7, 8]; |
| 438 | + assert_eq!(keys(map), expected); |
| 439 | + } |
| 440 | + |
| 441 | + #[test] |
| 442 | + fn test_insert_presorted_first_elem_equal() { |
| 443 | + let mut map = SortedMap::new(); |
| 444 | + map.insert(2, 2); |
| 445 | + map.insert(8, 8); |
| 446 | + |
| 447 | + map.insert_presorted(vec![(2, 0), (7, 7)]); |
| 448 | + |
| 449 | + let expected = vec![(2, 0), (7, 7), (8, 8)]; |
| 450 | + assert_eq!(elements(map), expected); |
| 451 | + } |
| 452 | + |
| 453 | + #[test] |
| 454 | + fn test_insert_presorted_last_elem_equal() { |
| 455 | + let mut map = SortedMap::new(); |
| 456 | + map.insert(2, 2); |
| 457 | + map.insert(8, 8); |
| 458 | + |
| 459 | + map.insert_presorted(vec![(3, 3), (8, 0)]); |
| 460 | + |
| 461 | + let expected = vec![(2, 2), (3, 3), (8, 0)]; |
| 462 | + assert_eq!(elements(map), expected); |
| 463 | + } |
| 464 | + |
| 465 | + #[test] |
| 466 | + fn test_insert_presorted_shuffle() { |
| 467 | + let mut map = SortedMap::new(); |
| 468 | + map.insert(2, 2); |
| 469 | + map.insert(7, 7); |
| 470 | + |
| 471 | + map.insert_presorted(vec![(1, 1), (3, 3), (8, 8)]); |
| 472 | + |
| 473 | + let expected = vec![(1, 1), (2, 2), (3, 3), (7, 7), (8, 8)]; |
| 474 | + assert_eq!(elements(map), expected); |
| 475 | + } |
| 476 | + |
| 477 | + #[test] |
| 478 | + fn test_insert_presorted_at_end() { |
| 479 | + let mut map = SortedMap::new(); |
| 480 | + map.insert(1, 1); |
| 481 | + map.insert(2, 2); |
| 482 | + |
| 483 | + map.insert_presorted(vec![(3, 3), (8, 8)]); |
| 484 | + |
| 485 | + let expected = vec![(1, 1), (2, 2), (3, 3), (8, 8)]; |
| 486 | + assert_eq!(elements(map), expected); |
| 487 | + } |
| 488 | +} |
0 commit comments