Skip to content

Commit

Permalink
Switch ptr_array_size to u32, add Debug for queue and Deque impleme…
Browse files Browse the repository at this point in the history
…nt bump to 0.13.1
  • Loading branch information
MrElectrify committed Apr 6, 2024
1 parent b94372b commit de85263
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "eastl-rs"
authors = ["Andrew Buck"]
description = "EASTL binary-compatible Rust implementations"
documentation = "https://docs.rs/crate/eastl-rs"
version = "0.13.0"
version = "0.13.1"
edition = "2021"
license-file = "LICENSE"
readme = "README.md"
Expand All @@ -15,6 +15,7 @@ nightly = []

[dependencies]
duplicate = "1.0"
itertools = "0.12"
moveit = "0.6.0"
superslice = "1.0"

Expand Down
30 changes: 20 additions & 10 deletions src/deque/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::allocator::{Allocator, DefaultAllocator};
use crate::deque::iter::{CompatIterMut, Iter, IterMut};
use itertools::Itertools;
use std::fmt::{Debug, Formatter};

pub mod iter;

Expand All @@ -10,7 +12,7 @@ pub type DefaultDeque<'a, V> = Deque<'a, V, DefaultAllocator>;
#[repr(C)]
pub struct Deque<'a, T: 'a, A: Allocator> {
ptr_array: *mut *mut T,
ptr_array_size: usize,
ptr_array_size: u32,
begin_it: CompatIterMut<'a, T>,
end_it: CompatIterMut<'a, T>,
allocator: A,
Expand All @@ -27,7 +29,7 @@ impl<'a, T: 'a, A: Allocator + Default> Deque<'a, T, A> {
}

impl<'a, T: 'a, A: Allocator> Deque<'a, T, A> {
const INITIAL_PTR_ARRAY_SIZE: usize = 8;
const INITIAL_PTR_ARRAY_SIZE: u32 = 8;
const SUBARRAY_SIZE: usize = Self::calculate_subarray_size();

/// Provides a reference to the back element, or `None` if the deque is empty.
Expand Down Expand Up @@ -174,7 +176,9 @@ impl<'a, T: 'a, A: Allocator> Deque<'a, T, A> {
}
} else {
// see if we need to allocate more pointers
if self.end_it.current_array == unsafe { self.ptr_array.add(self.ptr_array_size - 1) } {
if self.end_it.current_array
== unsafe { self.ptr_array.add(self.ptr_array_size as usize - 1) }
{
self.realloc_ptr_array(1, false);
}
// write our element to the last position in the subarray
Expand Down Expand Up @@ -243,7 +247,7 @@ impl<'a, T: 'a, A: Allocator> Deque<'a, T, A> {
fn free_ptr_array(&mut self) {
unsafe {
self.allocator
.deallocate(self.ptr_array, self.ptr_array_size)
.deallocate(self.ptr_array, self.ptr_array_size as usize)
}
}

Expand Down Expand Up @@ -277,11 +281,11 @@ impl<'a, T: 'a, A: Allocator> Deque<'a, T, A> {
self.ptr_array_size = Self::INITIAL_PTR_ARRAY_SIZE;

// allocate the subarrays
let ptr_array = self.allocate_ptr_array(self.ptr_array_size);
let ptr_array = self.allocate_ptr_array(self.ptr_array_size as usize);
ptr_array.fill_with(std::ptr::null_mut);

// we start with an empty deque, so only allocate the first array mid-way through
ptr_array[(Self::INITIAL_PTR_ARRAY_SIZE - 1) / 2] = self.allocate_subarray();
ptr_array[((Self::INITIAL_PTR_ARRAY_SIZE - 1) / 2) as usize] = self.allocate_subarray();

// setup the iterators
unsafe {
Expand Down Expand Up @@ -311,12 +315,12 @@ impl<'a, T: 'a, A: Allocator> Deque<'a, T, A> {
.current_array
.offset_from(self.begin_it.current_array)
} + 1) as usize;
let unused_ptrs_at_back = (self.ptr_array_size - unused_ptrs_at_front) - used_ptrs;
let unused_ptrs_at_back = (self.ptr_array_size as usize - unused_ptrs_at_front) - used_ptrs;
let current_array_start =
unsafe { self.begin_it.current_array.offset_from(self.ptr_array) } as usize;
let current_array_end = current_array_start + used_ptrs;
let ptr_array =
unsafe { std::slice::from_raw_parts_mut(self.ptr_array, self.ptr_array_size) };
unsafe { std::slice::from_raw_parts_mut(self.ptr_array, self.ptr_array_size as usize) };

let new_array_start;

Expand Down Expand Up @@ -346,9 +350,9 @@ impl<'a, T: 'a, A: Allocator> Deque<'a, T, A> {
ptr_array.copy_within(current_array_start..current_array_end, new_array_start);
} else {
let new_ptr_array_size =
self.ptr_array_size + self.ptr_array_size.max(additional_capacity) + 2;
self.ptr_array_size + self.ptr_array_size.max(additional_capacity as u32) + 2;
// allocate at least double + 2 pointers
let new_ptr_array = self.allocate_ptr_array(new_ptr_array_size);
let new_ptr_array = self.allocate_ptr_array(new_ptr_array_size as usize);

// copy the old pointers over
new_array_start = unused_ptrs_at_front + if front { additional_capacity } else { 0 };
Expand Down Expand Up @@ -376,6 +380,12 @@ impl<'a, T: 'a, A: Allocator> Deque<'a, T, A> {
}
}

impl<'a, T: 'a + Debug, A: Allocator> Debug for Deque<'a, T, A> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[ {:?} ]", self.iter().format(", "))
}
}

impl<'a, T: 'a, A: Allocator + Default> Default for Deque<'a, T, A> {
fn default() -> Self {
Self::new()
Expand Down
7 changes: 7 additions & 0 deletions src/queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::allocator::{Allocator, DefaultAllocator};
use crate::deque::iter::{Iter, IterMut};
use crate::deque::Deque;
use std::fmt::{Debug, Formatter};

/// Queue with the default allocator.
pub type DefaultQueue<'a, V> = Queue<'a, V, DefaultAllocator>;
Expand Down Expand Up @@ -80,6 +81,12 @@ impl<'a, T: 'a, A: Allocator> Queue<'a, T, A> {
}
}

impl<'a, T: 'a + Debug, A: Allocator> Debug for Queue<'a, T, A> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.deque.fmt(f)
}
}

impl<'a, T: 'a, A: Allocator + Default> Default for Queue<'a, T, A> {
fn default() -> Self {
Self::new()
Expand Down

0 comments on commit de85263

Please sign in to comment.