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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#![doc(html_root_url = "https://docs.rs/col_macros/0.1.1/")]

//! This crate is a collection of general purporse collection macros.

#[allow(unused)]
pub use high_mem_utils::{DontDropOpt, DropBy};
use std::hint::unreachable_unchecked;

pub mod types {
    //! These are the standard collection types aliased with the names that their respective macros depends, import them if you do
    //! not want define to them.

    /// [vector!](../macro.vector.html)
    pub type Vector<T> = Vec<T>;
    /// [vec_deque!](../macro.vec_deque.html)
    pub type Deque<T> = std::collections::VecDeque<T>; 
    /// [hash_map!](../macro.hash_map.html)
    pub type Map<K, V> = std::collections::HashMap<K, V>;
    /// [hash_set!](../macro.hash_set.html)
    pub type Set<K> = std::collections::HashSet<K>;
    /// [btree_map!](../macro.btree_map.html)
    pub type BTMap<K, V> = std::collections::BTreeMap<K, V>;
    /// [btree_set!](../macro.btree_set.html)
    pub type BTSet<K> = std::collections::BTreeSet<K>;
}

/// Construct or update a vector of type Vector\<T\> which can be and is an alias to Vec<T> in the types module.
/// 
/// It does so by simply creating an array from the elements,set the capacity of the vec at they length,read each entry into 
/// and [mem::forget](https://doc.rust-lang.org/std/mem/fn.forget.html) the array. 
/// 
/// Collections with a capacity and the push method are accepted,like `BinaryHeap`.  
/// 
/// # Examples 
/// 
/// ```
/// use col_macros::vector;
/// use col_macros::types::Vector;
/// 
/// let mut vec: Vec<u32> = vector![1, 2, 3];
/// 
/// assert_eq!(vec, vec![1, 2, 3]);
/// 
/// vector![for &mut vec, ins 4, 5, 6].push(7); // we still have a mutable reference 
/// 
/// assert_eq!(vec, vector![1, 2, 3, 4, 5, 6, 7]);
/// 
/// assert_eq!(vec![0; 8], vector![0; 8]);
/// ```
#[macro_export]
macro_rules! vector {
    (for $map:expr, ins $( $k:expr ),*) => {
        {
            fn gen<'a, T>(arr: &[T], mutable_ref: &'a mut Vec<T>) -> &'a mut Vec<T> {
                let offset = mutable_ref.len(); 
                let arrlen = arr.len(); 
                mutable_ref.reserve(arrlen); 
                
                unsafe {
                    let arr_ptr = arr.as_ptr();
                    let map_ptr: *mut T = mutable_ref.as_mut_ptr().add(offset); 
                    map_ptr.write(arr_ptr.read()); 
                
                    for i in 1..arrlen {  
                        map_ptr.add(i).write(arr_ptr.add(i).read());
                    }
                
                    mutable_ref.set_len(offset+arrlen); // the most unsafe set of a length in the world,this will change in the future
                }

                std::mem::forget(arr);
                mutable_ref
            }

            let arr = [$($k),*];
            gen(&arr, $map)
        }
    };

    (for $map:tt, ins $( $k:tt ,)*) => {
        $crate::vector!(for $map:tt, ins $( $k:tt ),*)
    };

    ( $( $k:expr ),*) => {
        {
            fn gen<T>(arr: &[T]) -> Vec<T> {
                let arrlen = arr.len();
                let mut map = <Vector<_>>::with_capacity(arrlen);
                
                unsafe {
                    let arr_ptr = arr.as_ptr();
                    let map_ptr: *mut T = map.as_mut_ptr();
                    map_ptr.write(arr_ptr.read());
                
                    for i in 1..arrlen {  
                        map_ptr.add(i).write(arr_ptr.add(i).read());
                    }
                
            
                    map.set_len(arrlen);
                }
                std::mem::forget(arr);
                map
            }

            let arr = [$( $k ),*];
            gen(&arr)
        }
    };

    (for $map:expr, ins $e:expr; $n:expr) => {
        {
            fn gen<T>(arr: [T; $n], mutable_ref: &mut Vec<T>) -> &mut Vec<T> {
                let offset = mutable_ref.len();
                let arrlen = arr.len();
                mutable_ref.reserve(arrlen);
                
                unsafe {
                    let arr_ptr = arr.as_ptr();
                    let map_ptr: *mut T = mutable_ref.as_mut_ptr().add(offset-1);
                    map_ptr.write(arr_ptr.read());
                
                    for i in 1..arrlen {  
                        map_ptr.add(i).write(arr_ptr.add(i).read());
                    }
                
            
                    mutable_ref.set_len(offset+arrlen);
                }
                std::mem::forget(arr);
                mutable_ref
            }

            let arr = [$e; $n];
            gen(arr, $map)
        }
    };

    ( $e:expr; $n:expr) => {
        {
            fn gen<T>(arr: [T; $n]) -> Vec<T> {
                let arrlen = arr.len();
                let mut map = <Vector<_>>::with_capacity(arrlen);
                
                unsafe {
                    let arr_ptr = arr.as_ptr();
                    let map_ptr: *mut T = map.as_mut_ptr();
                    map_ptr.write(arr_ptr.read());
                
                    for i in 1..arrlen {  
                        map_ptr.add(i).write(arr_ptr.add(i).read());
                    }
                
            
                    map.set_len(arrlen);
                }
                std::mem::forget(arr);
                map
            }

            let arr = [$e; $n];
            gen(arr)
        }
    };

    ( $( $k:tt ,)*) => {
        $crate::vector!($( $k ),*)
    };
}

/// Construct a double-ended queue of type Deque\<T\> which can be and is an alias to VecDeque<T> in the types module.
/// 
/// It does so by simply creating an array from the elements,set the capacity of the vec at they length,read each entry into 
/// and [mem::forget](https://doc.rust-lang.org/std/mem/fn.forget.html) the array.
///  
/// Collections with a capacity,the push_back and push_front methods,are accepted LinkedList not.  
/// 
/// # Examples 
/// 
/// ```
/// use col_macros::vec_deque;
/// use col_macros::types::Deque;
/// 
/// let mut vec = vec_deque![1, 2, 3 => 4, 5, 6];
/// let mut vec2 = {
///     let mut vec1 = Deque::new();
///     
///     vec1.push_front(1);
///     vec1.push_front(2);
///     vec1.push_front(3);
///     
///     vec1.push_back(4);
///     vec1.push_back(5);
///     vec1.push_back(6);
///     vec1
/// };
/// 
/// assert_eq!(vec, vec2);
/// ```
#[macro_export]
macro_rules! vec_deque {
    ($($front:expr),* => $($back:expr),*) => {
    {
        let arr_front = [$($front),*];
        let arr_back = [$($back),*];
        let arr_front_len = arr_front.len();
        let arr_back_len = arr_back.len();
        let mut temp_vec_deque = <Deque<_>>::with_capacity(arr_front_len+arr_back_len);

        unsafe {
            let arr_front_ptr = arr_front.as_ptr();
            let arr_back_ptr = arr_back.as_ptr();
            temp_vec_deque.push_front(arr_front_ptr.read());
            temp_vec_deque.push_back(arr_back_ptr.read());
            
            for i in 1..arr_front_len {  
                temp_vec_deque.push_front(arr_front_ptr.add(i).read());
            }

            for i in 1..arr_back_len {  
                temp_vec_deque.push_back(arr_back_ptr.add(i).read());
            }
        }
        
        std::mem::forget(arr_front);
        std::mem::forget(arr_back);

        temp_vec_deque
    }
    };

    ($e:expr; $n:expr) => {
    {
        let arr_back = [$e; $n];
        let arr_back_len = arr_back.len();
        let mut temp_vec_deque = <Deque<_>>::with_capacity(arr_front_len+arr_back_len);

        unsafe {
            let arr_back_ptr = arr_back.as_ptr();
            temp_vec_deque.push_back(arr_back_ptr.read());
            
            for i in 1..arr_back_len {  
                temp_vec_deque.push_front(arr_front_ptr.add(i).read());
            }
        }
        
        std::mem::forget(arr_back);

        temp_vec_deque
    }
    };
}

/// Construct or update a map of type Map<K, V> which can be and is an alias to HashMap<K, V> in the types module.
/// 
/// It does so by simply creating an array from key-value tuples,set the capacity of the map at they length,read each tuple,insert the keys to values 
/// and [mem::forget](https://doc.rust-lang.org/std/mem/fn.forget.html) the array.
/// 
/// Maps with a capacity and the insert method are accepted,BTreeMap not.  
/// 
/// # Examples 
/// 
/// ```
/// use col_macros::hash_map;
/// use col_macros::types::Map;
/// 
/// let mut hash = hash_map!["key1" => 1, "key2" => 2];
/// let mut hash2 = {
///     let mut temp = Map::with_capacity(2);
///     temp.insert("key1", 1);
///     temp.insert("key2", 2);
///     temp
/// };
/// 
/// assert_eq!(hash["key1"], 1);
/// assert_eq!(hash["key2"], 2);
/// 
/// assert_eq!(hash2["key1"], 1);
/// assert_eq!(hash2["key2"], 2);
/// 
/// hash_map![for &mut hash, ins "key3" => 3, "key4" => 4];
/// 
/// assert_eq!(hash["key3"], 3);
/// assert_eq!(hash["key4"], 4);
/// ```
#[macro_export]
macro_rules! hash_map {
    (for $map:expr, ins $( $k:expr => $v:expr ),*) => {
        {
            let arr = [$( ($k, $v) ),*];
            let mut map = $map;
            map.reserve(arr.len());

            unsafe {
            let arr_ptr = arr.as_ptr();

            for i in 0..arr.len() { 
                let tuple = arr_ptr.add(i).read(); // this will never be UB 'cause the first value is zero 
                let _ = map.insert(tuple.0, tuple.1);                // and the end is arr.len-1
            }

            }

            std::mem::forget(arr);
        
            map
        }
    };

    (for $map:tt, ins $( $k:tt => $v:tt ,)*) => {
        $crate::hash_map!(for $map; $( $k => $v ),*)
    };

    ( $( $k:expr => $v:expr ),*) => {
        {
            let arr = [$( ($k, $v) ),*];
            let mut map = <Map<_, _>>::with_capacity(arr.len());

            unsafe {
            let arr_ptr = arr.as_ptr();

            for i in 0..arr.len() { 
                let tuple = arr_ptr.add(i).read(); // this will never be UB 'cause the first value is zero 
                let _ = map.insert(tuple.0, tuple.1);                // and the end is arr.len-1
            }

            }

            std::mem::forget(arr);
        
            map
        }
    };

    ( $( $k:tt => $v:tt ,)*) => {
        $crate::hash_map!($( $k => $v ),*)
    };
}

/// Construct or update a set of type Set\<K\> which can be and is an alias to HashSet<K> in the types module.
/// 
/// It does so by simply creating an array from the keys,set the capacity of the set at they length,read and insert the keys into
/// and [mem::forget](https://doc.rust-lang.org/std/mem/fn.forget.html) the array. 
/// 
/// Collections with a capacity and the insert method are accepted,BTreeSet not.  
/// 
/// # Examples 
/// 
/// ```
/// use col_macros::hash_set;
/// use col_macros::types::Set;
/// 
/// let mut hash = hash_set!["key1", "key2"];
/// 
/// assert!(hash.contains("key1"));
/// assert!(hash.contains("key2"));
/// 
/// hash = hash_set![for hash, ins "key3", "key4"];
/// 
/// assert!(hash.contains("key3"));
/// assert!(hash.contains("key4"));
/// ```
#[macro_export]
macro_rules! hash_set {
    (for $map:expr, ins $( $k:expr ),*) => {
        {
            let arr = [$($k),*];
            let mut map = $map;
            map.reserve(arr.len());

            unsafe {
            let arr_ptr = arr.as_ptr();

            for i in 0..arr.len() {  // this will never be UB 'cause the first value is zero 
                let _ = map.insert(arr_ptr.add(i).read()); // and the end is arr.len-1
            }

            }

            std::mem::forget(arr);
        
            map
        }
    };

    (for $map:tt, ins $( $k:tt ,)*) => {
        hash_set!(for $map, ins $( $k ),*)
    };

    ( $( $k:expr ),*) => {
        {
            let arr = [$($k),*];
            let mut map = <Set<_>>::with_capacity(arr.len());

            unsafe {
            let arr_ptr = arr.as_ptr();

            for i in 0..arr.len() { // this will never be UB 'cause the first value is zero 
                let _ = map.insert(arr_ptr.add(i).read()); // and the end is arr.len-1
            }

            }

            std::mem::forget(arr);
        
            map
        }
    };

    ( $( $k:tt => $v:tt ,)*) => {
        hash_set!($( $k ),*)
    };
}

/// Construct or update a map of type BTMap<K, V> which can be and is an alias to BTreeMap<K, V> in the types module.
/// 
/// It does so by simply creating an array from key-value tuples,read each one,inserting the keys to values and [mem::forget](https://doc.rust-lang.org/std/mem/fn.forget.html) the array. 
/// 
/// All types of maps are accepted but if a map has capacity and their respective methods, [hash_map!](./macro.hash_map.html) is preferred.  
/// 
/// # Examples 
/// 
/// ```
/// use col_macros::btree_map;
/// use col_macros::types::BTMap;
/// 
/// let mut hash = btree_map!["key1" => 1, "key2" => 2];
/// let mut hash2 = {
///     let mut temp = BTMap::new();
///     temp.insert("key1", 1);
///     temp.insert("key2", 2);
///     temp
/// };
/// 
/// assert_eq!(hash["key1"], 1);
/// assert_eq!(hash["key2"], 2);
/// 
/// assert_eq!(hash2["key1"], 1);
/// assert_eq!(hash2["key2"], 2);
/// 
/// btree_map![for &mut hash, ins "key3" => 3, "key4" => 4];
/// 
/// assert_eq!(hash["key3"], 3);
/// assert_eq!(hash["key4"], 4);
/// ```
#[macro_export]
macro_rules! btree_map {
    (for $map:expr, ins $( $k:expr => $v:expr ),*) => {
        {
            let mut map = $map;
            $(let _ = map.insert($k, $v);)* 
            map
        }
    };

    (for $map:tt, ins $( $k:tt => $v:tt ,)*) => {
        btree_map!(for $map, ins $( $k => $v ),*)
    };

    ( $( $k:expr => $v:expr ),*) => {
        {
            let mut map = <BTMap<_, _>>::new();
            $(let _ = map.insert($k, $v);)*
            map
        }
    };

    ( $( $k:tt => $v:tt ,)*) => {
        btree_map!($( $k => $v ),*)
    };
}

/// Construct or update a set of type BTSet\<K\> which can be and is an alias to BTreeSet<K> in the types module.
/// 
/// It does so by simply creating an array from the keys,read each one,insert the keys into and [mem::forget](https://doc.rust-lang.org/std/mem/fn.forget.html) the array.
///  
/// All sets are accepted but if a set implements capacity and their respective methods [hash_set!](./macro.hash_set.html) is preferred.  
/// 
/// # Examples 
/// 
/// ```
/// use col_macros::btree_set;
/// use col_macros::types::BTSet;
/// 
/// let mut hash = btree_set!["key1", "key2"];
/// 
/// assert!(hash.contains("key1"));
/// assert!(hash.contains("key2"));
/// 
/// btree_set![for &mut hash, ins "key3", "key4"];
/// 
/// assert!(hash.contains("key3"));
/// assert!(hash.contains("key4"));
/// ```
#[macro_export]
macro_rules! btree_set {
    (for $map:expr, ins $( $k:expr ),*) => {
        {
            let mut map = $map;
            $(let _ = map.insert($k);)* 
            map
        }
    };

    (for $map:tt, ins $( $k:tt ,)*) => {
        $crate::btree_set!(for $map, ins $( $k ),*)
    };

    ( $( $k:expr ),*) => {
        {
            let mut map = <BTSet<_>>::new();
            $(let _ = map.insert($k);)*
            map
        }
    };

    ( $( $k:tt ,)*) => {
        $crate::btree_set!($( $k ),*)
    };
}

fn unwrap_or_never<T>(o: &Option<T>) -> &T {
    unsafe {
        match o {
            Some(e) => &e,
            None => unreachable_unchecked(),
        }
    }
}