diff --git a/Cargo.toml b/Cargo.toml
index 0b2bbdb6..38b39881 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -33,7 +33,7 @@ scoped_threadpool = "0.1.8"
[dependencies]
as-slice = "0.1.0"
-generic-array = "0.13.0"
+# generic-array = "0.13.0"
hash32 = "0.1.0"
[dependencies.serde]
diff --git a/src/binary_heap.rs b/src/binary_heap.rs
index aa56c57b..15d16aa9 100644
--- a/src/binary_heap.rs
+++ b/src/binary_heap.rs
@@ -16,9 +16,8 @@ use core::{
ptr, slice,
};
-use generic_array::{ArrayLength, GenericArray};
-
use crate::sealed::binary_heap::Kind;
+use crate::vec::Vec;
/// Min-heap
pub enum Min {}
@@ -26,17 +25,6 @@ pub enum Min {}
/// Max-heap
pub enum Max {}
-impl crate::i::BinaryHeap {
- /// `BinaryHeap` `const` constructor; wrap the returned value in
- /// [`BinaryHeap`](../struct.BinaryHeap.html)
- pub const fn new() -> Self {
- Self {
- _kind: PhantomData,
- data: crate::i::Vec::new(),
- }
- }
-}
-
/// A priority queue implemented with a binary heap.
///
/// This can be either a min-heap or a max-heap.
@@ -47,9 +35,8 @@ impl crate::i::BinaryHeap {
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
-/// use heapless::consts::*;
///
-/// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+/// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
///
/// // We can use peek to look at the next item in the heap. In this case,
/// // there's no items in there yet so we get None.
@@ -84,18 +71,19 @@ impl crate::i::BinaryHeap {
/// // The heap should now be empty.
/// assert!(heap.is_empty())
/// ```
-pub struct BinaryHeap(
- #[doc(hidden)] pub crate::i::BinaryHeap, KIND>,
-)
-where
- T: Ord,
- N: ArrayLength,
- KIND: Kind;
-impl BinaryHeap
+pub struct BinaryHeap
+where
+ T: Ord,
+ K: Kind,
+{
+ pub(crate) _kind: PhantomData,
+ pub(crate) data: Vec,
+}
+
+impl BinaryHeap
where
T: Ord,
- N: ArrayLength,
K: Kind,
{
/* Constructors */
@@ -103,32 +91,33 @@ where
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
- /// use heapless::consts::*;
///
/// // allocate the binary heap on the stack
- /// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+ /// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
/// heap.push(4).unwrap();
///
/// // allocate the binary heap in a static variable
- /// static mut HEAP: BinaryHeap = BinaryHeap(heapless::i::BinaryHeap::new());
+ /// static mut HEAP: BinaryHeap = BinaryHeap::new();
/// ```
- pub fn new() -> Self {
- BinaryHeap(crate::i::BinaryHeap::new())
+ pub const fn new() -> Self {
+ Self {
+ _kind: PhantomData,
+ data: Vec::new(),
+ }
}
/* Public API */
/// Returns the capacity of the binary heap.
pub fn capacity(&self) -> usize {
- self.0.data.capacity()
+ self.data.capacity()
}
/// Drops all items from the binary heap.
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
- /// use heapless::consts::*;
///
- /// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+ /// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
/// heap.push(1).unwrap();
/// heap.push(3).unwrap();
///
@@ -139,32 +128,30 @@ where
/// assert!(heap.is_empty());
/// ```
pub fn clear(&mut self) {
- self.0.data.clear()
+ self.data.clear()
}
/// Returns the length of the binary heap.
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
- /// use heapless::consts::*;
///
- /// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+ /// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
/// heap.push(1).unwrap();
/// heap.push(3).unwrap();
///
/// assert_eq!(heap.len(), 2);
/// ```
pub fn len(&self) -> usize {
- self.0.data.len
+ self.data.len()
}
/// Checks if the binary heap is empty.
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
- /// use heapless::consts::*;
///
- /// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+ /// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
///
/// assert!(heap.is_empty());
///
@@ -182,9 +169,8 @@ where
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
- /// use heapless::consts::*;
///
- /// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+ /// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
/// heap.push(1).unwrap();
/// heap.push(2).unwrap();
/// heap.push(3).unwrap();
@@ -197,7 +183,7 @@ where
/// }
/// ```
pub fn iter(&self) -> slice::Iter<'_, T> {
- self.0.data.as_slice().iter()
+ self.data.as_slice().iter()
}
/// Returns a mutable iterator visiting all values in the underlying vector, in arbitrary order.
@@ -205,7 +191,7 @@ where
/// **WARNING** Mutating the items in the binary heap can leave the heap in an inconsistent
/// state.
pub fn iter_mut(&mut self) -> slice::IterMut<'_, T> {
- self.0.data.as_mut_slice().iter_mut()
+ self.data.as_mut_slice().iter_mut()
}
/// Returns the *top* (greatest if max-heap, smallest if min-heap) item in the binary heap, or
@@ -213,9 +199,8 @@ where
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
- /// use heapless::consts::*;
///
- /// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+ /// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
/// assert_eq!(heap.peek(), None);
///
/// heap.push(1).unwrap();
@@ -224,7 +209,7 @@ where
/// assert_eq!(heap.peek(), Some(&5));
/// ```
pub fn peek(&self) -> Option<&T> {
- self.0.data.as_slice().get(0)
+ self.data.as_slice().get(0)
}
/// Returns a mutable reference to the greatest item in the binary heap, or
@@ -239,9 +224,8 @@ where
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
- /// use heapless::consts::*;
///
- /// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+ /// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
/// assert!(heap.peek_mut().is_none());
///
/// heap.push(1);
@@ -254,7 +238,7 @@ where
///
/// assert_eq!(heap.peek(), Some(&2));
/// ```
- pub fn peek_mut(&mut self) -> Option> {
+ pub fn peek_mut(&mut self) -> Option> {
if self.is_empty() {
None
} else {
@@ -270,9 +254,8 @@ where
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
- /// use heapless::consts::*;
///
- /// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+ /// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
/// heap.push(1).unwrap();
/// heap.push(3).unwrap();
///
@@ -291,10 +274,10 @@ where
/// Removes the *top* (greatest if max-heap, smallest if min-heap) item from the binary heap and
/// returns it, without checking if the binary heap is empty.
pub unsafe fn pop_unchecked(&mut self) -> T {
- let mut item = self.0.data.pop_unchecked();
+ let mut item = self.data.pop_unchecked();
if !self.is_empty() {
- mem::swap(&mut item, self.0.data.as_mut_slice().get_unchecked_mut(0));
+ mem::swap(&mut item, self.data.as_mut_slice().get_unchecked_mut(0));
self.sift_down_to_bottom(0);
}
item
@@ -304,9 +287,8 @@ where
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
- /// use heapless::consts::*;
///
- /// let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
+ /// let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
/// heap.push(3).unwrap();
/// heap.push(5).unwrap();
/// heap.push(1).unwrap();
@@ -315,7 +297,7 @@ where
/// assert_eq!(heap.peek(), Some(&5));
/// ```
pub fn push(&mut self, item: T) -> Result<(), T> {
- if self.0.data.is_full() {
+ if self.data.is_full() {
return Err(item);
}
@@ -326,7 +308,7 @@ where
/// Pushes an item onto the binary heap without first checking if it's full.
pub unsafe fn push_unchecked(&mut self, item: T) {
let old_len = self.len();
- self.0.data.push_unchecked(item);
+ self.data.push_unchecked(item);
self.sift_up(0, old_len);
}
@@ -335,7 +317,7 @@ where
let end = self.len();
let start = pos;
unsafe {
- let mut hole = Hole::new(self.0.data.as_mut_slice(), pos);
+ let mut hole = Hole::new(self.data.as_mut_slice(), pos);
let mut child = 2 * pos + 1;
while child < end {
let right = child + 1;
@@ -354,7 +336,7 @@ where
fn sift_up(&mut self, start: usize, pos: usize) -> usize {
unsafe {
// Take out the value at `pos` and create a hole.
- let mut hole = Hole::new(self.0.data.as_mut_slice(), pos);
+ let mut hole = Hole::new(self.data.as_mut_slice(), pos);
while hole.pos() > start {
let parent = (hole.pos() - 1) / 2;
@@ -437,20 +419,18 @@ impl<'a, T> Hole<'a, T> {
///
/// [`peek_mut`]: struct.BinaryHeap.html#method.peek_mut
/// [`BinaryHeap`]: struct.BinaryHeap.html
-pub struct PeekMut<'a, T, N, K>
+pub struct PeekMut<'a, T, K, const N: usize>
where
T: Ord,
- N: ArrayLength,
K: Kind,
{
- heap: &'a mut BinaryHeap,
+ heap: &'a mut BinaryHeap,
sift: bool,
}
-impl Drop for PeekMut<'_, T, N, K>
+impl Drop for PeekMut<'_, T, K, N>
where
T: Ord,
- N: ArrayLength,
K: Kind,
{
fn drop(&mut self) {
@@ -460,41 +440,38 @@ where
}
}
-impl Deref for PeekMut<'_, T, N, K>
+impl Deref for PeekMut<'_, T, K, N>
where
T: Ord,
- N: ArrayLength,
K: Kind,
{
type Target = T;
fn deref(&self) -> &T {
debug_assert!(!self.heap.is_empty());
// SAFE: PeekMut is only instantiated for non-empty heaps
- unsafe { self.heap.0.data.as_slice().get_unchecked(0) }
+ unsafe { self.heap.data.as_slice().get_unchecked(0) }
}
}
-impl DerefMut for PeekMut<'_, T, N, K>
+impl DerefMut for PeekMut<'_, T, K, N>
where
T: Ord,
- N: ArrayLength,
K: Kind,
{
fn deref_mut(&mut self) -> &mut T {
debug_assert!(!self.heap.is_empty());
// SAFE: PeekMut is only instantiated for non-empty heaps
- unsafe { self.heap.0.data.as_mut_slice().get_unchecked_mut(0) }
+ unsafe { self.heap.data.as_mut_slice().get_unchecked_mut(0) }
}
}
-impl<'a, T, N, K> PeekMut<'a, T, N, K>
+impl<'a, T, K, const N: usize> PeekMut<'a, T, K, N>
where
T: Ord,
- N: ArrayLength,
K: Kind,
{
/// Removes the peeked value from the heap and returns it.
- pub fn pop(mut this: PeekMut<'a, T, N, K>) -> T {
+ pub fn pop(mut this: PeekMut<'a, T, K, N>) -> T {
let value = this.heap.pop().unwrap();
this.sift = false;
value
@@ -512,10 +489,9 @@ impl<'a, T> Drop for Hole<'a, T> {
}
}
-impl Default for BinaryHeap
+impl Default for BinaryHeap
where
T: Ord,
- N: ArrayLength,
K: Kind,
{
fn default() -> Self {
@@ -523,34 +499,31 @@ where
}
}
-impl Clone for BinaryHeap
+impl Clone for BinaryHeap
where
- N: ArrayLength,
K: Kind,
T: Ord + Clone,
{
fn clone(&self) -> Self {
- BinaryHeap(crate::i::BinaryHeap {
- _kind: self.0._kind,
- data: self.0.data.clone(),
- })
+ Self {
+ _kind: self._kind,
+ data: self.data.clone(),
+ }
}
}
-impl Drop for BinaryHeap
+impl Drop for BinaryHeap
where
- N: ArrayLength,
K: Kind,
T: Ord,
{
fn drop(&mut self) {
- unsafe { ptr::drop_in_place(self.0.data.as_mut_slice()) }
+ unsafe { ptr::drop_in_place(self.data.as_mut_slice()) }
}
}
-impl fmt::Debug for BinaryHeap
+impl fmt::Debug for BinaryHeap
where
- N: ArrayLength,
K: Kind,
T: Ord + fmt::Debug,
{
@@ -559,9 +532,8 @@ where
}
}
-impl<'a, T, N, K> IntoIterator for &'a BinaryHeap
+impl<'a, T, K, const N: usize> IntoIterator for &'a BinaryHeap
where
- N: ArrayLength,
K: Kind,
T: Ord,
{
@@ -577,19 +549,16 @@ where
mod tests {
use std::vec::Vec;
- use crate::{
- binary_heap::{self, BinaryHeap, Min},
- consts::*,
- };
+ use crate::binary_heap::{BinaryHeap, Max, Min};
#[test]
fn static_new() {
- static mut _B: BinaryHeap = BinaryHeap(crate::i::BinaryHeap::new());
+ static mut _B: BinaryHeap = BinaryHeap::new();
}
#[test]
fn min() {
- let mut heap = BinaryHeap::<_, U16, Min>::new();
+ let mut heap = BinaryHeap::<_, Min, 16>::new();
heap.push(1).unwrap();
heap.push(2).unwrap();
heap.push(3).unwrap();
@@ -641,7 +610,7 @@ mod tests {
#[test]
fn max() {
- let mut heap = BinaryHeap::<_, U16, binary_heap::Max>::new();
+ let mut heap = BinaryHeap::<_, Max, 16>::new();
heap.push(1).unwrap();
heap.push(2).unwrap();
heap.push(3).unwrap();
diff --git a/src/de.rs b/src/de.rs
index af740193..7da79d58 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -1,6 +1,8 @@
+//! missing doc
+
use core::{fmt, marker::PhantomData};
-use generic_array::{typenum::PowerOfTwo, ArrayLength};
+// use generic_array::{typenum::PowerOfTwo, ArrayLength};
use hash32::{BuildHasherDefault, Hash, Hasher};
use serde::de::{self, Deserialize, Deserializer, Error, MapAccess, SeqAccess};
@@ -12,25 +14,24 @@ use crate::{
// Sequential containers
-impl<'de, T, N, KIND> Deserialize<'de> for BinaryHeap
+impl<'de, T, KIND, const N: usize> Deserialize<'de> for BinaryHeap
where
T: Ord + Deserialize<'de>,
- N: ArrayLength,
+
KIND: BinaryHeapKind,
{
fn deserialize(deserializer: D) -> Result
where
D: Deserializer<'de>,
{
- struct ValueVisitor<'de, T, N, KIND>(PhantomData<(&'de (), T, N, KIND)>);
+ struct ValueVisitor<'de, T, KIND, const N: usize>(PhantomData<(&'de (), T, KIND)>);
- impl<'de, T, N, KIND> de::Visitor<'de> for ValueVisitor<'de, T, N, KIND>
+ impl<'de, T, KIND, const N: usize> de::Visitor<'de> for ValueVisitor<'de, T, KIND, N>
where
T: Ord + Deserialize<'de>,
- N: ArrayLength,
KIND: BinaryHeapKind,
{
- type Value = BinaryHeap;
+ type Value = BinaryHeap;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a sequence")
@@ -55,25 +56,23 @@ where
}
}
-impl<'de, T, N, S> Deserialize<'de> for IndexSet>
+impl<'de, T, S, const N: usize> Deserialize<'de> for IndexSet, N>
where
T: Eq + Hash + Deserialize<'de>,
S: Hasher + Default,
- N: ArrayLength> + ArrayLength