-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.rs
404 lines (377 loc) · 10.7 KB
/
array.rs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
#![feature(collections_range)]
#![feature(drain_filter)]
#![feature(slice_rsplit)]
#![feature(slice_get_slice)]
#![feature(vec_resize_default)]
#![feature(vec_remove_item)]
#![feature(collections_range)]
#![feature(slice_rotate)]
#![feature(swap_with_slice)]
*/
foo bar baz
use collections::range::RangeArgument;
use std::cmp::Ordering;
use std::borrow::Borrow;
use std::vec::{Drain,Splice,DrainFilter};
use std::ops::{Deref,DerefMut,Index,IndexMut};
use std::slice::{Iter,IterMut,Windows,Chunks,ChunksMut,Split,SplitMut,RSplit,RSplitMut,RSplitN,RSplitNMut,SplitN,SplitNMut,SliceIndex};
use std::marker::PhantomData; // this sucks!
//todo, how to handle 'enumerate'.
// would we have to impl 'my_enumerate' or something?
// wrapper for Vec<T> with indexing defaulting to i32
// todo , real vector impl, with smallvec stuff
pub trait IndexTrait { // TODO - would be better to use official from/into, but it doesn't let us impl
fn my_from(x:usize)->Self;
fn my_into(self)->usize;
}
impl IndexTrait for i32{
fn my_from(x:usize)->Self{x as Self}
fn my_into(self)->usize{self as usize}
}
// grrr. can't impl theirs this way round?!
//trait MyInto {
//}
//TODO - wrapper or macro to roll a 'strongly typed index'
// e.g. I32<Polygon>
/*
impl Into<usize> for i32{
fn into(self)->usize{ self as usize }
}
impl Into<usize> for u32{
fn into(self)->usize{ self as usize }
}
impl Into<usize> for i16{
fn into(self)->usize{ self as usize }
}
impl Into<usize> for u32{
fn into(self)->usize{ self as usize }
}
impl Into<usize> for i8{
fn into(self)->usize{ self as usize }
}
impl Into<usize> for u8{
fn into(self)->usize{ self as usize }
}
impl Into<usize> for isize{
fn into(self)->usize{ self as usize }
}
*/
#[derive(Debug)]
pub struct Array<T,I=i32>(pub Vec<T>,PhantomData<I>);
// my array helper fn's
impl<T:Clone,I:IndexTrait+Clone> Array<T,I>{
/// TODO - better name. preserves ordering of vec![v;count].
pub fn from_val_n(val:T, n:i32)->Self{
let v=vec![val; n as usize];
Array(v,PhantomData)
}
pub fn from_fn<F:Fn(I)->T>(count:I,f:F)->Self{
let mut v=Vec::new();
v.reserve(count.clone().my_into());
for x in 0..count.my_into() {v.push(f(I::my_from(x)))}
Array(v,PhantomData)
}
pub fn map<B,F:Fn(&T)->B>(&self,f:F)->Array<B,I>{
let mut out=Array::<B,I>::new();
out.reserve(self.len());
for x in self.iter(){
out.push(f(x))
}
out
}
}
impl<T,I:IndexTrait+Clone> Array<T,I>{
pub fn num_elems(&self)->i32{ self.0.len() as i32} // TODO - figure out generic int
pub fn new()->Self{ Array(Vec::new(),PhantomData) }
pub fn reserve(&mut self, additional: I){
self.0.reserve(additional.my_into());
}
pub fn push(&mut self,val:T){self.0.push(val)}
pub fn shrink_to_fit(&mut self){self.0.shrink_to_fit()}
pub fn truncate(&mut self, len: I){
self.0.truncate(len.my_into());
}
pub fn as_slice(&self) -> &[T]{
self.0.as_slice()
}
pub fn as_mut_slice(&mut self) -> &mut [T]{
self.0.as_mut_slice()
}
pub fn swap_remove(&mut self, index: I) -> T{
self.0.swap_remove(index.my_into())
}
pub fn insert(&mut self, index: I, element: T){
self.0.insert(index.my_into(),element)
}
pub fn remove(&mut self, index: I) -> T{
self.0.remove(index.my_into())
}
// aka filter in place
pub fn retain<F:FnMut(&T)->bool>(&mut self, f: F) {
self.0.retain(f)
}
pub fn dedup_by_key<F:FnMut(&mut T)->K, K:PartialEq<K>>(&mut self, key: F) {
self.0.dedup_by_key(key)
}
pub fn dedup_by<F:FnMut(&mut T,&mut T)->bool>(&mut self, same_bucket: F) {
self.0.dedup_by(same_bucket)
}
#[cfg(nightly_vector)]
pub fn place_back(&mut self) -> PlaceBack<T>{
self.0.place_back()
}
pub fn pop(&mut self) -> Option<T>{
self.0.pop()
}
pub fn append(&mut self, other: &mut Vec<T>){
self.0.append(other)
}
#[cfg(UseRangeArgument)]
pub fn drain<R:RangeArgument<I>>(&mut self, range: R) -> Drain<T>
{
self.0.drain(range)
}
pub fn clear(&mut self){
self.0.clear()
}
// pub fn len(&self)->I{
// self.0.len() as Index
// }
// pub fn is_empty(&self)->bool{ self.0.is_empty()}
pub fn split_off(&mut self,at:I)->Array<T>{
Array(self.0.split_off(at.my_into()),PhantomData)
}
}
impl<T:Clone,I:IndexTrait> Array<T,I>{
pub fn resize(&mut self, new_len:I, value:T){
self.0.resize(new_len.my_into(),value)
}
pub fn extend_from_slice(&mut self, other:&[T]){
self.0.extend_from_slice(other)
}
}
impl<T:Default,I:IndexTrait> Array<T,I>{
pub fn resize_default(&mut self, new_len:I){
self.0.resize_default(new_len.my_into())
}
}
impl<T:PartialEq<T>,I:IndexTrait> Array<T,I>{
pub fn dedup(&mut self){
self.0.dedup()
}
pub fn remove_item(&mut self, item:&T)->Option<T>{
self.0.remove_item(item)
}
}
impl<T,INDEX:IndexTrait> Array<T,INDEX>{
/// TODO - figure out how to convert RangeArguemnt indices
pub fn splice<I:IntoIterator<Item=T>,R:RangeArgument<usize>>(&mut self, range:R, replace_with:I)-> Splice<<I as IntoIterator>::IntoIter>
{
self.0.splice(range,replace_with)
}
pub fn drain_filter<F:FnMut(&mut T)->bool>(&mut self, filter: F) -> DrainFilter<T, F> {
self.0.drain_filter(filter)
}
}
impl<T,INDEX:IndexTrait> Deref for Array<T,INDEX>{
type Target=[T];
fn deref(&self)->&Self::Target { self.0.deref() }
}
impl<T,INDEX:IndexTrait> Array<T,INDEX>{
fn len(&self)->INDEX{INDEX::my_from(self.0.len())}
fn is_empty(&self)->bool{self.0.is_empty()}
fn first(&self)->Option<&T>{self.0.first()}
fn first_mut(&mut self)->Option<&mut T>{self.0.first_mut()}
fn split_first(&self)->Option<(&T,&[T])>{self.0.split_first()}
fn split_first_mut(&mut self)->Option<(&mut T, &mut [T])>{ self.0.split_first_mut() }
fn split_last(&self)->Option<(&T,&[T])>{self.0.split_last()}
fn split_last_mut(&mut self)->Option<(&mut T, &mut[T])>{self.0.split_last_mut()}
fn last(&self)->Option<&T>{self.0.last()}
fn last_mut(&mut self)->Option<&mut T>{self.0.last_mut()}
fn get<I>(&self, index:I)->Option<&<I as SliceIndex<[T]> >::Output>
where I:SliceIndex<[T]>
{
self.0.get(index)
}
fn get_mut<I>(&mut self, index:I)->Option<&mut <I as SliceIndex<[T]>>::Output>
where I:SliceIndex<[T]>
{
self.0.get_mut(index)
}
unsafe fn get_unchecked<I>(&self, index: I) -> &<I as SliceIndex<[T]>>::Output
where
I: SliceIndex<[T]> {self.0.get_unchecked(index)}
unsafe fn get_unchecked_mut<I>(
&mut self,
index: I
) -> &mut <I as SliceIndex<[T]>>::Output
where
I: SliceIndex<[T]>{
self.0.get_unchecked_mut(index)
}
fn as_ptr(&self)->*const T{self.0.as_ptr()}
fn as_mut_ptr(&mut self)->*mut T{self.0.as_mut_ptr()}
fn swap(&mut self, a:INDEX,b:INDEX){
self.0.swap(a.my_into(),b.my_into())
}
fn reverse(&mut self){self.0.reverse()}
fn iter(&self)->Iter<T>{self.0.iter()}
fn iter_mut(&mut self)->IterMut<T>{self.0.iter_mut()}
fn windows(&self,size:INDEX)->Windows<T>{self.0.windows(size.my_into())}
fn chunks(&self,chunk_size:INDEX)->Chunks<T>{self.0.chunks(chunk_size.my_into())}
fn chunks_mut(&mut self,chunk_size:INDEX)->ChunksMut<T>{self.0.chunks_mut(chunk_size.my_into())}
fn split_at(&self, mid: INDEX) -> (&[T], &[T]){
self.0.split_at(mid.my_into())
}
fn split_at_mut(&mut self, mid: INDEX) -> (&mut [T], &mut [T]){
self.0.split_at_mut(mid.my_into())
}
fn split<F>(&self, pred: F) -> Split<T, F>
where F:FnMut(&T)->bool
{
self.0.split(pred)
}
fn split_mut<F>(&mut self, pred: F) -> SplitMut<T, F>
where F: FnMut(&T) -> bool
{
self.0.split_mut(pred)
}
fn rsplit<F>(&self, pred: F) -> RSplit<T, F>
where F: FnMut(&T) -> bool,
{
self.0.rsplit(pred)
}
fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<T, F>
where F: FnMut(&T) -> bool
{
self.0.rsplit_mut(pred)
}
fn splitn<F>(&self, n: INDEX, pred: F) -> SplitN<T, F>
where F: FnMut(&T) -> bool
{
self.0.splitn(n.my_into(),pred)
}
fn splitn_mut<F>(&mut self, n: INDEX, pred: F) -> SplitNMut<T, F>
where F: FnMut(&T) -> bool
{
self.0.splitn_mut(n.my_into(),pred)
}
fn rsplitn<F>(&self, n: INDEX, pred: F) -> RSplitN<T, F>
where F: FnMut(&T) -> bool{
self.0.rsplitn(n.my_into(),pred)
}
fn rsplitn_mut<F>(&mut self, n: INDEX, pred: F) -> RSplitNMut<T, F>
where
F: FnMut(&T) -> bool{
self.0.rsplitn_mut(n.my_into(),pred)
}
fn contains(&self, x: &T) -> bool
where
T: PartialEq<T>{
self.0.contains(x)
}
fn starts_with(&self, needle: &[T]) -> bool
where
T: PartialEq<T>{
self.0.starts_with(needle)
}
fn ends_with(&self, needle: &[T]) -> bool
where
T: PartialEq<T>{
self.0.ends_with(needle)
}
fn binary_search(&self, a: &T) -> Result<INDEX, INDEX>
where
T: Ord{
match self.0.binary_search(a){
Ok(x)=>Ok(INDEX::my_from(x)),
Err(x)=>Err(INDEX::my_from(x))
}
}
fn binary_search_by<'a, F>(&'a self, f: F) -> Result<INDEX, INDEX>
where F: FnMut(&'a T) -> Ordering{
match self.0.binary_search_by(f){
Ok(x)=>Ok(INDEX::my_from(x)),
Err(x)=>Err(INDEX::my_from(x))
}
}
fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<INDEX, INDEX>
where
B: Ord,
F: FnMut(&'a T) -> B,
T: Ord
{
match self.0.binary_search_by_key(b,f){
Ok(x)=>Ok(INDEX::my_from(x)),
Err(x)=>Err(INDEX::my_from(x))
}
}
fn sort(&mut self) where T:Ord{
self.0.sort()
}
fn sort_by<F>(&mut self,f:F) where F:FnMut(&T,&T)->Ordering{
self.0.sort_by(f)
}
fn sort_by_key<F,B>(&mut self,f:F) where B:Ord,F:FnMut(&T)->B{
self.0.sort_by_key(f)
}
fn sort_unstable(&mut self)where T:Ord{self.0.sort_unstable()}
fn sort_unstable_by<F>(&mut self,f:F)where T:Ord,F:FnMut(&T,&T)->Ordering{self.0.sort_unstable_by(f)}
fn sort_unstable_by_key<B:Ord,F>(&mut self,f:F)where T:Ord,F:FnMut(&T)->B{self.0.sort_unstable_by_key(f)}
fn rotate(&mut self,mid:INDEX){
self.0.rotate(mid.my_into())
}
fn clone_from_slice(&mut self, src:&[T]) where T:Clone{
self.0.clone_from_slice(src)
}
fn copy_from_slice(&mut self, src:&[T]) where T:Copy{
self.0.copy_from_slice(src)
}
fn swap_with_slice(&mut self, src:&mut[T]){
self.0.swap_with_slice(src)
}
fn to_vec(&self)->Array<T> where T:Clone{
Array(self.0.to_vec(),PhantomData)
}
}
impl<T,INDEX:IndexTrait> Index<INDEX> for Array<T,INDEX>{
type Output=T;
fn index(&self,i:INDEX)->&T{
&self.0.index(i.my_into())
}
}
impl<T,INDEX:IndexTrait> IndexMut<INDEX> for Array<T,INDEX>{
fn index_mut(&mut self,i:INDEX)->&mut T{
self.0.index_mut(i.my_into())
}
}
impl<T:Clone,INDEX:IndexTrait> Clone for Array<T,INDEX>{
fn clone(&self)->Self{
Array(self.0.clone(),PhantomData)
}
fn clone_from(&mut self, other:&Self){
self.0.clone_from(&other.0);
self.1.clone_from(&other.1);
}
}
impl<T,INDEX:IndexTrait> Default for Array<T,INDEX>{
fn default()->Self{
Array(Vec::<T>::default(),PhantomData)
}
}
impl<T,INDEX:IndexTrait> Borrow<[T]> for Array<T,INDEX>{
fn borrow(&self) -> &[T]{
self.0.borrow()
}
}
impl<T,INDEX:IndexTrait> AsRef<[T]> for Array<T,INDEX>{
fn as_ref(&self)->&[T]{
self.0.as_ref()
}
}
impl<T,INDEX:IndexTrait> AsRef<Array<T,INDEX>> for Array<T,INDEX>{
fn as_ref(&self)->&Self{
self
}
}