Skip to content

Commit 0d11d87

Browse files
authored
Merge pull request #28 from orxfun/get_ptr_mut-and-useful-From-implementations
`get_ptr_mut` and useful `From` implementations
2 parents 77d3ede + ae56dec commit 0d11d87

31 files changed

+642
-191
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "orx-split-vec"
3-
version = "2.3.0"
3+
version = "2.4.0"
44
edition = "2021"
55
authors = ["orxfun <[email protected]>"]
66
description = "An efficient constant access time vector with dynamic capacity and pinned elements."
@@ -10,7 +10,7 @@ keywords = ["vec", "array", "split", "fragments", "pinned"]
1010
categories = ["data-structures", "rust-patterns"]
1111

1212
[dependencies]
13-
orx-pinned-vec = "2.2"
13+
orx-pinned-vec = "2.3"
1414

1515
[[bench]]
1616
name = "serial_access"

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ The second method `fn get_fragment_and_inner_indices<T>(&self, vec_len: usize, f
7676
### D.1. Usage similar to `std::vec::Vec`
7777

7878
```rust
79-
use orx_split_vec::prelude::*;
79+
use orx_split_vec::*;
8080

8181
let mut vec = SplitVec::new();
8282

@@ -101,7 +101,7 @@ assert_eq!(&std_vec, &[0, 1, 2, 3]);
101101
### D.2. `SplitVec` Specific Operations
102102

103103
```rust
104-
use orx_split_vec::prelude::*;
104+
use orx_split_vec::*;
105105

106106
#[derive(Clone)]
107107
struct MyCustomGrowth;
@@ -163,7 +163,7 @@ assert_eq!(slice[1], &[4]);
163163
Unless elements are removed from the vector, the memory location of an element already pushed to the `SplitVec` <ins>never</ins> changes unless explicitly changed.
164164

165165
```rust
166-
use orx_split_vec::prelude::*;
166+
use orx_split_vec::*;
167167

168168
let mut vec = SplitVec::new(); // Doubling growth as the default strategy
169169

benches/append.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
2-
use orx_split_vec::prelude::*;
2+
use orx_split_vec::*;
33
use rand::prelude::*;
44
use rand_chacha::ChaCha8Rng;
55

benches/grow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use criterion::{
22
black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, BenchmarkId,
33
Criterion,
44
};
5-
use orx_split_vec::prelude::*;
5+
use orx_split_vec::*;
66

77
fn get_value<const N: usize>(i: usize) -> [u64; N] {
88
let modulo = i % 3;

benches/random_access.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use criterion::{
22
black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, BenchmarkId,
33
Criterion,
44
};
5-
use orx_split_vec::prelude::*;
5+
use orx_split_vec::*;
66
use rand::prelude::*;
77
use rand_chacha::ChaCha8Rng;
88

benches/serial_access.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use criterion::{
22
black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, BenchmarkId,
33
Criterion,
44
};
5-
use orx_split_vec::prelude::*;
5+
use orx_split_vec::*;
66

77
fn get_value<const N: usize>(i: usize) -> [u64; N] {
88
let modulo = i % 3;

src/common_traits/debug.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ where
1717

1818
#[cfg(test)]
1919
mod tests {
20-
use crate::prelude::*;
20+
use crate::*;
2121

2222
#[test]
2323
fn debug() {
2424
let mut vec = SplitVec::with_doubling_growth();
25-
for i in 0..8 {
25+
for i in 0..13 {
2626
vec.push(i);
2727
}
2828

2929
let debug_str = format!("{:?}", vec);
3030
assert_eq!(
31-
"SplitVec [\n [0, 1, 2, 3]\n [4, 5, 6, 7]\n]\n",
31+
"SplitVec [\n [0, 1, 2, 3]\n [4, 5, 6, 7, 8, 9, 10, 11]\n [12]\n]\n",
3232
debug_str
3333
);
3434
}

src/common_traits/eq.rs

+72-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{eq::are_fragments_eq_to_slice, Growth, SplitVec};
1+
use crate::*;
22

33
impl<T, G, U> PartialEq<U> for SplitVec<T, G>
44
where
@@ -11,10 +11,76 @@ where
1111
}
1212
}
1313

14+
impl<T: PartialEq, G> PartialEq<SplitVec<T, G>> for [T]
15+
where
16+
G: Growth,
17+
{
18+
fn eq(&self, other: &SplitVec<T, G>) -> bool {
19+
are_fragments_eq_to_slice(&other.fragments, self)
20+
}
21+
}
22+
23+
impl<T: PartialEq, G> PartialEq<SplitVec<T, G>> for Vec<T>
24+
where
25+
G: Growth,
26+
{
27+
fn eq(&self, other: &SplitVec<T, G>) -> bool {
28+
are_fragments_eq_to_slice(&other.fragments, self)
29+
}
30+
}
31+
32+
impl<T: PartialEq, G, const N: usize> PartialEq<SplitVec<T, G>> for [T; N]
33+
where
34+
G: Growth,
35+
{
36+
fn eq(&self, other: &SplitVec<T, G>) -> bool {
37+
are_fragments_eq_to_slice(&other.fragments, self)
38+
}
39+
}
40+
41+
impl<T: PartialEq, G> PartialEq<SplitVec<T, G>> for SplitVec<T, G>
42+
where
43+
G: Growth,
44+
{
45+
fn eq(&self, other: &SplitVec<T, G>) -> bool {
46+
let mut iter1 = self.iter();
47+
let mut iter2 = other.iter();
48+
loop {
49+
match (iter1.next(), iter2.next()) {
50+
(Some(x), Some(y)) => {
51+
if x != y {
52+
return false;
53+
}
54+
}
55+
(None, None) => return true,
56+
_ => return false,
57+
}
58+
}
59+
}
60+
}
61+
62+
impl<T: PartialEq, G: Growth> Eq for SplitVec<T, G> {}
63+
64+
pub(crate) fn are_fragments_eq_to_slice<T: PartialEq>(
65+
fragments: &[Fragment<T>],
66+
slice: &[T],
67+
) -> bool {
68+
let mut slice_beg = 0;
69+
for fragment in fragments {
70+
let slice_end = slice_beg + fragment.len();
71+
let slice_of_slice = &slice[slice_beg..slice_end];
72+
if fragment.data != slice_of_slice {
73+
return false;
74+
}
75+
slice_beg = slice_end;
76+
}
77+
true
78+
}
79+
1480
#[cfg(test)]
1581
mod tests {
16-
use crate::prelude::*;
1782
use crate::test_all_growth_types;
83+
use crate::*;
1884

1985
#[test]
2086
fn eq() {
@@ -24,8 +90,12 @@ mod tests {
2490
}
2591

2692
let eq_vec: Vec<_> = (0..vec.capacity()).collect();
93+
let eq_vec_as_ref: &[usize] = eq_vec.as_ref();
94+
assert_eq!(vec, eq_vec_as_ref);
95+
assert_eq!(&vec, &eq_vec);
2796
assert_eq!(vec, eq_vec);
2897
}
98+
2999
test_all_growth_types!(test);
30100
}
31101
}

src/common_traits/index.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ where
1616
/// # Examples
1717
///
1818
/// ```
19-
/// use orx_split_vec::prelude::*;
19+
/// use orx_split_vec::*;
2020
///
2121
/// let mut vec = SplitVec::with_linear_growth(4);
2222
///
@@ -47,7 +47,7 @@ where
4747
/// # Examples
4848
///
4949
/// ```
50-
/// use orx_split_vec::prelude::*;
50+
/// use orx_split_vec::*;
5151
///
5252
/// let mut vec = SplitVec::with_linear_growth(2);
5353
///
@@ -93,7 +93,7 @@ where
9393
/// `(i / N, i % N)`.
9494
///
9595
/// ```
96-
/// use orx_split_vec::prelude::*;
96+
/// use orx_split_vec::*;
9797
///
9898
/// let mut vec = SplitVec::with_linear_growth(2);
9999
///
@@ -150,7 +150,7 @@ where
150150
/// `(i / N, i % N)`.
151151
///
152152
/// ```
153-
/// use orx_split_vec::prelude::*;
153+
/// use orx_split_vec::*;
154154
///
155155
/// let mut vec = SplitVec::with_linear_growth(2);
156156
///
@@ -189,8 +189,8 @@ where
189189

190190
#[cfg(test)]
191191
mod tests {
192-
use crate::prelude::*;
193192
use crate::test_all_growth_types;
193+
use crate::*;
194194

195195
#[test]
196196
fn index() {

src/common_traits/iterator/iter.rs

+18
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,22 @@ mod tests {
113113
}
114114
test_all_growth_types!(test);
115115
}
116+
117+
#[test]
118+
fn clone() {
119+
fn test<G: Growth>(mut vec: SplitVec<usize, G>) {
120+
let n = 564;
121+
let stdvec: Vec<_> = (0..n).collect();
122+
vec.extend(stdvec);
123+
124+
let iter1 = vec.iter();
125+
let iter2 = iter1.clone();
126+
127+
for (i, (a, b)) in iter1.zip(iter2).enumerate() {
128+
assert_eq!(i, *a);
129+
assert_eq!(i, *b);
130+
}
131+
}
132+
test_all_growth_types!(test);
133+
}
116134
}

src/common_traits/iterator/iter_rev.rs

+18
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,22 @@ mod tests {
116116
}
117117
test_all_growth_types!(test);
118118
}
119+
120+
#[test]
121+
fn clone() {
122+
fn test<G: Growth>(mut vec: SplitVec<usize, G>) {
123+
let n = 564;
124+
let stdvec: Vec<_> = (0..n).collect();
125+
vec.extend(stdvec);
126+
127+
let iter1 = vec.iter_rev();
128+
let iter2 = iter1.clone();
129+
130+
for (i, (a, b)) in iter1.zip(iter2).enumerate() {
131+
assert_eq!(n - i - 1, *a);
132+
assert_eq!(n - i - 1, *b);
133+
}
134+
}
135+
test_all_growth_types!(test);
136+
}
119137
}

src/eq.rs

-68
This file was deleted.

0 commit comments

Comments
 (0)