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
use std::fmt;
use std::cell::RefCell;
use std::rc::Rc;

use shape::Shape;


/// Array structure
pub struct Array<T: Copy> {
    pub data: Rc<RefCell<Vec<T>>>,
    pub shape: Shape,
}

impl<T: Copy> Array<T> {
    /// Creates new bounded array
    ///
    /// # Arguments
    ///
    /// * `data` - array elements
    /// * `shape` - vector representing array shape
    /// * `start` - start offset of array data
    /// * `end` - end offset of array data
    #[inline]
    pub fn new_bounded(data: Vec<T>, shape: Vec<i32>, start: usize, end: usize) -> Array<T> {
        return Array {
            data: Rc::new(RefCell::new(data)),
            shape: Shape::new(shape, start, end)
        };
    }

    /// Creates new array
    ///
    /// # Arguments
    ///
    /// * `data` - array elements
    /// * `shape` - vector representing array shape
    pub fn new(data: Vec<T>, shape: Vec<i32>) -> Array<T> {
        let length = data.len();

        return Array::new_bounded(data, shape, 0, length);
    }

    /// Returns Shape instance
    #[inline]
    pub fn shape(&self) -> &Shape {
        &self.shape
    }

    /// Returns vector representing array shape
    #[inline]
    pub fn get_shape(&self) -> &Vec<i32> {
        self.shape.get_shape()
    }

    /// Sets array shape
    ///
    /// # Arguments
    ///
    /// * `shape` - vector representing new array shape
    #[inline]
    pub fn reshape(&mut self, shape: Vec<i32>) -> &Array<T> {
        self.shape.set_shape(shape);
        return self;
    }

    /// Sets array shape
    ///
    /// # Arguments
    ///
    /// * `shape` - vector representing new array shape
    #[inline]
    pub fn set_shape(&mut self, shape: Vec<i32>) -> () {
        self.shape.set_shape(shape);
    }

    /// Validates indices have right dimension
    ///
    /// # Arguments
    ///
    /// * `indices` - Indices
    fn check_indices_size(&self, indices: &Vec<usize>) -> () {
        let shape_len = self.shape.get_shape().len();

        if indices.len() / 2 > shape_len {
            panic!(
                "Invalid indices given: shape dimensions {}, indices dimensions {}",
                shape_len,
                indices.len() / 2
            );
        }
    }

    /// Set values on given indices to given value
    ///
    /// # Arguments
    ///
    /// * `indices` - vector of indices
    /// * `value` - value to fill it with
    ///
    /// # Examples
    ///
    /// ```
    /// #[macro_use] extern crate numas;
    /// use numas::array::Array;
    ///
    /// let f_array = Array::new(vec![1,2,3,4,5,6,7,8,9], vec![3,3]);
    /// 
    /// // first row
    /// f_array.set(s![0], -1);
    /// assert_eq!(f_array.collect(), vec![-1,-1,-1,4,5,6,7,8,9]);
    ///
    /// let s_array = Array::new(vec![1,2,3,4,5,6,7,8,9], vec![3,3]);
    ///
    /// // fist two rows
    /// s_array.set(s![0 => 2], -1);
    /// assert_eq!(s_array.collect(), vec![-1,-1,-1,-1,-1,-1,7,8,9]);
    ///
    /// let t_array = Array::new(vec![1,2,3,4,5,6,7,8,9], vec![3,3]);
    ///
    /// // second row, second column
    /// t_array.set(s![1; 1], -1);
    /// assert_eq!(t_array.collect(), vec![1,2,3,4,-1,6,7,8,9]);
    ///
    /// let x_array = Array::new(vec![1,2,3,4,5,6,7,8,9], vec![3,3]);
    ///
    /// // last row, two last columns
    /// x_array.set(s![2; 1 => 3], -1);
    /// assert_eq!(x_array.collect(), vec![1,2,3,4,5,6,7,-1,-1]);
    /// ```
    pub fn set(&self, indices: Vec<usize>, value: T) -> () {
        self.check_indices_size(&indices);

        let shape = self.shape.indices_to_shape(indices);
        let mut data = self.data.borrow_mut();

        for i in shape.get_bounds() {
            data[i] = value;
        }
    }

    /// Fills array with given value
    ///
    /// # Arguments
    ///
    /// * `value` - fill value
    pub fn fill(&self, value: T) -> &Array<T> {
        let mut data = self.data.borrow_mut();

        for i in self.shape.get_bounds() {
            data[i] = value;
        }

        return self;
    }

    /// Return new Array from given indices
    ///
    /// # Arguments
    ///
    /// * `indices` - vector of indices
    ///
    /// # Examples
    ///
    /// ```
    /// #[macro_use] extern crate numas;
    /// use numas::array::Array;
    ///
    /// let array = Array::new(vec![1,2,3,4,5,6,7,8,9], vec![3,3]);
    ///
    /// // first row
    /// assert_eq!(array.get(s![0]).collect(), vec![1,2,3]);
    ///
    /// // fist two rows
    /// assert_eq!(array.get(s![0 => 2]).collect(), vec![1,2,3,4,5,6]);
    ///
    /// // second row, second column
    /// assert_eq!(array.get(s![1; 1]).collect(), vec![5]);
    ///
    /// // last row, two last columns
    /// assert_eq!(array.get(s![2; 1 => 3]).collect(), vec![8,9]);
    /// ```
    pub fn get(&self, indices: Vec<usize>) -> Array<T> {
        // Handle invalid indices length
        self.check_indices_size(&indices);

        let new_shape = self.shape.indices_to_shape(indices);

        return Array {
            shape: new_shape,
            data: self.data.clone(),
        };
    }

    /// Returns length of array
    pub fn len(&self) -> usize {
        return Shape::total_len(&self.shape) as usize;
    }

    /// Returns base length of array
    #[inline]
    pub fn base_len(&self) -> usize {
        return self.data.borrow().len();
    }

    /// Creates view into array
    #[inline]
    pub fn view(&self) -> Array<T> {
        return Array {
            data: self.data.clone(),
            shape: self.shape.clone(),
        }
    }

    /// Creates bounded view into array
    ///
    /// # Arguments
    ///
    /// * `shape` - vector representing array shape
    /// * `start` - start offset of array data
    /// * `end` - end offset of array data
    #[inline]
    pub fn bounded_view(&self, shape: &Vec<i32>, start: usize, end: usize) -> Array<T> {
        return Array {
            data: self.data.clone(),
            shape: Shape::new(shape.clone(), start, end),
        }
    }

    /// Collects elements of array into vector
    pub fn collect(&self) -> Vec<T> {
        let data = self.data.borrow();
        return data[self.shape.get_bounds()].to_vec();
    }
}

impl<T: Copy> Clone for Array<T> {
    /// Clones array object
    fn clone(&self) -> Array<T> {
        let data = self.data.borrow().to_vec().clone();

        return Array {
            data: Rc::new(RefCell::new(data)),
            shape: self.shape.clone(),
        };
    }
}

impl <T: fmt::Debug + Copy> fmt::Debug for Array<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let data = self.data.borrow();
        return write!(f, "data: \t{:?}\nshape: \t{:?}", &data[self.shape.get_bounds()], self.get_shape());
    }
}