grit_bitvec/
static_proto_bitvec.rs

1use std::ops::RangeFrom;
2
3use crate::{
4    BitProto, 
5    RawBitVec, 
6    SProtoBitVecDrain,
7    SProtoBitVecIter,
8    Range,
9    ManuallyDrop
10};
11
12/// ## `SProtoBitVec`: "Static Prototype Bitwise Vector"  
13/// A `BitVec` where the bit-width and masking data ([`BitProto`]) is saved in static reference to the associated [`BitProto`]
14/// 
15/// This is a thin wrapper around [`RawBitVec`] that simply calls the underlying raw method and passes the associated
16/// [`BitProto`] along with it. Unlike [`RawBitVec`] this is safe because it is impossible to ever use the wrong [`BitProto`]
17/// 
18/// ### Pros
19/// - Simpler, safer API than [`RawBitVec`]
20/// - No mono-morphization (smaller binary than [`CProtoBitVec`](crate::const_proto_bitvec::CProtoBitVec))
21/// - Can store [`SProtoBitVec`]'s in a homogenous collection (`Array`, [`Vec`], [`HashMap`](std::collections::HashMap), etc.)
22/// 
23/// ### Cons
24/// - One extra pointer to the static [`BitProto`] stored in every [`SProtoBitVec`] (4 usize total)
25/// - No constant-propogation optimizations
26pub struct SProtoBitVec {
27    pub(crate) proto: &'static BitProto,
28    pub(crate) vec: RawBitVec
29}
30
31impl SProtoBitVec {
32    #[inline(always)]
33    pub fn len(&self) -> usize {
34        self.vec.len
35    }
36
37    #[inline(always)]
38    pub fn cap(&self) -> usize {
39        unsafe {self.vec.cap(*self.proto)}
40    }
41
42    #[inline(always)]
43    pub fn free(&self) -> usize {
44        unsafe{self.vec.free(*self.proto)}
45    }
46
47    #[inline(always)]
48    pub fn new(proto_ref: &'static BitProto) -> Self {
49        Self { proto: proto_ref, vec: RawBitVec::new() }
50    }
51
52    #[inline(always)]
53    pub fn with_capacity(proto_ref: &'static BitProto, cap: usize) -> Self {
54        Self { proto: proto_ref, vec: RawBitVec::with_capacity(*proto_ref, cap) }
55    }
56
57    #[inline(always)]
58    pub fn grow_exact_for_additional_elements_if_needed(&mut self, extra_elements: usize) -> Result<(), String> {
59        unsafe {self.vec.grow_exact_for_additional_elements_if_needed(*self.proto, extra_elements)}
60    }
61
62    #[inline(always)]
63    pub fn grow_exact_for_total_elements_if_needed(&mut self, total_elements: usize) -> Result<(), String> {
64        unsafe {self.vec.grow_exact_for_total_elements_if_needed(*self.proto, total_elements)}
65    }
66
67    #[inline(always)]
68    pub fn grow_for_additional_elements_if_needed(&mut self, extra_elements: usize) -> Result<(), String> {
69        unsafe {self.vec.grow_for_additional_elements_if_needed(*self.proto, extra_elements)}
70    }
71
72    #[inline(always)]
73    pub fn grow_for_total_elements_if_needed(&mut self, total_elements: usize) -> Result<(), String> {
74        unsafe {self.vec.grow_for_total_elements_if_needed(*self.proto, total_elements)}
75    }
76
77    #[inline(always)]
78    pub fn clear(&mut self) {
79        self.vec.clear()
80    }
81
82    #[inline(always)]
83    pub fn push(&mut self, val: usize) -> Result<(), String> {
84        unsafe {self.vec.push(*self.proto, val)}
85    }
86
87    #[inline(always)]
88    pub unsafe fn push_unchecked(&mut self, val: usize) {
89        self.vec.push_unchecked(*self.proto, val)
90    }
91
92    #[inline(always)]
93    pub fn pop(&mut self) -> Result<usize, String> {
94        unsafe{self.vec.pop(*self.proto)}
95    }
96
97    #[inline(always)]
98    pub unsafe fn pop_unchecked(&mut self) -> usize {
99        self.vec.pop_unchecked(*self.proto)
100    }
101
102    #[inline(always)]
103    pub fn insert(&mut self, idx: usize, val: usize) -> Result<(), String> {
104        unsafe{self.vec.insert(*self.proto, idx, val)}
105    }
106
107    #[inline(always)]
108    pub unsafe fn insert_unchecked(&mut self, idx: usize, val: usize) {
109        self.vec.insert_unchecked(*self.proto, idx, val)
110    }
111
112    #[inline(always)]
113    pub fn insert_bitvec(&mut self, insert_idx: usize, bitvec: Self) -> Result<(), String> {
114        unsafe{self.vec.insert_bitvec(*self.proto, insert_idx, bitvec.into_raw())}
115    }
116
117    #[inline(always)]
118    pub unsafe fn insert_bitvec_unchecked(&mut self, insert_idx: usize, bitvec: Self) {
119        self.vec.insert_bitvec_unchecked(*self.proto, insert_idx, bitvec.into_raw())
120    }
121
122    #[inline]
123    pub fn insert_iter<II, TO, ESI>(&mut self, insert_idx: usize, source: II) -> Result<(), String>
124    where II: IntoIterator<Item = TO, IntoIter = ESI>, TO: ToOwned<Owned = usize>, ESI: ExactSizeIterator + Iterator<Item = TO> {
125        unsafe {self.vec.insert_iter(*self.proto, insert_idx, source)}
126    }
127
128    #[inline]
129    pub unsafe fn insert_iter_unchecked<II, TO, ESI>(&mut self, insert_idx: usize, source: II)
130    where II: IntoIterator<Item = TO, IntoIter = ESI>, TO: ToOwned<Owned = usize>, ESI: ExactSizeIterator + Iterator<Item = TO> {
131        self.vec.insert_iter_unchecked(*self.proto, insert_idx, source)
132    }
133
134    #[inline(always)]
135    pub fn remove(&mut self, idx: usize) -> Result<usize, String> {
136        unsafe{self.vec.remove(*self.proto, idx)}
137    }
138
139    #[inline(always)]
140    pub unsafe fn remove_unchecked(&mut self, idx: usize) -> usize {
141        self.vec.remove_unchecked(*self.proto, idx)
142    }
143
144    #[inline(always)]
145    pub fn remove_range(&mut self, idx_range: Range<usize>) -> Result<Self, String> {
146        match unsafe{self.vec.remove_range(*self.proto, idx_range)} {
147            Ok(raw) => Ok(Self{
148                proto: self.proto,
149                vec: raw
150            }),
151            Err(e) => Err(e),
152        }
153    }
154
155    #[inline(always)]
156    pub unsafe fn remove_range_unchecked(&mut self, idx_range: Range<usize>) -> Self {
157        Self {
158            proto: self.proto,
159            vec: self.vec.remove_range_unchecked(*self.proto, idx_range)
160        }
161    }
162
163    #[inline(always)]
164    pub fn trim_range(&mut self, idx_range: RangeFrom<usize>) -> Result<Self, String> {
165        match unsafe{self.vec.trim_range(*self.proto, idx_range)} {
166            Ok(raw) => Ok(Self{
167                proto: self.proto,
168                vec: raw
169            }),
170            Err(e) => Err(e),
171        }
172    }
173
174    #[inline(always)]
175    pub unsafe fn trim_range_unchecked(&mut self, idx_range: RangeFrom<usize>) -> Self {
176        Self {
177            proto: self.proto,
178            vec: self.vec.trim_range_unchecked(*self.proto, idx_range)
179        }
180    }
181
182    #[inline(always)]
183    pub fn swap(&mut self, idx_a: usize, idx_b: usize) -> Result<(), String> {
184        unsafe{self.vec.swap(*self.proto, idx_a, idx_b)}
185    }
186
187    #[inline(always)]
188    pub unsafe fn swap_unchecked(&mut self, idx_a: usize, idx_b: usize) {
189        self.vec.swap_unchecked(*self.proto, idx_a, idx_b)
190}
191
192    #[inline(always)]
193    pub fn swap_pop(&mut self, idx: usize) -> Result<usize, String> {
194        unsafe{self.vec.swap_pop(*self.proto, idx)}
195    }
196
197    #[inline(always)]
198    pub unsafe fn swap_pop_unchecked(&mut self, idx: usize) -> usize {
199        self.vec.swap_pop_unchecked(*self.proto, idx)
200    }
201
202    #[inline(always)]
203    pub fn trim_excess_capacity(&mut self, extra_capacity_to_keep: usize) -> Result<(), String> {
204        unsafe{self.vec.trim_excess_capacity(*self.proto, extra_capacity_to_keep)}
205    }
206
207    #[inline(always)]
208    pub fn append_bitvec(&mut self, bitvec: Self) -> Result<(), String> {
209        unsafe{self.vec.append_bitvec(*self.proto, bitvec.into_raw())}
210    }
211
212    #[inline(always)]
213    pub unsafe fn append_bitvec_unchecked(&mut self, bitvec: Self) {
214        self.vec.append_bitvec_unchecked(*self.proto, bitvec.into_raw())
215    }
216
217    #[inline(always)]
218    pub fn append_iter<II, TO, ESI>(&mut self, source: II) -> Result<(), String>
219    where II: IntoIterator<Item = TO, IntoIter = ESI>, TO: ToOwned<Owned = usize>, ESI: ExactSizeIterator + Iterator<Item = TO> {
220        unsafe{self.vec.append_iter(*self.proto, source)}
221    }
222
223    #[inline(always)]
224    pub unsafe fn append_iter_unchecked<I, TO>(&mut self, iter: I)
225    where I: Iterator<Item = TO> + ExactSizeIterator, TO: ToOwned<Owned = usize> {
226        self.vec.append_iter_unchecked(*self.proto, iter)
227    }
228
229    #[inline(always)]
230    pub fn get(&self, idx: usize) -> Result<usize, String> {
231        unsafe{self.vec.get(*self.proto, idx)}
232    }
233
234    #[inline(always)]
235    pub unsafe fn get_unchecked(&self, idx: usize) -> usize {
236        self.vec.get_unchecked(*self.proto, idx)
237    }
238
239    #[inline(always)]
240    pub fn replace(&mut self, idx: usize, val: usize) -> Result<usize, String> {
241        unsafe{self.vec.replace(*self.proto, idx, val)}
242    }
243
244    #[inline(always)]
245    pub unsafe fn replace_unchecked(&mut self, idx: usize, val: usize) -> usize {
246        self.vec.replace_unchecked(*self.proto, idx, val)
247    }
248
249    #[inline(always)]
250    pub fn set(&mut self, idx: usize, val: usize) -> Result<(), String> {
251        unsafe{self.vec.set(*self.proto, idx, val)}
252    }
253
254    #[inline(always)]
255    pub unsafe fn set_unchecked(&mut self, idx: usize, val: usize) {
256        self.vec.set_unchecked(*self.proto, idx, val)
257    }
258
259    #[inline]
260    pub fn discard_from_end(&mut self, count: usize) {
261        self.vec.discard_from_end(count)
262    }
263
264    #[inline(always)]
265    pub fn drain<'vec>(&'vec mut self) -> SProtoBitVecDrain<'vec> {
266        SProtoBitVecDrain{
267            proto: self.proto,
268            drain: self.vec.drain()
269        }
270    }
271
272    #[inline(always)]
273    pub unsafe fn into_raw(self) -> RawBitVec {
274        let nodrop_self = ManuallyDrop::new(self);
275        RawBitVec {
276            ptr: nodrop_self.vec.ptr,
277            len: nodrop_self.vec.len, 
278            true_cap: nodrop_self.vec.true_cap 
279        }
280    }
281}
282
283impl IntoIterator for SProtoBitVec {
284    type Item = usize;
285
286    type IntoIter = SProtoBitVecIter;
287
288    #[inline(always)]
289    fn into_iter(self) -> Self::IntoIter {
290        SProtoBitVecIter{
291            proto: self.proto,
292            iter: unsafe{self.into_raw().into_iter()}
293        }
294    }
295}
296
297impl Drop for SProtoBitVec {
298    #[inline(always)]
299    fn drop(&mut self) {/* RawBitVec::drop() will take care of the deallocation */}
300}