vec_map-0.8.1/.gitignore000064400000000000000000000000371327532554700133250ustar0000000000000000*~ *.swp target Cargo.lock vec_map-0.8.1/.travis.yml000064400000000000000000000010101327532554700134360ustar0000000000000000language: rust sudo: false matrix: include: - rust: stable - rust: beta - rust: nightly - rust: nightly env: FEATURES="--features eders" script: - cargo build $FEATURES - cargo test $FEATURES - cargo doc --no-deps after_success: | [ "$TRAVIS_RUST_VERSION" = nightly ] && [ "$FEATURES" = "" ] && [ "$TRAVIS_BRANCH" = master ] && [ "$TRAVIS_PULL_REQUEST" = false ] && bash deploy-docs.sh notifications: webhooks: http://huon.me:54857/travis vec_map-0.8.1/Cargo.toml.orig000064400000000000000000000033101327532557400142210ustar0000000000000000[package] name = "vec_map" version = "0.8.1" authors = ["Alex Crichton ", "Jorge Aparicio ", "Alexis Beingessner ", "Brian Anderson <>", "tbu- <>", "Manish Goregaokar <>", "Aaron Turon ", "Adolfo Ochagavía <>", "Niko Matsakis <>", "Steven Fackler <>", "Chase Southwood ", "Eduard Burtescu <>", "Florian Wilkens <>", "Félix Raimundo <>", "Tibor Benke <>", "Markus Siemens ", "Josh Branchaud ", "Huon Wilson ", "Corey Farwell ", "Aaron Liblong <>", "Nick Cameron ", "Patrick Walton ", "Felix S Klock II <>", "Andrew Paseltiner ", "Sean McArthur ", "Vadim Petrochenkov <>"] license = "MIT/Apache-2.0" description = "A simple map based on a vector for small integer keys" repository = "https://github.com/contain-rs/vec-map" homepage = "https://github.com/contain-rs/vec-map" documentation = "https://contain-rs.github.io/vec-map/vec_map" keywords = ["data-structures", "collections", "vecmap", "vec_map", "contain-rs"] readme = "README.md" [features] # This feature is kept for backwards compatibility. Use feature "serde" instead. eders = [ "serde" ] [dependencies] serde = { version = "1.0", features = [ "derive" ], optional = true } vec_map-0.8.1/Cargo.toml0000644000000034660000000000000105220ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "vec_map" version = "0.8.1" authors = ["Alex Crichton ", "Jorge Aparicio ", "Alexis Beingessner ", "Brian Anderson <>", "tbu- <>", "Manish Goregaokar <>", "Aaron Turon ", "Adolfo Ochagavía <>", "Niko Matsakis <>", "Steven Fackler <>", "Chase Southwood ", "Eduard Burtescu <>", "Florian Wilkens <>", "Félix Raimundo <>", "Tibor Benke <>", "Markus Siemens ", "Josh Branchaud ", "Huon Wilson ", "Corey Farwell ", "Aaron Liblong <>", "Nick Cameron ", "Patrick Walton ", "Felix S Klock II <>", "Andrew Paseltiner ", "Sean McArthur ", "Vadim Petrochenkov <>"] description = "A simple map based on a vector for small integer keys" homepage = "https://github.com/contain-rs/vec-map" documentation = "https://contain-rs.github.io/vec-map/vec_map" readme = "README.md" keywords = ["data-structures", "collections", "vecmap", "vec_map", "contain-rs"] license = "MIT/Apache-2.0" repository = "https://github.com/contain-rs/vec-map" [dependencies.serde] version = "1.0" features = ["derive"] optional = true [features] eders = ["serde"] vec_map-0.8.1/deploy-docs.sh000064400000000000000000000006741327532554700141220ustar0000000000000000#!/bin/bash set -o errexit -o nounset rev=$(git rev-parse --short HEAD) cd target/doc git init git config user.email 'FlashCat@users.noreply.github.com' git config user.name 'FlashCat' git remote add upstream "https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git" git fetch upstream gh-pages git reset upstream/gh-pages touch . git add -A . git commit -m "rebuild pages at ${rev}" git push -q upstream HEAD:gh-pages vec_map-0.8.1/LICENSE-APACHE000064400000000000000000000254531327532554700132720ustar0000000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. vec_map-0.8.1/LICENSE-MIT000064400000000000000000000021101327532554700127630ustar0000000000000000Copyright (c) 2015 The Rust Project Developers Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. vec_map-0.8.1/README.md000064400000000000000000000010141327532554700126100ustar0000000000000000**WARNING: THIS PROJECT IS IN MAINTENANCE MODE, DUE TO INSUFFICIENT MAINTAINER RESOURCES** It works fine, but will generally no longer be improved. We are currently only accepting changes which: * keep this compiling with the latest versions of Rust or its dependencies. * have minimal review requirements, such as documentation changes (so not totally new APIs). ------ A simple map based on a vector for small integer keys. Documentation is available at https://contain-rs.github.io/vec-map/vec_map. vec_map-0.8.1/src/lib.rs000064400000000000000000001306011327532554700132410ustar0000000000000000// Copyright 2012-2018 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #![deny(missing_docs)] //! A simple map based on a vector for small integer keys. Space requirements //! are O(highest integer key). // optional serde support #[cfg(feature = "serde")] #[macro_use] extern crate serde; use self::Entry::*; use std::cmp::{Ordering, max}; use std::fmt; use std::hash::{Hash, Hasher}; use std::iter::{Enumerate, FilterMap, FromIterator}; use std::mem::{replace, swap}; use std::ops::{Index, IndexMut}; use std::slice; use std::vec; /// A map optimized for small integer keys. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut months = VecMap::new(); /// months.insert(1, "Jan"); /// months.insert(2, "Feb"); /// months.insert(3, "Mar"); /// /// if !months.contains_key(12) { /// println!("The end is near!"); /// } /// /// assert_eq!(months.get(1), Some(&"Jan")); /// /// if let Some(value) = months.get_mut(3) { /// *value = "Venus"; /// } /// /// assert_eq!(months.get(3), Some(&"Venus")); /// /// // Print out all months /// for (key, value) in &months { /// println!("month {} is {}", key, value); /// } /// /// months.clear(); /// assert!(months.is_empty()); /// ``` #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct VecMap { n: usize, v: Vec>, } /// A view into a single entry in a map, which may either be vacant or occupied. pub enum Entry<'a, V: 'a> { /// A vacant Entry Vacant(VacantEntry<'a, V>), /// An occupied Entry Occupied(OccupiedEntry<'a, V>), } /// A vacant Entry. pub struct VacantEntry<'a, V: 'a> { map: &'a mut VecMap, index: usize, } /// An occupied Entry. pub struct OccupiedEntry<'a, V: 'a> { map: &'a mut VecMap, index: usize, } impl Default for VecMap { #[inline] fn default() -> Self { Self::new() } } impl Hash for VecMap { fn hash(&self, state: &mut H) { // In order to not traverse the `VecMap` twice, count the elements // during iteration. let mut count: usize = 0; for elt in self { elt.hash(state); count += 1; } count.hash(state); } } impl VecMap { /// Creates an empty `VecMap`. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// let mut map: VecMap<&str> = VecMap::new(); /// ``` pub fn new() -> Self { VecMap { n: 0, v: vec![] } } /// Creates an empty `VecMap` with space for at least `capacity` /// elements before resizing. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// let mut map: VecMap<&str> = VecMap::with_capacity(10); /// ``` pub fn with_capacity(capacity: usize) -> Self { VecMap { n: 0, v: Vec::with_capacity(capacity) } } /// Returns the number of elements the `VecMap` can hold without /// reallocating. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// let map: VecMap = VecMap::with_capacity(10); /// assert!(map.capacity() >= 10); /// ``` #[inline] pub fn capacity(&self) -> usize { self.v.capacity() } /// Reserves capacity for the given `VecMap` to contain `len` distinct keys. /// In the case of `VecMap` this means reallocations will not occur as long /// as all inserted keys are less than `len`. /// /// The collection may reserve more space to avoid frequent reallocations. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// let mut map: VecMap<&str> = VecMap::new(); /// map.reserve_len(10); /// assert!(map.capacity() >= 10); /// ``` pub fn reserve_len(&mut self, len: usize) { let cur_len = self.v.len(); if len >= cur_len { self.v.reserve(len - cur_len); } } /// Reserves the minimum capacity for the given `VecMap` to contain `len` distinct keys. /// In the case of `VecMap` this means reallocations will not occur as long as all inserted /// keys are less than `len`. /// /// Note that the allocator may give the collection more space than it requests. /// Therefore capacity cannot be relied upon to be precisely minimal. Prefer /// `reserve_len` if future insertions are expected. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// let mut map: VecMap<&str> = VecMap::new(); /// map.reserve_len_exact(10); /// assert!(map.capacity() >= 10); /// ``` pub fn reserve_len_exact(&mut self, len: usize) { let cur_len = self.v.len(); if len >= cur_len { self.v.reserve_exact(len - cur_len); } } /// Trims the `VecMap` of any excess capacity. /// /// The collection may reserve more space to avoid frequent reallocations. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// let mut map: VecMap<&str> = VecMap::with_capacity(10); /// map.shrink_to_fit(); /// assert_eq!(map.capacity(), 0); /// ``` pub fn shrink_to_fit(&mut self) { // strip off trailing `None`s if let Some(idx) = self.v.iter().rposition(Option::is_some) { self.v.truncate(idx + 1); } else { self.v.clear(); } self.v.shrink_to_fit() } /// Returns an iterator visiting all keys in ascending order of the keys. /// The iterator's element type is `usize`. pub fn keys(&self) -> Keys { Keys { iter: self.iter() } } /// Returns an iterator visiting all values in ascending order of the keys. /// The iterator's element type is `&'r V`. pub fn values(&self) -> Values { Values { iter: self.iter() } } /// Returns an iterator visiting all values in ascending order of the keys. /// The iterator's element type is `&'r mut V`. pub fn values_mut(&mut self) -> ValuesMut { ValuesMut { iter_mut: self.iter_mut() } } /// Returns an iterator visiting all key-value pairs in ascending order of the keys. /// The iterator's element type is `(usize, &'r V)`. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map = VecMap::new(); /// map.insert(1, "a"); /// map.insert(3, "c"); /// map.insert(2, "b"); /// /// // Print `1: a` then `2: b` then `3: c` /// for (key, value) in map.iter() { /// println!("{}: {}", key, value); /// } /// ``` pub fn iter(&self) -> Iter { Iter { front: 0, back: self.v.len(), n: self.n, yielded: 0, iter: self.v.iter() } } /// Returns an iterator visiting all key-value pairs in ascending order of the keys, /// with mutable references to the values. /// The iterator's element type is `(usize, &'r mut V)`. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map = VecMap::new(); /// map.insert(1, "a"); /// map.insert(2, "b"); /// map.insert(3, "c"); /// /// for (key, value) in map.iter_mut() { /// *value = "x"; /// } /// /// for (key, value) in &map { /// assert_eq!(value, &"x"); /// } /// ``` pub fn iter_mut(&mut self) -> IterMut { IterMut { front: 0, back: self.v.len(), n: self.n, yielded: 0, iter: self.v.iter_mut() } } /// Moves all elements from `other` into the map while overwriting existing keys. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut a = VecMap::new(); /// a.insert(1, "a"); /// a.insert(2, "b"); /// /// let mut b = VecMap::new(); /// b.insert(3, "c"); /// b.insert(4, "d"); /// /// a.append(&mut b); /// /// assert_eq!(a.len(), 4); /// assert_eq!(b.len(), 0); /// assert_eq!(a[1], "a"); /// assert_eq!(a[2], "b"); /// assert_eq!(a[3], "c"); /// assert_eq!(a[4], "d"); /// ``` pub fn append(&mut self, other: &mut Self) { self.extend(other.drain()); } /// Splits the collection into two at the given key. /// /// Returns a newly allocated `Self`. `self` contains elements `[0, at)`, /// and the returned `Self` contains elements `[at, max_key)`. /// /// Note that the capacity of `self` does not change. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut a = VecMap::new(); /// a.insert(1, "a"); /// a.insert(2, "b"); /// a.insert(3, "c"); /// a.insert(4, "d"); /// /// let b = a.split_off(3); /// /// assert_eq!(a[1], "a"); /// assert_eq!(a[2], "b"); /// /// assert_eq!(b[3], "c"); /// assert_eq!(b[4], "d"); /// ``` pub fn split_off(&mut self, at: usize) -> Self { let mut other = VecMap::new(); if at == 0 { // Move all elements to other // The swap will also fix .n swap(self, &mut other); return other } else if at >= self.v.len() { // No elements to copy return other; } // Look up the index of the first non-None item let first_index = self.v.iter().position(|el| el.is_some()); let start_index = match first_index { Some(index) => max(at, index), None => { // self has no elements return other; } }; // Fill the new VecMap with `None`s until `start_index` other.v.extend((0..start_index).map(|_| None)); // Move elements beginning with `start_index` from `self` into `other` let mut taken = 0; other.v.extend(self.v[start_index..].iter_mut().map(|el| { let el = el.take(); if el.is_some() { taken += 1; } el })); other.n = taken; self.n -= taken; other } /// Returns an iterator visiting all key-value pairs in ascending order of /// the keys, emptying (but not consuming) the original `VecMap`. /// The iterator's element type is `(usize, &'r V)`. Keeps the allocated memory for reuse. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map = VecMap::new(); /// map.insert(1, "a"); /// map.insert(3, "c"); /// map.insert(2, "b"); /// /// let vec: Vec<(usize, &str)> = map.drain().collect(); /// /// assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]); /// ``` pub fn drain(&mut self) -> Drain { fn filter((i, v): (usize, Option)) -> Option<(usize, A)> { v.map(|v| (i, v)) } let filter: fn((usize, Option)) -> Option<(usize, V)> = filter; // coerce to fn ptr self.n = 0; Drain { iter: self.v.drain(..).enumerate().filter_map(filter) } } /// Returns the number of elements in the map. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut a = VecMap::new(); /// assert_eq!(a.len(), 0); /// a.insert(1, "a"); /// assert_eq!(a.len(), 1); /// ``` pub fn len(&self) -> usize { self.n } /// Returns true if the map contains no elements. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut a = VecMap::new(); /// assert!(a.is_empty()); /// a.insert(1, "a"); /// assert!(!a.is_empty()); /// ``` pub fn is_empty(&self) -> bool { self.n == 0 } /// Clears the map, removing all key-value pairs. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut a = VecMap::new(); /// a.insert(1, "a"); /// a.clear(); /// assert!(a.is_empty()); /// ``` pub fn clear(&mut self) { self.n = 0; self.v.clear() } /// Returns a reference to the value corresponding to the key. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map = VecMap::new(); /// map.insert(1, "a"); /// assert_eq!(map.get(1), Some(&"a")); /// assert_eq!(map.get(2), None); /// ``` pub fn get(&self, key: usize) -> Option<&V> { if key < self.v.len() { self.v[key].as_ref() } else { None } } /// Returns true if the map contains a value for the specified key. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map = VecMap::new(); /// map.insert(1, "a"); /// assert_eq!(map.contains_key(1), true); /// assert_eq!(map.contains_key(2), false); /// ``` #[inline] pub fn contains_key(&self, key: usize) -> bool { self.get(key).is_some() } /// Returns a mutable reference to the value corresponding to the key. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map = VecMap::new(); /// map.insert(1, "a"); /// if let Some(x) = map.get_mut(1) { /// *x = "b"; /// } /// assert_eq!(map[1], "b"); /// ``` pub fn get_mut(&mut self, key: usize) -> Option<&mut V> { if key < self.v.len() { self.v[key].as_mut() } else { None } } /// Inserts a key-value pair into the map. If the key already had a value /// present in the map, that value is returned. Otherwise, `None` is returned. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map = VecMap::new(); /// assert_eq!(map.insert(37, "a"), None); /// assert_eq!(map.is_empty(), false); /// /// map.insert(37, "b"); /// assert_eq!(map.insert(37, "c"), Some("b")); /// assert_eq!(map[37], "c"); /// ``` pub fn insert(&mut self, key: usize, value: V) -> Option { let len = self.v.len(); if len <= key { self.v.extend((0..key - len + 1).map(|_| None)); } let was = replace(&mut self.v[key], Some(value)); if was.is_none() { self.n += 1; } was } /// Removes a key from the map, returning the value at the key if the key /// was previously in the map. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map = VecMap::new(); /// map.insert(1, "a"); /// assert_eq!(map.remove(1), Some("a")); /// assert_eq!(map.remove(1), None); /// ``` pub fn remove(&mut self, key: usize) -> Option { if key >= self.v.len() { return None; } let result = &mut self.v[key]; let was = result.take(); if was.is_some() { self.n -= 1; } was } /// Gets the given key's corresponding entry in the map for in-place manipulation. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut count: VecMap = VecMap::new(); /// /// // count the number of occurrences of numbers in the vec /// for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4] { /// *count.entry(x).or_insert(0) += 1; /// } /// /// assert_eq!(count[1], 3); /// ``` pub fn entry(&mut self, key: usize) -> Entry { // FIXME(Gankro): this is basically the dumbest implementation of // entry possible, because weird non-lexical borrows issues make it // completely insane to do any other way. That said, Entry is a border-line // useless construct on VecMap, so it's hardly a big loss. if self.contains_key(key) { Occupied(OccupiedEntry { map: self, index: key, }) } else { Vacant(VacantEntry { map: self, index: key, }) } } /// Retains only the elements specified by the predicate. /// /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map: VecMap = (0..8).map(|x|(x, x*10)).collect(); /// map.retain(|k, _| k % 2 == 0); /// assert_eq!(map.len(), 4); /// ``` pub fn retain(&mut self, mut f: F) where F: FnMut(usize, &mut V) -> bool { for (i, e) in self.v.iter_mut().enumerate() { let remove = match *e { Some(ref mut value) => !f(i, value), None => false, }; if remove { *e = None; self.n -= 1; } } } } impl<'a, V> Entry<'a, V> { /// Ensures a value is in the entry by inserting the default if empty, and /// returns a mutable reference to the value in the entry. pub fn or_insert(self, default: V) -> &'a mut V { match self { Occupied(entry) => entry.into_mut(), Vacant(entry) => entry.insert(default), } } /// Ensures a value is in the entry by inserting the result of the default /// function if empty, and returns a mutable reference to the value in the /// entry. pub fn or_insert_with V>(self, default: F) -> &'a mut V { match self { Occupied(entry) => entry.into_mut(), Vacant(entry) => entry.insert(default()), } } } impl<'a, V> VacantEntry<'a, V> { /// Sets the value of the entry with the VacantEntry's key, /// and returns a mutable reference to it. pub fn insert(self, value: V) -> &'a mut V { let index = self.index; self.map.insert(index, value); &mut self.map[index] } } impl<'a, V> OccupiedEntry<'a, V> { /// Gets a reference to the value in the entry. pub fn get(&self) -> &V { let index = self.index; &self.map[index] } /// Gets a mutable reference to the value in the entry. pub fn get_mut(&mut self) -> &mut V { let index = self.index; &mut self.map[index] } /// Converts the entry into a mutable reference to its value. pub fn into_mut(self) -> &'a mut V { let index = self.index; &mut self.map[index] } /// Sets the value of the entry with the OccupiedEntry's key, /// and returns the entry's old value. pub fn insert(&mut self, value: V) -> V { let index = self.index; self.map.insert(index, value).unwrap() } /// Takes the value of the entry out of the map, and returns it. pub fn remove(self) -> V { let index = self.index; self.map.remove(index).unwrap() } } impl Clone for VecMap { #[inline] fn clone(&self) -> Self { VecMap { n: self.n, v: self.v.clone() } } #[inline] fn clone_from(&mut self, source: &Self) { self.v.clone_from(&source.v); self.n = source.n; } } impl PartialEq for VecMap { fn eq(&self, other: &Self) -> bool { self.n == other.n && self.iter().eq(other.iter()) } } impl Eq for VecMap {} impl PartialOrd for VecMap { #[inline] fn partial_cmp(&self, other: &Self) -> Option { self.iter().partial_cmp(other.iter()) } } impl Ord for VecMap { #[inline] fn cmp(&self, other: &Self) -> Ordering { self.iter().cmp(other.iter()) } } impl fmt::Debug for VecMap { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_map().entries(self).finish() } } impl FromIterator<(usize, V)> for VecMap { fn from_iter>(iter: I) -> Self { let mut map = Self::new(); map.extend(iter); map } } impl IntoIterator for VecMap { type Item = (usize, T); type IntoIter = IntoIter; /// Returns an iterator visiting all key-value pairs in ascending order of /// the keys, consuming the original `VecMap`. /// The iterator's element type is `(usize, &'r V)`. /// /// # Examples /// /// ``` /// use vec_map::VecMap; /// /// let mut map = VecMap::new(); /// map.insert(1, "a"); /// map.insert(3, "c"); /// map.insert(2, "b"); /// /// let vec: Vec<(usize, &str)> = map.into_iter().collect(); /// /// assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]); /// ``` fn into_iter(self) -> IntoIter { IntoIter { n: self.n, yielded: 0, iter: self.v.into_iter().enumerate() } } } impl<'a, T> IntoIterator for &'a VecMap { type Item = (usize, &'a T); type IntoIter = Iter<'a, T>; fn into_iter(self) -> Iter<'a, T> { self.iter() } } impl<'a, T> IntoIterator for &'a mut VecMap { type Item = (usize, &'a mut T); type IntoIter = IterMut<'a, T>; fn into_iter(self) -> IterMut<'a, T> { self.iter_mut() } } impl Extend<(usize, V)> for VecMap { fn extend>(&mut self, iter: I) { for (k, v) in iter { self.insert(k, v); } } } impl<'a, V: Copy> Extend<(usize, &'a V)> for VecMap { fn extend>(&mut self, iter: I) { self.extend(iter.into_iter().map(|(key, &value)| (key, value))); } } impl Index for VecMap { type Output = V; #[inline] fn index(&self, i: usize) -> &V { self.get(i).expect("key not present") } } impl<'a, V> Index<&'a usize> for VecMap { type Output = V; #[inline] fn index(&self, i: &usize) -> &V { self.get(*i).expect("key not present") } } impl IndexMut for VecMap { #[inline] fn index_mut(&mut self, i: usize) -> &mut V { self.get_mut(i).expect("key not present") } } impl<'a, V> IndexMut<&'a usize> for VecMap { #[inline] fn index_mut(&mut self, i: &usize) -> &mut V { self.get_mut(*i).expect("key not present") } } macro_rules! iterator { (impl $name:ident -> $elem:ty, $($getter:ident),+) => { impl<'a, V> Iterator for $name<'a, V> { type Item = $elem; #[inline] fn next(&mut self) -> Option<$elem> { while self.front < self.back { if let Some(elem) = self.iter.next() { if let Some(x) = elem$(. $getter ())+ { let index = self.front; self.front += 1; self.yielded += 1; return Some((index, x)); } } self.front += 1; } None } #[inline] fn size_hint(&self) -> (usize, Option) { (self.n - self.yielded, Some(self.n - self.yielded)) } } } } macro_rules! double_ended_iterator { (impl $name:ident -> $elem:ty, $($getter:ident),+) => { impl<'a, V> DoubleEndedIterator for $name<'a, V> { #[inline] fn next_back(&mut self) -> Option<$elem> { while self.front < self.back { if let Some(elem) = self.iter.next_back() { if let Some(x) = elem$(. $getter ())+ { self.back -= 1; return Some((self.back, x)); } } self.back -= 1; } None } } } } /// An iterator over the key-value pairs of a map. pub struct Iter<'a, V: 'a> { front: usize, back: usize, n: usize, yielded: usize, iter: slice::Iter<'a, Option> } // FIXME(#19839) Remove in favor of `#[derive(Clone)]` impl<'a, V> Clone for Iter<'a, V> { fn clone(&self) -> Iter<'a, V> { Iter { front: self.front, back: self.back, n: self.n, yielded: self.yielded, iter: self.iter.clone() } } } iterator! { impl Iter -> (usize, &'a V), as_ref } impl<'a, V> ExactSizeIterator for Iter<'a, V> {} double_ended_iterator! { impl Iter -> (usize, &'a V), as_ref } /// An iterator over the key-value pairs of a map, with the /// values being mutable. pub struct IterMut<'a, V: 'a> { front: usize, back: usize, n: usize, yielded: usize, iter: slice::IterMut<'a, Option> } iterator! { impl IterMut -> (usize, &'a mut V), as_mut } impl<'a, V> ExactSizeIterator for IterMut<'a, V> {} double_ended_iterator! { impl IterMut -> (usize, &'a mut V), as_mut } /// An iterator over the keys of a map. pub struct Keys<'a, V: 'a> { iter: Iter<'a, V>, } // FIXME(#19839) Remove in favor of `#[derive(Clone)]` impl<'a, V> Clone for Keys<'a, V> { fn clone(&self) -> Keys<'a, V> { Keys { iter: self.iter.clone() } } } /// An iterator over the values of a map. pub struct Values<'a, V: 'a> { iter: Iter<'a, V>, } // FIXME(#19839) Remove in favor of `#[derive(Clone)]` impl<'a, V> Clone for Values<'a, V> { fn clone(&self) -> Values<'a, V> { Values { iter: self.iter.clone() } } } /// An iterator over the values of a map. pub struct ValuesMut<'a, V: 'a> { iter_mut: IterMut<'a, V>, } /// A consuming iterator over the key-value pairs of a map. pub struct IntoIter { n: usize, yielded: usize, iter: Enumerate>>, } /// A draining iterator over the key-value pairs of a map. pub struct Drain<'a, V: 'a> { iter: FilterMap< Enumerate>>, fn((usize, Option)) -> Option<(usize, V)>> } impl<'a, V> Iterator for Drain<'a, V> { type Item = (usize, V); fn next(&mut self) -> Option<(usize, V)> { self.iter.next() } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl<'a, V> ExactSizeIterator for Drain<'a, V> {} impl<'a, V> DoubleEndedIterator for Drain<'a, V> { fn next_back(&mut self) -> Option<(usize, V)> { self.iter.next_back() } } impl<'a, V> Iterator for Keys<'a, V> { type Item = usize; fn next(&mut self) -> Option { self.iter.next().map(|e| e.0) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl<'a, V> ExactSizeIterator for Keys<'a, V> {} impl<'a, V> DoubleEndedIterator for Keys<'a, V> { fn next_back(&mut self) -> Option { self.iter.next_back().map(|e| e.0) } } impl<'a, V> Iterator for Values<'a, V> { type Item = &'a V; fn next(&mut self) -> Option<(&'a V)> { self.iter.next().map(|e| e.1) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl<'a, V> ExactSizeIterator for Values<'a, V> {} impl<'a, V> DoubleEndedIterator for Values<'a, V> { fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back().map(|e| e.1) } } impl<'a, V> Iterator for ValuesMut<'a, V> { type Item = &'a mut V; fn next(&mut self) -> Option<(&'a mut V)> { self.iter_mut.next().map(|e| e.1) } fn size_hint(&self) -> (usize, Option) { self.iter_mut.size_hint() } } impl<'a, V> ExactSizeIterator for ValuesMut<'a, V> {} impl<'a, V> DoubleEndedIterator for ValuesMut<'a, V> { fn next_back(&mut self) -> Option<&'a mut V> { self.iter_mut.next_back().map(|e| e.1) } } impl Iterator for IntoIter { type Item = (usize, V); fn next(&mut self) -> Option<(usize, V)> { loop { match self.iter.next() { None => return None, Some((i, Some(value))) => { self.yielded += 1; return Some((i, value)) }, _ => {} } } } fn size_hint(&self) -> (usize, Option) { (self.n - self.yielded, Some(self.n - self.yielded)) } } impl ExactSizeIterator for IntoIter {} impl DoubleEndedIterator for IntoIter { fn next_back(&mut self) -> Option<(usize, V)> { loop { match self.iter.next_back() { None => return None, Some((i, Some(value))) => return Some((i, value)), _ => {} } } } } #[allow(dead_code)] fn assert_properties() { fn vec_map_covariant<'a, T>(map: VecMap<&'static T>) -> VecMap<&'a T> { map } fn into_iter_covariant<'a, T>(iter: IntoIter<&'static T>) -> IntoIter<&'a T> { iter } fn iter_covariant<'i, 'a, T>(iter: Iter<'i, &'static T>) -> Iter<'i, &'a T> { iter } fn keys_covariant<'i, 'a, T>(iter: Keys<'i, &'static T>) -> Keys<'i, &'a T> { iter } fn values_covariant<'i, 'a, T>(iter: Values<'i, &'static T>) -> Values<'i, &'a T> { iter } } #[cfg(test)] mod test { use super::VecMap; use super::Entry::{Occupied, Vacant}; use std::hash::{Hash, Hasher}; use std::collections::hash_map::DefaultHasher; #[test] fn test_get_mut() { let mut m = VecMap::new(); assert!(m.insert(1, 12).is_none()); assert!(m.insert(2, 8).is_none()); assert!(m.insert(5, 14).is_none()); let new = 100; match m.get_mut(5) { None => panic!(), Some(x) => *x = new } assert_eq!(m.get(5), Some(&new)); } #[test] fn test_len() { let mut map = VecMap::new(); assert_eq!(map.len(), 0); assert!(map.is_empty()); assert!(map.insert(5, 20).is_none()); assert_eq!(map.len(), 1); assert!(!map.is_empty()); assert!(map.insert(11, 12).is_none()); assert_eq!(map.len(), 2); assert!(!map.is_empty()); assert!(map.insert(14, 22).is_none()); assert_eq!(map.len(), 3); assert!(!map.is_empty()); } #[test] fn test_clear() { let mut map = VecMap::new(); assert!(map.insert(5, 20).is_none()); assert!(map.insert(11, 12).is_none()); assert!(map.insert(14, 22).is_none()); map.clear(); assert!(map.is_empty()); assert!(map.get(5).is_none()); assert!(map.get(11).is_none()); assert!(map.get(14).is_none()); } #[test] fn test_insert() { let mut m = VecMap::new(); assert_eq!(m.insert(1, 2), None); assert_eq!(m.insert(1, 3), Some(2)); assert_eq!(m.insert(1, 4), Some(3)); } #[test] fn test_remove() { let mut m = VecMap::new(); m.insert(1, 2); assert_eq!(m.remove(1), Some(2)); assert_eq!(m.remove(1), None); } #[test] fn test_keys() { let mut map = VecMap::new(); map.insert(1, 'a'); map.insert(2, 'b'); map.insert(3, 'c'); let keys: Vec<_> = map.keys().collect(); assert_eq!(keys.len(), 3); assert!(keys.contains(&1)); assert!(keys.contains(&2)); assert!(keys.contains(&3)); } #[test] fn test_values() { let mut map = VecMap::new(); map.insert(1, 'a'); map.insert(2, 'b'); map.insert(3, 'c'); let values: Vec<_> = map.values().cloned().collect(); assert_eq!(values.len(), 3); assert!(values.contains(&'a')); assert!(values.contains(&'b')); assert!(values.contains(&'c')); } #[test] fn test_iterator() { let mut m = VecMap::new(); assert!(m.insert(0, 1).is_none()); assert!(m.insert(1, 2).is_none()); assert!(m.insert(3, 5).is_none()); assert!(m.insert(6, 10).is_none()); assert!(m.insert(10, 11).is_none()); let mut it = m.iter(); assert_eq!(it.size_hint(), (5, Some(5))); assert_eq!(it.next().unwrap(), (0, &1)); assert_eq!(it.size_hint(), (4, Some(4))); assert_eq!(it.next().unwrap(), (1, &2)); assert_eq!(it.size_hint(), (3, Some(3))); assert_eq!(it.next().unwrap(), (3, &5)); assert_eq!(it.size_hint(), (2, Some(2))); assert_eq!(it.next().unwrap(), (6, &10)); assert_eq!(it.size_hint(), (1, Some(1))); assert_eq!(it.next().unwrap(), (10, &11)); assert_eq!(it.size_hint(), (0, Some(0))); assert!(it.next().is_none()); } #[test] fn test_iterator_size_hints() { let mut m = VecMap::new(); assert!(m.insert(0, 1).is_none()); assert!(m.insert(1, 2).is_none()); assert!(m.insert(3, 5).is_none()); assert!(m.insert(6, 10).is_none()); assert!(m.insert(10, 11).is_none()); assert_eq!(m.iter().size_hint(), (5, Some(5))); assert_eq!(m.iter().rev().size_hint(), (5, Some(5))); assert_eq!(m.iter_mut().size_hint(), (5, Some(5))); assert_eq!(m.iter_mut().rev().size_hint(), (5, Some(5))); } #[test] fn test_mut_iterator() { let mut m = VecMap::new(); assert!(m.insert(0, 1).is_none()); assert!(m.insert(1, 2).is_none()); assert!(m.insert(3, 5).is_none()); assert!(m.insert(6, 10).is_none()); assert!(m.insert(10, 11).is_none()); for (k, v) in &mut m { *v += k as isize; } let mut it = m.iter(); assert_eq!(it.next().unwrap(), (0, &1)); assert_eq!(it.next().unwrap(), (1, &3)); assert_eq!(it.next().unwrap(), (3, &8)); assert_eq!(it.next().unwrap(), (6, &16)); assert_eq!(it.next().unwrap(), (10, &21)); assert!(it.next().is_none()); } #[test] fn test_rev_iterator() { let mut m = VecMap::new(); assert!(m.insert(0, 1).is_none()); assert!(m.insert(1, 2).is_none()); assert!(m.insert(3, 5).is_none()); assert!(m.insert(6, 10).is_none()); assert!(m.insert(10, 11).is_none()); let mut it = m.iter().rev(); assert_eq!(it.next().unwrap(), (10, &11)); assert_eq!(it.next().unwrap(), (6, &10)); assert_eq!(it.next().unwrap(), (3, &5)); assert_eq!(it.next().unwrap(), (1, &2)); assert_eq!(it.next().unwrap(), (0, &1)); assert!(it.next().is_none()); } #[test] fn test_mut_rev_iterator() { let mut m = VecMap::new(); assert!(m.insert(0, 1).is_none()); assert!(m.insert(1, 2).is_none()); assert!(m.insert(3, 5).is_none()); assert!(m.insert(6, 10).is_none()); assert!(m.insert(10, 11).is_none()); for (k, v) in m.iter_mut().rev() { *v += k as isize; } let mut it = m.iter(); assert_eq!(it.next().unwrap(), (0, &1)); assert_eq!(it.next().unwrap(), (1, &3)); assert_eq!(it.next().unwrap(), (3, &8)); assert_eq!(it.next().unwrap(), (6, &16)); assert_eq!(it.next().unwrap(), (10, &21)); assert!(it.next().is_none()); } #[test] fn test_move_iter() { let mut m: VecMap> = VecMap::new(); m.insert(1, Box::new(2)); let mut called = false; for (k, v) in m { assert!(!called); called = true; assert_eq!(k, 1); assert_eq!(v, Box::new(2)); } assert!(called); } #[test] fn test_drain_iterator() { let mut map = VecMap::new(); map.insert(1, "a"); map.insert(3, "c"); map.insert(2, "b"); let vec: Vec<_> = map.drain().collect(); assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]); assert_eq!(map.len(), 0); } #[test] fn test_append() { let mut a = VecMap::new(); a.insert(1, "a"); a.insert(2, "b"); a.insert(3, "c"); let mut b = VecMap::new(); b.insert(3, "d"); // Overwrite element from a b.insert(4, "e"); b.insert(5, "f"); a.append(&mut b); assert_eq!(a.len(), 5); assert_eq!(b.len(), 0); // Capacity shouldn't change for possible reuse assert!(b.capacity() >= 4); assert_eq!(a[1], "a"); assert_eq!(a[2], "b"); assert_eq!(a[3], "d"); assert_eq!(a[4], "e"); assert_eq!(a[5], "f"); } #[test] fn test_split_off() { // Split within the key range let mut a = VecMap::new(); a.insert(1, "a"); a.insert(2, "b"); a.insert(3, "c"); a.insert(4, "d"); let b = a.split_off(3); assert_eq!(a.len(), 2); assert_eq!(b.len(), 2); assert_eq!(a[1], "a"); assert_eq!(a[2], "b"); assert_eq!(b[3], "c"); assert_eq!(b[4], "d"); // Split at 0 a.clear(); a.insert(1, "a"); a.insert(2, "b"); a.insert(3, "c"); a.insert(4, "d"); let b = a.split_off(0); assert_eq!(a.len(), 0); assert_eq!(b.len(), 4); assert_eq!(b[1], "a"); assert_eq!(b[2], "b"); assert_eq!(b[3], "c"); assert_eq!(b[4], "d"); // Split behind max_key a.clear(); a.insert(1, "a"); a.insert(2, "b"); a.insert(3, "c"); a.insert(4, "d"); let b = a.split_off(5); assert_eq!(a.len(), 4); assert_eq!(b.len(), 0); assert_eq!(a[1], "a"); assert_eq!(a[2], "b"); assert_eq!(a[3], "c"); assert_eq!(a[4], "d"); } #[test] fn test_show() { let mut map = VecMap::new(); let empty = VecMap::::new(); map.insert(1, 2); map.insert(3, 4); let map_str = format!("{:?}", map); assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}"); assert_eq!(format!("{:?}", empty), "{}"); } #[test] fn test_clone() { let mut a = VecMap::new(); a.insert(1, 'x'); a.insert(4, 'y'); a.insert(6, 'z'); assert_eq!(a.clone().iter().collect::>(), [(1, &'x'), (4, &'y'), (6, &'z')]); } #[test] fn test_eq() { let mut a = VecMap::new(); let mut b = VecMap::new(); assert!(a == b); assert!(a.insert(0, 5).is_none()); assert!(a != b); assert!(b.insert(0, 4).is_none()); assert!(a != b); assert!(a.insert(5, 19).is_none()); assert!(a != b); assert!(!b.insert(0, 5).is_none()); assert!(a != b); assert!(b.insert(5, 19).is_none()); assert!(a == b); a = VecMap::new(); b = VecMap::with_capacity(1); assert!(a == b); } #[test] fn test_lt() { let mut a = VecMap::new(); let mut b = VecMap::new(); assert!(!(a < b) && !(b < a)); assert!(b.insert(2, 5).is_none()); assert!(a < b); assert!(a.insert(2, 7).is_none()); assert!(!(a < b) && b < a); assert!(b.insert(1, 0).is_none()); assert!(b < a); assert!(a.insert(0, 6).is_none()); assert!(a < b); assert!(a.insert(6, 2).is_none()); assert!(a < b && !(b < a)); } #[test] fn test_ord() { let mut a = VecMap::new(); let mut b = VecMap::new(); assert!(a <= b && a >= b); assert!(a.insert(1, 1).is_none()); assert!(a > b && a >= b); assert!(b < a && b <= a); assert!(b.insert(2, 2).is_none()); assert!(b > a && b >= a); assert!(a < b && a <= b); } #[test] fn test_hash() { fn hash(t: &T) -> u64 { let mut s = DefaultHasher::new(); t.hash(&mut s); s.finish() } let mut x = VecMap::new(); let mut y = VecMap::new(); assert!(hash(&x) == hash(&y)); x.insert(1, 'a'); x.insert(2, 'b'); x.insert(3, 'c'); y.insert(3, 'c'); y.insert(2, 'b'); y.insert(1, 'a'); assert!(hash(&x) == hash(&y)); x.insert(1000, 'd'); x.remove(1000); assert!(hash(&x) == hash(&y)); } #[test] fn test_from_iter() { let xs = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]; let map: VecMap<_> = xs.iter().cloned().collect(); for &(k, v) in &xs { assert_eq!(map.get(k), Some(&v)); } } #[test] fn test_index() { let mut map = VecMap::new(); map.insert(1, 2); map.insert(2, 1); map.insert(3, 4); assert_eq!(map[3], 4); } #[test] #[should_panic] fn test_index_nonexistent() { let mut map = VecMap::new(); map.insert(1, 2); map.insert(2, 1); map.insert(3, 4); map[4]; } #[test] fn test_entry() { let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)]; let mut map: VecMap<_> = xs.iter().cloned().collect(); // Existing key (insert) match map.entry(1) { Vacant(_) => unreachable!(), Occupied(mut view) => { assert_eq!(view.get(), &10); assert_eq!(view.insert(100), 10); } } assert_eq!(map.get(1).unwrap(), &100); assert_eq!(map.len(), 6); // Existing key (update) match map.entry(2) { Vacant(_) => unreachable!(), Occupied(mut view) => { let v = view.get_mut(); *v *= 10; } } assert_eq!(map.get(2).unwrap(), &200); assert_eq!(map.len(), 6); // Existing key (take) match map.entry(3) { Vacant(_) => unreachable!(), Occupied(view) => { assert_eq!(view.remove(), 30); } } assert_eq!(map.get(3), None); assert_eq!(map.len(), 5); // Inexistent key (insert) match map.entry(10) { Occupied(_) => unreachable!(), Vacant(view) => { assert_eq!(*view.insert(1000), 1000); } } assert_eq!(map.get(10).unwrap(), &1000); assert_eq!(map.len(), 6); } #[test] fn test_extend_ref() { let mut a = VecMap::new(); a.insert(1, "one"); let mut b = VecMap::new(); b.insert(2, "two"); b.insert(3, "three"); a.extend(&b); assert_eq!(a.len(), 3); assert_eq!(a[&1], "one"); assert_eq!(a[&2], "two"); assert_eq!(a[&3], "three"); } #[test] #[cfg(feature = "serde")] fn test_serde() { use serde::{Serialize, Deserialize}; fn impls_serde_traits<'de, S: Serialize + Deserialize<'de>>() {} impls_serde_traits::>(); } #[test] fn test_retain() { let mut map = VecMap::new(); map.insert(1, "one"); map.insert(2, "two"); map.insert(3, "three"); map.retain(|k, v| match k { 1 => false, 2 => { *v = "two changed"; true }, 3 => false, _ => panic!(), }); assert_eq!(map.len(), 1); assert_eq!(map.get(1), None); assert_eq!(map[2], "two changed"); assert_eq!(map.get(3), None); } }