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
use std::alloc::{Layout, realloc};
use std::marker::PhantomData;
use std::ops::{Index, IndexMut};
pub mod iterator;

pub struct Array<T> {
    phantom_data: PhantomData<T>, // For compile time borrow checking correctness
    layout: Layout,
    data: *mut T,
    capacity: usize,
}

impl<T> Array<T> {
    #[inline(always)]
    pub fn capacity(&self) -> usize {
        return self.capacity;
    }

    pub fn extend(&mut self, new_capacity: usize){
        let new_layout = Layout::array::<T>(new_capacity).expect("Failed to create layout");
        let new_ptr = unsafe{realloc(self.data as *mut u8, new_layout, new_layout.size())};
        if new_ptr.is_null(){
            panic!("Failed to allocate memory");
        }
        self.data = new_ptr as *mut T;
        self.capacity = new_capacity;
        self.layout = new_layout;
    }
    #[inline(always)]
    pub fn extend_by(&mut self, additional_capacity: usize){
        self.extend(self.capacity + additional_capacity);
    }

    pub fn new(capacity: usize) -> Self {
        let layout = Layout::array::<T>(capacity).expect("Failed to create layout");
        let data = unsafe { std::alloc::alloc(layout) as *mut T };
        if data.is_null(){
            panic!("Failed to allocate memory");
        }
        return Array {
            phantom_data: PhantomData,
            layout,
            data,
            capacity,
        };
    }
    #[inline(always)]
    pub fn iter(&self) -> iterator::ArrayIterator<T> {
        return iterator::ArrayIterator {
            phantom_data: &self.phantom_data,
            data: self.data,
            end: unsafe { self.data.offset(self.capacity as isize) },
        };
    }
    #[inline(always)]
    pub fn iter_mut(&mut self) -> iterator::ArrayIteratorMut<T> {
        return iterator::ArrayIteratorMut {
            phantom_data: &mut self.phantom_data,
            data: self.data,
            end: unsafe { self.data.offset(self.capacity as isize) },
        };
    }
}

impl<T> Drop for Array<T>{
    fn drop(&mut self) {
        unsafe {
            std::alloc::dealloc(self.data as *mut u8, self.layout);
        }
    }
}

impl<T> Index<usize> for Array<T>{
    type Output = T;

    #[inline(always)]
    fn index(&self, index: usize) -> &Self::Output {
        if index >= self.capacity {
            panic!("Index out of bounds");
        }

        unsafe {
            return &*self.data.offset(index as isize);
        }
    }
}

impl<T> IndexMut<usize> for Array<T>{
    #[inline(always)]
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        if index >= self.capacity {
            panic!("Index out of bounds");
        }
        unsafe {
            return &mut *self.data.offset(index as isize);
        }
    }
}