1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use core::nonzero::NonZero;
use core::raw::Slice as RawSlice;
use alloc::heap::EMPTY;
use alloc::heap::allocate;
use alloc::heap::deallocate;
use std::usize;
use std::ptr::copy_nonoverlapping_memory;
use std::mem;
use std::ptr;
use std::num::Int;
use std::ops::{Index, IndexMut};
#[unsafe_no_drop_flag]
pub struct HeapArray<A> {
pointer: NonZero<*mut A>,
capacity: usize,
}
impl<A> HeapArray<A> {
#[inline]
pub fn with_capacity(capacity: usize) -> HeapArray<A> {
let a_size = mem::size_of::<A>();
if a_size == 0 {
HeapArray {
pointer: unsafe {
NonZero::new(EMPTY as *mut A)
},
capacity: usize::MAX,
}
} else if capacity == 0 {
HeapArray {
pointer: unsafe {
NonZero::new(EMPTY as *mut A)
},
capacity: 0,
}
} else {
let bytes = capacity.checked_mul(a_size).expect("capacity overflow");
let pointer = unsafe {
allocate(bytes, mem::min_align_of::<A>())
};
if pointer.is_null() { ::alloc::oom() }
HeapArray {
pointer: unsafe {
NonZero::new(pointer as *mut A)
},
capacity: capacity,
}
}
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [A] {
unsafe {
mem::transmute(RawSlice {
data: *self.pointer,
len: self.capacity,
})
}
}
pub fn copy(&self, capacity: usize) -> HeapArray<A> {
let mut new_array = HeapArray::with_capacity(capacity);
unsafe {
copy_nonoverlapping_memory(&mut new_array[0], &self[0], self.capacity);
}
new_array
}
pub fn swap(&mut self, a: usize, b: usize) {
unsafe {
let ptr_a = self.pointer.offset(a as isize);
let ptr_b = self.pointer.offset(b as isize);
ptr::swap(ptr_a, ptr_b);
}
}
pub fn capacity(&self) -> usize {
self.capacity
}
}
impl<A> AsSlice<A> for HeapArray<A> {
#[inline]
fn as_slice<'a>(&'a self) -> &'a [A] {
unsafe {
mem::transmute(RawSlice {
data: *self.pointer,
len: self.capacity,
})
}
}
}
impl<A> Index<usize> for HeapArray<A> {
type Output = A;
#[inline]
fn index<'a>(&'a self, index: &usize) -> &'a A {
&self.as_slice()[*index]
}
}
impl<A> IndexMut<usize> for HeapArray<A> {
type Output = A;
#[inline]
fn index_mut<'a>(&'a mut self, index: &usize) -> &'a mut A {
&mut self.as_mut_slice()[*index]
}
}
#[inline]
unsafe fn dealloc<T>(ptr: *mut T, len: usize) {
if mem::size_of::<T>() != 0 {
deallocate(ptr as *mut u8,
len * mem::size_of::<T>(),
mem::min_align_of::<T>())
}
}
#[test]
fn basic_tests() {
let mut a = HeapArray::with_capacity(10);
assert_eq!(10, a.capacity());
a[0] = 5u8;
a[1] = 10u8;
assert_eq!(15, a[0] + a[1]);
unsafe {
let ptr: *mut u8 = mem::transmute(&(a[0]));
*(ptr.offset(2)) = 20u8;
}
assert_eq!(20u8, a[2]);
}
#[test]
fn copy_test() {
let mut a = HeapArray::with_capacity(2);
a[0] = 5u8;
a[1] = 10u8;
let b = a.copy(4);
assert_eq!(5u8, a[0]);
assert_eq!(10u8, a[1]);
a[0] = 6u8;
a[1] = 11u8;
assert_eq!(5u8, b[0]);
assert_eq!(10u8, b[1]);
assert_eq!(6u8, a[0]);
assert_eq!(11u8, a[1]);
assert_eq!(4, b.capacity());
}