Expand description
This crate provides ordered maps and sets backed by sorted arrays or slices.
The crate provides five structures with different internal data types and map/set distinctions:
OrderedArrayMap— owned fixed-size array-backed mapOrderedArraySet— owned fixed-size array-backed setOrderedSliceMap— borrowed sorted slice-backed read-only mapOrderedSliceMapMut— borrowed sorted slice-backed map with mutable valuesOrderedSliceSet— borrowed sorted slice-backed read-only set
These structures provide a subset of the methods offered by the standard library’s
BTreeMap and BTreeSet. They use binary search for element lookup, resulting in
logarithmic time complexity for most methods relative to the number of elements.
The current version does not provide methods that change the size (such as append or remove).
(no_std developers may need these. If you do, please let us know.)
§Features
§Compile-time constants
Each structure provides a const function called new_unchecked. This function takes a
sorted array or slice as an argument and returns a map or set. It is the programmer’s
responsibility to ensure the input is sorted.
Since new_unchecked is a const function, it can be used in compile-time constant definitions.
§no_std support
The structures in this crate hold only arrays or slices as internal state.
They work in no_std environments since they do not allocate memory dynamically.
§Support for PartialOrd
The new_unchecked function assumes the programmer guarantees that the input is sorted
(including that it contains no incomparable values like f64::NAN). This allows you to
create maps and sets of types without a total order, such as f32 and f64.
Methods other than new_unchecked treat incomparable values as greater than all other values.
Therefore, behavior is well-defined when incomparable values are passed to any method.
Refer to the description of each method for details.
§Correspondence with the standard library
Methods in these structures that share names with BTreeMap or BTreeSet in the standard
library behave identically. Even for methods that return iterators, the returned iterator
enumerates the same values.
§Example
use std::ops::Bound;
use static_mapset::OrderedArrayMap;
static MONTHS: OrderedArrayMap<i32, &str, 12> = OrderedArrayMap::new_unchecked([
(31, "January"), (59, "February"), (90, "March"),
(120, "April"), (151, "May"), (181, "June"),
(212, "July"), (243, "August"), (273, "September"),
(304, "October"), (334, "November"), (365, "December")
]);
fn main() {
println!(
"The 200th day of a year is on {}.",
MONTHS.upper_bound(Bound::Excluded(&200)).next().unwrap().1);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_order_of_month() {
// You can use `is_valid` method for unit testing.
assert!(MONTHS.is_valid(), "MONTHS is not ordered correctly");
}
}§Compatibility
Except for is_valid method, the behavior when new_unchecked receives
an unsorted collection is undefined. We do not plan to maintain compatibility in this case.
§Copyright Notice
The APIs of the types provided by this crate are modelled after Rust’s BTreeMap and BTreeSet.
Consequently, the documentation and tests of this crate incorporate work from the Rust standard library.
Copyright (c) The Rust Project Contributors
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.
Re-exports§
pub use ordered_array_map::OrderedArrayMap;pub use ordered_array_set::OrderedArraySet;pub use ordered_slice_map::OrderedSliceMap;pub use ordered_slice_map_mut::OrderedSliceMapMut;pub use ordered_slice_set::OrderedSliceSet;