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
//! # Go Heap in Rust
//!
//! `go_heap_rs` implements the Go's [heap](https://golang.org/pkg/container/heap/) interface in Rust
//! The heap it self is backed by another collection which it must implement the `Heap` trait for it
//!
//! # Advantages
//! * More control over swap method
//! * Added `fix` and `remove` methods
//! * Full access to underlying data
//!
//! # Disadvantages
//! * Must be backed by a collection like Vector
//! * Not really easy to work with
//! * Lack of some methods like `from`


use std::marker::PhantomData;

/// All types which you want to create heap from them must implement this trait
///
/// # Example of Min Heap
/// ```
/// use go_heap_rs::Heap;
/// struct MinHeap(Vec<i32>);
/// impl Heap<i32> for MinHeap {
///     fn len(&self) -> usize {
///         self.0.len()
///     }
///
///     fn less(&self, i: usize, j: usize) -> bool {
///         self.0[i] < self.0[j]
///     }
///
///     fn swap(&mut self, i: usize, j: usize) {
///         self.0.swap(i, j);
///     }
///
///     fn push(&mut self, x: i32) {
///         self.0.push(x);
///     }
///
///     fn pop(&mut self) -> i32 {
///         self.0.pop().expect("pop on an empty vec!")
///     }
///
///     fn peak(&self) -> Option<&i32> {
///         self.0.get(0)
///     }
/// }
/// ```
pub trait Heap<T> {
    /// length must return the length of the heap
    fn len(&self) -> usize;
    /// Compares two elements of the heap at i and j index
    /// It is guaranteed that the i and j are always valid
    fn less(&self, i: usize, j: usize) -> bool;
    /// This function must swap the i and j element in the heap array
    /// It is guaranteed that the i and j are always valid
    fn swap(&mut self, i: usize, j: usize);
    /// push is just like push in vector. It should add x to the last of array
    fn push(&mut self, x: T);
    /// This method should remove the last element of array and return it
    /// This function is guaranteed to be called when at least one element in available
    fn pop(&mut self) -> T;
    /// This method must return first element in your collection
    fn peak(&self) -> Option<&T>;
}

/// A heap structure which holds a type which derives from Heap
pub struct HeapType<T, E> where T: Heap<E> {
    pub data: T,
    #[doc(hidden)]
    phantom: PhantomData<E>,
}

impl<T: Heap<E>, E> HeapType<T, E> {
    /// Initialized a new heap from a Heap type
    ///
    /// # Arguments
    ///
    /// * `init`: The Heap type to be initialized
    ///
    /// returns: HeapType<T, E> which is your heap data structure
    ///
    /// # Examples
    ///
    /// ```
    /// use go_heap_rs::{HeapType, MinHeap};
    ///
    /// let my_vec = MinHeap(vec![4, 3, 2, 1]); // see min heap implementation in Heap trait
    /// let mut heap = HeapType::new(my_vec);
    /// assert_eq!(heap.peak(), Some(&1));
    /// ```
    pub fn new(init: T) -> HeapType<T, E> where T: Heap<E> {
        let mut data = HeapType {
            data: init,
            phantom: PhantomData,
        };
        // Init
        let n = data.data.len();
        for i in (0..=n / 2 - 1).rev() {
            data.down(i, n);
        }
        data
    }

    /// Pushes an element into heap
    ///
    /// # Arguments
    ///
    /// * `x`: The element to push it into heap
    pub fn push(&mut self, x: E) {
        self.data.push(x);
        self.up(self.data.len() - 1);
    }

    /// Removes the greatest item from the binary heap and returns it, or None if it is empty.
    ///
    /// returns E: The first element in list
    pub fn pop(&mut self) -> Option<E> {
        if self.data.len() == 0 { // empty heap
            return None;
        }
        let n = self.data.len() - 1;
        self.data.swap(0, n);
        self.down(0, n);
        Some(self.data.pop())
    }

    /// Removes an element from heap by it's index in it's underlying container
    ///
    /// # Arguments
    ///
    /// * `i`: The index to remove
    ///
    /// returns E: The element which as been removed
    ///
    /// # Panics
    /// This method might panic (based on implementation of `swap`) if `i` is bigger than `len()`
    ///
    /// # Examples
    ///
    /// ```
    /// use go_heap_rs::{HeapType, MinHeap};
    ///
    /// let my_vec = MinHeap(vec![1, 4, 3]);
    /// let mut heap = HeapType::new(my_vec); // [1, 4, 3]
    /// assert_eq!(heap.remove(1), 4);
    /// assert_eq!(heap.pop(), Some(1));
    /// assert_eq!(heap.pop(), Some(3));
    /// assert_eq!(heap.pop(), None);
    /// ```
    pub fn remove(&mut self, i: usize) -> E {
        let n = self.data.len() - 1;
        if n != i {
            self.data.swap(i, n);
            if !self.down(i, n) {
                self.up(i);
            }
        }
        self.data.pop()
    }


    /// Fix re-establishes the heap ordering after the element at index i has changed its value.
    /// Changing the value of the element at index i and then calling Fix is equivalent to,
    /// but less expensive than, calling Remove(h, i) followed by a Push of the new value.
    ///
    /// # Arguments
    ///
    /// * `i`: The index to fix
    ///
    /// # Examples
    ///
    /// ```
    /// use go_heap_rs::{HeapType, MinHeap};
    ///
    /// let my_vec = MinHeap(vec![10, 4, 3]);
    /// let mut heap = HeapType::new(my_vec); // [3, 4, 10]
    /// heap.data.0[1] = 0;
    /// heap.fix(0);
    /// assert_eq!(heap.pop(), Some(0));
    /// assert_eq!(heap.pop(), Some(3));
    /// assert_eq!(heap.pop(), Some(10));
    /// assert_eq!(heap.pop(), None);
    /// ```
    pub fn fix(&mut self, i: usize) {
        if !self.down(i, self.data.len()) {
            self.up(i);
        }
    }

    /// peak will return the top of heap if available
    pub fn peak(&self) -> Option<&E> {
        self.data.peak()
    }

    fn up(&mut self, mut j: usize) {
        loop {
            let i = (((j as isize) - 1) / 2) as usize; // parent
            if i == j || !self.data.less(j, i) {
                break;
            }
            self.data.swap(i, j);
            j = i
        }
    }

    fn down(&mut self, i0: usize, n: usize) -> bool {
        let mut i = i0;
        loop {
            let j1: isize = (2 * i + 1) as isize;
            if j1 >= n as isize || j1 < 0 { // j1 < 0 after int overflow
                break;
            }
            let mut j = j1 as usize; // left child
            let j2 = j1 as usize + 1;
            if j2 < n && self.data.less(j2, j1 as usize) {
                j = j2 // = 2*i + 2  // right child
            }
            if !self.data.less(j, i) {
                break;
            }
            self.data.swap(i, j);
            i = j
        }
        return i > i0;
    }
}

/// A very simple min heap implementation
pub struct MinHeap<T: Ord>(pub Vec<T>);

impl<T: Ord> Heap<T> for MinHeap<T> {
    fn len(&self) -> usize {
        self.0.len()
    }

    fn less(&self, i: usize, j: usize) -> bool {
        self.0[i] < self.0[j]
    }

    fn swap(&mut self, i: usize, j: usize) {
        self.0.swap(i, j);
    }

    fn push(&mut self, x: T) {
        self.0.push(x);
    }

    fn pop(&mut self) -> T {
        self.0.pop().expect("pop on an empty vec!")
    }

    fn peak(&self) -> Option<&T> {
        self.0.get(0)
    }
}

#[cfg(test)]
mod tests {
    use crate::{Heap, HeapType, MinHeap};

    #[test]
    fn simple_vec() {
        // Create a vector
        let my_vec = MinHeap(vec![2, 4, 3, 1]);
        let mut heap = HeapType::new(my_vec);
        // Change it a bit
        assert_eq!(heap.peak().unwrap(), &1);
        assert_eq!(heap.pop().unwrap(), 1);
        heap.push(-1);
        assert_eq!(heap.pop().unwrap(), -1);
        assert_eq!(heap.pop().unwrap(), 2);
        assert_eq!(heap.pop().unwrap(), 3);
        assert_eq!(heap.pop().unwrap(), 4);
        assert_eq!(heap.data.len(), 0);
    }

    #[test]
    fn simple_remove() {
        let my_vec = MinHeap(vec![1, 4, 3]);
        let mut heap = HeapType::new(my_vec); // [1, 4, 3]
        assert_eq!(heap.remove(1), 4);
        assert_eq!(heap.pop(), Some(1));
        assert_eq!(heap.pop(), Some(3));
        assert_eq!(heap.pop(), None);
    }

    #[test]
    fn simple_fix() {
        let my_vec = MinHeap(vec![10, 4, 3]);
        let mut heap = HeapType::new(my_vec); // [3, 4, 10]
        heap.data.0[1] = 0;
        heap.fix(0);
        assert_eq!(heap.pop(), Some(0));
        assert_eq!(heap.pop(), Some(3));
        assert_eq!(heap.pop(), Some(10));
        assert_eq!(heap.pop(), None);
    }
}