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
//! `Arr` module.
//! An array container which can hold an arbitrary number of elements of a single type.

use {OverError, OverResult};
use std::cell::RefCell;
use std::rc::Rc;
use types::Type;
use value::Value;

#[derive(Clone, Debug)]
struct ArrInner {
    vec: Vec<Value>,
    t: Type,
}

/// `Arr` struct.
#[derive(Clone, Debug)]
pub struct Arr {
    inner: Rc<RefCell<ArrInner>>,
}

impl Arr {
    /// Returns a new, empty `Arr`.
    pub fn new() -> Arr {
        Arr {
            inner: Rc::new(RefCell::new(ArrInner {
                vec: Vec::new(),
                t: Type::Empty,
            })),
        }
    }

    /// Returns a new `Arr` with the given value vector as elements.
    pub fn from_vec(vec: Vec<Value>) -> OverResult<Arr> {
        let mut t = Type::Empty;

        for value in &vec {
            let tnew = value.get_type();
            if let Type::Empty = t {
                t = tnew.clone()
            } else if t != tnew {
                return Err(OverError::ArrTypeMismatch(tnew, t));
            }
        }

        Ok(Arr { inner: Rc::new(RefCell::new(ArrInner { vec, t })) })
    }

    /// Returns the type of all elements in this `Arr`.
    pub fn get_type(&self) -> Type {
        self.inner.borrow().t.clone()
    }

    /// Gets the value at `index`.
    /// Returns an error if `index` is out of bounds.
    pub fn get(&self, index: usize) -> OverResult<Value> {
        let inner = self.inner.borrow();

        if index >= inner.vec.len() {
            Err(OverError::ArrOutOfBounds(index))
        } else {
            Ok(inner.vec[index].clone())
        }
    }

    /// Returns the length of this `Arr`.
    // TODO: test this
    pub fn len(&self) -> usize {
        self.inner.borrow().vec.len()
    }

    /// Returns whether this `Arr` is empty.
    pub fn is_empty(&self) -> bool {
        self.inner.borrow().vec.is_empty()
    }

    /// Returns whether this `Arr` and `other` point to the same data.
    pub fn ptr_eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.inner, &other.inner)
    }

    /// Sets the value at `index` to `value`.
    /// Returns an error if `index` is out of bounds.
    pub fn set(&mut self, index: usize, value: Value) -> OverResult<()> {
        let mut inner = self.inner.borrow_mut();

        if index >= inner.vec.len() {
            Err(OverError::ArrOutOfBounds(index))
        } else {
            inner.vec[index] = value;
            Ok(())
        }
    }

    /// Adds a `Value` to the `Arr`.
    /// Returns an error if the new `Value` is type-incompatible with the `Arr`.
    pub fn push(&mut self, value: Value) -> OverResult<()> {
        let mut inner = self.inner.borrow_mut();

        let inner_type = inner.t.clone();
        let val_type = value.get_type();

        if val_type != inner_type {
            Err(OverError::ArrTypeMismatch(val_type, inner_type))
        } else {
            // Update type of this `Arr`.
            if inner_type.is(&Type::Empty) {
                inner.t = val_type;
            }

            inner.vec.push(value);

            Ok(())
        }
    }

    /// Inserts a `Value` into the `Arr` at the given index.
    /// Returns an error if the new `Value` is type-incompatible with the `Arr`
    /// or if the index is out of bounds.
    pub fn insert(&mut self, index: usize, value: Value) -> OverResult<()> {
        let mut inner = self.inner.borrow_mut();

        let inner_type = inner.t.clone();
        let val_type = value.get_type();

        if index > inner.vec.len() {
            return Err(OverError::ArrOutOfBounds(index));
        }
        if val_type != inner_type {
            Err(OverError::ArrTypeMismatch(val_type, inner_type))
        } else {
            // Update type of this `Arr`.
            if inner_type.is(&Type::Empty) {
                inner.t = val_type;
            }

            inner.vec.insert(index, value);

            Ok(())
        }
    }

    /// Removes and returns a `Value` from the `Arr` at the given index.
    /// Sets the Arr type to Empty if the new length is 0, otherwise the type is left unchanged.
    /// Returns an error if the index is out of bounds.
    pub fn remove(&mut self, index: usize) -> OverResult<Value> {
        let mut inner = self.inner.borrow_mut();

        if index > inner.vec.len() {
            return Err(OverError::ArrOutOfBounds(index));
        }

        let res = inner.vec.remove(index);

        if inner.vec.is_empty() {
            inner.t = Type::Empty;
        }

        Ok(res)
    }
}

impl Default for Arr {
    fn default() -> Self {
        Self::new()
    }
}

impl PartialEq for Arr {
    fn eq(&self, other: &Self) -> bool {
        let inner = self.inner.borrow();
        let other_inner = other.inner.borrow();

        if inner.t != other_inner.t {
            return false;
        }

        inner.vec == other_inner.vec
    }
}