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
use std::cmp::Ordering;
use std::fmt::{Debug, Formatter, Error, Display};
use std::mem;

mod iter;
pub use self::iter::Iter;


/// ALO means At Least One.
/// It cointains at least one item.
/// If there is only one item, ALO does not heap allocate.
/// If there is more than one items, ALO does heap allocate.
#[derive(Clone)]
pub struct ALO<T>(T, Vec<T>);

impl<T> ALO<T> {
    pub fn with_item(item: T) -> ALO<T> {
        ALO(item, Vec::new())
    }

    pub fn add(&mut self, item: T) {
        let &mut ALO(_, ref mut vec) = self;
        vec.push(item);
    }

    pub fn get(&self, index: usize) -> Option<&T> {
        if index == 0 {
            Some(&self.0)
        } else {
            self.1.get(index - 1)
        }
    }

    /// Remove item in given index.
    ///
    /// #Panics
    /// Panics if ALO has only one item.
    /// Panics if index is out of bounds.
    pub fn remove(&mut self, index: usize) -> T {
        if index == 0 {
            mem::replace(&mut self.0, self.1.remove(0))
        } else {
            self.1.remove(index - 1)
        }
    }

    pub fn len(&self) -> usize {
        self.1.len() + 1
    }


    pub fn iter<'a>(&'a self) -> Iter<'a, T> {
        Iter::with(&self.0, self.1.iter())
    }
}


impl<T> Debug for ALO<T>
    where T: Debug
{
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        let &ALO(ref first, ref another) = self;
        write!(f, "ALO ( {:?}, {:?} )", first, another)
    }
}


impl<T> Display for ALO<T>
    where T: Display
{
    /// This prints like below
    /// "ALO ( "String", and more 9 items )
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        let &ALO(ref first, ref another) = self;
        write!(f, "ALO ( {}, and more {} items )", first, another.len())
    }
}


impl<A, B> PartialEq<ALO<B>> for ALO<A>
    where A: PartialEq<B>
{
    fn eq(&self, other: &ALO<B>) -> bool {
        let &ALO(ref self_first, ref self_another) = self;
        let &ALO(ref other_first, ref other_another) = other;
        self_first.eq(other_first) && self_another.eq(other_another)
    }
}


impl<T> Eq for ALO<T> where T: Eq {}


impl<T> PartialOrd for ALO<T>
    where T: PartialOrd<T>
{
    fn partial_cmp(&self, other: &ALO<T>) -> Option<Ordering> {
        let &ALO(ref self_first, ref self_another) = self;
        let &ALO(ref other_first, ref other_another) = other;
        match self_first.partial_cmp(other_first) {
            None => None,
            Some(Ordering::Less) => Some(Ordering::Less),
            Some(Ordering::Greater) => Some(Ordering::Greater),
            Some(Ordering::Equal) => self_another.partial_cmp(other_another),
        }
    }
}


impl<T> Ord for ALO<T>
    where T: Ord
{
    fn cmp(&self, other: &ALO<T>) -> Ordering {
        let &ALO(ref self_first, ref self_another) = self;
        let &ALO(ref other_first, ref other_another) = other;
        match self_first.cmp(other_first) {
            Ordering::Less => Ordering::Less,
            Ordering::Greater => Ordering::Greater,
            Ordering::Equal => self_another.cmp(other_another),
        }
    }
}


#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn ord_singles() {
        let alo1 = ALO::with_item(42);
        let alo2 = ALO::with_item(420);
        assert!(alo1 < alo2);
    }

    #[test]
    fn ord_single_and_many() {
        let alo3 = ALO::with_item(42);
        let mut alo4 = ALO::with_item(42);
        alo4.add(4);
        assert!(alo3 < alo4);
    }

    #[test]
    fn ord_many_and_many() {
        let mut alo5 = ALO::with_item(42);
        alo5.add(1);
        alo5.add(1);
        let mut alo6 = ALO::with_item(42);
        alo6.add(1);
        alo6.add(4);
        assert!(alo5 < alo6);

        let mut alo5 = ALO::with_item(42);
        alo5.add(4);
        let mut alo6 = ALO::with_item(42);
        alo6.add(1);
        alo6.add(4);
        assert!(alo5 > alo6);
    }

    #[test]
    fn removeing_item_should_return_removed_value() {
        let mut alo1 = ALO::with_item(42);
        alo1.add(50);
        assert_eq!(42, alo1.remove(0));
        assert_eq!(Some(&50), alo1.get(0));
    }

    #[test]
    #[should_panic]
    fn removing_from_alo_which_contains_only_one_item_should_panic() {
        let mut alo = ALO::with_item(42);
        alo.remove(0);
    }

    #[test]
    #[should_panic]
    fn removing_by_out_of_bound_index_should_panic() {
        let mut alo = ALO::with_item(42);
        alo.add(42);
        alo.remove(2);
    }


    #[test]
    fn get() {
        let mut alo = ALO::with_item(0);
        alo.add(1);
        assert_eq!(alo.get(0), Some(&0));
        assert_eq!(alo.get(1), Some(&1));
        assert_eq!(alo.get(2), None);
    }
}